Skip to content

Commit dd5b781

Browse files
committed
Fix the force_ci_trigger feature (force-push with an SSH deploy key) for GitHub Actions
1 parent addd1f4 commit dd5b781

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/utilities/git.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function get_git_name_and_email(; env=ENV)
2121
end
2222

2323
function git_push(
24+
forge::Forge,
25+
ci_cfg::GitHubActions,
26+
repo::GitHub.Repo,
2427
remote::AbstractString,
2528
branch::AbstractString,
2629
pkey_filename::Union{AbstractString,Nothing}=nothing;
@@ -29,20 +32,32 @@ function git_push(
2932
)
3033
force_flag = force ? ["-f"] : []
3134
name, email = get_git_name_and_email(; env=env)
32-
enable_ssh_verbose_str = get(ENV, "JULIA_COMPATHELPER_ENABLE_SSH_VERBOSE", "false")
33-
enable_ssh_verbose_b = parse(Bool, enable_ssh_verbose_str)::Bool
34-
ssh = enable_ssh_verbose_b ? "ssh -vvvv" : "ssh"
35-
git_ssh_command = isnothing(pkey_filename) ? ssh : "$(ssh) -i $pkey_filename"
36-
35+
git_ssh_command = _get_git_ssh_command(; pkey_filename)
3736
env2 = copy(ENV);
3837
env2["GIT_SSH_COMMAND"] = git_ssh_command
39-
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $remote $branch`
38+
if isnothing(pkey_filename)
39+
true_remote = remote
40+
else
41+
# We need to convert the remote URL to SSH format.
42+
# Otherwise, the SSH private key will be ignored.
43+
true_remote = get_url_for_ssh(forge, ci_cfg, repo)
44+
end
45+
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $true_remote $branch`
4046
@debug "Attempting to run Git push command" cmd env2["GIT_SSH_COMMAND"]
4147
run(setenv(cmd, env2))
4248

4349
return nothing
4450
end
4551

52+
function _get_git_ssh_command(; pkey_filename::Union{AbstractString,Nothing})
53+
enable_ssh_verbose_str = get(ENV, "JULIA_COMPATHELPER_ENABLE_SSH_VERBOSE", "false")
54+
enable_ssh_verbose_b = parse(Bool, enable_ssh_verbose_str)::Bool
55+
ssh = enable_ssh_verbose_b ? "ssh -vvvv" : "ssh"
56+
git_ssh_command = isnothing(pkey_filename) ? ssh : "$(ssh) -i $pkey_filename"
57+
git_ssh_command = "sshFOOBAR -vvvv"
58+
return git_ssh_command
59+
end
60+
4661
function git_reset(commit::AbstractString; soft=false)
4762
soft_flag = soft ? ["--soft"] : []
4863
run(`git reset $soft_flag "$commit"`)

src/utilities/new_versions.jl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function make_pr_for_new_version(
289289

290290
options.cc_user && cc_mention_user(forge, repo, new_pr; env=env)
291291
options.unsub_from_prs && unsub_from_pr(forge, new_pr)
292-
force_ci_trigger(forge, new_pr_title, new_branch_name, pkey_filename; env=env)
292+
force_ci_trigger(forge, ci_cfg, repo, new_pr_title, new_branch_name, pkey_filename; env=env)
293293

294294
# Return to the master branch
295295
git_checkout(master_branch_name)
@@ -301,8 +301,15 @@ function make_pr_for_new_version(
301301
return created_pr
302302
end
303303

304+
function git_push(
305+
forge::Forge,
306+
GitHubActions,
307+
repo::GitHub.Repo,
308+
304309
function force_ci_trigger(
305310
api::GitLab.GitLabAPI,
311+
ci_cfg::CIService,
312+
repo::GitLab.Project
306313
pr_title::AbstractString,
307314
branch_name::AbstractString,
308315
pkey_filename::Union{AbstractString,Nothing};
@@ -314,6 +321,8 @@ end
314321

315322
function force_ci_trigger(
316323
api::GitHub.GitHubAPI,
324+
ci_cfg::CIService,
325+
repo::GitHub.Repo
317326
pr_title::AbstractString,
318327
branch_name::AbstractString,
319328
pkey_filename::Union{AbstractString,Nothing};
@@ -337,7 +346,7 @@ function force_ci_trigger(
337346
# Force push the changes to trigger the PR
338347
api_retry() do
339348
@debug "force_ci_trigger: force-pushing the changes to trigger CI on the PR"
340-
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env)
349+
@mock git_push(api, ci_cfg, repo, "origin", branch_name, pkey_filename; force=true, env=env)
341350
end
342351
end
343352

0 commit comments

Comments
 (0)