{"action":"created","comment":{"url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/comments/73737839","id":73737839,"diff_hunk":"@@ -0,0 +1,184 @@\n+'use strict';\n+const Executor = require('screwdriver-executor-base');\n+const path = require('path');\n+const request = require('request');\n+const jenkins = require('jenkins');\n+const fs = require('fs');\n+const async = require('async');\n+\n+class J5sExecutor extends Executor {\n+\n+    /**\n+     * Constructor\n+     * @method constructor\n+     * @param  {Object} options           Configuration options\n+     * @param  {Object} options.host      Jenkins hostname to make requests to\n+     * @param  {Object} options.username  Jenkins username\n+     * @param  {Object} options.password  Jenkins password/token\n+     */\n+    constructor(options) {\n+        super();\n+        this.host = options.host;\n+        this.username = options.username;\n+        this.password = options.password;\n+        // need to pass port nubmer in the future\n+        this.baseUrl = `http://${this.username}:${this.password}@${this.host}:8080`;\n+        this.crumbUrl = this.baseUrl.concat('/crumbIssuer/api/json');\n+    }\n+\n+    /**\n+     * Get CSRF token crumb for Jenkins\n+     * @method getCrumb\n+     * @param  {Function} callback          Callback with error and crumb object\n+     */\n+    getCrumb(callback) {\n+        const options = {\n+            url: this.crumbUrl,\n+            method: 'GET'\n+        };\n+\n+        request(options, (error, response) => {\n+            if (error) return callback(new Error(error.message));\n+\n+            if (response.statusCode !== 200) {\n+                const msg = `Failed to get crumb: ${JSON.stringify(response.body)}`;\n+\n+                return callback(new Error(msg));\n+            }\n+\n+            return callback(null, response.body);\n+        });\n+    }\n+\n+    /**\n+     * Initialize the jenkins client with crumb\n+     * @method initJenkinsClient\n+     * @param  crumb                        CSRF token from jenkins api\n+     * @param  {Function} callback          Callback with error and jenkins client object\n+     */\n+    initJenkinsClient(crumb, callback) {\n+        const data = JSON.parse(crumb);\n+\n+        const jenkinsClient = jenkins({\n+            baseUrl: this.baseUrl,\n+            headers: {\n+                [data.crumbRequestField]: data.crumb\n+            }\n+        });\n+\n+        if (jenkinsClient) return callback(null, jenkinsClient);\n+\n+        return callback(new Error('Failed to instantiate jenkins client'));\n+    }\n+\n+    /**\n+     * Get build number of the job's last build\n+     * @method getBuildNumber\n+     * @param  jobName                      name of the jenkins job and also buildID in config\n+     * @param  {Function} callback          Callback with error, jenkins client object and build number\n+     */\n+    getBuildNumber(jobName, jenkinsClient, callback) {\n+        const get = jenkinsClient.job.get.bind(jenkinsClient.job);\n+\n+        return get(jobName, (err, data) => {\n+            try {\n+                callback(err, jenkinsClient, data.lastBuild.number);\n+            } catch (e) {\n+                callback(e);\n+            }\n+        });\n+    }\n+\n+    /**\n+     * Create a jenkins job and start the build\n+     * @method _start\n+     * @param  {Object}   config            A configuration object\n+     * @param  {String}   config.buildId    ID for the build and also name of the job in jenkins\n+     * @param  {String}   config.jobId      ID for the job\n+     * @param  {String}   config.pipelineId ID for the pipeline\n+     * @param  {String}   config.container  Container for the build to run in\n+     * @param  {String}   config.scmUrl     Scm URL to use in the build\n+     * @param  {Function} callback          Callback with null if successful otherwise error\n+     */\n+    _start(config, callback) {\n+        const fakeConfigPath = path.resolve(__dirname, './config/test-job.xml');\n+        const xml = fs.readFileSync(fakeConfigPath, 'utf-8');\n+\n+        async.waterfall([\n+            this.getCrumb.bind(this),\n+            this.initJenkinsClient.bind(this),\n+            (jenkinsClient, cb) => {\n+                const create = jenkinsClient.job.create.bind(jenkinsClient.job);\n+\n+                return create(config.buildId, xml, (err) => {\n+                    cb(err, jenkinsClient);\n+                });\n+            },\n+            (jenkinsClient, cb) => {\n+                const build = jenkinsClient.job.build.bind(jenkinsClient.job);\n+\n+                return build(config.buildId, cb);\n+            }\n+        ], (error) => {","path":"index.js","position":null,"original_position":122,"commit_id":"e07b486a37ed9483739c9561e064ad55f6926a50","original_commit_id":"0b8273e91103c534df0ca070e2ee4e05460ef24e","user":{"login":"minz1027","id":20427140,"avatar_url":"https://avatars.githubusercontent.com/u/20427140?v=3","gravatar_id":"","url":"https://api.github.com/users/minz1027","html_url":"https://github.com/minz1027","followers_url":"https://api.github.com/users/minz1027/followers","following_url":"https://api.github.com/users/minz1027/following{/other_user}","gists_url":"https://api.github.com/users/minz1027/gists{/gist_id}","starred_url":"https://api.github.com/users/minz1027/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minz1027/subscriptions","organizations_url":"https://api.github.com/users/minz1027/orgs","repos_url":"https://api.github.com/users/minz1027/repos","events_url":"https://api.github.com/users/minz1027/events{/privacy}","received_events_url":"https://api.github.com/users/minz1027/received_events","type":"User","site_admin":false},"body":"done","created_at":"2016-08-05T18:22:50Z","updated_at":"2016-08-05T18:22:50Z","html_url":"https://github.com/screwdriver-cd/executor-j5s/pull/1#discussion_r73737839","pull_request_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/comments/73737839"},"html":{"href":"https://github.com/screwdriver-cd/executor-j5s/pull/1#discussion_r73737839"},"pull_request":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1","id":79990225,"html_url":"https://github.com/screwdriver-cd/executor-j5s/pull/1","diff_url":"https://github.com/screwdriver-cd/executor-j5s/pull/1.diff","patch_url":"https://github.com/screwdriver-cd/executor-j5s/pull/1.patch","issue_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/1","number":1,"state":"closed","locked":false,"title":"Implement jenkins executor","user":{"login":"minz1027","id":20427140,"avatar_url":"https://avatars.githubusercontent.com/u/20427140?v=3","gravatar_id":"","url":"https://api.github.com/users/minz1027","html_url":"https://github.com/minz1027","followers_url":"https://api.github.com/users/minz1027/followers","following_url":"https://api.github.com/users/minz1027/following{/other_user}","gists_url":"https://api.github.com/users/minz1027/gists{/gist_id}","starred_url":"https://api.github.com/users/minz1027/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/minz1027/subscriptions","organizations_url":"https://api.github.com/users/minz1027/orgs","repos_url":"https://api.github.com/users/minz1027/repos","events_url":"https://api.github.com/users/minz1027/events{/privacy}","received_events_url":"https://api.github.com/users/minz1027/received_events","type":"User","site_admin":false},"body":"- `_start`, `_stop` and `_stream` methods are implemented\r\n\r\n- need to call `getCrumb` and then `initJenkinsClient` to attach CSRF token Jenkins-Crumb with every request\r\n\r\n- `_stream` is not really stream for now. It will just return whatever available in the log when the function is called.","created_at":"2016-08-03T22:28:12Z","updated_at":"2016-08-05T18:22:50Z","closed_at":"2016-08-05T00:26:10Z","merged_at":null,"merge_commit_sha":"95c27a5a1e221cb5822c221dd73b100d91b8b59b","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1/commits","review_comments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1/comments","review_comment_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/comments{/number}","comments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/1/comments","statuses_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/statuses/e07b486a37ed9483739c9561e064ad55f6926a50","head":{"label":"screwdriver-cd:Jenkins-2","ref":"Jenkins-2","sha":"e07b486a37ed9483739c9561e064ad55f6926a50","user":{"login":"screwdriver-cd","id":19417863,"avatar_url":"https://avatars.githubusercontent.com/u/19417863?v=3","gravatar_id":"","url":"https://api.github.com/users/screwdriver-cd","html_url":"https://github.com/screwdriver-cd","followers_url":"https://api.github.com/users/screwdriver-cd/followers","following_url":"https://api.github.com/users/screwdriver-cd/following{/other_user}","gists_url":"https://api.github.com/users/screwdriver-cd/gists{/gist_id}","starred_url":"https://api.github.com/users/screwdriver-cd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/screwdriver-cd/subscriptions","organizations_url":"https://api.github.com/users/screwdriver-cd/orgs","repos_url":"https://api.github.com/users/screwdriver-cd/repos","events_url":"https://api.github.com/users/screwdriver-cd/events{/privacy}","received_events_url":"https://api.github.com/users/screwdriver-cd/received_events","type":"Organization","site_admin":false},"repo":{"id":64177099,"name":"executor-j5s","full_name":"screwdriver-cd/executor-j5s","owner":{"login":"screwdriver-cd","id":19417863,"avatar_url":"https://avatars.githubusercontent.com/u/19417863?v=3","gravatar_id":"","url":"https://api.github.com/users/screwdriver-cd","html_url":"https://github.com/screwdriver-cd","followers_url":"https://api.github.com/users/screwdriver-cd/followers","following_url":"https://api.github.com/users/screwdriver-cd/following{/other_user}","gists_url":"https://api.github.com/users/screwdriver-cd/gists{/gist_id}","starred_url":"https://api.github.com/users/screwdriver-cd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/screwdriver-cd/subscriptions","organizations_url":"https://api.github.com/users/screwdriver-cd/orgs","repos_url":"https://api.github.com/users/screwdriver-cd/repos","events_url":"https://api.github.com/users/screwdriver-cd/events{/privacy}","received_events_url":"https://api.github.com/users/screwdriver-cd/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/screwdriver-cd/executor-j5s","description":"Jenkins Executor for Screwdriver","fork":false,"url":"https://api.github.com/repos/screwdriver-cd/executor-j5s","forks_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/forks","keys_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/keys{/key_id}","collaborators_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/teams","hooks_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/hooks","issue_events_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/events{/number}","events_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/events","assignees_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/assignees{/user}","branches_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/branches{/branch}","tags_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/tags","blobs_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/refs{/sha}","trees_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/trees{/sha}","statuses_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/statuses/{sha}","languages_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/languages","stargazers_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/stargazers","contributors_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/contributors","subscribers_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/subscribers","subscription_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/subscription","commits_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/commits{/sha}","git_commits_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/commits{/sha}","comments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/comments{/number}","issue_comment_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/comments{/number}","contents_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/contents/{+path}","compare_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/compare/{base}...{head}","merges_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/merges","archive_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/downloads","issues_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues{/number}","pulls_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls{/number}","milestones_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/milestones{/number}","notifications_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/labels{/name}","releases_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/releases{/id}","deployments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/deployments","created_at":"2016-07-26T00:32:38Z","updated_at":"2016-07-26T00:32:38Z","pushed_at":"2016-08-05T18:08:01Z","git_url":"git://github.com/screwdriver-cd/executor-j5s.git","ssh_url":"git@github.com:screwdriver-cd/executor-j5s.git","clone_url":"https://github.com/screwdriver-cd/executor-j5s.git","svn_url":"https://github.com/screwdriver-cd/executor-j5s","homepage":null,"size":38,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"screwdriver-cd:master","ref":"master","sha":"cc04fcf3a66d2e201b34d45d0d226bab9525e637","user":{"login":"screwdriver-cd","id":19417863,"avatar_url":"https://avatars.githubusercontent.com/u/19417863?v=3","gravatar_id":"","url":"https://api.github.com/users/screwdriver-cd","html_url":"https://github.com/screwdriver-cd","followers_url":"https://api.github.com/users/screwdriver-cd/followers","following_url":"https://api.github.com/users/screwdriver-cd/following{/other_user}","gists_url":"https://api.github.com/users/screwdriver-cd/gists{/gist_id}","starred_url":"https://api.github.com/users/screwdriver-cd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/screwdriver-cd/subscriptions","organizations_url":"https://api.github.com/users/screwdriver-cd/orgs","repos_url":"https://api.github.com/users/screwdriver-cd/repos","events_url":"https://api.github.com/users/screwdriver-cd/events{/privacy}","received_events_url":"https://api.github.com/users/screwdriver-cd/received_events","type":"Organization","site_admin":false},"repo":{"id":64177099,"name":"executor-j5s","full_name":"screwdriver-cd/executor-j5s","owner":{"login":"screwdriver-cd","id":19417863,"avatar_url":"https://avatars.githubusercontent.com/u/19417863?v=3","gravatar_id":"","url":"https://api.github.com/users/screwdriver-cd","html_url":"https://github.com/screwdriver-cd","followers_url":"https://api.github.com/users/screwdriver-cd/followers","following_url":"https://api.github.com/users/screwdriver-cd/following{/other_user}","gists_url":"https://api.github.com/users/screwdriver-cd/gists{/gist_id}","starred_url":"https://api.github.com/users/screwdriver-cd/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/screwdriver-cd/subscriptions","organizations_url":"https://api.github.com/users/screwdriver-cd/orgs","repos_url":"https://api.github.com/users/screwdriver-cd/repos","events_url":"https://api.github.com/users/screwdriver-cd/events{/privacy}","received_events_url":"https://api.github.com/users/screwdriver-cd/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/screwdriver-cd/executor-j5s","description":"Jenkins Executor for Screwdriver","fork":false,"url":"https://api.github.com/repos/screwdriver-cd/executor-j5s","forks_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/forks","keys_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/keys{/key_id}","collaborators_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/teams","hooks_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/hooks","issue_events_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/events{/number}","events_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/events","assignees_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/assignees{/user}","branches_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/branches{/branch}","tags_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/tags","blobs_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/refs{/sha}","trees_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/trees{/sha}","statuses_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/statuses/{sha}","languages_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/languages","stargazers_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/stargazers","contributors_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/contributors","subscribers_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/subscribers","subscription_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/subscription","commits_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/commits{/sha}","git_commits_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/git/commits{/sha}","comments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/comments{/number}","issue_comment_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/comments{/number}","contents_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/contents/{+path}","compare_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/compare/{base}...{head}","merges_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/merges","archive_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/downloads","issues_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues{/number}","pulls_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls{/number}","milestones_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/milestones{/number}","notifications_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/labels{/name}","releases_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/releases{/id}","deployments_url":"https://api.github.com/repos/screwdriver-cd/executor-j5s/deployments","created_at":"2016-07-26T00:32:38Z","updated_at":"2016-07-26T00:32:38Z","pushed_at":"2016-08-05T18:08:01Z","git_url":"git://github.com/screwdriver-cd/executor-j5s.git","ssh_url":"git@github.com:screwdriver-cd/executor-j5s.git","clone_url":"https://github.com/screwdriver-cd/executor-j5s.git","svn_url":"https://github.com/screwdriver-cd/executor-j5s","homepage":null,"size":38,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1"},"html":{"href":"https://github.com/screwdriver-cd/executor-j5s/pull/1"},"issue":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/1"},"comments":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/screwdriver-cd/executor-j5s/statuses/e07b486a37ed9483739c9561e064ad55f6926a50"}}}}
{"actor":{"display_login":"minz1027"}}