mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-30 13:44:36 -04:00
[TESTS] Move CreateDeclarativeRepo
to more accessible location
- This allows `CreateDeclarativeRepo` to be used by other testing packages such as E2EE testing. - Removes unused function in `services/webhook/sourcehut/builds_test.go`.
This commit is contained in:
parent
78e4736db6
commit
f78e397dd6
32 changed files with 196 additions and 258 deletions
|
@ -28,15 +28,11 @@ import (
|
|||
"code.gitea.io/gitea/cmd"
|
||||
"code.gitea.io/gitea/models/auth"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
unit_model "code.gitea.io/gitea/models/unit"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/graceful"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/testlogger"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
@ -44,14 +40,10 @@ import (
|
|||
"code.gitea.io/gitea/routers"
|
||||
"code.gitea.io/gitea/services/auth/source/remote"
|
||||
gitea_context "code.gitea.io/gitea/services/context"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
files_service "code.gitea.io/gitea/services/repository/files"
|
||||
user_service "code.gitea.io/gitea/services/user"
|
||||
wiki_service "code.gitea.io/gitea/services/wiki"
|
||||
"code.gitea.io/gitea/tests"
|
||||
|
||||
"github.com/PuerkitoBio/goquery"
|
||||
gouuid "github.com/google/uuid"
|
||||
"github.com/markbates/goth"
|
||||
"github.com/markbates/goth/gothic"
|
||||
goth_github "github.com/markbates/goth/providers/github"
|
||||
|
@ -693,140 +685,3 @@ func GetHTMLTitle(t testing.TB, session *TestSession, urlStr string) string {
|
|||
doc := NewHTMLParser(t, resp.Body)
|
||||
return doc.Find("head title").Text()
|
||||
}
|
||||
|
||||
type DeclarativeRepoOptions struct {
|
||||
Name optional.Option[string]
|
||||
EnabledUnits optional.Option[[]unit_model.Type]
|
||||
DisabledUnits optional.Option[[]unit_model.Type]
|
||||
Files optional.Option[[]*files_service.ChangeRepoFile]
|
||||
WikiBranch optional.Option[string]
|
||||
AutoInit optional.Option[bool]
|
||||
IsTemplate optional.Option[bool]
|
||||
}
|
||||
|
||||
func CreateDeclarativeRepoWithOptions(t *testing.T, owner *user_model.User, opts DeclarativeRepoOptions) (*repo_model.Repository, string, func()) {
|
||||
t.Helper()
|
||||
|
||||
// Not using opts.Name.ValueOrDefault() here to avoid unnecessarily
|
||||
// generating an UUID when a name is specified.
|
||||
var repoName string
|
||||
if opts.Name.Has() {
|
||||
repoName = opts.Name.Value()
|
||||
} else {
|
||||
repoName = gouuid.NewString()
|
||||
}
|
||||
|
||||
var autoInit bool
|
||||
if opts.AutoInit.Has() {
|
||||
autoInit = opts.AutoInit.Value()
|
||||
} else {
|
||||
autoInit = true
|
||||
}
|
||||
|
||||
// Create the repository
|
||||
repo, err := repo_service.CreateRepository(db.DefaultContext, owner, owner, repo_service.CreateRepoOptions{
|
||||
Name: repoName,
|
||||
Description: "Temporary Repo",
|
||||
AutoInit: autoInit,
|
||||
Gitignores: "",
|
||||
License: "WTFPL",
|
||||
Readme: "Default",
|
||||
DefaultBranch: "main",
|
||||
IsTemplate: opts.IsTemplate.Value(),
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, repo)
|
||||
|
||||
// Populate `enabledUnits` if we have any enabled.
|
||||
var enabledUnits []repo_model.RepoUnit
|
||||
if opts.EnabledUnits.Has() {
|
||||
units := opts.EnabledUnits.Value()
|
||||
enabledUnits = make([]repo_model.RepoUnit, len(units))
|
||||
|
||||
for i, unitType := range units {
|
||||
enabledUnits[i] = repo_model.RepoUnit{
|
||||
RepoID: repo.ID,
|
||||
Type: unitType,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Adjust the repo units according to our parameters.
|
||||
if opts.EnabledUnits.Has() || opts.DisabledUnits.Has() {
|
||||
err := repo_service.UpdateRepositoryUnits(db.DefaultContext, repo, enabledUnits, opts.DisabledUnits.ValueOrDefault(nil))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Add files, if any.
|
||||
var sha string
|
||||
if opts.Files.Has() {
|
||||
assert.True(t, autoInit, "Files cannot be specified if AutoInit is disabled")
|
||||
files := opts.Files.Value()
|
||||
|
||||
resp, err := files_service.ChangeRepoFiles(git.DefaultContext, repo, owner, &files_service.ChangeRepoFilesOptions{
|
||||
Files: files,
|
||||
Message: "add files",
|
||||
OldBranch: "main",
|
||||
NewBranch: "main",
|
||||
Author: &files_service.IdentityOptions{
|
||||
Name: owner.Name,
|
||||
Email: owner.Email,
|
||||
},
|
||||
Committer: &files_service.IdentityOptions{
|
||||
Name: owner.Name,
|
||||
Email: owner.Email,
|
||||
},
|
||||
Dates: &files_service.CommitDateOptions{
|
||||
Author: time.Now(),
|
||||
Committer: time.Now(),
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
|
||||
sha = resp.Commit.SHA
|
||||
}
|
||||
|
||||
// If there's a Wiki branch specified, create a wiki, and a default wiki page.
|
||||
if opts.WikiBranch.Has() {
|
||||
// Set the wiki branch in the database first
|
||||
repo.WikiBranch = opts.WikiBranch.Value()
|
||||
err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "wiki_branch")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Initialize the wiki
|
||||
err = wiki_service.InitWiki(db.DefaultContext, repo)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Add a new wiki page
|
||||
err = wiki_service.AddWikiPage(db.DefaultContext, owner, repo, "Home", "Welcome to the wiki!", "Add a Home page")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
// Return the repo, the top commit, and a defer-able function to delete the
|
||||
// repo.
|
||||
return repo, sha, func() {
|
||||
repo_service.DeleteRepository(db.DefaultContext, owner, repo, false)
|
||||
}
|
||||
}
|
||||
|
||||
func CreateDeclarativeRepo(t *testing.T, owner *user_model.User, name string, enabledUnits, disabledUnits []unit_model.Type, files []*files_service.ChangeRepoFile) (*repo_model.Repository, string, func()) {
|
||||
t.Helper()
|
||||
|
||||
var opts DeclarativeRepoOptions
|
||||
|
||||
if name != "" {
|
||||
opts.Name = optional.Some(name)
|
||||
}
|
||||
if enabledUnits != nil {
|
||||
opts.EnabledUnits = optional.Some(enabledUnits)
|
||||
}
|
||||
if disabledUnits != nil {
|
||||
opts.DisabledUnits = optional.Some(disabledUnits)
|
||||
}
|
||||
if files != nil {
|
||||
opts.Files = optional.Some(files)
|
||||
}
|
||||
|
||||
return CreateDeclarativeRepoWithOptions(t, owner, opts)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue