-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rb
53 lines (46 loc) · 1.51 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
53
# coding: utf-8
require 'octokit'
require 'open-uri'
class Github
REPOS = 'hayashima/bondgate'
def initialize
Octokit.auto_paginate = true
@client = Octokit::Client.new(:access_token => ENV['GITHUB_ACCESS_KEY'])
@request = @client.pull_requests(REPOS).select { |v| v.head.ref == ENV['BRANCH'] }.first
end
def notice(status)
exit unless @request
case status
when 'failed'
failed
when 'passed'
passed
else
raise 'status not found.'
end
end
private
def passed
message = '![](https://raw.githubusercontent.com/hayashima/github_notification/master/img/passed.png)'
@client.add_comment(REPOS, @request.number, message)
@client.create_status(REPOS, @request.head.sha, 'success')
end
def failed
message = 'Jenkins build failed.'
begin
base_uri = "http://jenkins.bondgate.jp/job/#{ENV['PARENT_JOB_NAME']}/#{ENV['PARENT_BUILD_NUMBER']}/console"
result = open("#{base_uri}Text",
:http_basic_authentication=>['github', ENV['JENKINS_PASSWORD']])
result.read.match(/Failed examples:[\s\S]*/) do |md|
message = "![](https://raw.githubusercontent.com/hayashima/github_notification/master/img/failed.jpg)\n" +
md[0].gsub(/#(\d+)/,':hash:\1') + "\n\n#{base_uri}"
end
rescue OpenURI::HTTPError
# pass
end
@client.add_comment(REPOS, @request.number, message)
@client.create_status(REPOS, @request.head.sha, 'failure')
end
end
github = Github.new
github.notice(ENV['STATUS'])