require 'rubygems'
require 'httparty'
require 'sinatra'
require 'json'
set :port, 5000
user_agent = 'ofiwg/signed-off-by-checker'
auth_token = ENV['GITHUB_AUTH_TOKEN']
if auth_token == nil then
puts "Someone forgot to set \$GITHUB_AUTH_TOKEN before launching me. Aborting!\n"
exit 1
end
get '/' do
'Nothing to see here but us chickens'
end
post '/' do
push = JSON.parse(request.body.read)
repo_name = push['repository']['full_name']
if push['action'] == nil || (push['action'] != 'synchronize' &&
push['action'] != 'opened') then
return [200, 'This is not a pull request opened or synchronize push; nothing for this bot to do!']
end
commits_url = push['pull_request']['commits_url']
commits = HTTParty.get(commits_url,
:headers => {
'Content-Type' => 'application/json',
'User-Agent' => user_agent,
'Authorization' => "token #{auth_token}" }
)
if commits.length == 0 then
return [200, 'Somehow there are no commits on this PR... Weird...']
end
happy = true
targetURL = 'https://github.com/ofiwg/libfabric/wiki#how-to-contribute'
debug_message = "checking debug URL: #{commits_url}\n\n"
final_message = ''
commits.each_with_index do |commit, index|
sha = commit['sha']
status_url = "https://api.github.com/repos/#{repo_name}/statuses/#{sha}"
debug_message += "examining commit index #{index} / sha #{sha}:\nstatus url: #{status_url}\n\n#{commit.to_json}\n\n"
status = {
'context' => 'Signed-off-by checker'
}
if /Signed-off-by/.match commit['commit']['message']
status['state'] = 'success'
status['description'] = 'This commit is signed off'
else
status['state'] = 'failure'
status['description'] = 'This commit is not signed off'
status['target_url'] = targetURL
happy = false
end
final_message = status['description']
if index == (commits.length - 1) && index > 0 then
if happy then
status['state'] = 'success'
status['description'] = 'All commits were signed off. Yay!'
else
status['state'] = 'failure'
status['description'] = 'At least one commit was not signed off'
status['target_url'] = targetURL
end
final_message = status['description']
end
HTTParty.post(status_url,
:body => status.to_json,
:headers => {
'Content-Type' => 'application/json',
'User-Agent' => user_agent,
'Authorization' => "token #{auth_token}" }
)
end
return [200, "#{debug_message} -- #{final_message}"]
end