Skip to content

Commit 521cbf4

Browse files
authored
Updated to handle no changes, and new fork (#9)
* Updated to handle no changes, and new fork * Boolean sign bug in script
1 parent 90919a6 commit 521cbf4

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

utils/pr_commenter.jl

Lines changed: 46 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ function gen_figures()
5252
# Set to a factor 2*2 of common errors
5353
eps = 2*[0.15, 0.015, 0.1, 0.01, 0.01, 0.02, 0.01, 0.15, 0.15, 0.01, 0.01]
5454
res = genplots(funcs, refs, eps=eps, popup=false)
55-
return res
55+
56+
ndiff = count(r -> r.status != EXACT_MATCH, res)
57+
58+
return res, ndiff
5659
end
5760

5861
function create_ControlExamplePlots_branch(ID)
@@ -68,7 +71,7 @@ end
6871

6972

7073
""" Replace old files with new and push to new branch"""
71-
function replace_and_push_files(res, org, origin, new_branch_name)
74+
function replace_and_push_files(res, new_org, origin, new_branch_name)
7275
# Create dir for temporary figures
7376
dir = joinpath(Pkg.devdir(), "ControlExamplePlots")
7477
cd(dir)
@@ -81,32 +84,44 @@ function replace_and_push_files(res, org, origin, new_branch_name)
8184
run(`git config --global user.name "JuliaControl Bot"`)
8285
run(`git add src/figures/*`)
8386
run(`git commit -m "automated plots test"`)
84-
run(`git remote set-url $origin https://JuliaControlBot:$(ENV["ACCESS_TOKEN_BOT"])@github.com/$org/ControlExamplePlots.jl.git`)
85-
run(`git push -u $origin $new_branch_name`)
87+
run(`git remote add bot https://JuliaControlBot:$(ENV["ACCESS_TOKEN_BOT"])@github.com/$(new_org)/ControlExamplePlots.jl.git`)
88+
run(`git push -u bot $new_branch_name`)
8689
return
8790
end
8891

8992
# Builds a message to post to github
90-
function get_message(res, org, old_commit, new_branch_name)
93+
function get_message(res, org, new_org, old_commit, new_branch_name)
9194
good = ":heavy_check_mark:"
9295
warning = ":warning:"
9396
error = ":x:"
9497

95-
str = """This is an automated message.
96-
Plots were compared to references, see results below.
97-
Difference | Reference Image | New Image
98-
-----------| ----------------| ---------
99-
"""
100-
98+
images_str = ""
99+
ndiff = 0
101100
for r in res
102-
fig_name = basename(r.refFilename)
103-
# Symbol in front of number
104-
diff = (isdefined(r, :diff) && isa(r.diff, Number)) ? r.diff : 1.0
105-
symbol = ( diff < 0.015 ? good : (diff < 0.03 ? 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"
101+
if r.status != EXACT_MATCH
102+
ndiff += 1
103+
diff = (isdefined(r, :diff) && isa(r.diff, Number)) ? r.diff : 1.0
104+
# Symbol in front of number
105+
symbol = ( diff < 0.015 ? good : (diff < 0.03 ? 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+
# Name of file
109+
fig_name = basename(r.refFilename)
110+
# Append figure to message
111+
images_str *= "$symbol $status | ![Reference](https://raw.githubusercontent.com/$org/ControlExamplePlots.jl/$old_commit/src/figures/$(fig_name)) | ![New](https://raw.githubusercontent.com/$(new_org)/ControlExamplePlots.jl/$(new_branch_name)/src/figures/$(fig_name))\n"
112+
end
113+
end
114+
115+
str = if ndiff > 0
116+
"""This is an automated message.
117+
Plots were compared to references. $(ndiff)/$(length(res)) images have changed, see differences below:
118+
Difference | Reference Image | New Image
119+
-----------| ----------------| ---------
120+
"""*images_str
121+
else
122+
"""This is an automated message.
123+
Plots were compared to references. No changes were detected.
124+
"""
110125
end
111126
return str
112127
end
@@ -123,6 +138,7 @@ println("Loading constants")
123138
# Values
124139
origin = "origin"
125140
org = "JuliaControl"
141+
new_org = "JuliaControlBot"
126142
ID = ENV["PR_ID"]
127143

128144
try
@@ -147,18 +163,23 @@ try
147163
using ControlExamplePlots
148164

149165
println("running gen_figures")
150-
res = gen_figures()
166+
res, ndiff = gen_figures()
151167

168+
println("$ndiff images have changes")
152169
import UUIDs
153170

154-
println("running create_ControlExamplePlots_branch")
155-
old_commit, new_branch_name = create_ControlExamplePlots_branch(ID)
171+
if ndiff > 0
172+
println("running create_ControlExamplePlots_branch")
173+
old_commit, new_branch_name = create_ControlExamplePlots_branch(ID)
156174

157-
println("running replace_and_push_files")
158-
replace_and_push_files(res, org, origin, new_branch_name)
175+
println("running replace_and_push_files")
176+
replace_and_push_files(res, new_org, origin, new_branch_name)
177+
else
178+
println("No changes will be pushed")
179+
end
159180

160181
println("running get_message")
161-
message = get_message(res, org, old_commit, new_branch_name)
182+
message = get_message(res, org, new_org, old_commit, new_branch_name)
162183

163184
#### Post Comment
164185
import GitHub

0 commit comments

Comments
 (0)