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 Oct 31, 2023
1 parent addd1f4 commit dd5b781
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 21 additions & 6 deletions src/utilities/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function get_git_name_and_email(; env=ENV)
end

function git_push(
forge::Forge,
ci_cfg::GitHubActions,
repo::GitHub.Repo,
remote::AbstractString,
branch::AbstractString,
pkey_filename::Union{AbstractString,Nothing}=nothing;
Expand All @@ -29,20 +32,32 @@ function git_push(
)
force_flag = force ? ["-f"] : []
name, email = get_git_name_and_email(; env=env)
enable_ssh_verbose_str = get(ENV, "JULIA_COMPATHELPER_ENABLE_SSH_VERBOSE", "false")
enable_ssh_verbose_b = parse(Bool, enable_ssh_verbose_str)::Bool
ssh = enable_ssh_verbose_b ? "ssh -vvvv" : "ssh"
git_ssh_command = isnothing(pkey_filename) ? ssh : "$(ssh) -i $pkey_filename"

git_ssh_command = _get_git_ssh_command(; pkey_filename)
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))

return nothing
end

function _get_git_ssh_command(; pkey_filename::Union{AbstractString,Nothing})
enable_ssh_verbose_str = get(ENV, "JULIA_COMPATHELPER_ENABLE_SSH_VERBOSE", "false")
enable_ssh_verbose_b = parse(Bool, enable_ssh_verbose_str)::Bool
ssh = enable_ssh_verbose_b ? "ssh -vvvv" : "ssh"
git_ssh_command = isnothing(pkey_filename) ? ssh : "$(ssh) -i $pkey_filename"
git_ssh_command = "sshFOOBAR -vvvv"
return git_ssh_command
end

function git_reset(commit::AbstractString; soft=false)
soft_flag = soft ? ["--soft"] : []
run(`git reset $soft_flag "$commit"`)
Expand Down
13 changes: 11 additions & 2 deletions src/utilities/new_versions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,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 @@ -301,8 +301,15 @@ function make_pr_for_new_version(
return created_pr
end

function git_push(
forge::Forge,
GitHubActions,
repo::GitHub.Repo,

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

function force_ci_trigger(
api::GitHub.GitHubAPI,
ci_cfg::CIService,
repo::GitHub.Repo
pr_title::AbstractString,
branch_name::AbstractString,
pkey_filename::Union{AbstractString,Nothing};
Expand All @@ -337,7 +346,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(api, ci_cfg, repo, "origin", branch_name, pkey_filename; force=true, env=env)
end
end

Expand Down

0 comments on commit dd5b781

Please sign in to comment.