|
| 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) | ) | /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