Skip to content

Commit c472fba

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

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

Diff for: src/utilities/git.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ function git_push(
2626
pkey_filename::Union{AbstractString,Nothing}=nothing;
2727
force=false,
2828
env=ENV,
29+
forge = nothing,
30+
ci_cfg = nothing,
31+
repo = nothing,
2932
)
3033
force_flag = force ? ["-f"] : []
3134
name, email = get_git_name_and_email(; env=env)
@@ -36,7 +39,14 @@ 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+
# We need to convert the remote URL to SSH format.
46+
# Otherwise, the SSH private key will be ignored.
47+
true_remote = get_url_for_ssh(forge, ci_cfg, repo)
48+
end
49+
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $true_remote $branch`
4050
@debug "Attempting to run Git push command" cmd env2["GIT_SSH_COMMAND"]
4151
run(setenv(cmd, env2))
4252

Diff for: src/utilities/new_versions.jl

+7-3
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function make_pr_for_new_version(
287287
@info("Commit was a success")
288288
api_retry() do
289289
@mock git_push(
290-
"origin", new_branch_name, pkey_filename; force=true, env=env
290+
"origin", new_branch_name, pkey_filename; force=true, env=env, forge=forge, ci_cfg=ci_cfg, repo=repo,
291291
)
292292
end
293293

@@ -297,7 +297,7 @@ function make_pr_for_new_version(
297297

298298
options.cc_user && cc_mention_user(forge, repo, new_pr; env=env)
299299
options.unsub_from_prs && unsub_from_pr(forge, new_pr)
300-
force_ci_trigger(forge, new_pr_title, new_branch_name, pkey_filename; env=env)
300+
force_ci_trigger(forge, ci_cfg, repo, new_pr_title, new_branch_name, pkey_filename; env=env)
301301

302302
# Return to the master branch
303303
git_checkout(master_branch_name)
@@ -311,6 +311,8 @@ end
311311

312312
function force_ci_trigger(
313313
api::GitLab.GitLabAPI,
314+
ci_cfg::Union{CIService, Nothing},
315+
repo::Union{GitLab.Project, Nothing},
314316
pr_title::AbstractString,
315317
branch_name::AbstractString,
316318
pkey_filename::Union{AbstractString,Nothing};
@@ -322,6 +324,8 @@ end
322324

323325
function force_ci_trigger(
324326
api::GitHub.GitHubAPI,
327+
ci_cfg::Union{CIService, Nothing},
328+
repo::Union{GitHub.Repo, Nothing},
325329
pr_title::AbstractString,
326330
branch_name::AbstractString,
327331
pkey_filename::Union{AbstractString,Nothing};
@@ -345,7 +349,7 @@ function force_ci_trigger(
345349
# Force push the changes to trigger the PR
346350
api_retry() do
347351
@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)
352+
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env, forge=api, ci_cfg=ci_cfg, repo=repo)
349353
end
350354
end
351355

Diff for: src/utilities/types.jl

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ struct Package
1010
uuid::UUIDs.UUID
1111
end
1212

13+
abstract type AbstractRepo end # TODO: delete this line
14+
1315
mutable struct DepInfo
1416
package::Package
1517
latest_version::Union{VersionNumber,Nothing}

Diff for: test/utilities/git.jl

+16-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ QQDtEmQvWdgz+HtIuTG1ySJ9FYO6LeCEXHtQX78aOfNaj2jqLTXHdqrMr0V5exJcNV4XSc
1919
-----END OPENSSH PRIVATE KEY-----
2020
"""
2121

22+
# TODO: begin delete these lines
23+
struct MyForge <: GitForge.Forge
24+
end
25+
struct MyCIService <: CompatHelper.CIService
26+
end
27+
struct MyRepo <: CompatHelper.AbstractRepo
28+
local_remote_path::String
29+
end
30+
CompatHelper.get_url_for_ssh(::MyForge, ::MyCIService, repo::MyRepo) = repo.local_remote_path
31+
# TODO: end delete these lines
32+
2233
@testset "git_push" begin
2334
function create_local_remote(dir::AbstractString)
2435
remote_path = joinpath(dir, "localremote.git")
@@ -114,7 +125,11 @@ QQDtEmQvWdgz+HtIuTG1ySJ9FYO6LeCEXHtQX78aOfNaj2jqLTXHdqrMr0V5exJcNV4XSc
114125
output = read(`git log --decorate`, String)
115126
@test !occursin(pushed_str, output)
116127

117-
CompatHelper.git_push("origin", "master", pkey)
128+
# TODO: use patches (via Mocking.jl) instead of the current approach.
129+
forge = MyForge()
130+
ci_cfg = MyCIService()
131+
repo = MyRepo(local_remote_path)
132+
CompatHelper.git_push("origin", "master", pkey; forge, ci_cfg, repo)
118133

119134
output = read(`git log --decorate`, String)
120135
@test occursin(pushed_str, output)

Diff for: test/utilities/new_versions.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,10 @@ end
266266

267267
hash = read(`git rev-parse HEAD`, String)
268268

269-
CompatHelper.force_ci_trigger(GitLab.GitLabAPI(), "title", "master", "pkey")
269+
api = GitLab.GitLabAPI()
270+
ci_cfg = nothing
271+
repo = nothing
272+
CompatHelper.force_ci_trigger(api, ci_cfg, repo, "title", "master", "pkey")
270273
new_hash = read(`git rev-parse HEAD`, String)
271274
@test hash == new_hash
272275
end
@@ -289,9 +292,12 @@ end
289292

290293
hash = read(`git rev-parse HEAD`, String)
291294

295+
api = GitHub.GitHubAPI()
296+
ci_cfg = nothing
297+
repo = nothing
292298
apply(git_push_patch) do
293299
CompatHelper.force_ci_trigger(
294-
GitHub.GitHubAPI(), "title", "master", "pkey"
300+
api, ci_cfg, repo, "title", "master", "pkey"
295301
)
296302
end
297303
new_hash = read(`git rev-parse HEAD`, String)
@@ -315,9 +321,12 @@ end
315321

316322
hash = read(`git rev-parse HEAD`, String)
317323

324+
api = GitHub.GitHubAPI()
325+
ci_cfg = nothing
326+
repo = nothing
318327
apply(git_push_patch) do
319328
CompatHelper.force_ci_trigger(
320-
GitHub.GitHubAPI(), "title", "master", nothing
329+
api, ci_cfg, repo, "title", "master", nothing
321330
)
322331
end
323332
new_hash = read(`git rev-parse HEAD`, String)

0 commit comments

Comments
 (0)