/
githubclient.rb
84 lines (68 loc) · 1.56 KB
/
githubclient.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/ruby
require_relative 'githubapi'
class GithubClient
RESULT_MESSAGES = {
success: 'succeeded',
failure: 'failed',
error: 'has an error',
pending: 'is pending',
}
def initialize(conf = {})
@config = conf
end
def organization
@config[:organization]
end
def repository
@config[:repository]
end
def org_repo
"#{organization}/#{repository}"
end
def context
@config[:context]
end
def create_status(commit, details)
result = details["status"].to_sym
GithubAPI.new.client.create_status(
org_repo,
commit,
result,
{ context: context,
description: status_description(details["message"], result),
target_url: details["target_url"].to_s
}
)
end
def status_description(description, status)
description ||= RESULT_MESSAGES[status]
description.to_s
end
def full_sha_status(sha)
begin
status = GithubAPI.new.client.status(org_repo, sha)
status.statuses.select{ |s| s.context == context }.first || {}
rescue
{}
end
end
def sha_status(sha)
full_sha_status(sha).state rescue ""
end
def pull_request(pull)
GithubAPI.new.client.pull_request(org_repo, pull)
end
def pull_info(pull)
pull_request(pull).to_attrs
end
def pull_latest_sha(pull)
pull_request(pull).head.sha rescue ''
end
def latest_sha?(pull, sha)
pull_latest_sha(pull) == sha
end
# state = [:open|:closed]
def all_pull_requests(state)
GithubAPI.new.client.pull_requests(org_repo, state: state.to_s)
end
end