mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-28 09:21:13 -05:00
5561e80b04
If a repository has git config --add push.pushOption submit=".sourcehut/*.yml" it failed when pushed because of the unknown submit push option. It will be ignored instead. Filtering out the push options is done in an earlier stage, when the hook command runs, before it submits the options map to the private endpoint. * move all the push options logic to modules/git/pushoptions * add 100% test coverage for modules/git/pushoptions Test coverage for the code paths from which code was moved to the modules/git/pushoptions package: * cmd/hook.go:runHookPreReceive * routers/private/hook_pre_receive.go:validatePushOptions tests/integration/git_push_test.go:TestOptionsGitPush runs through both. The test verifying the option is rejected was removed and, if added again, will fail because the option is now ignored instead of being rejected. * cmd/hook.go:runHookProcReceive * services/agit/agit.go:ProcReceive tests/integration/git_test.go: doCreateAgitFlowPull runs through both. It uses variations of AGit related push options. * cmd/hook.go:runHookPostReceive * routers/private/hook_post_receive.go:HookPostReceive tests/integration/git_test.go:doPushCreate called by TestGit/HTTP/sha1/PushCreate runs through both. Note that although it provides coverage for this code path it does not use push options. Fixes: https://codeberg.org/forgejo/forgejo/issues/3651
113 lines
2.1 KiB
Go
113 lines
2.1 KiB
Go
// Copyright twenty-panda <twenty-panda@posteo.com>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package pushoptions
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Key string
|
|
|
|
const (
|
|
RepoPrivate = Key("repo.private")
|
|
RepoTemplate = Key("repo.template")
|
|
AgitTopic = Key("topic")
|
|
AgitForcePush = Key("force-push")
|
|
AgitTitle = Key("title")
|
|
AgitDescription = Key("description")
|
|
|
|
envPrefix = "GIT_PUSH_OPTION"
|
|
EnvCount = envPrefix + "_COUNT"
|
|
EnvFormat = envPrefix + "_%d"
|
|
)
|
|
|
|
type Interface interface {
|
|
ReadEnv() Interface
|
|
Parse(string) bool
|
|
Map() map[string]string
|
|
|
|
ChangeRepoSettings() bool
|
|
|
|
Empty() bool
|
|
|
|
GetBool(key Key, def bool) bool
|
|
GetString(key Key) (val string, ok bool)
|
|
}
|
|
|
|
type gitPushOptions map[string]string
|
|
|
|
func New() Interface {
|
|
pushOptions := gitPushOptions(make(map[string]string))
|
|
return &pushOptions
|
|
}
|
|
|
|
func NewFromMap(o *map[string]string) Interface {
|
|
return (*gitPushOptions)(o)
|
|
}
|
|
|
|
func (o *gitPushOptions) ReadEnv() Interface {
|
|
if pushCount, err := strconv.Atoi(os.Getenv(EnvCount)); err == nil {
|
|
for idx := 0; idx < pushCount; idx++ {
|
|
_ = o.Parse(os.Getenv(fmt.Sprintf(EnvFormat, idx)))
|
|
}
|
|
}
|
|
return o
|
|
}
|
|
|
|
func (o *gitPushOptions) Parse(data string) bool {
|
|
key, value, found := strings.Cut(data, "=")
|
|
if !found {
|
|
value = "true"
|
|
}
|
|
switch Key(key) {
|
|
case RepoPrivate:
|
|
case RepoTemplate:
|
|
case AgitTopic:
|
|
case AgitForcePush:
|
|
case AgitTitle:
|
|
case AgitDescription:
|
|
default:
|
|
return false
|
|
}
|
|
(*o)[key] = value
|
|
return true
|
|
}
|
|
|
|
func (o gitPushOptions) Map() map[string]string {
|
|
return o
|
|
}
|
|
|
|
func (o gitPushOptions) ChangeRepoSettings() bool {
|
|
if o.Empty() {
|
|
return false
|
|
}
|
|
for _, key := range []Key{RepoPrivate, RepoTemplate} {
|
|
_, ok := o[string(key)]
|
|
if ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (o gitPushOptions) Empty() bool {
|
|
return len(o) == 0
|
|
}
|
|
|
|
func (o gitPushOptions) GetBool(key Key, def bool) bool {
|
|
if val, ok := o[string(key)]; ok {
|
|
if b, err := strconv.ParseBool(val); err == nil {
|
|
return b
|
|
}
|
|
}
|
|
return def
|
|
}
|
|
|
|
func (o gitPushOptions) GetString(key Key) (string, bool) {
|
|
val, ok := o[string(key)]
|
|
return val, ok
|
|
}
|