Skip to content

Commit

Permalink
Fix the force_ci_trigger feature (force-push with an SSH deploy key…
Browse files Browse the repository at this point in the history
…) for GitHub Actions
  • Loading branch information
DilumAluthge committed Jan 11, 2024
1 parent 18bd2a4 commit c472fba
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/utilities/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ function git_push(
pkey_filename::Union{AbstractString,Nothing}=nothing;
force=false,
env=ENV,
forge = nothing,
ci_cfg = nothing,
repo = nothing,
)
force_flag = force ? ["-f"] : []
name, email = get_git_name_and_email(; env=env)
Expand All @@ -36,7 +39,14 @@ function git_push(

env2 = copy(ENV)
env2["GIT_SSH_COMMAND"] = git_ssh_command
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $remote $branch`
if isnothing(pkey_filename)
true_remote = remote
else
# We need to convert the remote URL to SSH format.
# Otherwise, the SSH private key will be ignored.
true_remote = get_url_for_ssh(forge, ci_cfg, repo)
end
cmd = `git -c user.name="$name" -c user.email="$email" -c committer.name="$name" -c committer.email="$email" push $force_flag $true_remote $branch`
@debug "Attempting to run Git push command" cmd env2["GIT_SSH_COMMAND"]
run(setenv(cmd, env2))

Expand Down
10 changes: 7 additions & 3 deletions src/utilities/new_versions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ function make_pr_for_new_version(
@info("Commit was a success")
api_retry() do
@mock git_push(
"origin", new_branch_name, pkey_filename; force=true, env=env
"origin", new_branch_name, pkey_filename; force=true, env=env, forge=forge, ci_cfg=ci_cfg, repo=repo,
)
end

Expand All @@ -297,7 +297,7 @@ function make_pr_for_new_version(

options.cc_user && cc_mention_user(forge, repo, new_pr; env=env)
options.unsub_from_prs && unsub_from_pr(forge, new_pr)
force_ci_trigger(forge, new_pr_title, new_branch_name, pkey_filename; env=env)
force_ci_trigger(forge, ci_cfg, repo, new_pr_title, new_branch_name, pkey_filename; env=env)

# Return to the master branch
git_checkout(master_branch_name)
Expand All @@ -311,6 +311,8 @@ end

function force_ci_trigger(
api::GitLab.GitLabAPI,
ci_cfg::Union{CIService, Nothing},
repo::Union{GitLab.Project, Nothing},
pr_title::AbstractString,
branch_name::AbstractString,
pkey_filename::Union{AbstractString,Nothing};
Expand All @@ -322,6 +324,8 @@ end

function force_ci_trigger(
api::GitHub.GitHubAPI,
ci_cfg::Union{CIService, Nothing},
repo::Union{GitHub.Repo, Nothing},
pr_title::AbstractString,
branch_name::AbstractString,
pkey_filename::Union{AbstractString,Nothing};
Expand All @@ -345,7 +349,7 @@ function force_ci_trigger(
# Force push the changes to trigger the PR
api_retry() do
@debug "force_ci_trigger: force-pushing the changes to trigger CI on the PR"
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env)
@mock git_push("origin", branch_name, pkey_filename; force=true, env=env, forge=api, ci_cfg=ci_cfg, repo=repo)
end
end

Expand Down
2 changes: 2 additions & 0 deletions src/utilities/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct Package
uuid::UUIDs.UUID
end

abstract type AbstractRepo end # TODO: delete this line

mutable struct DepInfo
package::Package
latest_version::Union{VersionNumber,Nothing}
Expand Down
17 changes: 16 additions & 1 deletion test/utilities/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ QQDtEmQvWdgz+HtIuTG1ySJ9FYO6LeCEXHtQX78aOfNaj2jqLTXHdqrMr0V5exJcNV4XSc
-----END OPENSSH PRIVATE KEY-----
"""

# TODO: begin delete these lines
struct MyForge <: GitForge.Forge
end
struct MyCIService <: CompatHelper.CIService
end
struct MyRepo <: CompatHelper.AbstractRepo
local_remote_path::String
end
CompatHelper.get_url_for_ssh(::MyForge, ::MyCIService, repo::MyRepo) = repo.local_remote_path
# TODO: end delete these lines

@testset "git_push" begin
function create_local_remote(dir::AbstractString)
remote_path = joinpath(dir, "localremote.git")
Expand Down Expand Up @@ -114,7 +125,11 @@ QQDtEmQvWdgz+HtIuTG1ySJ9FYO6LeCEXHtQX78aOfNaj2jqLTXHdqrMr0V5exJcNV4XSc
output = read(`git log --decorate`, String)
@test !occursin(pushed_str, output)

CompatHelper.git_push("origin", "master", pkey)
# TODO: use patches (via Mocking.jl) instead of the current approach.
forge = MyForge()
ci_cfg = MyCIService()
repo = MyRepo(local_remote_path)
CompatHelper.git_push("origin", "master", pkey; forge, ci_cfg, repo)

output = read(`git log --decorate`, String)
@test occursin(pushed_str, output)
Expand Down
15 changes: 12 additions & 3 deletions test/utilities/new_versions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ end

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

CompatHelper.force_ci_trigger(GitLab.GitLabAPI(), "title", "master", "pkey")
api = GitLab.GitLabAPI()
ci_cfg = nothing
repo = nothing
CompatHelper.force_ci_trigger(api, ci_cfg, repo, "title", "master", "pkey")
new_hash = read(`git rev-parse HEAD`, String)
@test hash == new_hash
end
Expand All @@ -289,9 +292,12 @@ end

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

api = GitHub.GitHubAPI()
ci_cfg = nothing
repo = nothing
apply(git_push_patch) do
CompatHelper.force_ci_trigger(
GitHub.GitHubAPI(), "title", "master", "pkey"
api, ci_cfg, repo, "title", "master", "pkey"
)
end
new_hash = read(`git rev-parse HEAD`, String)
Expand All @@ -315,9 +321,12 @@ end

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

api = GitHub.GitHubAPI()
ci_cfg = nothing
repo = nothing
apply(git_push_patch) do
CompatHelper.force_ci_trigger(
GitHub.GitHubAPI(), "title", "master", nothing
api, ci_cfg, repo, "title", "master", nothing
)
end
new_hash = read(`git rev-parse HEAD`, String)
Expand Down

0 comments on commit c472fba

Please sign in to comment.