Adding upstream version 3.10.8.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
37e9b6d587
commit
03bfe4079e
356 changed files with 28857 additions and 0 deletions
81
forges/helpers/auth/auth.go
Normal file
81
forges/helpers/auth/auth.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type ForgeAuthInterface interface {
|
||||
SetURL(url string)
|
||||
GetURL() string
|
||||
|
||||
GetPushURL() string
|
||||
|
||||
SetUsername(username string)
|
||||
GetUsername() string
|
||||
|
||||
SetPassword(password string)
|
||||
GetPassword() string
|
||||
|
||||
SetToken(token string)
|
||||
GetToken() string
|
||||
}
|
||||
|
||||
type ForgeAuth struct {
|
||||
url string
|
||||
username string
|
||||
password string
|
||||
token string
|
||||
}
|
||||
|
||||
func NewForgeAuth() ForgeAuth {
|
||||
return ForgeAuth{}
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetPushURL() string {
|
||||
u, err := url.Parse(o.url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if o.GetToken() != "" {
|
||||
u.User = url.UserPassword("token", o.GetToken())
|
||||
} else {
|
||||
u.User = url.UserPassword(o.GetUsername(), o.GetPassword())
|
||||
}
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) SetURL(url string) {
|
||||
o.url = url
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetURL() string {
|
||||
return o.url
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) SetUsername(username string) {
|
||||
o.username = username
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetUsername() string {
|
||||
return o.username
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) SetPassword(password string) {
|
||||
o.password = password
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetPassword() string {
|
||||
return o.password
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) SetToken(token string) {
|
||||
o.token = token
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetToken() string {
|
||||
return o.token
|
||||
}
|
61
forges/helpers/auth/cli.go
Normal file
61
forges/helpers/auth/cli.go
Normal file
|
@ -0,0 +1,61 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/urfave/cli/v3"
|
||||
)
|
||||
|
||||
func ForgeUserOption(prefix string) string {
|
||||
return prefix + "-user"
|
||||
}
|
||||
|
||||
func ForgePasswordOption(prefix string) string {
|
||||
return prefix + "-password"
|
||||
}
|
||||
|
||||
func ForgeTokenOption(prefix string) string {
|
||||
return prefix + "-token"
|
||||
}
|
||||
|
||||
func ForgeURLOption(prefix string) string {
|
||||
return prefix + "-url"
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) FromFlags(ctx context.Context, c *cli.Command, prefix string) {
|
||||
o.SetUsername(c.String(ForgeUserOption(prefix)))
|
||||
o.SetPassword(c.String(ForgePasswordOption(prefix)))
|
||||
o.SetToken(c.String(ForgeTokenOption(prefix)))
|
||||
o.SetURL(c.String(ForgeURLOption(prefix)))
|
||||
}
|
||||
|
||||
func (o *ForgeAuth) GetFlags(prefix, category string) []cli.Flag {
|
||||
flags := make([]cli.Flag, 0, 10)
|
||||
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Name: ForgeUserOption(prefix),
|
||||
Usage: "`USER` to access the forge API",
|
||||
Category: prefix,
|
||||
})
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Name: ForgePasswordOption(prefix),
|
||||
Usage: "`PASSWORD` of the user",
|
||||
Category: prefix,
|
||||
})
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Name: ForgeTokenOption(prefix),
|
||||
Usage: "`TOKEN` of the user",
|
||||
Category: prefix,
|
||||
})
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Name: ForgeURLOption(prefix),
|
||||
Usage: "`URL` of the forge",
|
||||
Category: prefix,
|
||||
})
|
||||
|
||||
return flags
|
||||
}
|
73
forges/helpers/pullrequest/helper.go
Normal file
73
forges/helpers/pullrequest/helper.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package pullrequest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/f3"
|
||||
helpers_repository "code.forgejo.org/f3/gof3/v3/forges/helpers/repository"
|
||||
"code.forgejo.org/f3/gof3/v3/id"
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
|
||||
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
Upsert(context.Context) id.NodeID
|
||||
Get(context.Context) bool
|
||||
}
|
||||
|
||||
type prInterface interface {
|
||||
logger.MessageInterface
|
||||
f3_tree.PullRequestDriverInterface
|
||||
GetNode() generic.NodeInterface
|
||||
SetFetchFunc(func(ctx context.Context, url, ref string))
|
||||
ToFormat() f3.Interface
|
||||
}
|
||||
|
||||
type helper struct {
|
||||
pr prInterface
|
||||
r helpers_repository.Interface
|
||||
}
|
||||
|
||||
func (o *helper) getRepository(ctx context.Context) helpers_repository.Interface {
|
||||
if o.r == nil {
|
||||
project := f3_tree.GetFirstNodeKind(o.pr.GetNode(), f3_tree.KindProject)
|
||||
r := project.Find(generic.NewPathFromString("repositories/vcs"))
|
||||
if r == generic.NilNode {
|
||||
panic(fmt.Errorf("no repository found for %s", project.GetCurrentPath()))
|
||||
}
|
||||
o.r = r.GetDriver().(f3_tree.ForgeDriverInterface).GetHelper().(helpers_repository.Interface)
|
||||
}
|
||||
return o.r
|
||||
}
|
||||
|
||||
func (o *helper) Get(ctx context.Context) bool {
|
||||
headURL := o.getRepository(ctx).GetRepositoryURL()
|
||||
headRef := o.pr.GetPullRequestHead()
|
||||
o.pr.SetFetchFunc(func(ctx context.Context, url, ref string) {
|
||||
helpers_repository.GitMirrorRef(ctx, o.pr, headURL, headRef, url, ref)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
func (o *helper) Upsert(ctx context.Context) id.NodeID {
|
||||
f := o.pr.ToFormat().(*f3.PullRequest)
|
||||
if f.FetchFunc != nil {
|
||||
pushURL := o.getRepository(ctx).GetRepositoryPushURL()
|
||||
for _, pushRef := range o.pr.GetPullRequestPushRefs() {
|
||||
f.FetchFunc(ctx, pushURL, pushRef)
|
||||
}
|
||||
}
|
||||
return o.pr.GetNode().GetID()
|
||||
}
|
||||
|
||||
func NewHelper(ctx context.Context, pr prInterface) Interface {
|
||||
return &helper{
|
||||
pr: pr,
|
||||
}
|
||||
}
|
103
forges/helpers/repository/git.go
Normal file
103
forges/helpers/repository/git.go
Normal file
|
@ -0,0 +1,103 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
)
|
||||
|
||||
func GitGetSha(ctx context.Context, log logger.MessageInterface, repo, ref string) string {
|
||||
sha := util.Command(ctx, log, "git", "-C", repo, "rev-parse", ref)
|
||||
return strings.TrimSuffix(sha, "\n")
|
||||
}
|
||||
|
||||
func disableHooks(ctx context.Context, log logger.MessageInterface, dir string) func() {
|
||||
if !util.FileExists(dir) {
|
||||
return func() {}
|
||||
}
|
||||
util.Command(ctx, log, "git", "-C", dir, "config", "core.hooksPath", "/dev/null")
|
||||
return func() {
|
||||
util.Command(ctx, log, "git", "-C", dir, "config", "--unset", "core.hooksPath")
|
||||
}
|
||||
}
|
||||
|
||||
func GitMirrorRef(ctx context.Context, log logger.MessageInterface, originURL, originRef, destinationURL, destinationRef string) {
|
||||
if log == nil {
|
||||
log = logger.NewLogger()
|
||||
}
|
||||
if originURL == destinationURL {
|
||||
log.Log(1, logger.Trace, "do nothing because origin & destination are the same %s\n", originURL)
|
||||
return
|
||||
}
|
||||
log.Log(1, logger.Trace, "%s:%s => %s:%s\n", originURL, originRef, destinationURL, destinationRef)
|
||||
defer disableHooks(ctx, log, destinationURL)()
|
||||
if util.FileExists(originURL) {
|
||||
util.Command(ctx, log, "git", "-C", originURL, "push", destinationURL, "+"+originRef+":"+destinationRef)
|
||||
} else {
|
||||
if util.FileExists(destinationURL) {
|
||||
util.Command(ctx, log, "git", "-C", destinationURL, "fetch", originURL, "+"+originRef+":"+destinationRef)
|
||||
} else {
|
||||
directory, err := os.MkdirTemp("", "driverRepository")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(directory)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
util.Command(ctx, log, "git", "clone", "--bare", "--depth", "1", originURL, directory)
|
||||
util.Command(ctx, log, "git", "-C", directory, "fetch", "origin", originRef)
|
||||
util.Command(ctx, log, "git", "-C", directory, "push", destinationURL, "+FETCH_HEAD:"+destinationRef)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GitMirror(ctx context.Context, log logger.MessageInterface, origin, destination string, internalRefs []string) {
|
||||
if log == nil {
|
||||
log = logger.NewLogger()
|
||||
}
|
||||
if origin == destination {
|
||||
log.Log(1, logger.Trace, "do nothing because origin & destination are the same %s\n", origin)
|
||||
return
|
||||
}
|
||||
log.Log(1, logger.Trace, "%s => %s\n", origin, destination)
|
||||
defer disableHooks(ctx, log, destination)()
|
||||
excludeInternalRefs := make([]string, 0, len(internalRefs))
|
||||
for _, ref := range internalRefs {
|
||||
excludeInternalRefs = append(excludeInternalRefs, fmt.Sprintf("^%s", ref))
|
||||
}
|
||||
if util.FileExists(origin) {
|
||||
args := append([]string{"-C", origin, "push", destination, "+refs/*:refs/*"}, excludeInternalRefs...)
|
||||
util.Command(ctx, log, "git", args...)
|
||||
} else {
|
||||
if util.FileExists(destination) {
|
||||
util.Command(ctx, log, "git", "-C", destination, "remote", "add", "--mirror=fetch", "fetchMirror", origin)
|
||||
defer func() { util.Command(ctx, log, "git", "-C", destination, "remote", "remove", "fetchMirror") }()
|
||||
util.Command(ctx, log, "git", "-C", destination, "fetch", "fetchMirror")
|
||||
} else {
|
||||
directory, err := os.MkdirTemp("", "driverRepository")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
err := os.RemoveAll(directory)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
util.Command(ctx, log, "git", "clone", "--mirror", origin, directory)
|
||||
args := append([]string{"-C", directory, "push", destination, "+refs/*:refs/*"}, excludeInternalRefs...)
|
||||
util.Command(ctx, log, "git", args...)
|
||||
}
|
||||
}
|
||||
}
|
33
forges/helpers/repository/git_test.go
Normal file
33
forges/helpers/repository/git_test.go
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_disableHooks(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
ctx := context.Background()
|
||||
log := logger.NewLogger()
|
||||
util.Command(ctx, log, "git", "-C", dir, "init")
|
||||
unset := "unset"
|
||||
get := func() string {
|
||||
out := util.Command(ctx, log, "git", "-C", dir, "config", "--get", "--default", unset, "core.hooksPath")
|
||||
return strings.TrimSuffix(out, "\n")
|
||||
}
|
||||
require.Equal(t, unset, get())
|
||||
enable := disableHooks(ctx, log, dir)
|
||||
require.NotEqual(t, unset, get())
|
||||
enable()
|
||||
require.Equal(t, unset, get())
|
||||
}
|
98
forges/helpers/repository/helper.go
Normal file
98
forges/helpers/repository/helper.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/f3"
|
||||
"code.forgejo.org/f3/gof3/v3/id"
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
f3_tree "code.forgejo.org/f3/gof3/v3/tree/f3"
|
||||
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
)
|
||||
|
||||
type Interface interface {
|
||||
f3_tree.RepositoryDriverInterface
|
||||
Upsert(context.Context, *f3.Repository) id.NodeID
|
||||
Get(context.Context) bool
|
||||
Fetch(context.Context) string
|
||||
}
|
||||
|
||||
type repositoryInterface interface {
|
||||
logger.MessageInterface
|
||||
f3_tree.RepositoryDriverInterface
|
||||
GetNode() generic.NodeInterface
|
||||
ToFormat() f3.Interface
|
||||
SetFetchFunc(func(ctx context.Context, destination string, internalRefs []string))
|
||||
}
|
||||
|
||||
type helper struct {
|
||||
r repositoryInterface
|
||||
dir *string
|
||||
}
|
||||
|
||||
func (o *helper) getDir() string {
|
||||
if o.dir == nil {
|
||||
dir, err := os.MkdirTemp("", "repositoryHelper")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
runtime.SetFinalizer(o, func(o *helper) {
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
o.dir = &dir
|
||||
}
|
||||
return *o.dir
|
||||
}
|
||||
|
||||
func (o *helper) GetRepositoryURL() string { return o.r.GetRepositoryURL() }
|
||||
func (o *helper) GetRepositoryPushURL() string { return o.r.GetRepositoryPushURL() }
|
||||
func (o *helper) GetRepositoryInternalRefs() []string { return o.r.GetRepositoryInternalRefs() }
|
||||
|
||||
func (o *helper) Fetch(ctx context.Context) string {
|
||||
to := o.getDir()
|
||||
o.r.Trace("%s", to)
|
||||
if err := util.CommandWithErr(ctx, util.CommandOptions{}, "git", "-C", to, "rev-parse", "--is-bare-repository"); err != nil {
|
||||
o.r.Trace(util.Command(ctx, o.r, "git", "-C", to, "init", "--bare"))
|
||||
}
|
||||
from := o.r.GetRepositoryURL()
|
||||
GitMirror(ctx, o.r, from, to, []string{})
|
||||
return to
|
||||
}
|
||||
|
||||
func (o *helper) Get(ctx context.Context) bool {
|
||||
from := o.r.GetRepositoryURL()
|
||||
o.r.Trace("%s", from)
|
||||
o.r.SetFetchFunc(func(ctx context.Context, destination string, internalRefs []string) {
|
||||
o.r.Trace("git clone %s %s", from, destination)
|
||||
GitMirror(ctx, o.r, from, destination, internalRefs)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
func (o *helper) Upsert(ctx context.Context, f *f3.Repository) id.NodeID {
|
||||
if f.FetchFunc != nil {
|
||||
to := o.r.GetRepositoryPushURL()
|
||||
internalRefs := o.r.GetRepositoryInternalRefs()
|
||||
o.r.Trace("%s", to)
|
||||
f.FetchFunc(ctx, to, internalRefs)
|
||||
} else {
|
||||
o.r.Trace("NO FETCH %s", o.r.GetRepositoryPushURL())
|
||||
panic("")
|
||||
}
|
||||
return o.r.GetNode().GetID()
|
||||
}
|
||||
|
||||
func NewHelper(r repositoryInterface) Interface {
|
||||
h := &helper{r: r}
|
||||
return h
|
||||
}
|
48
forges/helpers/tests/repository/helper_test.go
Normal file
48
forges/helpers/tests/repository/helper_test.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/f3"
|
||||
helpers_repository "code.forgejo.org/f3/gof3/v3/forges/helpers/repository"
|
||||
"code.forgejo.org/f3/gof3/v3/logger"
|
||||
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type repositoryMock struct {
|
||||
logger.Logger
|
||||
url string
|
||||
}
|
||||
|
||||
func newRepositoryMock(url string) *repositoryMock {
|
||||
r := &repositoryMock{
|
||||
url: url,
|
||||
}
|
||||
r.SetLogger(logger.NewLogger())
|
||||
return r
|
||||
}
|
||||
|
||||
func (o *repositoryMock) GetNode() generic.NodeInterface { return nil }
|
||||
func (o *repositoryMock) GetRepositoryPushURL() string { return o.url }
|
||||
func (o *repositoryMock) GetRepositoryInternalRefs() []string { return []string{} }
|
||||
func (o *repositoryMock) GetRepositoryURL() string { return o.url }
|
||||
func (o *repositoryMock) ToFormat() f3.Interface { return nil }
|
||||
func (o *repositoryMock) SetFetchFunc(func(ctx context.Context, destination string, internalRefs []string)) {
|
||||
}
|
||||
|
||||
func TestRepositoryHelper(t *testing.T) {
|
||||
url := t.TempDir()
|
||||
repositoryHelper := NewTestHelper(t, url, nil)
|
||||
repositoryHelper.CreateRepositoryContent("").PushMirror()
|
||||
|
||||
h := helpers_repository.NewHelper(newRepositoryMock(url))
|
||||
assert.True(t, util.FileExists(h.Fetch(context.Background())))
|
||||
}
|
15
forges/helpers/tests/repository/interface.go
Normal file
15
forges/helpers/tests/repository/interface.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type TestingT interface {
|
||||
assert.TestingT
|
||||
TempDir() string
|
||||
Skip(args ...any)
|
||||
}
|
182
forges/helpers/tests/repository/repositoryhelpers.go
Normal file
182
forges/helpers/tests/repository/repositoryhelpers.go
Normal file
|
@ -0,0 +1,182 @@
|
|||
// Copyright Earl Warren <contact@earl-warren.org>
|
||||
// Copyright Loïc Dachary <loic@dachary.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.forgejo.org/f3/gof3/v3/f3"
|
||||
helpers_repository "code.forgejo.org/f3/gof3/v3/forges/helpers/repository"
|
||||
"code.forgejo.org/f3/gof3/v3/tree/generic"
|
||||
"code.forgejo.org/f3/gof3/v3/util"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type TestHelper struct {
|
||||
t TestingT
|
||||
clone string
|
||||
bare string
|
||||
node generic.NodeInterface
|
||||
}
|
||||
|
||||
func NewTestHelper(t TestingT, bare string, node generic.NodeInterface) *TestHelper {
|
||||
o := &TestHelper{
|
||||
t: t,
|
||||
node: node,
|
||||
}
|
||||
|
||||
o.bare = bare
|
||||
if o.bare == "" {
|
||||
if node == nil {
|
||||
panic("")
|
||||
}
|
||||
bare, err := os.MkdirTemp("", "repositoryHelperBare")
|
||||
assert.NoError(t, err)
|
||||
o.bare = bare
|
||||
o.InitBare()
|
||||
node.ToFormat().(*f3.Repository).FetchFunc(context.Background(), bare, []string{})
|
||||
} else {
|
||||
o.InitBare()
|
||||
}
|
||||
|
||||
clone, err := os.MkdirTemp("", "repositoryHelperClone")
|
||||
assert.NoError(t, err)
|
||||
o.clone = clone
|
||||
util.Command(context.Background(), nil, "git", "clone", o.bare, o.clone)
|
||||
o.setRepositoryConfig()
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *TestHelper) GetClone() string {
|
||||
return o.clone
|
||||
}
|
||||
|
||||
func (o *TestHelper) PullClone() {
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "pull")
|
||||
}
|
||||
|
||||
func (o *TestHelper) GetBare() string {
|
||||
return o.bare
|
||||
}
|
||||
|
||||
func (o *TestHelper) GetNode() generic.NodeInterface {
|
||||
return o.node
|
||||
}
|
||||
|
||||
func (o *TestHelper) InitBare(args ...string) string {
|
||||
if !util.FileExists(o.bare) {
|
||||
assert.NoError(o.t, os.Mkdir(o.bare, 0o700))
|
||||
}
|
||||
init := []string{"-C", o.bare, "init", "--bare"}
|
||||
init = append(init, args...)
|
||||
return util.Command(context.Background(), nil, "git", init...)
|
||||
}
|
||||
|
||||
func (o *TestHelper) RevList() string {
|
||||
return util.Command(context.Background(), nil, "git", "-C", o.clone, "rev-list", "--all")
|
||||
}
|
||||
|
||||
func (o *TestHelper) AssertReadmeContains(content string) {
|
||||
o.PullClone()
|
||||
readme, err := os.ReadFile(filepath.Join(o.GetClone(), "README.md"))
|
||||
assert.NoError(o.t, err)
|
||||
assert.Contains(o.t, string(readme), content)
|
||||
}
|
||||
|
||||
func (o *TestHelper) AssertRepositoryNotFileExists(file string) {
|
||||
assert.NotContains(o.t, o.repositoryLsFile(file), file)
|
||||
}
|
||||
|
||||
func (o *TestHelper) AssertRepositoryFileExists(file string) {
|
||||
assert.Contains(o.t, o.repositoryLsFile(file), file)
|
||||
}
|
||||
|
||||
func (o *TestHelper) AssertRepositoryTagExists(tag string) {
|
||||
assert.EqualValues(o.t, tag+"\n", util.Command(context.Background(), nil, "git", "-C", o.clone, "tag", "-l", tag))
|
||||
}
|
||||
|
||||
func (o *TestHelper) AssertRepositoryBranchExists(branch string) {
|
||||
assert.EqualValues(o.t, "refs/heads/"+branch+"\n", util.Command(context.Background(), nil, "git", "-C", o.bare, "branch", "--format", "%(refname)", "-l", branch))
|
||||
}
|
||||
|
||||
func (o *TestHelper) repositoryCountObject() int64 {
|
||||
out := util.Command(context.Background(), nil, "git", "-C", o.clone, "count-objects")
|
||||
count := strings.Split(out, " ")[0]
|
||||
return util.ParseInt(count)
|
||||
}
|
||||
|
||||
func (o *TestHelper) repositoryLsFile(file string) string {
|
||||
if o.repositoryCountObject() > 0 {
|
||||
return util.Command(context.Background(), nil, "git", "-C", o.clone, "ls-tree", "HEAD", file)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (o *TestHelper) PushMirror() {
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "push", "--all", "origin")
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "push", "--tags", "origin")
|
||||
if o.node != nil {
|
||||
f := o.node.ToFormat().(*f3.Repository)
|
||||
f.FetchFunc = func(ctx context.Context, destination string, internalRefs []string) {
|
||||
helpers_repository.GitMirror(ctx, nil, o.bare, destination, internalRefs)
|
||||
}
|
||||
if f.GetID() == "" {
|
||||
panic(fmt.Errorf("id %s", o.node.GetID()))
|
||||
}
|
||||
o.node.FromFormat(f)
|
||||
o.node.Upsert(context.Background())
|
||||
}
|
||||
}
|
||||
|
||||
func (o *TestHelper) CreateRepositoryContent(content string) *TestHelper {
|
||||
o.commitREADME(content)
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *TestHelper) CreateRepositoryTag(tag, commit string) *TestHelper {
|
||||
o.createRepositoryTag(tag, commit)
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *TestHelper) createRepositoryTag(tag, commit string) {
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "tag", tag, commit)
|
||||
}
|
||||
|
||||
func (o *TestHelper) setRepositoryConfig() {
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "config", "user.email", "author@example.com")
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "config", "user.name", "Author")
|
||||
}
|
||||
|
||||
func (o *TestHelper) commitREADME(content string) {
|
||||
readme := fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s\n%s", o.clone, content)
|
||||
assert.NoError(o.t, os.WriteFile(filepath.Join(o.clone, "README.md"), []byte(readme), 0o644))
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "add", "README.md")
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "commit", "-m", "Add README", "README.md")
|
||||
}
|
||||
|
||||
func (o *TestHelper) GetRepositorySha(branch string) string {
|
||||
sha := util.Command(context.Background(), nil, "git", "-C", o.clone, "rev-parse", "origin/"+branch)
|
||||
return strings.TrimSuffix(sha, "\n")
|
||||
}
|
||||
|
||||
func (o *TestHelper) BranchRepositoryFeature(branch, content string) *TestHelper {
|
||||
o.InternalBranchRepositoryFeature(branch, content)
|
||||
return o
|
||||
}
|
||||
|
||||
func (o *TestHelper) InternalBranchRepositoryFeature(branch, content string) {
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "checkout", "-b", branch, "master")
|
||||
assert.NoError(o.t, os.WriteFile(filepath.Join(o.clone, "README.md"), []byte(content), 0o644))
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "add", "README.md")
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "commit", "-m", "feature README", "README.md")
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "push", "origin", branch)
|
||||
util.Command(context.Background(), nil, "git", "-C", o.clone, "checkout", "master")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue