Merge pull request '[FEAT] Allow non-explicit push options' (#2984) from gusted/forgejo-push-options into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2984
Reviewed-by: oliverpool <oliverpool@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-04-03 19:00:16 +00:00
commit c360b630e3
3 changed files with 24 additions and 7 deletions

View file

@ -487,10 +487,11 @@ func pushOptions() map[string]string {
if pushCount, err := strconv.Atoi(os.Getenv(private.GitPushOptionCount)); err == nil { if pushCount, err := strconv.Atoi(os.Getenv(private.GitPushOptionCount)); err == nil {
for idx := 0; idx < pushCount; idx++ { for idx := 0; idx < pushCount; idx++ {
opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx)) opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx))
kv := strings.SplitN(opt, "=", 2) key, value, found := strings.Cut(opt, "=")
if len(kv) == 2 { if !found {
opts[kv[0]] = kv[1] value = "true"
} }
opts[key] = value
} }
} }
return opts return opts
@ -630,10 +631,11 @@ Forgejo or set your environment appropriately.`, "")
break break
} }
kv := strings.SplitN(string(rs.Data), "=", 2) key, value, found := strings.Cut(string(rs.Data), "=")
if len(kv) == 2 { if !found {
hookOptions.GitPushOptions[kv[0]] = kv[1] value = "true"
} }
hookOptions.GitPushOptions[key] = value
} }
} }

View file

@ -15,6 +15,7 @@ import (
"testing" "testing"
"time" "time"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
@ -162,3 +163,17 @@ func TestDelayWriter(t *testing.T) {
require.Empty(t, out) require.Empty(t, out)
}) })
} }
func TestPushOptions(t *testing.T) {
require.NoError(t, os.Setenv(private.GitPushOptionCount, "3"))
require.NoError(t, os.Setenv("GIT_PUSH_OPTION_0", "force-push"))
require.NoError(t, os.Setenv("GIT_PUSH_OPTION_1", "option=value"))
require.NoError(t, os.Setenv("GIT_PUSH_OPTION_2", "option-double=another=value"))
require.NoError(t, os.Setenv("GIT_PUSH_OPTION_3", "not=valid"))
assert.Equal(t, map[string]string{
"force-push": "true",
"option": "value",
"option-double": "another=value",
}, pushOptions())
}

View file

@ -1025,7 +1025,7 @@ func doCreateAgitFlowPull(dstPath string, ctx *APITestContext, baseBranch, headB
t.Run("Succeeds", func(t *testing.T) { t.Run("Succeeds", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
_, _, gitErr := git.NewCommand(git.DefaultContext, "push", "origin", "-o", "force-push=true").AddDynamicArguments("HEAD:refs/for/master/" + headBranch + "-force-push").RunStdString(&git.RunOpts{Dir: dstPath}) _, _, gitErr := git.NewCommand(git.DefaultContext, "push", "origin", "-o", "force-push").AddDynamicArguments("HEAD:refs/for/master/" + headBranch + "-force-push").RunStdString(&git.RunOpts{Dir: dstPath})
assert.NoError(t, gitErr) assert.NoError(t, gitErr)
currentHeadCommitID, err := upstreamGitRepo.GetRefCommitID(pr.GetGitRefName()) currentHeadCommitID, err := upstreamGitRepo.GetRefCommitID(pr.GetGitRefName())