Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added embedded API Viewer #822

Merged
merged 5 commits into from
Apr 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ project(':job-dsl-core') {
mainClassName = 'javaposse.jobdsl.Run'

ext {
apiDocData = "${project.buildDir}/dsl-${version}.json"
apiDocData = "${project.buildDir}/dsl.json"
}

dependencies {
Expand Down Expand Up @@ -127,6 +127,12 @@ project(':job-dsl-core') {
outputs.file apiDocData
}

jar {
from(tasks.generateApiDoc) {
into 'javaposse/jobdsl/dsl'
}
}

run {
if (project.hasProperty('args')) {
args project.args.split('\\s+')
Expand Down
2 changes: 2 additions & 0 deletions docs/Home.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Browse the Jenkins issue tracker to see any [open issues](https://issues.jenkins
* 1.46 (unreleased)
* Added support for [[Automatically Generated DSL]]
([#816](https://github.com/jenkinsci/job-dsl-plugin/pull/816))
* Integrated the API Viewer into the plugin
([#822](https://github.com/jenkinsci/job-dsl-plugin/pull/822))
* Added support for the [Maven Info Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Maven+Info+Plugin)
([JENKINS-32196](https://issues.jenkins-ci.org/browse/JENKINS-32196))
* Added support for the [Log Parser Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Log+Parser+Plugin)
Expand Down
11 changes: 11 additions & 0 deletions job-dsl-api-viewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ task copyData(type: Copy) {
into "${project.buildDir}/data"
}

task downloadUpdateCenter(type: Download) {
src 'http://updates.jenkins-ci.org/update-center.json'
dest 'build/data/update-center.jsonp'
onlyIfNewer true
}

(38..45).each { minorVersion ->
def version = "1.${minorVersion}"
task "downloadData-${version}"(type: Download) {
Expand All @@ -39,6 +45,11 @@ task copyData(type: Copy) {
tasks.gulp_build.dependsOn("downloadData-${version}")
}

tasks.gulp_build.inputs.file('gulpfile.js')
tasks.gulp_build.inputs.dir('src')
tasks.gulp_build.inputs.dir('node_modules')
tasks.gulp_build.outputs.dir('build/dist')
tasks.gulp_build.dependsOn('downloadUpdateCenter')
tasks.gulp_build.dependsOn('npmInstall')
tasks.gulp_build.dependsOn('copyData')
tasks.publishGhPages.dependsOn('gulp_build')
Expand Down
13 changes: 2 additions & 11 deletions job-dsl-api-viewer/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
var less = require('gulp-less');
var path = require('path');
var cleanCss = require('gulp-clean-css');
var connect = require('gulp-connect');
var watch = require('gulp-watch');
var del = require('del');
var merge = require('merge-stream');
var request = require('request');
var source = require('vinyl-source-stream');

gulp.task('templates', function(){
var templates = gulp.src('./src/templates/*.hbs')
Expand Down Expand Up @@ -45,16 +42,10 @@ gulp.task('connect', ['watch'], function() {
connect.server();
});

gulp.task('download', function () {
return request('http://updates.jenkins-ci.org/update-center.json')
.pipe(source('update-center.jsonp'))
.pipe(gulp.dest('./build/data/'));
});

gulp.task('clean', function() {
del.sync(['build']);
});

gulp.task('build', ['download', 'templates', 'less']);
gulp.task('build', ['templates', 'less']);

gulp.task('default', ['build']);
gulp.task('default', ['build']);
2 changes: 1 addition & 1 deletion job-dsl-api-viewer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<button class="btn pull-right toggle-plugins"><span class="glyphicon glyphicon-filter"></span></button>
<select class="version-select form-control">
<!-- ADD VERSIONS HERE! -->
<option value="build/data/dsl-1.46-SNAPSHOT.json">v1.46-SNAPSHOT</option>
<option value="build/data/dsl.json">v1.46-SNAPSHOT</option>
<option value="build/data/dsl-1.45.json">v1.45</option>
<option value="build/data/dsl-1.44.json">v1.44</option>
<option value="build/data/dsl-1.43.json">v1.43</option>
Expand Down
2 changes: 0 additions & 2 deletions job-dsl-api-viewer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
"gulp-clean-css": "^2.0.4",
"gulp-watch": "^4.1.1",
"del": "^1.1.1",
"vinyl-source-stream": "^1.1.0",
"request": "^2.58.0",
"merge-stream": "^0.1.7"
}
}
1 change: 1 addition & 0 deletions job-dsl-api-viewer/src/css/main.less
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
float: right;
margin-top: 8px;
margin-right: 10px;
display: none;
}
.toggle-plugins {
margin-top: 8px;
Expand Down
3 changes: 3 additions & 0 deletions job-dsl-api-viewer/src/js/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Marionette.Renderer.render = function(template, data) {
this.initLayout();
this.loadSelectedDsl().then(function() {
Backbone.history.start({pushState: false});
if (!this.dsl.isEmbedded()) {
$('.version-select').show()
}
}.bind(this));

$('.version-select').change(function() {
Expand Down
4 changes: 4 additions & 0 deletions job-dsl-api-viewer/src/js/Dsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ _.extend(App.Dsl.prototype, {
});
},

isEmbedded: function() {
return this.data.embedded;
},

getContext: function(contextClass) {
return this.data.contexts[contextClass];
},
Expand Down
10 changes: 10 additions & 0 deletions job-dsl-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ configurations {
compile.exclude group: 'org.kohsuke', module: 'windows-package-checker'
}

jpi {
into('api-viewer') {
from(project(':job-dsl-api-viewer').projectDir) {
include 'index.html'
include 'build/dist/**'
}
}
dependsOn tasks.getByPath(':job-dsl-api-viewer:gulp_build')
}

dependencies {
compile project(':job-dsl-core')
optionalJenkinsPlugins 'org.jenkins-ci.plugins:cloudbees-folder:4.4@jar'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javaposse.jobdsl.dsl.GeneratedView;
import javaposse.jobdsl.dsl.JobManagement;
import javaposse.jobdsl.dsl.ScriptRequest;
import javaposse.jobdsl.plugin.actions.ApiViewerAction;
import javaposse.jobdsl.plugin.actions.GeneratedConfigFilesAction;
import javaposse.jobdsl.plugin.actions.GeneratedConfigFilesBuildAction;
import javaposse.jobdsl.plugin.actions.GeneratedJobsAction;
Expand Down Expand Up @@ -184,7 +185,8 @@ public Collection<? extends Action> getProjectActions(AbstractProject<?, ?> proj
new GeneratedJobsAction(project),
new GeneratedViewsAction(project),
new GeneratedConfigFilesAction(project),
new GeneratedUserContentsAction(project)
new GeneratedUserContentsAction(project),
new ApiViewerAction()
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package javaposse.jobdsl.plugin

import hudson.Plugin
import javaposse.jobdsl.dsl.JobManagement
import jenkins.model.Jenkins
import net.sf.json.JSONObject
import org.kohsuke.stapler.StaplerRequest
import org.kohsuke.stapler.StaplerResponse

import javax.servlet.ServletException

import static hudson.model.UpdateCenter.ID_DEFAULT

class JobDslPlugin extends Plugin {
private JSONObject api

@Override
void postInitialize() throws Exception {
api = JSONObject.fromObject(JobManagement.getResource('dsl.json').text)
api.element('embedded', true)
}

@Override
void doDynamic(StaplerRequest request, StaplerResponse response) throws IOException, ServletException {
String path = request.restOfPath
if (path == '/api-viewer') {
response.sendRedirect("${request.requestURI}/index.html")
} else if (path == '/api-viewer/build/data/update-center.jsonp') {
JSONObject data = Jenkins.instance.updateCenter.getById(ID_DEFAULT).JSONObject
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add

response.setContentType('application/javascript')

here to get this to work. I was getting:

Refused to execute script from 'http://localhost:8080/plugin/job-dsl/api-viewer/build/data/update-center.jsonp' because its MIME type ('') is not executable, and strict MIME type checking is enabled.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. I want to get rid of the JSONP stuff. Maybe the JSONP can be processed in Gradle or Grunt to remove the padding. But that's another PR.

response.contentType = 'application/javascript'
response.writer.print("updateCenter.post(${data.toString()})")
} else if (path == '/api-viewer/build/data/dsl.json') {
response.contentType = 'application/json'
response.writer.print(api.toString())
} else {
super.doDynamic(request, response)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package javaposse.jobdsl.plugin.actions

import hudson.model.Action

class ApiViewerAction implements Action {
final String iconFileName = '/plugin/job-dsl/images/48x48/directory.png'
final String displayName = 'Job DSL API Reference'
final String urlName = '/plugin/job-dsl/api-viewer'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">

<f:block>
<p>See <a href="${rootURL}/plugin/job-dsl/api-viewer" target="_blank">Job DSL API</a> for syntax reference.</p>
</f:block>
<f:radioBlock name="scriptLocation" value="true" title="Use the provided DSL script" checked="${instance.isUsingScriptText()}">
<f:entry title='DSL Script' field='scriptText'>
<f:textarea codemirror-mode="groovy"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import hudson.slaves.DumbSlave
import hudson.tasks.Shell
import javaposse.jobdsl.dsl.GeneratedJob
import javaposse.jobdsl.dsl.GeneratedView
import javaposse.jobdsl.plugin.actions.ApiViewerAction
import javaposse.jobdsl.plugin.actions.GeneratedConfigFilesAction
import javaposse.jobdsl.plugin.actions.GeneratedJobsAction
import javaposse.jobdsl.plugin.actions.GeneratedJobsBuildAction
Expand Down Expand Up @@ -49,11 +50,12 @@ class ExecuteDslScriptsSpec extends Specification {

then:
actions != null
actions.size() == 4
actions.size() == 5
actions[0] instanceof GeneratedJobsAction
actions[1] instanceof GeneratedViewsAction
actions[2] instanceof GeneratedConfigFilesAction
actions[3] instanceof GeneratedUserContentsAction
actions[4] instanceof ApiViewerAction
}

def scheduleBuildOnMasterUsingScriptText() {
Expand Down