Class: GithubInformer

Inherits:
Object
  • Object
show all
Defined in:
lib/github_informer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ GithubInformer

Create a new GithubInformer GithubInformer.new( :context => ‘HiveCI’,

:url => 'http://hive.local',
:repo => '/path/to/checkout' )


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/github_informer.rb', line 11

def initialize( args = {} )

  ::Octokit.configure do |c|
    c.access_token = args[:github_auth] || ENV['GITHUB_AUTH']
    c.auto_paginate = true
  end

  @params = {}
  @params[:context]    = args[:context] or raise "Requires a context"
  @params[:target_url] = args[:url]
  
  path = args[:repo] || '.'
  @repo = GithubInformer.determine_repo(path)
  @sha  = GithubInformer.determine_sha(path)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/github_informer.rb', line 5

def params
  @params
end

#repoObject

Returns the value of attribute repo.



5
6
7
# File 'lib/github_informer.rb', line 5

def repo
  @repo
end

#resultObject

Returns the value of attribute result.



5
6
7
# File 'lib/github_informer.rb', line 5

def result
  @result
end

#shaObject

Returns the value of attribute sha.



5
6
7
# File 'lib/github_informer.rb', line 5

def sha
  @sha
end

Class Method Details

.determine_repo(path) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/github_informer.rb', line 71

def self.determine_repo(path)
  output = `cd #{path} && git remote -v | grep github`
  res = output.match(/github.com:(.*\/.*).git/)
  repo = res.captures.first
  raise "Couldn't determine repo" if !repo
  repo
end

.determine_sha(path) ⇒ Object



79
80
81
82
83
# File 'lib/github_informer.rb', line 79

def self.determine_sha(path)
  sha = `cd #{path} && git rev-parse HEAD`.strip
  raise "Couldn't determine sha" if !sha || sha.length == 0
  sha
end

.normalise_status(status) ⇒ Object

Make sure the statuses are what the github api expect



59
60
61
62
63
64
65
66
67
68
# File 'lib/github_informer.rb', line 59

def self.normalise_status(status)
  case
  when status.to_s.match(/fail/)
    'failure'
  when status.to_s.match(/pass/)
    'success'
  else
    status.to_s
  end
end

Instance Method Details

#execute(cmd) ⇒ Object



33
34
35
36
# File 'lib/github_informer.rb', line 33

def execute( cmd )
  system( cmd )
  @result = $?.exitstatus
end

#report_end(args = { 0 => [:pass, 'Good to merge'], :default => [:fail, 'Do not merge' ] }) ⇒ Object

Report ghi.report_end( 0 => [ :pass, ‘Build passed successfully’],

1..100   => [ :fail, 'Build had errors' ],
:default => [ :error, 'Build totally broken'] )


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/github_informer.rb', line 42

def report_end( args = { 0        => [:pass, 'Good to merge'],
                         :default => [:fail, 'Do not merge' ] } )
  if result
    (status,description) = args[:default] || [ :fail, 'Do not merge' ]
    hash = args.select { |a| a === result }
    if !hash.empty?
      (status, description) = hash.values.flatten
    end
    
    status = GithubInformer.normalise_status(status)
    Octokit.create_status( repo, sha, status, params.merge( {:description => description} ) )
  else
    Octokit.create_status( repo, sha, 'error', params.merge( {:description => 'Program never completed'} ) )
  end
end

#report_start(args = {}) ⇒ Object

Report start of job to github ghi.report_start( :description => ‘Build job in progress’)



29
30
31
# File 'lib/github_informer.rb', line 29

def report_start( args = {} )
  Octokit.create_status( repo, sha, 'pending', params.merge(args) )
end