forked from joshrendek/github-postcommit-shinies
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgh.rb
More file actions
150 lines (131 loc) · 5.41 KB
/
Copy pathgh.rb
File metadata and controls
150 lines (131 loc) · 5.41 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'sinatra'
require 'yaml'
require 'json'
require 'curb'
require 'erubis'
begin
# local
config = YAML::load(Erubis::Eruby.new(File.open('config.yml').read).result)
rescue
# heroku
config = YAML::load(Erubis::Eruby.new(File.open('config.yml.heroku_sample').read).result)
end
set :sessions, true
set :logging, true
set :port, 3000
set :gh_user, config['user']
set :gh_pass, config['pass']
set :gh_token, config['token']
set :gh_api, "https://api.github.com/"
set :gh_issue, "repos/:owner/:repo/issues/:number/labels" # get
set :gh_add_label, "repos/:owner/:repo/issues/:number/labels" # post
set :gh_remove_label, "repos/:owner/:repo/issues/:number/labels/:name" # delete
set :gh_update_label, "repos/:owner/:repo/issues/:number/labels" # put
set :gh_edit_issue, "repos/:owner/:repo/issues/:number" # patch
def get_labels(user, repo, issue)
endpoint = options.gh_issue.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue)
curl = Curl::Easy.http_get(options.gh_api + endpoint + '?access_token=' + options.gh_token) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
json = JSON.parse(curl.body_str)
json['issue']['labels']
end
def add_labels(user, repo, issue, label)
endpoint = options.gh_add_label.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue)
begin
Curl::Easy.http_post(options.gh_api + endpoint + '?access_token=' + options.gh_token, label) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
p 'added label(s)'
rescue e
p e.to_s
end
end
def remove_label(user, repo, issue, label)
endpoint = options.gh_remove_label.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue).gsub(':name', label)
Curl::Easy.http_delete(options.gh_api + endpoint + '?access_token=' + options.gh_token) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
p 'removed label(s)'
end
def update_labels(user, repo, issue, label)
endpoint = options.gh_update_label.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue)
Curl::Easy.http_put(options.gh_api + endpoint + '?access_token=' + options.gh_token, label) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
p 'updated labels'
end
def assign_issue(user, repo, issue, assignee)
endpoint = options.gh_edit_issue.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue)
Curl.patch(options.gh_api + endpoint + '?access_token=' + options.gh_token, {:assignee => assignee}.to_json) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
p 'assigned issue to ' + assignee
end
def assign_milestone(user, repo, issue, milestone)
endpoint = options.gh_edit_issue.gsub(':owner', user).gsub(':repo', repo).gsub(':number', issue)
Curl.patch(options.gh_api + endpoint + '?access_token=' + options.gh_token, {:milestone => milestone}.to_json) do |c|
c.http_auth_types = :basic
c.username = options.gh_user
c.password = options.gh_pass
c.headers['User-Agent'] = 'Github-Issuehooks'
end
p 'assigned new milestone'
end
get '/test' do
p 'hello world'
end
post '/' do
request.body.rewind
json = request.body.read.to_s
push = json && json.length >= 2 ? JSON.parse(json) : nil
repo = push['repository']['name']
owner = push['repository']['owner']['name']
push['commits'].each do |c|
m = c['message']
issue = m.scan(/\#[0-9]+/)
if issue.size == 1 #only check for other goodies if an issue is mentioned
# user assignment
user = m.scan(/\=[a-zA-Z0-9\-]+/)[0] # hyphens and alphanumerics are allowed in github usernames
assign_issue(owner, repo, issue[0], user[1..-1]) if user
# label toggles
label_toggles = m.scan(/\~[a-zA-Z0-9]+|\~".*?"|\~'.*?'/)
if label_toggles.size > 0
current_labels = get_labels()
label_toggles.map { |s| s[1..-1] } # remove beginning ~
label_toggles.reject { |l| current_labels.any? { |x| x[:name] == l } } # remove strings from label_toggles that match the name of any hash in current_labels
update_label(owner, repo, issue[0], label_toggles)
end
# label adding
label_additions = m.scan(/\+[a-zA-Z0-9]+|\+".*?"|\+'.*?'/)
if label_additions.size > 0
add_labels(owner, repo, issue[0], label_additions)
end
# label removing
label_deletions = m.scan(/\-[a-zA-Z0-9]+|\-".*?"|\-'.*?'/)
if label_deletions.size > 0
remove_labels(owner, repo, issue[0], label_deletions)
end
# milestone assignment
milestone = m.scan(/\^[0-9]+/)[0]
assign_milestone(owner, repo, issue[0], milestone[1..-1]) if milestone
end
end
status 200
body ''
end