-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathCommentBot.jl
257 lines (220 loc) · 9.44 KB
/
CommentBot.jl
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
module CommentBot
using Sockets
using GitHub
using HTTP
using Distributed
using Pkg
using Logging
using Dates
using JSON
using MbedTLS
import Pkg: TOML
import ..Registrator: post_on_slack_channel, pull_request_contents
import RegistryTools: RegBranch, RegisterParams
import Base: string
using ..Messaging
include("trigger_types.jl")
include("parse_comment.jl")
include("github_utils.jl")
include("verify_projectfile.jl")
include("param_types.jl")
include("approval.jl")
include("../management.jl")
const event_queue = Channel{RequestParams}(1024)
const CONFIG = Dict{String,Any}()
const httpsock = Ref{Sockets.TCPServer}()
function print_entry_log(rp::RequestParams{PullRequestTrigger})
@info "Creating registration pull request" reponame=rp.reponame prid=rp.trigger_src.prid
end
function print_entry_log(rp::RequestParams{CommitCommentTrigger})
@info "Creating registration pull request" reponame=rp.reponame sha=get_comment_commit_id(rp.evt)
end
function print_entry_log(rp::RequestParams{IssueTrigger})
@info "Creating registration pull request" reponame=rp.reponame branch=rp.trigger_src.branch
end
get_trigger_id(rp::RequestParams{PullRequestTrigger}) = rp.trigger_src.prid
get_trigger_id(rp::RequestParams{IssueTrigger}) = get_prid(rp.evt.payload)
get_trigger_id(rp::RequestParams{CommitCommentTrigger}) = get_comment_commit_id(rp.evt)
tag_name(version, subdir) = subdir == "" ? "v$version" : splitdir(subdir)[end] * "-v$version"
function make_pull_request(pp::ProcessedParams, rp::RequestParams, rbrn::RegBranch, target_registry::Dict{String,Any})
name = rbrn.name
ver = rbrn.version
brn = rbrn.branch
subdir = rp.subdir
subdir_string = subdir == "" ? "" : ", subdir=$subdir"
@info("Creating pull request name=$name, ver=$ver, branch=$brn" * subdir_string)
payload = rp.evt.payload
creator = get_user_login(payload)
reviewer = payload["sender"]["login"]
@debug("Pull request creator=$creator, reviewer=$reviewer")
trigger_id = get_trigger_id(rp)
meta = JSON.json(Dict("request_type"=> string(rp),
"pkg_repo_name"=> rp.reponame,
"trigger_id"=> trigger_id,
"tree_sha"=> pp.tree_sha,
"version"=> string(ver),
"subdir"=> subdir))
key = CONFIG["enc_key"]
enc_meta = "<!-- " * bytes2hex(encrypt(MbedTLS.CIPHER_AES_128_CBC, key, meta, key)) * " -->"
params = Dict("base"=>target_registry["base_branch"],
"head"=>brn,
"maintainer_can_modify"=>true)
ref = get_html_url(rp.evt.payload)
description = something(rp.evt.repository.description, "")
params["title"], params["body"] = pull_request_contents(;
registration_type=get(rbrn.metadata, "kind", ""),
package=name,
repo=string(rp.evt.repository.html_url),
user="@$creator",
version=ver,
commit=pp.sha,
release_notes=rp.release_notes,
reviewer="@$reviewer",
reference=ref,
meta=enc_meta,
description=description,
)
repo = join(split(target_registry["repo"], "/")[end-1:end], "/")
pr, msg = create_or_find_pull_request(repo, params, rbrn)
tag = tag_name(ver, subdir)
cbody = """
Registration pull request $msg: [$(repo)/$(pr.number)]($(pr.html_url))
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the [Julia TagBot GitHub Action](https://github.com/marketplace/actions/julia-tagbot) is installed, or can be done manually through the github interface, or via:
```
git tag -a $tag -m "<description of version>" $(pp.sha)
git push origin $tag
```
"""
if get(rbrn.metadata, "warning", nothing) !== nothing
cbody *= """
Also, note the warning: $(rbrn.metadata["warning"])
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.
"""
end
@debug(cbody)
make_comment(rp.evt, cbody)
return pr
end
string(::RequestParams{PullRequestTrigger}) = "pull_request"
string(::RequestParams{CommitCommentTrigger}) = "commit_comment"
string(::RequestParams{IssueTrigger}) = "issue"
function action(rp::RequestParams{T}, zsock::RequestSocket) where T <: RegisterTrigger
if rp.target === nothing
target_registry_name, target_registry = first(CONFIG["targets"])
else
filteredtargets = filter(x->(x[1]==rp.target), CONFIG["targets"])
if length(filteredtargets) == 0
msg = "Error: target $(rp.target) not found"
@debug(msg)
make_comment(rp.evt, msg)
set_error_status(rp)
return
else
target_registry_name, target_registry = filteredtargets[1]
end
end
pp = ProcessedParams(rp)
@info("Processing register event", reponame=rp.reponame, target_registry_name)
try
if pp.cparams.isvalid
registry_deps=map(String, get(CONFIG, "registry_deps", String[]))
regp = RegisterParams(pp.cloneurl,
pp.project,
pp.tree_sha;
subdir=rp.subdir,
registry=target_registry["repo"],
registry_deps=registry_deps,
push=true,
)
rbrn = sendrecv(zsock, regp; nretry=10)
if rbrn === nothing || get(rbrn.metadata, "error", nothing) !== nothing
if rbrn === nothing
msg = "ERROR: Registrator backend service unreachable"
else
msg = "Error while trying to register: $(rbrn.metadata["error"])"
end
@debug(msg)
make_comment(rp.evt, msg)
set_error_status(rp)
else
make_pull_request(pp, rp, rbrn, target_registry)
set_success_status(rp)
end
else
@info("Error while processing event: $(repr(pp.cparams.error))")
if pp.cparams.report_error
msg = "Error while trying to register: $(repr(pp.cparams.error))"
@debug(msg)
make_comment(rp.evt, msg)
end
set_error_status(rp)
end
catch ex
bt = get_backtrace(ex)
@info("Unexpected error: $bt")
raise_issue(rp.evt, rp.phrase, bt)
end
@info("Done processing register event", reponame=rp.reponame, target_registry_name)
nothing
end
function comment_handler(event::WebhookEvent, phrase::RegexMatch)
@debug("Received event for $(event.repository.full_name), phrase: $phrase")
try
rp = RequestParams(event, phrase)
isa(rp.trigger_src, EmptyTrigger) && rp.cparams.error === nothing && return
if rp.cparams.isvalid && rp.cparams.error === nothing
print_entry_log(rp)
put!(event_queue, rp)
set_pending_status(rp)
elseif rp.cparams.error !== nothing
@info("Error while processing event: $(rp.cparams.error)")
if rp.cparams.report_error
msg = "Error while trying to register: $(rp.cparams.error)"
@debug(msg)
make_comment(event, msg)
end
set_error_status(rp)
end
catch ex
bt = get_backtrace(ex)
@info("Unexpected error: $bt")
raise_issue(event, phrase, bt)
end
return HTTP.Messages.Response(200)
end
make_trigger(cfg=CONFIG) = Regex(cfg["trigger"] * "(.*)", "i")
function github_webhook(http_ip=CONFIG["http_ip"],
http_port=get(CONFIG, "http_port", parse(Int, get(ENV, "PORT", "8001"))); kwargs...)
auth = get_jwt_auth()
trigger = make_trigger()
listener = GitHub.CommentListener(comment_handler, trigger; check_collab=false, auth=auth, secret=CONFIG["github"]["secret"])
httpsock[] = Sockets.listen(IPv4(http_ip), http_port)
do_action() = GitHub.run(listener, httpsock[], IPv4(http_ip), http_port; kwargs...)
handle_exception(ex) = (isa(ex, Base.IOError) && (ex.code == -103)) ? :exit : :continue
keep_running() = isopen(httpsock[])
@info("GitHub webhook starting...", trigger, http_ip, http_port)
recover("github_webhook", keep_running, do_action, handle_exception)
end
function request_processor(zsock::RequestSocket)
do_action() = action(take!(event_queue), zsock)
handle_exception(ex) = (isa(ex, InvalidStateException) && (ex.state == :closed)) ? :exit : :continue
keep_running() = isopen(httpsock[])
recover("request_processor", keep_running, do_action, handle_exception)
end
function main(config::AbstractString=isempty(ARGS) ? "config.toml" : first(ARGS); kwargs...)
merge!(CONFIG, Pkg.TOML.parsefile(config)["commentbot"])
if get(CONFIG, "enable_logging", true)
global_logger(SimpleLogger(stdout, get_log_level(CONFIG["log_level"])))
end
zsock = RequestSocket(get(CONFIG, "backend_port", 5555))
@info("Starting server...")
t1 = @async request_processor(zsock)
t2 = @async status_monitor(CONFIG["stop_file"], event_queue, httpsock)
github_webhook(; kwargs...)
wait(t1)
wait(t2)
# The !stopped! part is grep'ed by restart.sh, do not change
@warn("Server !stopped!")
end
end # module