Skip to content

Commit 9b6550f

Browse files
authored
refactor to allow errors in PR commenter, tweaked message (#5)
1 parent 0cb3025 commit 9b6550f

File tree

1 file changed

+73
-45
lines changed

1 file changed

+73
-45
lines changed

utils/pr_commenter.jl

Lines changed: 73 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# GLOBALS that should be set
2-
println("Running comment script!")
3-
println("PR_ID is $(ENV["PR_ID"])")
2+
println("Running comment script")
43

54
# Set plot globals
65
ENV["PLOTS_TEST"] = "true"
76
ENV["GKSwstype"] = "100"
87

8+
println("Defining functions")
9+
910
# Stolen from https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592/6
1011
""" Read output from terminal command """
1112
function communicate(cmd::Cmd, input)
@@ -29,21 +30,6 @@ function communicate(cmd::Cmd, input)
2930
)
3031
end
3132

32-
# Values
33-
origin = "origin"
34-
org = "JuliaControl"
35-
ID = ENV["PR_ID"]
36-
37-
using Pkg
38-
# Makes sure we can push to this later
39-
println("deving ControlExamplePlots")
40-
Pkg.develop(Pkg.PackageSpec(url="https://github.com/$org/ControlExamplePlots.jl.git"))
41-
42-
println("adding packages")
43-
Pkg.add("UUIDs")
44-
Pkg.add("GitHub")
45-
Pkg.add("ImageMagick") # Has no UUID
46-
4733
""" Checkout ControlSystems PR"""
4834
function checkout_ControlSystems_PR(org, origin, ID)
4935
Pkg.develop(Pkg.PackageSpec(url="https://github.com/$org/ControlSystems.jl.git"))
@@ -54,12 +40,6 @@ function checkout_ControlSystems_PR(org, origin, ID)
5440
return
5541
end
5642

57-
println("running checkout_ControlSystems_PR")
58-
checkout_ControlSystems_PR(org, origin, ENV["PR_ID"])
59-
60-
println("using ControlExamplePlots")
61-
using ControlExamplePlots
62-
6343
""" Generate figures for plot tests"""
6444
function gen_figures()
6545
#### Test Plots
@@ -75,11 +55,6 @@ function gen_figures()
7555
return res
7656
end
7757

78-
println("running gen_figures")
79-
res = gen_figures()
80-
81-
import UUIDs
82-
8358
function create_ControlExamplePlots_branch(ID)
8459
dir = joinpath(Pkg.devdir(), "ControlExamplePlots")
8560
cd(dir)
@@ -91,8 +66,6 @@ function create_ControlExamplePlots_branch(ID)
9166
return master_sha1, new_branch_name
9267
end
9368

94-
println("running create_ControlExamplePlots_branch")
95-
old_commit, new_branch_name = create_ControlExamplePlots_branch(ID)
9669

9770
""" Replace old files with new and push to new branch"""
9871
function replace_and_push_files(org, origin, ID, new_branch_name)
@@ -113,30 +86,31 @@ function replace_and_push_files(org, origin, ID, new_branch_name)
11386
return
11487
end
11588

116-
println("running replace_and_push_files")
117-
replace_and_push_files(org, origin, ID, new_branch_name)
118-
11989
# Builds a message to post to github
12090
function get_message(res, org, old_commit, new_branch_name)
91+
good = ":heavy_check_mark:"
92+
warning = ":warning:"
93+
error = ":x:"
94+
12195
str = """This is an automated message.
122-
Plot tests were run, see results below.
96+
Plots were compared to references, see results below.
12397
Difference | Reference Image | New Image
12498
-----------| ----------------| ---------
12599
"""
100+
126101
for r in res
127102
fig_name = basename(r.refFilename)
128-
status = (isdefined(r, :diff) && isa(r.diff, Number)) ? round(r.diff, digits=4) : string(r.status)
129-
str *= "$(status) | ![Reference](https://raw.githubusercontent.com/$org/ControlExamplePlots.jl/$old_commit/src/figures/$(fig_name)) | ![New](https://raw.githubusercontent.com/$org/ControlExamplePlots.jl/$(new_branch_name)/src/figures/$(fig_name))\n"
103+
# Symbol in front of number
104+
diff = (isdefined(r, :diff) && isa(r.diff, Number)) ? r.diff : 1.0
105+
symbol = ( diff < 0.15 ? good : (diff < 0.3 ? warning : error))
106+
# Number/message we print
107+
status = (isdefined(r, :diff) && isa(r.diff, Number)) ? round(r.diff, digits=3) : string(r.status)
108+
# Append figure to message
109+
str *= "$symbol $status | ![Reference](https://raw.githubusercontent.com/$org/ControlExamplePlots.jl/$old_commit/src/figures/$(fig_name)) | ![New](https://raw.githubusercontent.com/$org/ControlExamplePlots.jl/$(new_branch_name)/src/figures/$(fig_name))\n"
130110
end
131111
return str
132112
end
133113

134-
println("running get_message")
135-
message = get_message(res, org, old_commit, new_branch_name)
136-
137-
#### Post Comment
138-
import GitHub
139-
140114
""" Post comment with result to original PR """
141115
function post_comment(org, message)
142116
token = ENV["ACCESS_TOKEN_BOT"]
@@ -145,7 +119,61 @@ function post_comment(org, message)
145119
GitHub.create_comment("$org/ControlSystems.jl", ID, :issue; auth = auth, params = Dict("body" => message))
146120
end
147121

148-
println("running post_comment")
149-
post_comment(org, message)
122+
println("Loading constants")
123+
# Values
124+
origin = "origin"
125+
org = "JuliaControl"
126+
ID = ENV["PR_ID"]
127+
128+
try
129+
println("Running main script.")
130+
println("PR_ID is $(ENV["PR_ID"])")
131+
132+
using Pkg
133+
134+
# Makes sure we can push to this later
135+
println("deving ControlExamplePlots")
136+
Pkg.develop(Pkg.PackageSpec(url="https://github.com/$org/ControlExamplePlots.jl.git"))
137+
138+
println("adding packages")
139+
Pkg.add("UUIDs")
140+
Pkg.add("GitHub")
141+
Pkg.add("ImageMagick") # Has no UUID
142+
143+
println("running checkout_ControlSystems_PR")
144+
checkout_ControlSystems_PR(org, origin, ENV["PR_ID"])
145+
146+
println("using ControlExamplePlots")
147+
using ControlExamplePlots
150148

151-
println("Done!")
149+
println("running gen_figures")
150+
res = gen_figures()
151+
152+
import UUIDs
153+
154+
println("running create_ControlExamplePlots_branch")
155+
old_commit, new_branch_name = create_ControlExamplePlots_branch(ID)
156+
157+
println("running replace_and_push_files")
158+
replace_and_push_files(org, origin, ID, new_branch_name)
159+
160+
println("running get_message")
161+
message = get_message(res, org, old_commit, new_branch_name)
162+
163+
#### Post Comment
164+
import GitHub
165+
println("running post_comment")
166+
post_comment(org, message)
167+
println("Done!")
168+
catch
169+
println("BUILD FAILED!")
170+
171+
message = "Something failed when generating plots. See the log at https://github.com/JuliaControl/ControlExamplePlots.jl/actions for more details."
172+
173+
import GitHub
174+
println("running post_comment")
175+
post_comment(org, message)
176+
println("Build failed, comment added to PR.")
177+
# Throw error to log
178+
rethrow()
179+
end

0 commit comments

Comments
 (0)