-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathgithub_utils.jl
More file actions
255 lines (227 loc) · 9.12 KB
/
github_utils.jl
File metadata and controls
255 lines (227 loc) · 9.12 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
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
248
249
250
251
252
253
254
255
function register_rights_error(evt, user)
if is_owned_by_organization(evt)
org = evt.repository.owner.login
return "**Register Failed**\n$(mention(user)), it looks like you are not a publicly listed member/owner in the parent organization ($(org)).\nIf you are a member/owner, you will need to change your membership to public. See [GitHub Help](https://help.github.com/en/articles/publicizing-or-hiding-organization-membership)"
else
return "**Register Failed**\n$(mention(user)), it looks like you don't have collaborator status on this repository."
end
end
get_access_token(event) = create_access_token(Installation(event.payload["installation"]), get_jwt_auth())
get_user_auth() = GitHub.authenticate(CONFIG["github"]["token"])
get_jwt_auth() = GitHub.JWTAuth(CONFIG["github"]["app_id"], CONFIG["github"]["priv_pem"])
function get_sha_from_branch(reponame, brn; auth = GitHub.AnonymousAuth())
try
b = branch(reponame, Branch(brn); auth=auth)
sha = b.sha !== nothing ? b.sha : b.commit.sha
return sha, nothing
catch ex
d = parse_github_exception(ex)
if d["Status Code"] == "404" && d["Message"] == "Not found"
return nothing, "Branch `$brn` not found"
else
rethrow(ex)
end
end
return nothing, nothing
end
is_owned_by_organization(event) = event.repository.owner.typ == "Organization"
function is_comment_by_collaborator(event)
@debug("Checking if comment is by collaborator")
user = get_user_login(event.payload)
user == "github-actions[bot]" && return true
return iscollaborator(event.repository, user; auth=get_access_token(event))
end
function is_comment_by_org_owner_or_member(event)
@debug("Checking if comment is by repository parent organization owner or member")
org = event.repository.owner.login
user = get_user_login(event.payload)
user == "github-actions[bot]" && return true
if get(CONFIG, "check_private_membership", false)
return GitHub.check_membership(org, user; auth=get_user_auth())
else
return GitHub.check_membership(org, user; public_only=true)
end
end
has_register_rights(event) = is_comment_by_collaborator(event) || is_owned_by_organization(event) && is_comment_by_org_owner_or_member(event)
is_pull_request(payload::Dict{<:AbstractString}) = haskey(payload, "pull_request") || haskey(payload, "issue") && haskey(payload["issue"], "pull_request")
is_commit_comment(payload::Dict{<:AbstractString}) = haskey(payload, "comment") && !haskey(payload, "issue")
function get_prid(payload::Dict{<:AbstractString})
if haskey(payload, "pull_request")
return payload["pull_request"]["number"]
elseif haskey(payload, "issue")
return payload["issue"]["number"]
else
error("Don't know how to get pull request number")
end
end
get_comment_commit_id(event) = event.payload["comment"]["commit_id"]
get_clone_url(event) = event.payload["repository"]["clone_url"]
function make_comment(evt::WebhookEvent, body::AbstractString)
CONFIG["reply_comment"] || return
@debug("Posting comment to PR/issue")
headers = Dict("private_token" => CONFIG["github"]["token"])
params = Dict("body" => body)
repo = evt.repository
auth = get_user_auth()
if is_commit_comment(evt.payload)
GitHub.create_comment(repo, get_comment_commit_id(evt),
:commit; headers=headers,
params=params, auth=auth)
else
GitHub.create_comment(repo, get_prid(evt.payload),
:issue; headers=headers,
params=params, auth=auth)
end
end
function get_html_url(payload::Dict{<:AbstractString})
if haskey(payload, "pull_request")
return payload["pull_request"]["html_url"]
elseif haskey(payload, "issue")
if haskey(payload, "comment")
return payload["comment"]["html_url"]
else
return payload["issue"]["html_url"]
end
elseif haskey(payload, "comment")
return payload["comment"]["html_url"]
else
error("Don't know how to get html_url")
end
end
function set_status(rp, state, desc)
CONFIG["set_status"] || return
repo = rp.reponame
kind = rp.evt.kind
payload = rp.evt.payload
if kind == "pull_request"
commit = payload["pull_request"]["head"]["sha"]
params = Dict("state" => state,
"context" => CONFIG["github"]["user"],
"description" => desc)
GitHub.create_status(repo, commit;
auth=get_access_token(rp.evt),
params=params)
end
end
set_pending_status(rp) = set_status(rp, "pending", "Processing request...")
set_error_status(rp) = set_status(rp, "error", "Failed to register")
set_success_status(rp) = set_status(rp, "success", "Done")
function parse_github_exception(ex::ErrorException)
msgs = map(strip, split(ex.msg, '\n'))
d = Dict()
for m in msgs
a, b = split(m, ":"; limit=2)
d[a] = strip(b)
end
return d
end
function is_pr_exists_exception(ex)
d = parse_github_exception(ex)
if d["Status Code"] == "422" &&
match(r"A pull request already exists", d["Errors"]) !== nothing
return true
end
return false
end
function get_user_login(payload::Dict{<:AbstractString})
if haskey(payload, "comment")
return payload["comment"]["user"]["login"]
elseif haskey(payload, "issue")
return payload["issue"]["user"]["login"]
elseif haskey(payload, "pull_request")
return payload["pull_request"]["user"]["login"]
else
error("Don't know how to get user login")
end
end
function get_user_id(payload::Dict{<:AbstractString})
if haskey(payload, "comment")
return payload["comment"]["user"]["id"]
elseif haskey(payload, "issue")
return payload["issue"]["user"]["id"]
elseif haskey(payload, "pull_request")
return payload["pull_request"]["user"]["id"]
else
error("Don't know how to get user id")
end
end
get_repo_owner_id(payload::Dict{<:AbstractString}) = payload["repository"]["owner"]["id"]
function get_body(payload::Dict{<:AbstractString})
if haskey(payload, "comment")
return payload["comment"]["body"]
elseif haskey(payload, "issue")
return payload["issue"]["body"]
elseif haskey(payload, "pull_request")
return payload["pull_request"]["body"]
else
error("Don't know how to get body")
end
end
# We will only remove these labels, since they are the ones we control
const REGISTRATOR_CONTROLLED_LABELS = ["new package", "major release", "minor release",
"patch release", "BREAKING"]
function create_or_find_pull_request(
repo::AbstractString,
params::Dict{<:AbstractString, Any},
rbrn::RegBranch
)
pr = nothing
msg = ""
auth = get_user_auth()
try
pr = create_pull_request(repo; auth=auth, params=params)
msg = "created"
@debug("Pull request created")
try # add labels
if get(rbrn.metadata, "labels", nothing) !== nothing
add_labels(repo, pr, rbrn.metadata["labels"]; auth=auth)
end
catch
@debug "Failed to add labels, ignoring."
end
catch ex
if is_pr_exists_exception(ex)
@debug("Pull request already exists, not creating")
msg = "updated"
else
rethrow(ex)
end
end
if pr === nothing
prs, _ = pull_requests(repo; auth=auth, params=Dict(
"state" => "open",
"base" => params["base"],
"head" => params["head"],
))
if !isempty(prs)
@assert length(prs) == 1 "PR lookup should only contain one result"
@debug("PR found")
pr = prs[1]
end
if pr === nothing
error("Registration PR already exists but unable to find it")
else
update_pull_request(repo, pr.number; auth=auth, params=Dict("body" => params["body"], "title" => params["title"]))
try # update labels
if get(rbrn.metadata, "labels", nothing) !== nothing
# Remove existing labels we control if they are not
# in `rbrn.metadata["labels"]`
existing_labels = [l.name for l in labels(repo, pr; auth=auth)]
for label in existing_labels
label in REGISTRATOR_CONTROLLED_LABELS || continue
label in rbrn.metadata["labels"] && continue
remove_label(repo, pr, label; auth=auth)
end
# Add any labels in `rbrn.metadata["labels"]` that don't
# already exist in the PR.
labels_to_add = setdiff(rbrn.metadata["labels"], existing_labels)
add_labels(repo, pr, labels_to_add; auth=auth)
end
catch
@debug "Failed to update labels, ignoring."
end
end
end
return pr, msg
end
mention(u::GitHub.Owner) = mention(u.login)