Skip to content

Commit ba64b5b

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

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

src/utilities/git.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@ end
2323
function git_push(
2424
remote::AbstractString,
2525
branch::AbstractString,
26-
pkey_filename::Union{AbstractString,Nothing}=nothing;
26+
pkey_filename::Union{AbstractString, Nothing} = nothing;
2727
force=false,
2828
env=ENV,
29+
forge::Union{Forge, Nothing} = nothing,
30+
ci_cfg::Union{CIService, Nothing} = nothing,
31+
repo::Union{GitHub.Repo, GitLab.Project, Nothing} = nothing,
2932
)
3033
force_flag = force ? ["-f"] : []
3134
name, email = get_git_name_and_email(; env=env)
@@ -36,7 +39,20 @@ function git_push(
3639

3740
env2 = copy(ENV);
3841
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`
42+
if isnothing(pkey_filename)
43+
true_remote = remote
44+
else
45+
# If `pkey_filename` is not `nothing`, then `forge`, `ci_cfg`, and `repo`
46+
# must all be not `nothing`.
47+
forge::Forge
48+
ci_cfg::CIService
49+
repo::Union{GitHub.Repo,GitLab.Project}
50+
51+
# We need to convert the remote URL to SSH format.
52+
# Otherwise, the SSH private key will be ignored.
53+
true_remote = get_url_for_ssh(forge, ci_cfg, repo)
54+
end
55+
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $true_remote $branch`
4056
@debug "Attempting to run Git push command" cmd env2["GIT_SSH_COMMAND"]
4157
run(setenv(cmd, env2))
4258

src/utilities/new_versions.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function make_pr_for_new_version(
279279
@info("Commit was a success")
280280
api_retry() do
281281
@mock git_push(
282-
"origin", new_branch_name, pkey_filename; force=true, env=env
282+
"origin", new_branch_name, pkey_filename; force=true, env=env, forge=forge, ci_cfg=ci_cfg, repo=repo,
283283
)
284284
end
285285

@@ -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)
@@ -303,6 +303,8 @@ end
303303

304304
function force_ci_trigger(
305305
api::GitLab.GitLabAPI,
306+
ci_cfg::Union{CIService, Nothing},
307+
repo::Union{GitLab.Project, Nothing},
306308
pr_title::AbstractString,
307309
branch_name::AbstractString,
308310
pkey_filename::Union{AbstractString,Nothing};
@@ -314,6 +316,8 @@ end
314316

315317
function force_ci_trigger(
316318
api::GitHub.GitHubAPI,
319+
ci_cfg::Union{CIService, Nothing},
320+
repo::Union{GitHub.Repo, Nothing},
317321
pr_title::AbstractString,
318322
branch_name::AbstractString,
319323
pkey_filename::Union{AbstractString,Nothing};
@@ -337,7 +341,7 @@ function force_ci_trigger(
337341
# Force push the changes to trigger the PR
338342
api_retry() do
339343
@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)
344+
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env, forge=api, ci_cfg=ci_cfg, repo=repo)
341345
end
342346
end
343347

src/utilities/utilities.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ function has_ssh_private_key(; env::AbstractDict=ENV)
1212
end
1313

1414
function api_retry(f::Function)
15-
delays = ExponentialBackOff(; n=10, max_delay=30.0)
15+
# delays = ExponentialBackOff(; n=10, max_delay=30.0) # TODO: uncomment this line
16+
delays = ExponentialBackOff(; n=1, max_delay=30.0) # TODO: delete this line
1617
return retry(f; delays)()
1718
end
1819

test/utilities/git.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ QQDtEmQvWdgz+HtIuTG1ySJ9FYO6LeCEXHtQX78aOfNaj2jqLTXHdqrMr0V5exJcNV4XSc
114114
output = read(`git log --decorate`, String)
115115
@test !occursin(pushed_str, output)
116116

117+
# TODO: fix this invocation of `git_push()`
117118
CompatHelper.git_push("origin", "master", pkey)
118119

119120
output = read(`git log --decorate`, String)

0 commit comments

Comments
 (0)