diff --git a/src/utilities/git.jl b/src/utilities/git.jl index abd8ceec..7bc8f891 100644 --- a/src/utilities/git.jl +++ b/src/utilities/git.jl @@ -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) @@ -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)) diff --git a/src/utilities/new_versions.jl b/src/utilities/new_versions.jl index 664aa0af..6471f1e2 100644 --- a/src/utilities/new_versions.jl +++ b/src/utilities/new_versions.jl @@ -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 @@ -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) @@ -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}; @@ -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}; @@ -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 diff --git a/src/utilities/types.jl b/src/utilities/types.jl index 523a1972..3cdafe85 100644 --- a/src/utilities/types.jl +++ b/src/utilities/types.jl @@ -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} diff --git a/test/utilities/git.jl b/test/utilities/git.jl index 100f1167..0fadff72 100644 --- a/test/utilities/git.jl +++ b/test/utilities/git.jl @@ -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") @@ -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) diff --git a/test/utilities/new_versions.jl b/test/utilities/new_versions.jl index e6596487..e13305ef 100644 --- a/test/utilities/new_versions.jl +++ b/test/utilities/new_versions.jl @@ -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 @@ -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) @@ -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)