Skip to content

Instantly share code, notes, and snippets.

@msimonin
Last active December 24, 2015 21:39
Show Gist options
  • Save msimonin/6866738 to your computer and use it in GitHub Desktop.
Save msimonin/6866738 to your computer and use it in GitHub Desktop.
Jenkins pull request receiver
#!/usr/bin/env ruby
# when jenkins received the post payload from github
# clone the repository, and try to merge the pull request before build.
# jenkins must be configured as parametrized one with "payload" parameter.
require 'rubygems'
require 'json'
payload = JSON.parse(ENV['payload'])
job_name = ENV['JOB_NAME']
#puts JSON.pretty_generate(payload)
pull_request_id = payload["number"]
base_git_url = payload["pull_request"]["base"]["repo"]["git_url"]
base_ref = payload["pull_request"]["base"]["ref"]
#base_repo_name = payload["pull_request"]["base"]["repo"]["name"]
#tmp_repo = "/tmp/#{base_repo_name}-#{pull_request_id}"
tmp_repo = "/tmp/#{job_name}"
`rm -rf #{tmp_repo}`
Dir.chdir "/tmp"
`git clone #{base_git_url} #{tmp_repo}`
# switching to base ref
Dir.chdir "#{tmp_repo}"
`git checkout #{base_ref}`
# fetch the pull request
`git fetch origin +refs/pull/#{pull_request_id}/head`
if (payload["pull_request"]["mergeable"])
# merge it
puts "The pull request is mergeable : merging ..."
`git merge FETCH_HEAD`
else
puts "The pull request is not mergeable : running in detached mode"
`git checkout -qf FETCH_HEAD`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment