This repository has been archived by the owner on Apr 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
/
command.ex
401 lines (362 loc) Β· 10.6 KB
/
command.ex
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
defmodule BorsNG.Command do
@moduledoc """
Resolve magic comments.
# try
The bors comment CLI allows parameters to be passed to try.
Assuming the activation phrase is "bors try", you can do things like this:
bors try --layout
And the commit will come out like:
Try #13: --layout
Your build scripts should then inspect the commit message
to pull out the commands.
"""
alias BorsNG.Worker.Attemptor
alias BorsNG.Worker.Batcher
alias BorsNG.Command
alias BorsNG.Database.Context.Permission
alias BorsNG.Database.Repo
alias BorsNG.Database.Patch
alias BorsNG.Database.Project
alias BorsNG.Database.User
alias BorsNG.GitHub
alias BorsNG.Worker.Syncer
import BorsNG.Router.Helpers
defstruct(
project: nil,
commenter: nil,
pr: nil,
pr_xref: nil,
patch: nil,
comment: "")
@command_trigger(
Application.get_env(:bors_frontend, BorsNG)[:command_trigger])
@type t :: %BorsNG.Command{
project: Project.t,
commenter: User.t,
pr: map | nil,
pr_xref: integer,
patch: Patch.t | nil,
comment: binary}
@doc """
If the GitHub PR is not already in this struct, fetch it.
"""
@spec fetch_pr(t) :: t
def fetch_pr(c) do
case {c.pr, c.pr_xref} do
{nil, pr_xref} ->
pr = c.project.repo_xref
|> Project.installation_connection(Repo)
|> GitHub.get_pr!(pr_xref)
%Command{c | pr: pr}
{_, _} ->
c
end
end
@doc """
If the Patch is not already in this struct, fetch it.
This will not re-sync from GitHub unless it isn't even in the database.
"""
@spec fetch_patch(t) :: t
def fetch_patch(c) do
case {c.patch, c.pr, c.pr_xref} do
{nil, nil, pr_xref} ->
case Repo.get_by(Patch, project_id: c.project.id, pr_xref: pr_xref) do
nil -> c |> fetch_pr() |> fetch_patch()
patch -> %Command{c | patch: patch}
end
{nil, pr, _} ->
patch = Syncer.sync_patch(c.project.id, pr)
%Command{c | patch: patch}
{_, _, _} ->
c
end
end
@type cmd ::
{:try, binary} |
{:activate_by, binary} |
{:set_priority, integer()} |
:activate |
:deactivate |
:delegate |
{:delegate_to, binary} |
{:autocorrect, binary} |
:ping
@doc """
Parse a comment for bors commands.
"""
@spec parse(nil) :: []
def parse(nil) do
[]
end
@spec parse(binary) :: [cmd]
def parse(comment) do
comment
|> String.splitter("\n")
|> Enum.flat_map(fn(string) ->
trim_and_parse_cmd(Regex.named_captures(regex(), string))
end)
end
def regex, do: ~r/^#{@command_trigger}:?\s(?<command>.+)/i
def trim_and_parse_cmd(%{"command" => cmd}) do
cmd
|> String.trim()
|> parse_cmd()
end
def trim_and_parse_cmd(_), do: []
def parse_cmd("try" <> arguments), do: [{:try, arguments}]
def parse_cmd("r+ p=" <> rest), do: parse_priority(rest) ++ [:activate]
def parse_cmd("r+" <> _), do: [:activate]
def parse_cmd("r-" <> _), do: [:deactivate]
def parse_cmd("r=" <> arguments), do: parse_activation_args(arguments)
def parse_cmd("delegate+" <> _), do: [:delegate]
def parse_cmd("delegate=" <> arguments), do: parse_delegation_args(arguments)
def parse_cmd("+r" <> _), do: [{:autocorrect, "r+"}]
def parse_cmd("-r" <> _), do: [{:autocorrect, "r-"}]
def parse_cmd("ping" <> _), do: [:ping]
def parse_cmd("p=" <> rest), do: parse_priority(rest)
def parse_cmd(_), do: []
@doc ~S"""
The username part of an activation-by command is defined like this:
* It may start with whitespace
* @-signs are stripped
* ", " is converted to ","
* Otherwise, whitespace ends it.
iex> alias BorsNG.Command
iex> Command.parse_activation_args("", " this, is, whitespace heavy")
"this,is,whitespace"
iex> Command.parse_activation_args("", " @this, @has, @ats")
"this,has,ats"
iex> Command.parse_activation_args("", " trimmed ")
"trimmed"
iex> Command.parse_activation_args("", "what\never")
"what"
iex> Command.parse_activation_args("", "")
""
iex> Command.parse_activation_args("somebody")
[{:activate_by, "somebody"}]
iex> Command.parse_activation_args("")
[]
iex> Command.parse_activation_args(" ")
[]
iex> Command.parse_activation_args("somebody p=10")
[{:set_priority, 10}, {:activate_by, "somebody"}]
"""
def parse_activation_args("", string) do
{rest, mentions} = string
|> String.trim()
|> String.replace(~r/, */, ",")
|> String.split("\n", parts: 2)
|> List.first()
|> String.trim()
|> String.split(~r/, */)
|> Enum.map(fn s -> String.replace(s, "@", "") end)
|> List.pop_at(-1)
[last_mention | rest_list] = rest
|> String.trim()
|> String.split(~r/\s+/, parts: 2)
mentions = mentions ++ [last_mention]
mentions = Enum.join(mentions, ",")
params = case rest_list do
[] -> nil
[rest] ->
rest
|> String.trim()
|> String.split("=", parts: 2)
|> Enum.map(&String.trim(&1))
end
case params do
["p", priority_s] ->
{priority_i, _} = Integer.parse(priority_s)
{ mentions, %{p: priority_i}}
_ -> mentions
end
end
def parse_activation_args(arguments) do
arguments = parse_activation_args("", arguments)
case arguments do
"" -> []
{mentions, %{p: p}} -> [{:set_priority, p}, {:activate_by, mentions}]
arguments -> [{:activate_by, arguments}]
end
end
@doc ~S"""
The username part of a delegate-to command is defined like this:
* It may start with whitespace
* @-signs are stripped
* ", " is converted to ","
* Otherwise, whitespace ends it.
* It's split on comma.
iex> alias BorsNG.Command
iex> Command.parse_delegation_args(" this, is, whitespace heavy")
[
{:delegate_to, "this"},
{:delegate_to, "is"},
{:delegate_to, "whitespace"}]
iex> Command.parse_delegation_args(" @this, @has, @ats")
[{:delegate_to, "this"}, {:delegate_to, "has"}, {:delegate_to, "ats"}]
iex> Command.parse_delegation_args(" trimmed ")
[{:delegate_to, "trimmed"}]
iex> Command.parse_delegation_args("what\never")
[{:delegate_to, "what"}]
iex> Command.parse_delegation_args("somebody")
[{:delegate_to, "somebody"}]
iex> Command.parse_delegation_args("")
[]
iex> Command.parse_delegation_args(" ")
[]
"""
def parse_delegation_args([], "", " " <> rest) do
parse_delegation_args([], "", rest)
end
def parse_delegation_args(l, nick, "@" <> rest) do
parse_delegation_args(l, nick, rest)
end
def parse_delegation_args(l, nick, ", " <> rest) do
parse_delegation_args([nick | l], "", rest)
end
def parse_delegation_args(l, nick, "," <> rest) do
parse_delegation_args([nick | l], "", rest)
end
def parse_delegation_args(l, nick, "\n" <> _) do
[nick | l]
end
def parse_delegation_args(l, nick, "") do
[nick | l]
end
def parse_delegation_args(l, nick, " " <> _) do
[nick | l]
end
def parse_delegation_args(l, nick, <<c :: 8, rest :: binary>>) do
parse_delegation_args(l, <<nick :: binary, c :: 8>>, rest)
end
def parse_delegation_args(arguments) do
[]
|> parse_delegation_args("", arguments)
|> :lists.reverse()
|> Enum.flat_map(fn
"" -> []
nick -> [{:delegate_to, nick}]
end)
end
def parse_priority(binary) do
{p, _} = Integer.parse(binary)
[{:set_priority, p}]
end
@doc """
Given a populated struct, run everything.
"""
@spec run(t) :: :ok
def run(c) do
c = c
|> fetch_pr()
|> fetch_patch()
cmd_list = parse(c.comment)
cmd_list
|> required_permission_level()
|> Permission.permission?(c.commenter, c.patch)
|> if do
Enum.each(cmd_list, &run(c, &1))
else
permission_denied(c)
end
end
def required_permission_level_cmd(:ping) do
:none
end
def required_permission_level_cmd({:autocorrect, _}) do
:none
end
def required_permission_level_cmd({:try, _}) do
:member
end
def required_permission_level_cmd(_) do
:reviewer
end
def required_permission_level(cmd_list) do
cmd_list
|> Enum.reduce(:none, fn cmd, perm ->
new_perm = cmd |> required_permission_level_cmd()
case {perm, new_perm} do
{:none, new_perm} -> new_perm
{perm, :none} -> perm
{_, :reviewer} -> :reviewer
{:reviewer, _} -> :reviewer
{p, p} -> p
end
end)
end
def permission_denied(c) do
login = c.commenter.login
url = project_url(
BorsNG.Endpoint,
:confirm_add_reviewer,
c.project,
login)
c.project.repo_xref
|> Project.installation_connection(Repo)
|> GitHub.post_comment!(
c.pr_xref,
"""
:lock: Permission denied
Existing reviewers: [click here to make #{login} a reviewer](#{url})
""")
end
@spec run(t, cmd) :: :ok
def run(c, :activate) do
run(c, {:activate_by, c.commenter.login})
end
def run(c, {:activate_by, username}) do
batcher = Batcher.Registry.get(c.project.id)
Batcher.reviewed(batcher, c.patch.id, username)
end
def run(c, {:set_priority, priority}) do
batcher = Batcher.Registry.get(c.project.id)
Batcher.set_priority(batcher, c.patch.id, priority)
end
def run(c, :deactivate) do
c = fetch_patch(c)
batcher = Batcher.Registry.get(c.project.id)
Batcher.cancel(batcher, c.patch.id)
end
def run(c, {:try, arguments}) do
c = fetch_patch(c)
attemptor = Attemptor.Registry.get(c.project.id)
Attemptor.tried(attemptor, c.patch.id, arguments)
end
def run(c, {:autocorrect, command}) do
c.project.repo_xref
|> Project.installation_connection(Repo)
|> GitHub.post_comment!(
c.pr_xref, ~s/Did you mean "#{command}"?/)
end
def run(c, :ping) do
c.project.repo_xref
|> Project.installation_connection(Repo)
|> GitHub.post_comment!(
c.pr_xref, "pong")
end
def run(c, :delegate) do
patch = Repo.preload(c.patch, :author)
delegate_to(c, patch.author)
end
def run(c, {:delegate_to, login}) do
delegatee = case Repo.get_by(User, login: login) do
nil ->
gh_user = GitHub.get_user_by_login!(
{:installation, c.project.installation.installation_xref},
login)
Repo.insert!(%User{
login: gh_user.login,
user_xref: gh_user.id})
user -> user
end
delegate_to(c, delegatee)
end
def delegate_to(c, delegatee) do
Permission.delegate(delegatee, c.patch)
c.project.repo_xref
|> Project.installation_connection(Repo)
|> GitHub.post_comment!(
c.pr_xref, ~s/:v: #{delegatee.login} can now approve this pull request/)
end
end