Module: Kender::GitHub

Extended by:
GitHub
Included in:
GitHub
Defined in:
lib/kender/github.rb

Overview

This module abstracts access to GitHub. It’s current, sole purpose is to allow commit statuses to be created.

See: github.com/blog/1227-commit-status-api

Instance Method Summary collapse

Instance Method Details

#update_commit_status(state, config) ⇒ Object

Update the commit status for the current HEAD commit. Assumes the working directory is a git repo and the “origin” remote points to a GitHub repo. The state variable must be one of :pending, :success or :failure. The config object must respond to build_number, build_url and github_auth_token.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kender/github.rb', line 19

def update_commit_status(state, config)

  # TODO: Refactor the following code to use gems like git/grit/rugged and
  # octokit. ~asmith

  body = %Q({
    "state": "#{state.to_s}",
    "target_url": "#{config.build_url}",
    "description": "Continuous integration run #{config.build_number}"
  })
  commit = `git log -1 --format=format:%H`
  remotes = `git remote --verbose`

  unless repo = /^origin\[email protected]:(\w+\/\w+)\b/.match(remotes)[1]
    put "Could not establish GitHub repo name from 'origin' remote"
    return
  end

  uri = URI("https://api.github.com/repos/#{repo}/statuses/#{commit}?access_token=#{config.github_auth_token}")

  if config.github_auth_token
    puts "Setting #{repo} commit #{commit} status to '#{state}' on GitHub"
  else
    puts "Skipping setting #{repo} commit #{commit} status to '#{state}' on GitHub because access token not configured"
    return
  end

  Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http|
    response = http.post(uri.request_uri, body)
    unless response.is_a?(Net::HTTPCreated)
      puts "Setting commit status FAILED", response
    end
  end
end