Skip to content

Instantly share code, notes, and snippets.

@tomstuart
Last active December 14, 2015 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomstuart/35ede17f981a7fb80c7c to your computer and use it in GitHub Desktop.
Save tomstuart/35ede17f981a7fb80c7c to your computer and use it in GitHub Desktop.
Ruby script to open latest Codeship build for current commit
#!/usr/bin/env ruby
require 'json'
require 'net/https'
require 'time'
API_ENDPOINT = 'https://codeship.com/api/v1/projects.json'
API_KEY = ENV['CODESHIP_API_KEY'] ||
raise('set CODESHIP_API_KEY first! get it from https://codeship.com/user/edit#user_api_key')
TRIES = 5
WAIT_BETWEEN_TRIES = 2
current_sha = `git rev-parse HEAD`.chomp
raise 'git failed — not a git repo?' unless $?.success?
projects_uri = URI.parse(API_ENDPOINT).tap do |uri|
uri.query = URI.encode_www_form(api_key: API_KEY)
end
build = nil
TRIES.times do |n|
puts "not on Codeship yet, retrying (#{n.succ} of #{TRIES})…" unless n.zero?
projects = JSON.parse(Net::HTTP.get(projects_uri))['projects']
build =
projects.
flat_map { |project| project['builds'] }.
select { |build| build['commit_id'] == current_sha }.
max_by { |build| Time.iso8601(build['started_at'] || Time.now) }
break if build
sleep WAIT_BETWEEN_TRIES
end
raise "no Codeship build matching #{current_sha}" unless build
project_id, build_id = build.values_at('project_id', 'id').map(&:to_s).map(&URI.method(:escape))
build_uri = "https://codeship.com/projects/#{project_id}/builds/#{build_id}"
puts "opening #{build_uri}…"
exec 'open', build_uri
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment