github_informer 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +51 -0
  4. data/bin/gh_execute +60 -0
  5. data/lib/github_informer.rb +85 -0
  6. metadata +64 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bede0acb6870c5ba232527f3eed280ea5b5c1c5d
4
+ data.tar.gz: 6ea2200407bbcfcc44c6986fae3e18337bd1823d
5
+ SHA512:
6
+ metadata.gz: 83e0f5b017e0d649aff7cd433ff76fd990618766c444981e6fca2e11741b0372a97fc3461188da768b64e10cacaa1dc5328c8a3123656231fc683af971ad8507
7
+ data.tar.gz: b93dc24ee431518428ff7c3ffe89c58284127a3eaa97400c2fa89b08affec3dbad5e73552815d894e3299195e6b2f49c7cb9fc3ed5e792e741e4caf29204cbcb
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 BBC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,51 @@
1
+ # github_informer
2
+
3
+ Gem for submitting useful metadata to github to inform pull requests
4
+
5
+ ## Submitting CI information
6
+
7
+ GithubInformer lets you wrap execution of your test or build scripts with github
8
+ hooks to let you add CI status check information to your commits. These status
9
+ checks appear alongside your pull request discussion in github and help with the
10
+ conversation.
11
+
12
+ ### Using the CLI
13
+
14
+ The CLI is the easiest method of capturing your status checks. Simply install
15
+ the gem and wrap your ci commands like this example:
16
+
17
+ gh_execute --cmd 'bundle exec rspec'
18
+
19
+ You can add a context to the execution so it's more identifiable in github:
20
+
21
+ gh_execute --context 'Rspec tests' --cmd 'bundle exec rspec'
22
+
23
+ There are numerous optional arguments for more control:
24
+
25
+ gh_execute --cmd 'bundle exec rspec'
26
+ [--context <CONTEXT>]
27
+ [--path <PATH_TO_CHECKOUT>]
28
+ [--url <URL_LINK_TO_CI_JOB>]
29
+
30
+ If you're running in jenkins, url will automatically get picked up by the
31
+ environment.
32
+
33
+ ### Using the API
34
+
35
+ You can have much more control over the messages that get reported into github
36
+ if you write a ruby script to wrap your execution. For example, the following
37
+ script gives comprehensive failure messages:
38
+
39
+ # Create new informer
40
+ g = GithubInformer.new(:context => 'HiveCI', :url => ENV['HIVE_JOB_URL'] )
41
+
42
+ # Add a pending status check to github
43
+ g.report_start( :description => 'Executing unit tests in Hive environment')
44
+
45
+ # Wrap execution of the script
46
+ g.execute( 'bundle exec rake unittests' )
47
+
48
+ # Report a different message depending on the exit value of the script
49
+ g.report_end( 0 => [:pass, 'Unit tests were fine: good to merge'],
50
+ 1 => [:pending, 'Tests are re-running. Hold off merge.'],
51
+ 2..200 => [:fail, 'Unit test failure. Do not merge'] )
@@ -0,0 +1,60 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'ostruct'
5
+ require 'github_informer'
6
+
7
+ class CLIParser
8
+
9
+ #
10
+ # Return a structure describing the options.
11
+ #
12
+ def self.parse(args)
13
+
14
+ # Default values
15
+ options = OpenStruct.new
16
+ options.context = 'Custom Check'
17
+ options.path = '.'
18
+ options.url = ENV['BUILD_URL'] #Chances are this is running in jenkins
19
+
20
+ opt_parser = OptionParser.new do |opts|
21
+ opts.banner = "Usage: ghi_execute [options]"
22
+
23
+ opts.on("-c", "--context NAME",
24
+ "Name to identify this check.") do |name|
25
+ options.context = name
26
+ end
27
+
28
+ opts.on("-p", "--path PATH",
29
+ "Path to the cloned github repo") do |path|
30
+ options.path = path
31
+ end
32
+
33
+ opts.on("-c", "--cmd COMMAND",
34
+ "The command to run") do |cmd|
35
+ options.cmd = cmd
36
+ end
37
+
38
+ opts.on("-u", "--url URL",
39
+ "Url to link back to the execution from github") do |path|
40
+ options.url = url
41
+ end
42
+
43
+ end
44
+
45
+ opt_parser.parse!(args)
46
+ options
47
+ end
48
+ end
49
+
50
+ options = CLIParser.parse(ARGV)
51
+
52
+ raise "Must include a cmd to execute ( --cmd 'make' )" if !options.cmd
53
+
54
+ g = GithubInformer.new(:context => options.context, :url => options.url, :repo => options.path)
55
+
56
+ g.report_start()
57
+
58
+ g.execute( options.cmd )
59
+
60
+ g.report_end()
@@ -0,0 +1,85 @@
1
+ require 'octokit'
2
+
3
+ class GithubInformer
4
+
5
+ attr_accessor :params, :repo, :sha, :result
6
+
7
+ # Create a new GithubInformer
8
+ # GithubInformer.new( :context => 'HiveCI',
9
+ # :url => 'http://hive.local',
10
+ # :repo => '/path/to/checkout' )
11
+ def initialize( args = {} )
12
+
13
+ ::Octokit.configure do |c|
14
+ c.access_token = args[:github_auth] || ENV['GITHUB_AUTH']
15
+ c.auto_paginate = true
16
+ end
17
+
18
+ @params = {}
19
+ @params[:context] = args[:context] or raise "Requires a context"
20
+ @params[:target_url] = args[:url]
21
+
22
+ path = args[:repo] || '.'
23
+ @repo = GithubInformer.determine_repo(path)
24
+ @sha = GithubInformer.determine_sha(path)
25
+ end
26
+
27
+ # Report start of job to github
28
+ # ghi.report_start( :description => 'Build job in progress')
29
+ def report_start( args = {} )
30
+ Octokit.create_status( repo, sha, 'pending', params.merge(args) )
31
+ end
32
+
33
+ def execute( cmd )
34
+ system( cmd )
35
+ @result = $?.exitstatus
36
+ end
37
+
38
+ # Report
39
+ # ghi.report_end( 0 => [ :pass, 'Build passed successfully'],
40
+ # 1..100 => [ :fail, 'Build had errors' ],
41
+ # :default => [ :error, 'Build totally broken'] )
42
+ def report_end( args = { 0 => [:pass, 'Good to merge'],
43
+ :default => [:fail, 'Do not merge' ] } )
44
+ if result
45
+ (status,description) = args[:default] || [ :fail, 'Do not merge' ]
46
+ hash = args.select { |a| a === result }
47
+ if !hash.empty?
48
+ (status, description) = hash.values.flatten
49
+ end
50
+
51
+ status = GithubInformer.normalise_status(status)
52
+ Octokit.create_status( repo, sha, status, params.merge( {:description => description} ) )
53
+ else
54
+ Octokit.create_status( repo, sha, 'error', params.merge( {:description => 'Program never completed'} ) )
55
+ end
56
+ end
57
+
58
+ # Make sure the statuses are what the github api expect
59
+ def self.normalise_status(status)
60
+ case
61
+ when status.to_s.match(/fail/)
62
+ 'failure'
63
+ when status.to_s.match(/pass/)
64
+ 'success'
65
+ else
66
+ status.to_s
67
+ end
68
+ end
69
+
70
+
71
+ def self.determine_repo(path)
72
+ output = `cd #{path} && git remote -v | grep github`
73
+ res = output.match(/github.com:(.*\/.*).git/)
74
+ repo = res.captures.first
75
+ raise "Couldn't determine repo" if !repo
76
+ repo
77
+ end
78
+
79
+ def self.determine_sha(path)
80
+ sha = `cd #{path} && git rev-parse HEAD`.strip
81
+ raise "Couldn't determine sha" if !sha || sha.length == 0
82
+ sha
83
+ end
84
+
85
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: github_informer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - BBC
8
+ - David Buckhurst
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-08-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: octokit
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ description: Publish CI results to Github
29
+ email: david.buckhurst@bbc.co.uk
30
+ executables:
31
+ - gh_execute
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - README.md
37
+ - bin/gh_execute
38
+ - lib/github_informer.rb
39
+ homepage: https://github.com/bbc/github_informer
40
+ licenses:
41
+ - MIT
42
+ metadata: {}
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubyforge_project:
59
+ rubygems_version: 2.4.5
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Github Informer
63
+ test_files: []
64
+ has_rdoc: