forked from huboard/huboard
/
pebble.rb
247 lines (192 loc) · 8.03 KB
/
pebble.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require 'time'
require 'json'
module Stint
class Pebble
attr_accessor :github
def build_board(user_name, repo)
issues = get_issues(user_name, repo)
issues_by_label = issues.group_by { |issue| issue["current_state"]["name"] }
all_labels = labels(user_name, repo)
all_labels = all_labels.each_with_index do |label, index|
x = issues_by_label[label[:name]]
label[:issues] = (x || []).sort_by { |i| i["_data"]["order"] || i["number"].to_f}
label
end
all_labels[0][:issues] = (issues_by_label["__nil__"] || []).concat(all_labels[0][:issues]).sort_by { |i| i["_data"]["order"] || i["number"].to_f} unless all_labels.empty?
{
labels: all_labels,
milestones: milestones(user_name, repo)
}
end
def board(user_name, repo)
board = build_board(user_name, repo)
labels = github.labels(user_name,repo)
labels
.select{ |l| @link_pattern.match l["name"] }
.each do |l|
match = @link_pattern.match l["name"]
user, repo = match[:user_name], match[:repo]
begin
linked_board = build_board user, repo
next if linked_board[:labels].size != board[:labels].size
board[:labels].each_with_index do |label, index|
linked_issues = linked_board[:labels][index][:issues].map do |i|
i["repo"][:color] = l["color"]
i
end
label[:issues] = label[:issues].concat(linked_issues).sort_by { |i| i["_data"]["order"] || i["number"].to_f}
end
board[:milestones].concat(linked_board[:milestones]).sort_by { |m| m["_data"]["order"] || m["number"].to_f}
rescue
puts "Warning: Unable to link board: #{user}, #{repo}"
end
end
return board
end
def get_issues(user_name, repo)
issues = github.get_issues user_name, repo
issues.each do |issue|
issue["current_state"] = current_state(issue)
issue["_data"] = embedded_data issue["body"]
issue["repo"] = {owner: {login:user_name},name: repo}
end
issues.sort_by { |i| i["_data"]["order"] || i["number"].to_f}
end
def reorder_issue(user_name, repo, number, index)
post_data = {"number" => number}
issue = github.issue_by_id user_name, repo, number
_data = embedded_data issue["body"]
if _data.empty?
post_data["body"] = issue["body"].concat "\r\n<!--\r\n@huboard:#{JSON.dump({:order => index.to_f})}\r\n-->\r\n"
else
post_data["body"] = issue["body"].gsub /@huboard:.*/, "@huboard:#{JSON.dump(_data.merge({"order" => index.to_f}))}"
end
github.update_issue user_name, repo, post_data
end
def reorder_milestone(user_name, repo, number, index, status)
post_data = {:number => number}
milestone = github.milestone user_name, repo, number
_data = embedded_data milestone["description"]
if _data.empty?
post_data["description"] = milestone["description"].concat "\r\n<!--\r\n@huboard:#{JSON.dump({"status" => status,"order" => index.to_f})}\r\n-->\r\n"
else
post_data["description"] = milestone["description"].gsub /@huboard:.*/, "@huboard:#{JSON.dump(_data.merge({"order" => index.to_f, "status" => status}))}"
end
github.update_milestone user_name, repo, post_data
end
def current_state(issue)
r = @column_pattern
issue["labels"].sort_by {|l| l["name"]}.reverse.find {|x| r.match(x["name"])} || {"name" => "__nil__"}
end
def labels(user_name, repo)
response = github.labels(user_name, repo)
labels = []
response.each do |label|
r = @column_pattern
hash = r.match (label["name"])
labels << { name: label["name"], index: hash[:id], text: hash[:name], color: label["color"]} unless hash.nil?
end
labels.sort_by { |l| l[:index].to_i }
end
def create_board(user_name, repo, hook)
github.create_label user_name, repo, :name => "0 - Backlog", :color => "CCCCCC"
github.create_label user_name, repo, :name => "1 - Ready", :color => "CCCCCC"
github.create_label user_name, repo, :name => "2 - Working", :color => "CCCCCC"
github.create_label user_name, repo, :name => "3 - Done", :color => "CCCCCC"
create_hook user_name, repo, hook
end
def hook_exists(user_name, repo, token)
hooks = github.hooks user_name, repo
return !hooks.find{ |x| x["config"]["url"] == token }.nil?
end
def create_hook(user_name, repo, token)
return { message: "hook already exists", success: false } if hook_exists user_name, repo, token
params = {
name:"web",
config: {
url: token
},
events: ["push"],
active: true
}
github.create_hook( user_name, repo, params).merge( { success: true, message: "hook created successfully"})
end
def push_card(user_name, repo, commit)
r = /(?<command>\w+) [gG][hH]-+(?<issue>\d+)/
match = r.match(commit["message"])
return "no match" unless match
return "no match" unless /push|pushes|moves?/i.match( match[:command])
issue = github.issue_by_id user_name, repo, match[:issue]
#return issue
state = current_state(issue)
sr = @column_pattern
next_state = sr.nil? ? 0 : (sr[:id].to_i + 1)
labels = github.labels user_name, repo
next_label = labels.find { |l| /#{next_state}\s*- *.+/.match(l["name"]) }
return github.close_issue(user_name, repo, issue) if next_label.nil?
issue["labels"] << next_label
issue["labels"] = issue["labels"].delete_if { |l| l["name"] == state["name"] }
github.update_issue user_name, repo, {"number" => issue["number"],"labels" => issue["labels"]}
end
def move_card(user_name, repo, the_issue, index)
labels = github.labels user_name, repo
new_state = labels.find { |l| /#{index}\s*- *.+/.match(l["name"]) }
issue = github.issue_by_id user_name, repo, the_issue["number"]
state = current_state(issue)
issue["labels"] << new_state unless new_state.nil?
issue["labels"] = issue["labels"].delete_if { |l| l["name"] == state["name"] }
github.update_issue user_name, repo, {"number" => issue["number"], "labels" => issue["labels"]}
end
def milestones(user_name, repo)
milestones = github.milestones user_name, repo
milestones = milestones.map { |m|
m["pull_requests"] = m[:issues].select {|i| !i["pull_request"]["html_url"].nil?}
m[:issues] = m[:issues].delete_if {|i| !i["pull_request"]["html_url"].nil?}
m["open_issues"] = m[:issues].size
m["_data"] = embedded_data m["description"]
m
}
milestones.sort_by { |m| m["_data"]["order"] || m["number"].to_f}
end
def embedded_data(body)
r = /@huboard:(.*)/
match = r.match body
return { } if match.nil?
begin
JSON.load(match[1])
rescue
return {}
end
end
def close_card(user_name, repo, the_issue)
github.close_issue(user_name, repo, the_issue)
end
def all_repos
the_repos = github.repos
github.orgs.each do |org|
the_repos.concat(github.repos(org["login"]))
end
the_repos.sort_by{|r| r["pushed_at"] || "1111111111111111"}.reverse
end
def self.register(command, &block)
@@sub ||= {}
@@sub[command] = block
end
def self.deliver(payload)
consumers = @@sub
r = /^(?<command>[A-Z]+) GH-(?<issue>[0-9]+)/
payload["commits"].each do |c|
match = r.match c["message"]
next if r.match match.nil?
next unless consumers.has_key? match[:command]
consumers[match[:command]].call payload, match[:issue]
end
end
def initialize(github)
@github = github
@column_pattern = /(?<id>\d+) *- *(?<name>.+)/
@priority_pattern = /(?<name>.+) - (?<id>\d+)/
@link_pattern = /Link <=> (?<user_name>.*)\/(?<repo>.*)/
end
end
end