Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Created April 8, 2016 13:21
Show Gist options
  • Save eoinkelly/9017e1814adc85463dda71d51002af56 to your computer and use it in GitHub Desktop.
Save eoinkelly/9017e1814adc85463dda71d51002af56 to your computer and use it in GitHub Desktop.
post push git hook that will automatically generate a codeship CI build badge that you can paste into your PR description
#!/usr/bin/env ruby
require "net/http"
require "json"
CODESHIP_API_KEY = "PUT_YOUR_CODESHIP_API_KEY_HERE".freeze
origin_remote_url = `git config --get remote.$(git config --get branch.master.remote).url`.chomp
current_project_name = %r{(?:\:|\/)(.+)\.git$}.match(origin_remote_url)[1]
current_branch_name = `git rev-parse --abbrev-ref HEAD`.chomp
codeship_projects_url = "https://codeship.com/api/v1/projects.json?api_key=#{CODESHIP_API_KEY}"
projects_json = Net::HTTP.get(URI(codeship_projects_url))
projects = JSON.parse(projects_json)["projects"]
project = projects.find { |p| p["repository_name"] == current_project_name }
exit unless project
codeship_project_uuid = project["uuid"]
codeship_project_id = project["id"]
codeship_badge_md = "[ ![Codeship Status for #{current_project_name}](https://codeship.com/projects/#{codeship_project_uuid}/status?branch=#{current_branch_name})](https://codeship.com/projects/#{codeship_project_id})" # rubocop:disable Metrics/LineLength
puts <<-EOM
Codeship build badge:
#{codeship_badge_md}
EOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment