-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpull_requests.jl
More file actions
258 lines (235 loc) · 9.01 KB
/
pull_requests.jl
File metadata and controls
258 lines (235 loc) · 9.01 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
256
257
258
const new_package_title_regex = r"^New package: (\w*?) v(\S*?)$"
const new_version_title_regex = r"^New version: (\w*?) v(\S*?)$"
const commit_regex = r"Commit: ([0-9a-f]+)"
function is_new_package(pull_request::GitHub.PullRequest)
return occursin(new_package_title_regex, title(pull_request))
end
function is_new_version(pull_request::GitHub.PullRequest)
return occursin(new_version_title_regex, title(pull_request))
end
function check_authorization(
pkg,
pr_author_login,
authorized_authors,
authorized_authors_special_jll_exceptions,
error_exit_if_automerge_not_applicable,
)
if pr_author_login ∉ vcat(authorized_authors, authorized_authors_special_jll_exceptions)
throw_not_automerge_applicable(
AutoMergeAuthorNotAuthorized,
"Author $(pr_author_login) is not authorized to automerge. Exiting...";
error_exit_if_automerge_not_applicable=error_exit_if_automerge_not_applicable,
)
return :not_authorized
end
# A JLL-only author (e.g. `jlbuild`) is not allowed to register
# non-JLL packages.
this_is_jll_package = is_jll_name(pkg)
if (!this_is_jll_package && pr_author_login ∉ authorized_authors)
throw_not_automerge_applicable(
AutoMergeAuthorNotAuthorized,
"This package is not a JLL package. Author $(pr_author_login) is not authorized to register non-JLL packages. Exiting...";
error_exit_if_automerge_not_applicable=error_exit_if_automerge_not_applicable,
)
return :not_authorized
end
if pr_author_login ∈ authorized_authors_special_jll_exceptions
return :jll
end
return :normal
end
function parse_pull_request_title(::NewVersion, pull_request::GitHub.PullRequest)
m = match(new_version_title_regex, title(pull_request))
pkg = convert(String, m.captures[1])::String
version = VersionNumber(m.captures[2])
return pkg, version
end
function commit_from_pull_request_body(pull_request::GitHub.PullRequest)
pr_body = body(pull_request)
m = match(commit_regex, pr_body)
commit = convert(String, m.captures[1])::String
always_assert(length(commit) == 40)
return commit
end
function parse_pull_request_title(::NewPackage, pull_request::GitHub.PullRequest)
m = match(new_package_title_regex, title(pull_request))
pkg = convert(String, m.captures[1])::String
version = VersionNumber(m.captures[2])
return pkg, version
end
function pull_request_build(
api::GitHub.GitHubAPI,
pr_number::Integer,
current_pr_head_commit_sha::String,
registry::GitHub.Repo,
registry_head::String;
whoami::String,
auth::GitHub.Authorization,
authorized_authors::Vector{String},
authorized_authors_special_jll_exceptions::Vector{String},
error_exit_if_automerge_not_applicable=error_exit_if_automerge_not_applicable,
master_branch::String,
master_branch_is_default_branch::Bool,
suggest_onepointzero::Bool,
point_to_slack::Bool,
registry_deps::Vector{<:AbstractString}=String[],
check_license::Bool,
public_registries::Vector{<:AbstractString}=String[],
read_only::Bool,
environment_variables_to_pass::Vector{<:AbstractString}=String[],
)::Nothing
pr = my_retry(() -> GitHub.pull_request(api, registry, pr_number; auth=auth))
_github_api_pr_head_commit_sha = pull_request_head_sha(pr)
if current_pr_head_commit_sha != _github_api_pr_head_commit_sha
throw(
AutoMergeShaMismatch(
"Current commit sha (\"$(current_pr_head_commit_sha)\") does not match what the GitHub API tells us (\"$(_github_api_pr_head_commit_sha)\")",
),
)
end
# 1. Check if the PR is open, if not quit.
# 2. Determine if it is a new package or new version of an
# existing package, if neither quit.
# 3. Check if the author is authorized, if not quit.
# 4. Call the appropriate method for new package or new version.
if !is_open(pr)
throw_not_automerge_applicable(
AutoMergePullRequestNotOpen,
"The pull request is not open. Exiting...";
error_exit_if_automerge_not_applicable=error_exit_if_automerge_not_applicable,
)
return nothing
end
if is_new_package(pr)
registration_type = NewPackage()
elseif is_new_version(pr)
registration_type = NewVersion()
else
throw_not_automerge_applicable(
AutoMergeNeitherNewPackageNorNewVersion,
"Neither a new package nor a new version. Exiting...";
error_exit_if_automerge_not_applicable=error_exit_if_automerge_not_applicable,
)
return nothing
end
# Here we check whether or not the PR author is authorized
# for merging. If not, we will fail a guideline, but
# we will still run all the guidelines.
pkg, version = parse_pull_request_title(registration_type, pr)
pr_author_login = author_login(pr)
authorization = check_authorization(
pkg,
pr_author_login,
authorized_authors,
authorized_authors_special_jll_exceptions,
error_exit_if_automerge_not_applicable,
)
registry_master = clone_repo(registry)
if !master_branch_is_default_branch
checkout_branch(registry_master, master_branch)
end
data = GitHubAutoMergeData(;
api=api,
registration_type=registration_type,
pr=pr,
pkg=pkg,
version=version,
current_pr_head_commit_sha=current_pr_head_commit_sha,
registry=registry,
auth=auth,
authorization=authorization,
registry_head=registry_head,
registry_master=registry_master,
suggest_onepointzero=suggest_onepointzero,
point_to_slack=point_to_slack,
whoami=whoami,
registry_deps=registry_deps,
public_registries=public_registries,
read_only=read_only,
environment_variables_to_pass=environment_variables_to_pass,
)
pull_request_build(data; check_license=check_license)
rm(registry_master; force=true, recursive=true)
return nothing
end
function pull_request_build(data::GitHubAutoMergeData; check_license)::Nothing
kind = package_or_version(data.registration_type)
this_is_jll_package = is_jll_name(data.pkg)
@info(
"This is a new $kind pull request",
pkg = data.pkg,
version = data.version,
this_is_jll_package
)
update_status(
data;
state="pending",
context="automerge/decision",
description="New $kind. Pending.",
)
this_pr_can_use_special_jll_exceptions =
this_is_jll_package && data.authorization == :jll
guidelines = get_automerge_guidelines(
data.registration_type;
check_license=check_license,
this_is_jll_package=this_is_jll_package,
this_pr_can_use_special_jll_exceptions=this_pr_can_use_special_jll_exceptions,
use_distance_check=perform_distance_check(data.pr.labels),
package_author_approved=has_package_author_approved_label(data.pr.labels)
)
checked_guidelines = Guideline[]
for (guideline, applicable) in guidelines
applicable || continue
if guideline == :update_status
if !all(passed, checked_guidelines)
update_status(
data;
state="failure",
context="automerge/decision",
description="New version. Failed.",
)
end
else
check!(guideline, data)
@info(
guideline.info,
meets_this_guideline = passed(guideline),
message = message(guideline)
)
push!(checked_guidelines, guideline)
end
end
if all(passed, checked_guidelines) # success
description = "New $kind. Approved. name=\"$(data.pkg)\". sha=\"$(data.current_pr_head_commit_sha)\""
update_status(
data; state="success", context="automerge/decision", description=description
)
this_pr_comment_pass = comment_text_pass(
data.registration_type,
data.suggest_onepointzero,
data.version,
this_pr_can_use_special_jll_exceptions,
)
my_retry(() -> update_automerge_comment!(data, this_pr_comment_pass))
else # failure
update_status(
data;
state="failure",
context="automerge/decision",
description="New $kind. Failed.",
)
failing_messages = message.(filter(!passed, checked_guidelines))
this_pr_comment_fail = comment_text_fail(
data.registration_type,
failing_messages,
data.suggest_onepointzero,
data.version;
point_to_slack=data.point_to_slack,
)
my_retry(() -> update_automerge_comment!(data, this_pr_comment_fail))
throw(AutoMergeGuidelinesNotMet("The automerge guidelines were not met."))
end
return nothing
end
package_or_version(::NewPackage) = "package"
package_or_version(::NewVersion) = "version"