Skip to content

Commit 0cb3025

Browse files
authored
ControlSystems PR commenter (#4)
ControlSystems PR commenter
1 parent a180a10 commit 0cb3025

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: ControlSystems PR Ping reciever
2+
3+
on: [repository_dispatch]
4+
5+
jobs:
6+
run-and-comment:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/[email protected]
10+
- uses: julia-actions/setup-julia@v1
11+
- name: Event Information
12+
run: |
13+
echo "Event '${{ github.event.action }}' received from '${{ github.event.client_payload.repository }}'"
14+
- name: Do stuff
15+
env:
16+
ACCESS_TOKEN_BOT: ${{ secrets.ACCESS_TOKEN_BOT }}
17+
PR_ID: ${{ github.event.client_payload.pr_number }}
18+
if: github.event.action == 'prupdate' && github.event.sender.login == 'JuliaControlBot'
19+
run: |
20+
echo "Got PR Update"
21+
echo "Verified user"
22+
xvfb-run julia ./utils/pr_commenter.jl

Project.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name = "ControlExamplePlots"
2+
uuid = "5e0795ec-8005-5a21-beb4-c9685e7e8997"
3+
authors = ["Dept. Automatic Control, Lund University"]
4+
repo = "https://github.com/JuliaControl/ControlExamplePlots.jl.git"
5+
version = "0.3.0"
6+
7+
[deps]
8+
ControlSystems = "a6e380b2-a6ca-5380-bf3e-84a91bcd477e"
9+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
10+
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"
11+
Gtk = "4c0ca9eb-093a-5379-98c5-f87ac0bbbf44"
12+
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"

utils/pr_commenter.jl

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
# GLOBALS that should be set
2+
println("Running comment script!")
3+
println("PR_ID is $(ENV["PR_ID"])")
4+
5+
# Set plot globals
6+
ENV["PLOTS_TEST"] = "true"
7+
ENV["GKSwstype"] = "100"
8+
9+
# Stolen from https://discourse.julialang.org/t/collecting-all-output-from-shell-commands/15592/6
10+
""" Read output from terminal command """
11+
function communicate(cmd::Cmd, input)
12+
inp = Pipe()
13+
out = Pipe()
14+
err = Pipe()
15+
16+
process = run(pipeline(cmd, stdin=inp, stdout=out, stderr=err), wait=false)
17+
close(out.in)
18+
close(err.in)
19+
20+
stdout = @async String(read(out))
21+
stderr = @async String(read(err))
22+
write(process, input)
23+
close(inp)
24+
wait(process)
25+
return (
26+
stdout = fetch(stdout),
27+
stderr = fetch(stderr),
28+
code = process.exitcode
29+
)
30+
end
31+
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+
47+
""" Checkout ControlSystems PR"""
48+
function checkout_ControlSystems_PR(org, origin, ID)
49+
Pkg.develop(Pkg.PackageSpec(url="https://github.com/$org/ControlSystems.jl.git"))
50+
dir = joinpath(Pkg.devdir(), "ControlSystems")
51+
cd(dir)
52+
run(`git fetch $origin pull/$ID/head:tests-$ID`)
53+
run(`git checkout tests-$ID`)
54+
return
55+
end
56+
57+
println("running checkout_ControlSystems_PR")
58+
checkout_ControlSystems_PR(org, origin, ENV["PR_ID"])
59+
60+
println("using ControlExamplePlots")
61+
using ControlExamplePlots
62+
63+
""" Generate figures for plot tests"""
64+
function gen_figures()
65+
#### Test Plots
66+
67+
ControlExamplePlots.Plots.gr()
68+
ControlExamplePlots.Plots.default(show=false)
69+
70+
funcs, refs, eps = getexamples()
71+
# Make it easier to pass tests on different systems
72+
# Set to a factor 2*2 of common errors
73+
eps = 2*[0.15, 0.015, 0.1, 0.01, 0.01, 0.02, 0.01, 0.15, 0.15, 0.01, 0.01]
74+
res = genplots(funcs, refs, eps=eps, popup=false)
75+
return res
76+
end
77+
78+
println("running gen_figures")
79+
res = gen_figures()
80+
81+
import UUIDs
82+
83+
function create_ControlExamplePlots_branch(ID)
84+
dir = joinpath(Pkg.devdir(), "ControlExamplePlots")
85+
cd(dir)
86+
master_sha1 = communicate(`git rev-parse HEAD`, "useless string")[1][1:end-1] # strip newline
87+
tmp_name = UUIDs.uuid1()
88+
# Create new branch
89+
new_branch_name = "tests-$ID-$tmp_name"
90+
run(`git checkout -b $new_branch_name`)
91+
return master_sha1, new_branch_name
92+
end
93+
94+
println("running create_ControlExamplePlots_branch")
95+
old_commit, new_branch_name = create_ControlExamplePlots_branch(ID)
96+
97+
""" Replace old files with new and push to new branch"""
98+
function replace_and_push_files(org, origin, ID, new_branch_name)
99+
# Create dir for temporary figures
100+
dir = joinpath(Pkg.devdir(), "ControlExamplePlots")
101+
cd(dir)
102+
for r in res
103+
# Copy results into repo
104+
mv(r.testFilename, r.refFilename, force=true)
105+
end
106+
# Add figures
107+
run(`git config --global user.email "[email protected]"`)
108+
run(`git config --global user.name "JuliaControl Bot"`)
109+
run(`git add src/figures/*`)
110+
run(`git commit -m "automated plots test"`)
111+
run(`git remote set-url $origin https://JuliaControlBot:$(ENV["ACCESS_TOKEN_BOT"])@github.com/$org/ControlExamplePlots.jl.git`)
112+
run(`git push -u $origin $new_branch_name`)
113+
return
114+
end
115+
116+
println("running replace_and_push_files")
117+
replace_and_push_files(org, origin, ID, new_branch_name)
118+
119+
# Builds a message to post to github
120+
function get_message(res, org, old_commit, new_branch_name)
121+
str = """This is an automated message.
122+
Plot tests were run, see results below.
123+
Difference | Reference Image | New Image
124+
-----------| ----------------| ---------
125+
"""
126+
for r in res
127+
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"
130+
end
131+
return str
132+
end
133+
134+
println("running get_message")
135+
message = get_message(res, org, old_commit, new_branch_name)
136+
137+
#### Post Comment
138+
import GitHub
139+
140+
""" Post comment with result to original PR """
141+
function post_comment(org, message)
142+
token = ENV["ACCESS_TOKEN_BOT"]
143+
auth = GitHub.authenticate(token)
144+
#Push the comment
145+
GitHub.create_comment("$org/ControlSystems.jl", ID, :issue; auth = auth, params = Dict("body" => message))
146+
end
147+
148+
println("running post_comment")
149+
post_comment(org, message)
150+
151+
println("Done!")

0 commit comments

Comments
 (0)