Skip to content

Commit 370e3db

Browse files
Bugfix: in the second push (force_ci_trigger()), make sure to actually push to the SSH remote (#505)
* Create a new remote with the url * Bump version * Apply suggestions from code review Co-authored-by: Dilum Aluthge <[email protected]> * Fix function signatures, move remote add closer to point of use * Fix tests, check remotes to determine add vs seturl * Downgrade version number bump to minor * Remove unneeded patch * Extract `remote_exists()`, add tests for new functions * Make tests more sensible * Expand seturl test --------- Co-authored-by: Dilum Aluthge <[email protected]>
1 parent 69d49c0 commit 370e3db

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CompatHelper"
22
uuid = "aa819f21-2bde-4658-8897-bab36330d9b7"
33
authors = ["Dilum Aluthge", "Brown Center for Biomedical Informatics", "contributors"]
4-
version = "3.11.0"
4+
version = "3.12.0"
55

66
[deps]
77
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/utilities/git.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,14 @@ function git_get_master_branch(master_branch::DefaultBranch)
8989
return string(strip(read(`git rev-parse --abbrev-ref HEAD`, String)))
9090
end
9191
git_get_master_branch(master_branch::AbstractString) = master_branch
92+
93+
function remote_exists(remote_name::AbstractString)
94+
return remote_name in split(strip(read(`git remote`, String)), '\n')
95+
end
96+
97+
function git_remote_add_or_seturl(remote_name::AbstractString, url::AbstractString)
98+
git_remote_subcommand = remote_exists(remote_name) ? "set-url" : "add"
99+
run(`git remote $git_remote_subcommand $remote_name $url`)
100+
101+
return nothing
102+
end

src/utilities/new_versions.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ function continue_with_pr(dep::DepInfo, bump_compat_containing_equality_specifie
205205
return true
206206
end
207207

208+
const COMPATHELPER_SSH_REMOTE_NAME = "compathelper-ssh-remote"
209+
208210
function make_pr_for_new_version(
209211
forge::Forge,
210212
repo::Union{GitHub.Repo,GitLab.Project},
@@ -286,6 +288,8 @@ function make_pr_for_new_version(
286288
if commit_was_success
287289
@info("Commit was a success")
288290
api_retry() do
291+
# For the first push, we intentionally push to the HTTPS remote (`origin`),
292+
# because we want to avoid triggering duplicate CI runs.
289293
@mock git_push(
290294
"origin", new_branch_name, pkey_filename; force=true, env=env
291295
)
@@ -297,6 +301,12 @@ function make_pr_for_new_version(
297301

298302
options.cc_user && cc_mention_user(forge, repo, new_pr; env=env)
299303
options.unsub_from_prs && unsub_from_pr(forge, new_pr)
304+
305+
# If we have an SSH key, we need to create a new remote (or update
306+
# it) with the appropriate URL (ssh/https) so that force_ci_trigger()
307+
# can use it
308+
git_remote_add_or_seturl(COMPATHELPER_SSH_REMOTE_NAME, repo_git_url)
309+
300310
force_ci_trigger(forge, new_pr_title, new_branch_name, pkey_filename; env=env)
301311

302312
# Return to the master branch
@@ -345,7 +355,7 @@ function force_ci_trigger(
345355
# Force push the changes to trigger the PR
346356
api_retry() do
347357
@debug "force_ci_trigger: force-pushing the changes to trigger CI on the PR"
348-
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env)
358+
@mock git_push(COMPATHELPER_SSH_REMOTE_NAME, branch_name, pkey_filename; force=true, env=env)
349359
end
350360
end
351361

test/utilities/git.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,48 @@ end
362362
end
363363
end
364364
end
365+
366+
@testset "remote_exists" begin
367+
mktempdir() do f
368+
cd(f) do
369+
run(`git init`)
370+
run(`git remote add origin foo-url`)
371+
run(`git remote add upstream bar-url`)
372+
373+
@testset "remote exists" begin
374+
@test CompatHelper.remote_exists("origin")
375+
@test CompatHelper.remote_exists("upstream")
376+
end
377+
@testset "remote does not exist" begin
378+
@test !CompatHelper.remote_exists("nonexistent")
379+
end
380+
end
381+
end
382+
end
383+
384+
@testset "git_remote_add_or_seturl" begin
385+
mktempdir() do f
386+
cd(f) do
387+
run(`git init`)
388+
run(`git remote add origin foo-url`)
389+
390+
@testset "set existing remote" begin
391+
url = "bar-url"
392+
CompatHelper.git_remote_add_or_seturl("origin", url)
393+
output = strip(read(`git remote get-url origin`, String))
394+
@test output == url
395+
396+
url2 = "bar-url2"
397+
CompatHelper.git_remote_add_or_seturl("origin", url2)
398+
output = strip(read(`git remote get-url origin`, String))
399+
@test output == url2
400+
end
401+
@testset "add new remote" begin
402+
url = "baz-url"
403+
CompatHelper.git_remote_add_or_seturl("upstream", url)
404+
output = strip(read(`git remote get-url upstream`, String))
405+
@test output == url
406+
end
407+
end
408+
end
409+
end

0 commit comments

Comments
 (0)