Skip to content

Instantly share code, notes, and snippets.

@ktheory
Created February 27, 2013 22:28
Show Gist options
  • Save ktheory/5052440 to your computer and use it in GitHub Desktop.
Save ktheory/5052440 to your computer and use it in GitHub Desktop.
script to report Github commit status with CircleCi
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
require 'octokit'
require 'yaml'
commit = ARGV.first || 'HEAD'
full_sha = `git rev-parse --verify #{commit}`.strip
github_creds = YAML.load_file(".github_oauth_credentials.yml")
client = Octokit::Client.new(github_creds)
# Assume you only have one remote
origin = `git remote -v`.split[1]
# Parse out organization/repo part of origin
repo = /(?<repo>\w+\/\w+)\.git$/.match(origin)[:repo]
puts "Checking test status for #{full_sha}"
statuses = client.statuses(repo, full_sha)
# Since pushing to a new branch re-runs tests:
# Use any successful status
status = statuses.detect{|stat| stat.state == "success"}
# Otherwise use the oldest pending status
status ||= statuses.select{|stat| stat.state == "pending"}.last
# Otherwise, use the newest status
status ||= statuses.first
if status
puts "#{status.state.upcase} -- #{status.target_url}"
exit status.state == "success" ? 0 : 1
else
puts "UNKNOWN (no statuses)"
exit 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment