Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
CoffeeScript JavaScript
Branch: master
Pull request Compare This branch is 71 commits ahead of gofullstack:master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
.gitignore
Procfile
README.md
app.coffee
commentator.coffee
github_caller.coffee
github_commenter.coffee
jira_caller.coffee
package.json
rest_caller.coffee
server.js
status_pusher.coffee

README.md

Things you'll need

We'll pretend the following:

  • You have a Jenkins install at http://jenkins.mycompany.com
  • You have a GitHub organization called MyCompany
  • You have a GitHub account called MyCompany-bot
  • You have a repo you'd like pull requests on called FooBar

Initial setup

Clone the repo locally

git clone git@github.com:gerjomarty/jenkins-comments.git
cd jenkins-comments 

Create a Heroku application

First we'll need a Heroku app running their Cedar stack, and a Redis server. We'll also set the url of our Jenkins server. Chose a name (we'll use "mycompany-jenkins-comments" for the example), and create it:

heroku create mycompany-jenkins-comments --stack cedar
git push heroku master

heroku addons:add redistogo:nano

heroku config:add JENKINS_URL=http://jenkins.mycompany.com
heroku config:add NODE_ENV=production

heroku scale web=1

Setup your app with permissions for GitHub

Create a new Authorization using the GitHub Authorizations API:

curl -u "MyCompany-bot:password" https://api.github.com/authorizations \
  -H "Content-Type: application/json" \
  -X POST \
  -d '{"scopes":["repo"],"note": "mycompany-jenkins-comments.herokuapp.com"}'
{
  "scopes": [
    "repo"
  ],
  "updated_at": "2012-05-21T16:33:05Z",
  "note_url": null,
  "app": {
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api",
    "name": "mycompany-jenkins-comments.herokuapp.com (API)"
  },
  "url": "https://api.github.com/authorizations/369874",
  "token": "a55199221f3f66a7d238be5fa32e2cd84735ffc1",
  "note": "mycompany-jenkins-comments.herokuapp.com",
  "created_at": "2012-05-21T16:33:05Z",
  "id": 369874
}

In the reponse is the token the app will use to comment on pull requests. Add that token to Heroku:

heroku config:add GITHUB_USER_TOKEN=a55199221f3f66a7d238be5fa32e2cd84735ffc1
heroku config:add USER_AGENT="User agent identifying app here (and a URL)"

USER_AGENT is the user agent string that is now required for Github API requests. Make it descriptive.

If branches start with a valid JIRA ticket and you would like to turn on JIRA integration for automatically transitioning stories between states, also add these environment variables:

heroku config:add JIRA_BASE_URI=https://mycompany.atlassian.net
heroku config:add JIRA_BOT_USERNAME=jenkinscommentator
heroku config:add JIRA_BOT_PASSWORD=sekrit
heroku config:add JIRA_MOVE_TO_CODE_REVIEW_COMMENT="Github Pull Request opened. Issue moved to Code Review."
heroku config:add JIRA_PASSED_CODE_REVIEW_COMMENT="Github Pull Request merged. Issue passed Code Review."
heroku config:add JIRA_FAILED_CODE_REVIEW_COMMENT="Github Pull Request closed without merging. Issue failed Code Review."

Per repo

Configure app options

To support different repos, add the username and repo in upper case after each environment variable title like below...

heroku config:add NO_OF_INDIVIDUAL_TESTS_USERNAME_REPONAME=3
heroku config:add PENDING_DESCRIPTION_USERNAME_REPONAME="Currently running Application & Logic Unit Tests and Functional Tests on Jenkins"

NO_OF_INDIVIDUAL_TESTS is the number of tests we run on each commit. Success will only be reported after this number of tests have run. PENDING_DESCRIPTION is the string that appears on the pull request while the tests are running.

Configure Jenkins Job

Under Build > Inject environemnt variables > Properties Content, set BUILD_STATUS to success. This will only be set if the build succeeds:

BUILD_STATUS=success

In Post-build Actions > Post build task > script, we'll add a curl statement to post the job status to mycompany-jenkins-comments.herokuapp.com:

curl "http://mycompany-jenkins-comments.herokuapp.com/jenkins/post_build?\
user=MyCompany\
&repo=FooBar\
&sha=$GIT_COMMIT\
&status=$BUILD_STATUS\
&job_name=$JOB_NAME\
&job_number=$BUILD_NUMBER\
&build_url=$BUILD_URL"

Configure GitHub to notify us of an opened pull request

We'll use the GitHub PubSubHubBub API to subscribe to pull requests events:

curl -u "MyCompany-bot:password" https://api.github.com/hub \
  -Fhub.mode=subscribe \
  -Fhub.topic=https://github.com/MyCompany/FooBar/events/pull_request \
  -Fhub.callback=http://mycompany-jenkins-comments.herokuapp.com/github/post_receive
Something went wrong with that request. Please try again.