From 6a4c0598e8210743ef01daf79a3c52ac42058a3b Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sat, 8 Sep 2012 12:57:32 -0700 Subject: [PATCH 01/11] Set node version in package --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 8a9bff4..dbe8d1f 100644 --- a/package.json +++ b/package.json @@ -11,5 +11,9 @@ , "underscore.string": "*" , "request": "*" , "async": "*" + }, + "engines": { + "node": "0.6.13", + "npm": "1.1.41" } } From 6c837f8075510575c6fb834cdbc24adb6f856b85 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sun, 9 Sep 2012 23:18:38 -0700 Subject: [PATCH 02/11] Set commit status when notifying github --- app.coffee | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app.coffee b/app.coffee index 8a32f80..c9123a4 100644 --- a/app.coffee +++ b/app.coffee @@ -54,6 +54,10 @@ class PullRequestCommenter @post "/issues/#{issue}/comments", (body: comment), (e, body) -> console.log e if e? + setCommitStatus: (issue, state) => + @post "/repos/#{@user}/#{@repo}/#{@sha}", (body: {state:'success', :target_url:'http://www.google.com', description:'DDetails'} ), (e, body) -> + console.log e if e? + successComment: -> "#{BUILDREPORT} :green_heart: `Succeeded` (#{@sha}, [job info](#{@job_url}))" @@ -82,6 +86,7 @@ class PullRequestCommenter makePullComment: (pull, cb) => comment = if @succeeded then @successComment() else @errorComment() @commentOnIssue pull.number, comment + @setCommitStatus pull.number, comment cb() updateComments: (cb) -> From c2a493a4b8ceef27a62c10d72bb2b6d7bbb8bd32 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sun, 9 Sep 2012 23:23:03 -0700 Subject: [PATCH 03/11] Fix typo --- app.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.coffee b/app.coffee index c9123a4..ff5668a 100644 --- a/app.coffee +++ b/app.coffee @@ -55,7 +55,7 @@ class PullRequestCommenter console.log e if e? setCommitStatus: (issue, state) => - @post "/repos/#{@user}/#{@repo}/#{@sha}", (body: {state:'success', :target_url:'http://www.google.com', description:'DDetails'} ), (e, body) -> + @post "/repos/#{@user}/#{@repo}/#{@sha}", (body: "{'state':'success', 'target_url':'http://www.google.com', 'description':'DDetails'}" ), (e, body) -> console.log e if e? successComment: -> From 9b2d2a062756c043a3db5f56d0af465396ec8e7d Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sun, 9 Sep 2012 23:28:35 -0700 Subject: [PATCH 04/11] Another fix --- app.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.coffee b/app.coffee index ff5668a..1fdbe7a 100644 --- a/app.coffee +++ b/app.coffee @@ -55,7 +55,7 @@ class PullRequestCommenter console.log e if e? setCommitStatus: (issue, state) => - @post "/repos/#{@user}/#{@repo}/#{@sha}", (body: "{'state':'success', 'target_url':'http://www.google.com', 'description':'DDetails'}" ), (e, body) -> + @post "/statuses/#{@sha}", (body: "{'state':'success', 'target_url':'http://www.google.com', 'description':'DDetails'}" ), (e, body) -> console.log e if e? successComment: -> From 5ce35c8dd872d86f23ae0044ab2dd51eed0732e0 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sun, 9 Sep 2012 23:33:42 -0700 Subject: [PATCH 05/11] Yet another test --- app.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.coffee b/app.coffee index 1fdbe7a..4be192a 100644 --- a/app.coffee +++ b/app.coffee @@ -55,7 +55,7 @@ class PullRequestCommenter console.log e if e? setCommitStatus: (issue, state) => - @post "/statuses/#{@sha}", (body: "{'state':'success', 'target_url':'http://www.google.com', 'description':'DDetails'}" ), (e, body) -> + @post "/statuses/#{@sha}", (state:'success', target_url:'http://www.google.com', description:'DDetails'), (e, body) -> console.log e if e? successComment: -> From 791a04d3a2f5da2bffad7b1dec817116847ba279 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Sun, 9 Sep 2012 23:39:27 -0700 Subject: [PATCH 06/11] Testing --- app.coffee | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app.coffee b/app.coffee index 4be192a..75d2cb8 100644 --- a/app.coffee +++ b/app.coffee @@ -54,8 +54,8 @@ class PullRequestCommenter @post "/issues/#{issue}/comments", (body: comment), (e, body) -> console.log e if e? - setCommitStatus: (issue, state) => - @post "/statuses/#{@sha}", (state:'success', target_url:'http://www.google.com', description:'DDetails'), (e, body) -> + setCommitStatus: (state) => + @post "/statuses/#{@sha}", (state:state, target_url:@job_url, description:'job info'), (e, body) -> console.log e if e? successComment: -> @@ -84,9 +84,8 @@ class PullRequestCommenter , () -> cb null, pull makePullComment: (pull, cb) => - comment = if @succeeded then @successComment() else @errorComment() - @commentOnIssue pull.number, comment - @setCommitStatus pull.number, comment + state = if @succeeded then 'success' else 'failure' + @setCommitStatus state cb() updateComments: (cb) -> From a986ab2238ff585b71bfb9868d8f059e191a4480 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Mon, 17 Jun 2013 17:26:23 -0700 Subject: [PATCH 07/11] Kaizen: Changes in github api make parts of this code obsolete --- app.coffee | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/app.coffee b/app.coffee index 75d2cb8..6f85fde 100644 --- a/app.coffee +++ b/app.coffee @@ -44,12 +44,6 @@ class PullRequestCommenter deleteComment: (id, cb) => @del "/issues/comments/#{id}", cb - getPulls: (cb) => - @get "/pulls", cb - - getPull: (id, cb) => - @get "/pulls/#{id}", cb - commentOnIssue: (issue, comment) => @post "/issues/#{issue}/comments", (body: comment), (e, body) -> console.log e if e? @@ -64,25 +58,6 @@ class PullRequestCommenter errorComment: -> "#{BUILDREPORT} :broken_heart: `Failed` (#{@sha}, [job info](#{@job_url}))" - # Find the first open pull with a matching HEAD sha - findMatchingPull: (pulls, cb) => - pulls = _.filter pulls, (p) => p.state is 'open' - async.detect pulls, (pull, detect_if) => - @getPull pull.number, (e, { head }) => - return cb e if e? - detect_if head.sha is @sha - , (match) => - return cb "No pull request for #{@sha} found" unless match? - cb null, match - - removePreviousPullComments: (pull, cb) => - @getCommentsForIssue pull.number, (e, comments) => - return cb e if e? - old_comments = _.filter comments, ({ body }) -> _s.include body, BUILDREPORT - async.forEach old_comments, (comment, done_delete) => - @deleteComment comment.id, done_delete - , () -> cb null, pull - makePullComment: (pull, cb) => state = if @succeeded then 'success' else 'failure' @setCommitStatus state @@ -90,9 +65,6 @@ class PullRequestCommenter updateComments: (cb) -> async.waterfall [ - @getPulls - @findMatchingPull - @removePreviousPullComments @makePullComment ], cb From 1354e43aff37e0b4a2d16758ea79cfca8e88d17e Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Mon, 17 Jun 2013 17:44:40 -0700 Subject: [PATCH 08/11] Debug whats going on with the post_receive hook. --- app.coffee | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/app.coffee b/app.coffee index 6f85fde..172171c 100644 --- a/app.coffee +++ b/app.coffee @@ -118,20 +118,21 @@ app.get '/jenkins/post_build', (req, res) -> # GitHub lets us know when a pull request has been opened. app.post '/github/post_receive', (req, res) -> payload = JSON.parse req.body.payload - - if payload.pull_request - sha = payload.pull_request.head.sha - - # Get the sha status from earlier and insta-comment the status - redis.hgetall sha, (err, obj) -> - # Convert stored string to boolean - obj.succeeded = (obj.succeeded == "true" ? true : false) - - commenter = new PullRequestCommenter sha, obj.job_name, obj.job_number, obj.user, obj.repo, obj.succeeded - commenter.updateComments (e, r) -> console.log e if e? - - res.send 201 - else - res.send 404 + console.log payload + res.send 200 + #if payload.pull_request + # sha = payload.pull_request.head.sha + + # # Get the sha status from earlier and insta-comment the status + # redis.hgetall sha, (err, obj) -> + # # Convert stored string to boolean + # obj.succeeded = (obj.succeeded == "true" ? true : false) + + # commenter = new PullRequestCommenter sha, obj.job_name, obj.job_number, obj.user, obj.repo, obj.succeeded + # commenter.updateComments (e, r) -> console.log e if e? + + # res.send 201 + #else + # res.send 404 app.listen app.settings.port From 9aa6c229ad6a8a558be75f1d13baf42e1293004c Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Mon, 17 Jun 2013 17:49:03 -0700 Subject: [PATCH 09/11] keep debugging --- app.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.coffee b/app.coffee index 172171c..8f46a57 100644 --- a/app.coffee +++ b/app.coffee @@ -117,8 +117,8 @@ app.get '/jenkins/post_build', (req, res) -> # GitHub lets us know when a pull request has been opened. app.post '/github/post_receive', (req, res) -> - payload = JSON.parse req.body.payload - console.log payload +# payload = JSON.parse req.body.payload + console.log "yei" res.send 200 #if payload.pull_request # sha = payload.pull_request.head.sha From 6740e0ee7b9ba8663f0460821f0e0864bf925c43 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Mon, 17 Jun 2013 18:04:18 -0700 Subject: [PATCH 10/11] Remove unneeded parameters from the callback --- app.coffee | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/app.coffee b/app.coffee index 8f46a57..6862414 100644 --- a/app.coffee +++ b/app.coffee @@ -58,7 +58,7 @@ class PullRequestCommenter errorComment: -> "#{BUILDREPORT} :broken_heart: `Failed` (#{@sha}, [job info](#{@job_url}))" - makePullComment: (pull, cb) => + makePullComment: (cb) => state = if @succeeded then 'success' else 'failure' @setCommitStatus state cb() @@ -117,22 +117,20 @@ app.get '/jenkins/post_build', (req, res) -> # GitHub lets us know when a pull request has been opened. app.post '/github/post_receive', (req, res) -> -# payload = JSON.parse req.body.payload - console.log "yei" - res.send 200 - #if payload.pull_request - # sha = payload.pull_request.head.sha - - # # Get the sha status from earlier and insta-comment the status - # redis.hgetall sha, (err, obj) -> - # # Convert stored string to boolean - # obj.succeeded = (obj.succeeded == "true" ? true : false) - - # commenter = new PullRequestCommenter sha, obj.job_name, obj.job_number, obj.user, obj.repo, obj.succeeded - # commenter.updateComments (e, r) -> console.log e if e? - - # res.send 201 - #else - # res.send 404 + payload = JSON.parse req.body.payload + if payload.pull_request + sha = payload.pull_request.head.sha + + # Get the sha status from earlier and insta-comment the status + redis.hgetall sha, (err, obj) -> + # Convert stored string to boolean + obj.succeeded = (obj.succeeded == "true" ? true : false) + + commenter = new PullRequestCommenter sha, obj.job_name, obj.job_number, obj.user, obj.repo, obj.succeeded + commenter.updateComments (e, r) -> console.log e if e? + + res.send 201 + else + res.send 404 app.listen app.settings.port From 59ccf6e3d831777ff9ca11e885fd0fdf68e916d9 Mon Sep 17 00:00:00 2001 From: Rafael Chacon Date: Mon, 17 Jun 2013 18:49:40 -0700 Subject: [PATCH 11/11] Set user agent in request header. --- app.coffee | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/app.coffee b/app.coffee index 6862414..bf3aabf 100644 --- a/app.coffee +++ b/app.coffee @@ -22,19 +22,19 @@ class PullRequestCommenter post: (path, obj, cb) => console.log "POST #{@api}#{path}#{@token}" console.dir obj - request.post { uri: "#{@api}#{path}#{@token}", json: obj }, (e, r, body) -> + request.post { uri: "#{@api}#{path}#{@token}", json: obj, headers: 'User-Agent': 'Skyscrpr Status Commenter' }, (e, r, body) -> console.log body if process.env.DEBUG cb e, body get: (path, cb) => console.log "GET #{@api}#{path}#{@token}" - request.get { uri: "#{@api}#{path}#{@token}", json: true }, (e, r, body) -> + request.get { uri: "#{@api}#{path}#{@token}", json: true, headers: 'User-Agent': 'Skyscrpr Status Commenter' }, (e, r, body) -> console.log body if process.env.DEBUG cb e, body del: (path, cb) => console.log "DELETE #{@api}#{path}#{@token}" - request.del { uri: "#{@api}#{path}#{@token}" }, (e, r, body) -> + request.del { uri: "#{@api}#{path}#{@token}", headers: 'User-Agent': 'Skyscrpr Status Commenter' }, (e, r, body) -> console.log body if process.env.DEBUG cb e, body @@ -52,12 +52,6 @@ class PullRequestCommenter @post "/statuses/#{@sha}", (state:state, target_url:@job_url, description:'job info'), (e, body) -> console.log e if e? - successComment: -> - "#{BUILDREPORT} :green_heart: `Succeeded` (#{@sha}, [job info](#{@job_url}))" - - errorComment: -> - "#{BUILDREPORT} :broken_heart: `Failed` (#{@sha}, [job info](#{@job_url}))" - makePullComment: (cb) => state = if @succeeded then 'success' else 'failure' @setCommitStatus state