mirror of
https://github.com/go-gitea/gitea
synced 2025-12-07 13:28:25 +00:00
Merge branch 'main' into api-repo-actions
This commit is contained in:
@@ -15,8 +15,6 @@ import (
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
_ "code.gitea.io/gitea/models" // https://discourse.gitea.io/t/testfixtures-could-not-clean-table-access-no-such-table-access/4137/4
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
|
||||
@@ -8,6 +8,10 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
|
||||
_ "code.gitea.io/gitea/models"
|
||||
_ "code.gitea.io/gitea/models/actions"
|
||||
_ "code.gitea.io/gitea/models/activities"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
@@ -250,6 +250,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) {
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Data["ContextUser"] = ctx.ContextUser
|
||||
|
||||
ctx.Data["CanReadProjects"] = ctx.Org.CanReadUnit(ctx, unit.TypeProjects)
|
||||
ctx.Data["CanReadPackages"] = ctx.Org.CanReadUnit(ctx, unit.TypePackages)
|
||||
|
||||
@@ -471,6 +471,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
|
||||
}
|
||||
ctx.Repo.Owner = owner
|
||||
ctx.ContextUser = owner
|
||||
ctx.Data["ContextUser"] = ctx.ContextUser
|
||||
ctx.Data["Username"] = ctx.Repo.Owner.Name
|
||||
|
||||
// redirect link to wiki
|
||||
|
||||
+12
-2
@@ -221,8 +221,18 @@ type RunOpts struct {
|
||||
Dir string
|
||||
|
||||
Stdout, Stderr io.Writer
|
||||
Stdin io.Reader
|
||||
PipelineFunc func(context.Context, context.CancelFunc) error
|
||||
|
||||
// Stdin is used for passing input to the command
|
||||
// The caller must make sure the Stdin writer is closed properly to finish the Run function.
|
||||
// Otherwise, the Run function may hang for long time or forever, especially when the Git's context deadline is not the same as the caller's.
|
||||
// Some common mistakes:
|
||||
// * `defer stdinWriter.Close()` then call `cmd.Run()`: the Run() would never return if the command is killed by timeout
|
||||
// * `go { case <- parentContext.Done(): stdinWriter.Close() }` with `cmd.Run(DefaultTimeout)`: the command would have been killed by timeout but the Run doesn't return until stdinWriter.Close()
|
||||
// * `go { if stdoutReader.Read() err != nil: stdinWriter.Close() }` with `cmd.Run()`: the stdoutReader may never return error if the command is killed by timeout
|
||||
// In the future, ideally the git module itself should have full control of the stdin, to avoid such problems and make it easier to refactor to a better architecture.
|
||||
Stdin io.Reader
|
||||
|
||||
PipelineFunc func(context.Context, context.CancelFunc) error
|
||||
}
|
||||
|
||||
func commonBaseEnvs() []string {
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime/pprof"
|
||||
"slices"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -20,7 +21,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"code.gitea.io/gitea/modules/queue"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -54,22 +54,22 @@ func index(ctx context.Context, indexer internal.Indexer, repoID int64) error {
|
||||
}
|
||||
|
||||
// skip forks from being indexed if unit is not present
|
||||
if !util.SliceContains(repoTypes, "forks") && repo.IsFork {
|
||||
if !slices.Contains(repoTypes, "forks") && repo.IsFork {
|
||||
return nil
|
||||
}
|
||||
|
||||
// skip mirrors from being indexed if unit is not present
|
||||
if !util.SliceContains(repoTypes, "mirrors") && repo.IsMirror {
|
||||
if !slices.Contains(repoTypes, "mirrors") && repo.IsMirror {
|
||||
return nil
|
||||
}
|
||||
|
||||
// skip templates from being indexed if unit is not present
|
||||
if !util.SliceContains(repoTypes, "templates") && repo.IsTemplate {
|
||||
if !slices.Contains(repoTypes, "templates") && repo.IsTemplate {
|
||||
return nil
|
||||
}
|
||||
|
||||
// skip regular repos from being indexed if unit is not present
|
||||
if !util.SliceContains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate {
|
||||
if !slices.Contains(repoTypes, "sources") && !repo.IsFork && !repo.IsMirror && !repo.IsTemplate {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -122,21 +122,6 @@ func Init() {
|
||||
indexer := *globalIndexer.Load()
|
||||
for _, indexerData := range items {
|
||||
log.Trace("IndexerData Process Repo: %d", indexerData.RepoID)
|
||||
|
||||
// FIXME: it seems there is a bug in `CatFileBatch` or `nio.Pipe`, which will cause the process to hang forever in rare cases
|
||||
/*
|
||||
sync.(*Cond).Wait(cond.go:70)
|
||||
github.com/djherbis/nio/v3.(*PipeReader).Read(sync.go:106)
|
||||
bufio.(*Reader).fill(bufio.go:106)
|
||||
bufio.(*Reader).ReadSlice(bufio.go:372)
|
||||
bufio.(*Reader).collectFragments(bufio.go:447)
|
||||
bufio.(*Reader).ReadString(bufio.go:494)
|
||||
code.gitea.io/gitea/modules/git.ReadBatchLine(batch_reader.go:149)
|
||||
code.gitea.io/gitea/modules/indexer/code.(*BleveIndexer).addUpdate(bleve.go:214)
|
||||
code.gitea.io/gitea/modules/indexer/code.(*BleveIndexer).Index(bleve.go:296)
|
||||
code.gitea.io/gitea/modules/indexer/code.(*wrappedIndexer).Index(wrapped.go:74)
|
||||
code.gitea.io/gitea/modules/indexer/code.index(indexer.go:105)
|
||||
*/
|
||||
if err := index(ctx, indexer, indexerData.RepoID); err != nil {
|
||||
unhandled = append(unhandled, indexerData)
|
||||
if !setting.IsInTesting {
|
||||
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
"code.gitea.io/gitea/modules/indexer/code/internal"
|
||||
|
||||
_ "code.gitea.io/gitea/models"
|
||||
_ "code.gitea.io/gitea/models/actions"
|
||||
_ "code.gitea.io/gitea/models/activities"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
_ "code.gitea.io/gitea/models"
|
||||
_ "code.gitea.io/gitea/models/actions"
|
||||
_ "code.gitea.io/gitea/models/activities"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
@@ -10,6 +10,7 @@ package tests
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -457,7 +458,7 @@ var cases = []*testIndexerCase{
|
||||
assert.Contains(t, data[v.ID].MentionIDs, int64(1))
|
||||
}
|
||||
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
|
||||
return util.SliceContains(v.MentionIDs, 1)
|
||||
return slices.Contains(v.MentionIDs, 1)
|
||||
}), result.Total)
|
||||
},
|
||||
},
|
||||
@@ -478,7 +479,7 @@ var cases = []*testIndexerCase{
|
||||
assert.Contains(t, data[v.ID].ReviewedIDs, int64(1))
|
||||
}
|
||||
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
|
||||
return util.SliceContains(v.ReviewedIDs, 1)
|
||||
return slices.Contains(v.ReviewedIDs, 1)
|
||||
}), result.Total)
|
||||
},
|
||||
},
|
||||
@@ -499,7 +500,7 @@ var cases = []*testIndexerCase{
|
||||
assert.Contains(t, data[v.ID].ReviewRequestedIDs, int64(1))
|
||||
}
|
||||
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
|
||||
return util.SliceContains(v.ReviewRequestedIDs, 1)
|
||||
return slices.Contains(v.ReviewRequestedIDs, 1)
|
||||
}), result.Total)
|
||||
},
|
||||
},
|
||||
@@ -520,7 +521,7 @@ var cases = []*testIndexerCase{
|
||||
assert.Contains(t, data[v.ID].SubscriberIDs, int64(1))
|
||||
}
|
||||
assert.Equal(t, countIndexerData(data, func(v *internal.IndexerData) bool {
|
||||
return util.SliceContains(v.SubscriberIDs, 1)
|
||||
return slices.Contains(v.SubscriberIDs, 1)
|
||||
}), result.Total)
|
||||
},
|
||||
},
|
||||
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
_ "code.gitea.io/gitea/models"
|
||||
_ "code.gitea.io/gitea/models/actions"
|
||||
_ "code.gitea.io/gitea/models/activities"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
+48
-13
@@ -1,27 +1,62 @@
|
||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Package queue implements a specialized queue system for Gitea.
|
||||
// Package queue implements a specialized concurrent queue system for Gitea.
|
||||
//
|
||||
// There are two major kinds of concepts:
|
||||
// Terminology:
|
||||
//
|
||||
// * The "base queue": channel, level, redis:
|
||||
// - They have the same abstraction, the same interface, and they are tested by the same testing code.
|
||||
// - The dummy(immediate) queue is special, it's not a real queue, it's only used as a no-op queue or a testing queue.
|
||||
// 1. Item:
|
||||
// - An item can be a simple value, such as an integer, or a more complex structure that has multiple fields.
|
||||
// Usually a item serves as a task or a message. Sets of items will be sent to a queue handler to be processed.
|
||||
// - It's represented as a JSON-marshaled binary slice in the queue
|
||||
//
|
||||
// * The WorkerPoolQueue: it uses the "base queue" to provide "worker pool" function.
|
||||
// - It calls the "handler" to process the data in the base queue.
|
||||
// - Its "Push" function doesn't block forever,
|
||||
// it will return an error if the queue is full after the timeout.
|
||||
// 2. Batch:
|
||||
// - A collection of items that are grouped together for processing. Each worker receives a batch of items.
|
||||
//
|
||||
// 3. Worker:
|
||||
// - Individual unit of execution designed to process items from the queue. It's a goroutine that calls the Handler.
|
||||
// - Workers will get new items through a channel (WorkerPoolQueue is responsible for the distribution).
|
||||
// - Workers operate in parallel. The default value of max workers is determined by the setting system.
|
||||
//
|
||||
// 4. Handler (represented by HandlerFuncT type):
|
||||
// - It's the function responsible for processing items. Each active worker will call it.
|
||||
// - If an item or some items are not psuccessfully rocessed, the handler could return them as "unhandled items".
|
||||
// In such scenarios, the queue system ensures these unhandled items are returned to the base queue after a brief delay.
|
||||
// This mechanism is particularly beneficial in cases where the processing entity (like a document indexer) is
|
||||
// temporarily unavailable. It ensures that no item is skipped or lost due to transient failures in the processing
|
||||
// mechanism.
|
||||
//
|
||||
// 5. Base queue:
|
||||
// - Represents the underlying storage mechanism for the queue. There are several implementations:
|
||||
// - Channel: Uses Go's native channel constructs to manage the queue, suitable for in-memory queuing.
|
||||
// - LevelDB: Especially useful in persistent queues for single instances.
|
||||
// - Redis: Suitable for clusters, where we may have multiple nodes.
|
||||
// - Dummy: This is special, it's not a real queue, it's a immediate no-op queue, which is useful for tests.
|
||||
// - They all have the same abstraction, the same interface, and they are tested by the same testing code.
|
||||
//
|
||||
// 6. WorkerPoolQueue:
|
||||
// - It's responsible to glue all together, using the "base queue" to provide "worker pool" functionality. It creates
|
||||
// new workers if needed and can flush the queue, running all the items synchronously till it finishes.
|
||||
// - Its "Push" function doesn't block forever, it will return an error if the queue is full after the timeout.
|
||||
//
|
||||
// 7. Manager:
|
||||
// - The purpose of it is to serve as a centralized manager for multiple WorkerPoolQueue instances. Whenever we want
|
||||
// to create a new queue, flush, or get a specific queue, we could use it.
|
||||
//
|
||||
// A queue can be "simple" or "unique". A unique queue will try to avoid duplicate items.
|
||||
// Unique queue's "Has" function can be used to check whether an item is already in the queue,
|
||||
// although it's not 100% reliable due to there is no proper transaction support.
|
||||
// although it's not 100% reliable due to the lack of proper transaction support.
|
||||
// Simple queue's "Has" function always returns "has=false".
|
||||
//
|
||||
// The HandlerFuncT function is called by the WorkerPoolQueue to process the data in the base queue.
|
||||
// If the handler returns "unhandled" items, they will be re-queued to the base queue after a slight delay,
|
||||
// in case the item processor (eg: document indexer) is not available.
|
||||
// A WorkerPoolQueue is a generic struct; this means it will work with any type but just for that type.
|
||||
// If you want another kind of items to run, you would have to call the manager to create a new WorkerPoolQueue for you
|
||||
// with a different handler that works with this new type of item. As an example of this:
|
||||
//
|
||||
// func Init() error {
|
||||
// itemQueue = queue.CreateSimpleQueue(graceful.GetManager().ShutdownContext(), "queue-name", handler)
|
||||
// ...
|
||||
// }
|
||||
// func handler(items ...*mypkg.QueueItem) []*mypkg.QueueItem { ... }
|
||||
package queue
|
||||
|
||||
import "code.gitea.io/gitea/modules/util"
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models/avatars"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/cache"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -34,42 +35,36 @@ type PushCommits struct {
|
||||
HeadCommit *PushCommit
|
||||
CompareURL string
|
||||
Len int
|
||||
|
||||
avatars map[string]string
|
||||
emailUsers map[string]*user_model.User
|
||||
}
|
||||
|
||||
// NewPushCommits creates a new PushCommits object.
|
||||
func NewPushCommits() *PushCommits {
|
||||
return &PushCommits{
|
||||
avatars: make(map[string]string),
|
||||
emailUsers: make(map[string]*user_model.User),
|
||||
}
|
||||
return &PushCommits{}
|
||||
}
|
||||
|
||||
// toAPIPayloadCommit converts a single PushCommit to an api.PayloadCommit object.
|
||||
func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
|
||||
func (pc *PushCommits) toAPIPayloadCommit(ctx context.Context, emailUsers map[string]*user_model.User, repoPath, repoLink string, commit *PushCommit) (*api.PayloadCommit, error) {
|
||||
var err error
|
||||
authorUsername := ""
|
||||
author, ok := pc.emailUsers[commit.AuthorEmail]
|
||||
author, ok := emailUsers[commit.AuthorEmail]
|
||||
if !ok {
|
||||
author, err = user_model.GetUserByEmail(ctx, commit.AuthorEmail)
|
||||
if err == nil {
|
||||
authorUsername = author.Name
|
||||
pc.emailUsers[commit.AuthorEmail] = author
|
||||
emailUsers[commit.AuthorEmail] = author
|
||||
}
|
||||
} else {
|
||||
authorUsername = author.Name
|
||||
}
|
||||
|
||||
committerUsername := ""
|
||||
committer, ok := pc.emailUsers[commit.CommitterEmail]
|
||||
committer, ok := emailUsers[commit.CommitterEmail]
|
||||
if !ok {
|
||||
committer, err = user_model.GetUserByEmail(ctx, commit.CommitterEmail)
|
||||
if err == nil {
|
||||
// TODO: check errors other than email not found.
|
||||
committerUsername = committer.Name
|
||||
pc.emailUsers[commit.CommitterEmail] = committer
|
||||
emailUsers[commit.CommitterEmail] = committer
|
||||
}
|
||||
} else {
|
||||
committerUsername = committer.Name
|
||||
@@ -107,11 +102,10 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
|
||||
commits := make([]*api.PayloadCommit, len(pc.Commits))
|
||||
var headCommit *api.PayloadCommit
|
||||
|
||||
if pc.emailUsers == nil {
|
||||
pc.emailUsers = make(map[string]*user_model.User)
|
||||
}
|
||||
emailUsers := make(map[string]*user_model.User)
|
||||
|
||||
for i, commit := range pc.Commits {
|
||||
apiCommit, err := pc.toAPIPayloadCommit(ctx, repoPath, repoLink, commit)
|
||||
apiCommit, err := pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, commit)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -123,7 +117,7 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
|
||||
}
|
||||
if pc.HeadCommit != nil && headCommit == nil {
|
||||
var err error
|
||||
headCommit, err = pc.toAPIPayloadCommit(ctx, repoPath, repoLink, pc.HeadCommit)
|
||||
headCommit, err = pc.toAPIPayloadCommit(ctx, emailUsers, repoPath, repoLink, pc.HeadCommit)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
@@ -134,35 +128,21 @@ func (pc *PushCommits) ToAPIPayloadCommits(ctx context.Context, repoPath, repoLi
|
||||
// AvatarLink tries to match user in database with e-mail
|
||||
// in order to show custom avatar, and falls back to general avatar link.
|
||||
func (pc *PushCommits) AvatarLink(ctx context.Context, email string) string {
|
||||
if pc.avatars == nil {
|
||||
pc.avatars = make(map[string]string)
|
||||
}
|
||||
avatar, ok := pc.avatars[email]
|
||||
if ok {
|
||||
return avatar
|
||||
}
|
||||
|
||||
size := avatars.DefaultAvatarPixelSize * setting.Avatar.RenderedSizeFactor
|
||||
|
||||
u, ok := pc.emailUsers[email]
|
||||
if !ok {
|
||||
var err error
|
||||
u, err = user_model.GetUserByEmail(ctx, email)
|
||||
v, _ := cache.GetWithContextCache(ctx, "push_commits", email, func() (string, error) {
|
||||
u, err := user_model.GetUserByEmail(ctx, email)
|
||||
if err != nil {
|
||||
pc.avatars[email] = avatars.GenerateEmailAvatarFastLink(ctx, email, size)
|
||||
if !user_model.IsErrUserNotExist(err) {
|
||||
log.Error("GetUserByEmail: %v", err)
|
||||
return ""
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
pc.emailUsers[email] = u
|
||||
return avatars.GenerateEmailAvatarFastLink(ctx, email, size), nil
|
||||
}
|
||||
}
|
||||
if u != nil {
|
||||
pc.avatars[email] = u.AvatarLinkWithSize(ctx, size)
|
||||
}
|
||||
return u.AvatarLinkWithSize(ctx, size), nil
|
||||
})
|
||||
|
||||
return pc.avatars[email]
|
||||
return v
|
||||
}
|
||||
|
||||
// CommitToPushCommit transforms a git.Commit to PushCommit type.
|
||||
@@ -189,7 +169,5 @@ func GitToPushCommits(gitCommits []*git.Commit) *PushCommits {
|
||||
HeadCommit: nil,
|
||||
CompareURL: "",
|
||||
Len: len(commits),
|
||||
avatars: make(map[string]string),
|
||||
emailUsers: make(map[string]*user_model.User),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,11 +103,9 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
|
||||
assert.EqualValues(t, []string{"readme.md"}, headCommit.Modified)
|
||||
}
|
||||
|
||||
func enableGravatar(t *testing.T) {
|
||||
err := system_model.SetSettingNoVersion(db.DefaultContext, system_model.KeyPictureDisableGravatar, "false")
|
||||
assert.NoError(t, err)
|
||||
func initGravatarSource(t *testing.T) {
|
||||
setting.GravatarSource = "https://secure.gravatar.com/avatar"
|
||||
err = system_model.Init(db.DefaultContext)
|
||||
err := system_model.Init(db.DefaultContext)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -134,7 +132,7 @@ func TestPushCommits_AvatarLink(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
enableGravatar(t)
|
||||
initGravatarSource(t)
|
||||
|
||||
assert.Equal(t,
|
||||
"https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?d=identicon&s="+strconv.Itoa(28*setting.Avatar.RenderedSizeFactor),
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"code.gitea.io/gitea/models/unit"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
@@ -156,142 +155,6 @@ func CreateRepositoryByExample(ctx context.Context, doer, u *user_model.User, re
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateRepoOptions contains the create repository options
|
||||
type CreateRepoOptions struct {
|
||||
Name string
|
||||
Description string
|
||||
OriginalURL string
|
||||
GitServiceType api.GitServiceType
|
||||
Gitignores string
|
||||
IssueLabels string
|
||||
License string
|
||||
Readme string
|
||||
DefaultBranch string
|
||||
IsPrivate bool
|
||||
IsMirror bool
|
||||
IsTemplate bool
|
||||
AutoInit bool
|
||||
Status repo_model.RepositoryStatus
|
||||
TrustModel repo_model.TrustModelType
|
||||
MirrorInterval string
|
||||
}
|
||||
|
||||
// CreateRepository creates a repository for the user/organization.
|
||||
func CreateRepository(doer, u *user_model.User, opts CreateRepoOptions) (*repo_model.Repository, error) {
|
||||
if !doer.IsAdmin && !u.CanCreateRepo() {
|
||||
return nil, repo_model.ErrReachLimitOfRepo{
|
||||
Limit: u.MaxRepoCreation,
|
||||
}
|
||||
}
|
||||
|
||||
if len(opts.DefaultBranch) == 0 {
|
||||
opts.DefaultBranch = setting.Repository.DefaultBranch
|
||||
}
|
||||
|
||||
// Check if label template exist
|
||||
if len(opts.IssueLabels) > 0 {
|
||||
if _, err := LoadTemplateLabelsByDisplayName(opts.IssueLabels); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
repo := &repo_model.Repository{
|
||||
OwnerID: u.ID,
|
||||
Owner: u,
|
||||
OwnerName: u.Name,
|
||||
Name: opts.Name,
|
||||
LowerName: strings.ToLower(opts.Name),
|
||||
Description: opts.Description,
|
||||
OriginalURL: opts.OriginalURL,
|
||||
OriginalServiceType: opts.GitServiceType,
|
||||
IsPrivate: opts.IsPrivate,
|
||||
IsFsckEnabled: !opts.IsMirror,
|
||||
IsTemplate: opts.IsTemplate,
|
||||
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
|
||||
Status: opts.Status,
|
||||
IsEmpty: !opts.AutoInit,
|
||||
TrustModel: opts.TrustModel,
|
||||
IsMirror: opts.IsMirror,
|
||||
DefaultBranch: opts.DefaultBranch,
|
||||
}
|
||||
|
||||
var rollbackRepo *repo_model.Repository
|
||||
|
||||
if err := db.WithTx(db.DefaultContext, func(ctx context.Context) error {
|
||||
if err := CreateRepositoryByExample(ctx, doer, u, repo, false, false); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// No need for init mirror.
|
||||
if opts.IsMirror {
|
||||
return nil
|
||||
}
|
||||
|
||||
repoPath := repo_model.RepoPath(u.Name, repo.Name)
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
if err != nil {
|
||||
log.Error("Unable to check if %s exists. Error: %v", repoPath, err)
|
||||
return err
|
||||
}
|
||||
if isExist {
|
||||
// repo already exists - We have two or three options.
|
||||
// 1. We fail stating that the directory exists
|
||||
// 2. We create the db repository to go with this data and adopt the git repo
|
||||
// 3. We delete it and start afresh
|
||||
//
|
||||
// Previously Gitea would just delete and start afresh - this was naughty.
|
||||
// So we will now fail and delegate to other functionality to adopt or delete
|
||||
log.Error("Files already exist in %s and we are not going to adopt or delete.", repoPath)
|
||||
return repo_model.ErrRepoFilesAlreadyExist{
|
||||
Uname: u.Name,
|
||||
Name: repo.Name,
|
||||
}
|
||||
}
|
||||
|
||||
if err = initRepository(ctx, repoPath, doer, repo, opts); err != nil {
|
||||
if err2 := util.RemoveAll(repoPath); err2 != nil {
|
||||
log.Error("initRepository: %v", err)
|
||||
return fmt.Errorf(
|
||||
"delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2)
|
||||
}
|
||||
return fmt.Errorf("initRepository: %w", err)
|
||||
}
|
||||
|
||||
// Initialize Issue Labels if selected
|
||||
if len(opts.IssueLabels) > 0 {
|
||||
if err = InitializeLabels(ctx, repo.ID, opts.IssueLabels, false); err != nil {
|
||||
rollbackRepo = repo
|
||||
rollbackRepo.OwnerID = u.ID
|
||||
return fmt.Errorf("InitializeLabels: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := CheckDaemonExportOK(ctx, repo); err != nil {
|
||||
return fmt.Errorf("checkDaemonExportOK: %w", err)
|
||||
}
|
||||
|
||||
if stdout, _, err := git.NewCommand(ctx, "update-server-info").
|
||||
SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)).
|
||||
RunStdString(&git.RunOpts{Dir: repoPath}); err != nil {
|
||||
log.Error("CreateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err)
|
||||
rollbackRepo = repo
|
||||
rollbackRepo.OwnerID = u.ID
|
||||
return fmt.Errorf("CreateRepository(git update-server-info): %w", err)
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
if rollbackRepo != nil {
|
||||
if errDelete := models.DeleteRepository(doer, rollbackRepo.OwnerID, rollbackRepo.ID); errDelete != nil {
|
||||
log.Error("Rollback deleteRepository: %v", errDelete)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
const notRegularFileMode = os.ModeSymlink | os.ModeNamedPipe | os.ModeSocket | os.ModeDevice | os.ModeCharDevice | os.ModeIrregular
|
||||
|
||||
// getDirectorySize returns the disk consumption for a given path
|
||||
|
||||
@@ -4,151 +4,16 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
activities_model "code.gitea.io/gitea/models/activities"
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/models/organization"
|
||||
"code.gitea.io/gitea/models/perm"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIncludesAllRepositoriesTeams(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
testTeamRepositories := func(teamID int64, repoIds []int64) {
|
||||
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
|
||||
assert.NoError(t, team.LoadRepositories(db.DefaultContext), "%s: GetRepositories", team.Name)
|
||||
assert.Len(t, team.Repos, team.NumRepos, "%s: len repo", team.Name)
|
||||
assert.Len(t, team.Repos, len(repoIds), "%s: repo count", team.Name)
|
||||
for i, rid := range repoIds {
|
||||
if rid > 0 {
|
||||
assert.True(t, models.HasRepository(team, rid), "%s: HasRepository(%d) %d", rid, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get an admin user.
|
||||
user, err := user_model.GetUserByID(db.DefaultContext, 1)
|
||||
assert.NoError(t, err, "GetUserByID")
|
||||
|
||||
// Create org.
|
||||
org := &organization.Organization{
|
||||
Name: "All_repo",
|
||||
IsActive: true,
|
||||
Type: user_model.UserTypeOrganization,
|
||||
Visibility: structs.VisibleTypePublic,
|
||||
}
|
||||
assert.NoError(t, organization.CreateOrganization(org, user), "CreateOrganization")
|
||||
|
||||
// Check Owner team.
|
||||
ownerTeam, err := org.GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err, "GetOwnerTeam")
|
||||
assert.True(t, ownerTeam.IncludesAllRepositories, "Owner team includes all repositories")
|
||||
|
||||
// Create repos.
|
||||
repoIds := make([]int64, 0)
|
||||
for i := 0; i < 3; i++ {
|
||||
r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: fmt.Sprintf("repo-%d", i)})
|
||||
assert.NoError(t, err, "CreateRepository %d", i)
|
||||
if r != nil {
|
||||
repoIds = append(repoIds, r.ID)
|
||||
}
|
||||
}
|
||||
// Get fresh copy of Owner team after creating repos.
|
||||
ownerTeam, err = org.GetOwnerTeam(db.DefaultContext)
|
||||
assert.NoError(t, err, "GetOwnerTeam")
|
||||
|
||||
// Create teams and check repositories.
|
||||
teams := []*organization.Team{
|
||||
ownerTeam,
|
||||
{
|
||||
OrgID: org.ID,
|
||||
Name: "team one",
|
||||
AccessMode: perm.AccessModeRead,
|
||||
IncludesAllRepositories: true,
|
||||
},
|
||||
{
|
||||
OrgID: org.ID,
|
||||
Name: "team 2",
|
||||
AccessMode: perm.AccessModeRead,
|
||||
IncludesAllRepositories: false,
|
||||
},
|
||||
{
|
||||
OrgID: org.ID,
|
||||
Name: "team three",
|
||||
AccessMode: perm.AccessModeWrite,
|
||||
IncludesAllRepositories: true,
|
||||
},
|
||||
{
|
||||
OrgID: org.ID,
|
||||
Name: "team 4",
|
||||
AccessMode: perm.AccessModeWrite,
|
||||
IncludesAllRepositories: false,
|
||||
},
|
||||
}
|
||||
teamRepos := [][]int64{
|
||||
repoIds,
|
||||
repoIds,
|
||||
{},
|
||||
repoIds,
|
||||
{},
|
||||
}
|
||||
for i, team := range teams {
|
||||
if i > 0 { // first team is Owner.
|
||||
assert.NoError(t, models.NewTeam(team), "%s: NewTeam", team.Name)
|
||||
}
|
||||
testTeamRepositories(team.ID, teamRepos[i])
|
||||
}
|
||||
|
||||
// Update teams and check repositories.
|
||||
teams[3].IncludesAllRepositories = false
|
||||
teams[4].IncludesAllRepositories = true
|
||||
teamRepos[4] = repoIds
|
||||
for i, team := range teams {
|
||||
assert.NoError(t, models.UpdateTeam(team, false, true), "%s: UpdateTeam", team.Name)
|
||||
testTeamRepositories(team.ID, teamRepos[i])
|
||||
}
|
||||
|
||||
// Create repo and check teams repositories.
|
||||
r, err := CreateRepository(user, org.AsUser(), CreateRepoOptions{Name: "repo-last"})
|
||||
assert.NoError(t, err, "CreateRepository last")
|
||||
if r != nil {
|
||||
repoIds = append(repoIds, r.ID)
|
||||
}
|
||||
teamRepos[0] = repoIds
|
||||
teamRepos[1] = repoIds
|
||||
teamRepos[4] = repoIds
|
||||
for i, team := range teams {
|
||||
testTeamRepositories(team.ID, teamRepos[i])
|
||||
}
|
||||
|
||||
// Remove repo and check teams repositories.
|
||||
assert.NoError(t, models.DeleteRepository(user, org.ID, repoIds[0]), "DeleteRepository")
|
||||
teamRepos[0] = repoIds[1:]
|
||||
teamRepos[1] = repoIds[1:]
|
||||
teamRepos[3] = repoIds[1:3]
|
||||
teamRepos[4] = repoIds[1:]
|
||||
for i, team := range teams {
|
||||
testTeamRepositories(team.ID, teamRepos[i])
|
||||
}
|
||||
|
||||
// Wipe created items.
|
||||
for i, rid := range repoIds {
|
||||
if i > 0 { // first repo already deleted.
|
||||
assert.NoError(t, models.DeleteRepository(user, org.ID, rid), "DeleteRepository %d", i)
|
||||
}
|
||||
}
|
||||
assert.NoError(t, organization.DeleteOrganization(db.DefaultContext, org), "DeleteOrganization")
|
||||
}
|
||||
|
||||
func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
|
||||
@@ -241,7 +241,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
|
||||
defaultBranch = templateRepo.DefaultBranch
|
||||
}
|
||||
|
||||
return initRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch)
|
||||
return InitRepoCommit(ctx, tmpDir, repo, repo.Owner, defaultBranch)
|
||||
}
|
||||
|
||||
func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *repo_model.Repository) (err error) {
|
||||
@@ -356,7 +356,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
|
||||
}
|
||||
}
|
||||
|
||||
if err = checkInitRepository(ctx, owner.Name, generateRepo.Name); err != nil {
|
||||
if err = CheckInitRepository(ctx, owner.Name, generateRepo.Name); err != nil {
|
||||
return generateRepo, err
|
||||
}
|
||||
|
||||
|
||||
@@ -108,12 +108,7 @@ done
|
||||
}
|
||||
|
||||
// CreateDelegateHooks creates all the hooks scripts for the repo
|
||||
func CreateDelegateHooks(repoPath string) error {
|
||||
return createDelegateHooks(repoPath)
|
||||
}
|
||||
|
||||
// createDelegateHooks creates all the hooks scripts for the repo
|
||||
func createDelegateHooks(repoPath string) (err error) {
|
||||
func CreateDelegateHooks(repoPath string) (err error) {
|
||||
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
|
||||
hookDir := filepath.Join(repoPath, "hooks")
|
||||
|
||||
|
||||
+4
-158
@@ -4,7 +4,6 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -21,7 +20,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/options"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/templates/vars"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
asymkey_service "code.gitea.io/gitea/services/asymkey"
|
||||
)
|
||||
@@ -126,95 +124,8 @@ func LoadRepoConfig() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts CreateRepoOptions) error {
|
||||
commitTimeStr := time.Now().Format(time.RFC3339)
|
||||
authorSig := repo.Owner.NewGitSig()
|
||||
|
||||
// Because this may call hooks we should pass in the environment
|
||||
env := append(os.Environ(),
|
||||
"GIT_AUTHOR_NAME="+authorSig.Name,
|
||||
"GIT_AUTHOR_EMAIL="+authorSig.Email,
|
||||
"GIT_AUTHOR_DATE="+commitTimeStr,
|
||||
"GIT_COMMITTER_NAME="+authorSig.Name,
|
||||
"GIT_COMMITTER_EMAIL="+authorSig.Email,
|
||||
"GIT_COMMITTER_DATE="+commitTimeStr,
|
||||
)
|
||||
|
||||
// Clone to temporary path and do the init commit.
|
||||
if stdout, _, err := git.NewCommand(ctx, "clone").AddDynamicArguments(repoPath, tmpDir).
|
||||
SetDescription(fmt.Sprintf("prepareRepoCommit (git clone): %s to %s", repoPath, tmpDir)).
|
||||
RunStdString(&git.RunOpts{Dir: "", Env: env}); err != nil {
|
||||
log.Error("Failed to clone from %v into %s: stdout: %s\nError: %v", repo, tmpDir, stdout, err)
|
||||
return fmt.Errorf("git clone: %w", err)
|
||||
}
|
||||
|
||||
// README
|
||||
data, err := options.Readme(opts.Readme)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoInitFile[%s]: %w", opts.Readme, err)
|
||||
}
|
||||
|
||||
cloneLink := repo.CloneLink()
|
||||
match := map[string]string{
|
||||
"Name": repo.Name,
|
||||
"Description": repo.Description,
|
||||
"CloneURL.SSH": cloneLink.SSH,
|
||||
"CloneURL.HTTPS": cloneLink.HTTPS,
|
||||
"OwnerName": repo.OwnerName,
|
||||
}
|
||||
res, err := vars.Expand(string(data), match)
|
||||
if err != nil {
|
||||
// here we could just log the error and continue the rendering
|
||||
log.Error("unable to expand template vars for repo README: %s, err: %v", opts.Readme, err)
|
||||
}
|
||||
if err = os.WriteFile(filepath.Join(tmpDir, "README.md"),
|
||||
[]byte(res), 0o644); err != nil {
|
||||
return fmt.Errorf("write README.md: %w", err)
|
||||
}
|
||||
|
||||
// .gitignore
|
||||
if len(opts.Gitignores) > 0 {
|
||||
var buf bytes.Buffer
|
||||
names := strings.Split(opts.Gitignores, ",")
|
||||
for _, name := range names {
|
||||
data, err = options.Gitignore(name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
|
||||
}
|
||||
buf.WriteString("# ---> " + name + "\n")
|
||||
buf.Write(data)
|
||||
buf.WriteString("\n")
|
||||
}
|
||||
|
||||
if buf.Len() > 0 {
|
||||
if err = os.WriteFile(filepath.Join(tmpDir, ".gitignore"), buf.Bytes(), 0o644); err != nil {
|
||||
return fmt.Errorf("write .gitignore: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// LICENSE
|
||||
if len(opts.License) > 0 {
|
||||
data, err = getLicense(opts.License, &licenseValues{
|
||||
Owner: repo.OwnerName,
|
||||
Email: authorSig.Email,
|
||||
Repo: repo.Name,
|
||||
Year: time.Now().Format("2006"),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("getLicense[%s]: %w", opts.License, err)
|
||||
}
|
||||
|
||||
if err = os.WriteFile(filepath.Join(tmpDir, "LICENSE"), data, 0o644); err != nil {
|
||||
return fmt.Errorf("write LICENSE: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initRepoCommit temporarily changes with work directory.
|
||||
func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
|
||||
// InitRepoCommit temporarily changes with work directory.
|
||||
func InitRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Repository, u *user_model.User, defaultBranch string) (err error) {
|
||||
commitTimeStr := time.Now().Format(time.RFC3339)
|
||||
|
||||
sig := u.NewGitSig()
|
||||
@@ -277,7 +188,7 @@ func initRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkInitRepository(ctx context.Context, owner, name string) (err error) {
|
||||
func CheckInitRepository(ctx context.Context, owner, name string) (err error) {
|
||||
// Somehow the directory could exist.
|
||||
repoPath := repo_model.RepoPath(owner, name)
|
||||
isExist, err := util.IsExist(repoPath)
|
||||
@@ -295,77 +206,12 @@ func checkInitRepository(ctx context.Context, owner, name string) (err error) {
|
||||
// Init git bare new repository.
|
||||
if err = git.InitRepository(ctx, repoPath, true); err != nil {
|
||||
return fmt.Errorf("git.InitRepository: %w", err)
|
||||
} else if err = createDelegateHooks(repoPath); err != nil {
|
||||
} else if err = CreateDelegateHooks(repoPath); err != nil {
|
||||
return fmt.Errorf("createDelegateHooks: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitRepository initializes README and .gitignore if needed.
|
||||
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
|
||||
if err = checkInitRepository(ctx, repo.OwnerName, repo.Name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Initialize repository according to user's choice.
|
||||
if opts.AutoInit {
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "gitea-"+repo.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to create temp dir for repository %s: %w", repo.RepoPath(), err)
|
||||
}
|
||||
defer func() {
|
||||
if err := util.RemoveAll(tmpDir); err != nil {
|
||||
log.Warn("Unable to remove temporary directory: %s: Error: %v", tmpDir, err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err = prepareRepoCommit(ctx, repo, tmpDir, repoPath, opts); err != nil {
|
||||
return fmt.Errorf("prepareRepoCommit: %w", err)
|
||||
}
|
||||
|
||||
// Apply changes and commit.
|
||||
if err = initRepoCommit(ctx, tmpDir, repo, u, opts.DefaultBranch); err != nil {
|
||||
return fmt.Errorf("initRepoCommit: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Re-fetch the repository from database before updating it (else it would
|
||||
// override changes that were done earlier with sql)
|
||||
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
|
||||
return fmt.Errorf("getRepositoryByID: %w", err)
|
||||
}
|
||||
|
||||
if !opts.AutoInit {
|
||||
repo.IsEmpty = true
|
||||
}
|
||||
|
||||
repo.DefaultBranch = setting.Repository.DefaultBranch
|
||||
|
||||
if len(opts.DefaultBranch) > 0 {
|
||||
repo.DefaultBranch = opts.DefaultBranch
|
||||
gitRepo, err := git.OpenRepository(ctx, repo.RepoPath())
|
||||
if err != nil {
|
||||
return fmt.Errorf("openRepository: %w", err)
|
||||
}
|
||||
defer gitRepo.Close()
|
||||
if err = gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
||||
return fmt.Errorf("setDefaultBranch: %w", err)
|
||||
}
|
||||
|
||||
if !repo.IsEmpty {
|
||||
if _, err := SyncRepoBranches(ctx, repo.ID, u.ID); err != nil {
|
||||
return fmt.Errorf("SyncRepoBranches: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = UpdateRepository(ctx, repo, false); err != nil {
|
||||
return fmt.Errorf("updateRepository: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitializeLabels adds a label set to a repository using a template
|
||||
func InitializeLabels(ctx context.Context, id int64, labelTemplate string, isOrg bool) error {
|
||||
list, err := LoadTemplateLabelsByDisplayName(labelTemplate)
|
||||
|
||||
@@ -13,14 +13,14 @@ import (
|
||||
"code.gitea.io/gitea/modules/options"
|
||||
)
|
||||
|
||||
type licenseValues struct {
|
||||
type LicenseValues struct {
|
||||
Owner string
|
||||
Email string
|
||||
Repo string
|
||||
Year string
|
||||
}
|
||||
|
||||
func getLicense(name string, values *licenseValues) ([]byte, error) {
|
||||
func GetLicense(name string, values *LicenseValues) ([]byte, error) {
|
||||
data, err := options.License(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetRepoInitFile[%s]: %w", name, err)
|
||||
@@ -28,7 +28,7 @@ func getLicense(name string, values *licenseValues) ([]byte, error) {
|
||||
return fillLicensePlaceholder(name, values, data), nil
|
||||
}
|
||||
|
||||
func fillLicensePlaceholder(name string, values *licenseValues, origin []byte) []byte {
|
||||
func fillLicensePlaceholder(name string, values *LicenseValues, origin []byte) []byte {
|
||||
placeholder := getLicensePlaceholder(name)
|
||||
|
||||
scanner := bufio.NewScanner(bytes.NewReader(origin))
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
func Test_getLicense(t *testing.T) {
|
||||
type args struct {
|
||||
name string
|
||||
values *licenseValues
|
||||
values *LicenseValues
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -25,7 +25,7 @@ func Test_getLicense(t *testing.T) {
|
||||
name: "regular",
|
||||
args: args{
|
||||
name: "MIT",
|
||||
values: &licenseValues{Owner: "Gitea", Year: "2023"},
|
||||
values: &LicenseValues{Owner: "Gitea", Year: "2023"},
|
||||
},
|
||||
want: `MIT License
|
||||
|
||||
@@ -49,11 +49,11 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := getLicense(tt.args.name, tt.args.values)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("getLicense(%v, %v)", tt.args.name, tt.args.values)) {
|
||||
got, err := GetLicense(tt.args.name, tt.args.values)
|
||||
if !tt.wantErr(t, err, fmt.Sprintf("GetLicense(%v, %v)", tt.args.name, tt.args.values)) {
|
||||
return
|
||||
}
|
||||
assert.Equalf(t, tt.want, string(got), "getLicense(%v, %v)", tt.args.name, tt.args.values)
|
||||
assert.Equalf(t, tt.want, string(got), "GetLicense(%v, %v)", tt.args.name, tt.args.values)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
||||
func Test_fillLicensePlaceholder(t *testing.T) {
|
||||
type args struct {
|
||||
name string
|
||||
values *licenseValues
|
||||
values *LicenseValues
|
||||
origin string
|
||||
}
|
||||
tests := []struct {
|
||||
@@ -73,7 +73,7 @@ func Test_fillLicensePlaceholder(t *testing.T) {
|
||||
name: "owner",
|
||||
args: args{
|
||||
name: "regular",
|
||||
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
origin: `
|
||||
<name of author>
|
||||
<owner>
|
||||
@@ -104,7 +104,7 @@ Gitea
|
||||
name: "email",
|
||||
args: args{
|
||||
name: "regular",
|
||||
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
origin: `
|
||||
[EMAIL]
|
||||
`,
|
||||
@@ -117,7 +117,7 @@ teabot@gitea.io
|
||||
name: "repo",
|
||||
args: args{
|
||||
name: "regular",
|
||||
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
origin: `
|
||||
<program>
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
@@ -132,7 +132,7 @@ gitea
|
||||
name: "year",
|
||||
args: args{
|
||||
name: "regular",
|
||||
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
origin: `
|
||||
<year>
|
||||
[YEAR]
|
||||
@@ -155,7 +155,7 @@ gitea
|
||||
name: "0BSD",
|
||||
args: args{
|
||||
name: "0BSD",
|
||||
values: &licenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
values: &LicenseValues{Year: "2023", Owner: "Gitea", Email: "teabot@gitea.io", Repo: "gitea"},
|
||||
origin: `
|
||||
Copyright (C) YEAR by AUTHOR EMAIL
|
||||
|
||||
|
||||
@@ -8,6 +8,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
|
||||
_ "code.gitea.io/gitea/models/actions"
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
|
||||
@@ -256,11 +256,11 @@ func cleanUpMigrateGitConfig(ctx context.Context, repoPath string) error {
|
||||
// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
|
||||
func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
|
||||
repoPath := repo.RepoPath()
|
||||
if err := createDelegateHooks(repoPath); err != nil {
|
||||
if err := CreateDelegateHooks(repoPath); err != nil {
|
||||
return repo, fmt.Errorf("createDelegateHooks: %w", err)
|
||||
}
|
||||
if repo.HasWiki() {
|
||||
if err := createDelegateHooks(repo.WikiPath()); err != nil {
|
||||
if err := CreateDelegateHooks(repo.WikiPath()); err != nil {
|
||||
return repo, fmt.Errorf("createDelegateHooks.(wiki): %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,10 +13,11 @@ import (
|
||||
// Actions settings
|
||||
var (
|
||||
Actions = struct {
|
||||
LogStorage *Storage // how the created logs should be stored
|
||||
ArtifactStorage *Storage // how the created artifacts should be stored
|
||||
Enabled bool
|
||||
DefaultActionsURL defaultActionsURL `ini:"DEFAULT_ACTIONS_URL"`
|
||||
LogStorage *Storage // how the created logs should be stored
|
||||
ArtifactStorage *Storage // how the created artifacts should be stored
|
||||
ArtifactRetentionDays int64 `ini:"ARTIFACT_RETENTION_DAYS"`
|
||||
Enabled bool
|
||||
DefaultActionsURL defaultActionsURL `ini:"DEFAULT_ACTIONS_URL"`
|
||||
}{
|
||||
Enabled: false,
|
||||
DefaultActionsURL: defaultActionsURLGitHub,
|
||||
@@ -76,5 +77,10 @@ func loadActionsFrom(rootCfg ConfigProvider) error {
|
||||
|
||||
Actions.ArtifactStorage, err = getStorage(rootCfg, "actions_artifacts", "", actionsSec)
|
||||
|
||||
// default to 90 days in Github Actions
|
||||
if Actions.ArtifactRetentionDays <= 0 {
|
||||
Actions.ArtifactRetentionDays = 90
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -174,9 +174,16 @@ func (s *iniConfigSection) ChildSections() (sections []ConfigSection) {
|
||||
return sections
|
||||
}
|
||||
|
||||
func configProviderLoadOptions() ini.LoadOptions {
|
||||
return ini.LoadOptions{
|
||||
KeyValueDelimiterOnWrite: " = ",
|
||||
IgnoreContinuation: true,
|
||||
}
|
||||
}
|
||||
|
||||
// NewConfigProviderFromData this function is mainly for testing purpose
|
||||
func NewConfigProviderFromData(configContent string) (ConfigProvider, error) {
|
||||
cfg, err := ini.Load(strings.NewReader(configContent))
|
||||
cfg, err := ini.LoadSources(configProviderLoadOptions(), strings.NewReader(configContent))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -190,7 +197,7 @@ func NewConfigProviderFromData(configContent string) (ConfigProvider, error) {
|
||||
// NewConfigProviderFromFile load configuration from file.
|
||||
// NOTE: do not print any log except error.
|
||||
func NewConfigProviderFromFile(file string, extraConfigs ...string) (ConfigProvider, error) {
|
||||
cfg := ini.Empty(ini.LoadOptions{KeyValueDelimiterOnWrite: " = "})
|
||||
cfg := ini.Empty(configProviderLoadOptions())
|
||||
loadedFromEmpty := true
|
||||
|
||||
if file != "" {
|
||||
@@ -339,6 +346,7 @@ func NewConfigProviderForLocale(source any, others ...any) (ConfigProvider, erro
|
||||
iniFile, err := ini.LoadSources(ini.LoadOptions{
|
||||
IgnoreInlineComment: true,
|
||||
UnescapeValueCommentSymbols: true,
|
||||
IgnoreContinuation: true,
|
||||
}, source, others...)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to load locale ini: %w", err)
|
||||
|
||||
@@ -30,6 +30,16 @@ key = 123
|
||||
secSub := cfg.Section("foo.bar.xxx")
|
||||
assert.Equal(t, "123", secSub.Key("key").String())
|
||||
})
|
||||
t.Run("TrailingSlash", func(t *testing.T) {
|
||||
cfg, _ := NewConfigProviderFromData(`
|
||||
[foo]
|
||||
key = E:\
|
||||
xxx = yyy
|
||||
`)
|
||||
sec := cfg.Section("foo")
|
||||
assert.Equal(t, "E:\\", sec.Key("key").String())
|
||||
assert.Equal(t, "yyy", sec.Key("xxx").String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestConfigProviderHelper(t *testing.T) {
|
||||
|
||||
@@ -46,6 +46,7 @@ var Service = struct {
|
||||
EnableNotifyMail bool
|
||||
EnableBasicAuth bool
|
||||
EnableReverseProxyAuth bool
|
||||
EnableReverseProxyAuthAPI bool
|
||||
EnableReverseProxyAutoRegister bool
|
||||
EnableReverseProxyEmail bool
|
||||
EnableReverseProxyFullName bool
|
||||
@@ -157,6 +158,7 @@ func loadServiceFrom(rootCfg ConfigProvider) {
|
||||
Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
|
||||
Service.EnableBasicAuth = sec.Key("ENABLE_BASIC_AUTHENTICATION").MustBool(true)
|
||||
Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
|
||||
Service.EnableReverseProxyAuthAPI = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION_API").MustBool()
|
||||
Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
|
||||
Service.EnableReverseProxyEmail = sec.Key("ENABLE_REVERSE_PROXY_EMAIL").MustBool()
|
||||
Service.EnableReverseProxyFullName = sec.Key("ENABLE_REVERSE_PROXY_FULL_NAME").MustBool()
|
||||
|
||||
@@ -50,7 +50,7 @@ func loadSessionFrom(rootCfg ConfigProvider) {
|
||||
}
|
||||
SessionConfig.CookieName = sec.Key("COOKIE_NAME").MustString("i_like_gitea")
|
||||
SessionConfig.CookiePath = AppSubURL + "/" // there was a bug, old code only set CookePath=AppSubURL, no trailing slash
|
||||
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(false)
|
||||
SessionConfig.Secure = sec.Key("COOKIE_SECURE").MustBool(strings.HasPrefix(strings.ToLower(AppURL), "https://"))
|
||||
SessionConfig.Gclifetime = sec.Key("GC_INTERVAL_TIME").MustInt64(86400)
|
||||
SessionConfig.Maxlifetime = sec.Key("SESSION_LIFE_TIME").MustInt64(86400)
|
||||
SessionConfig.Domain = sec.Key("DOMAIN").String()
|
||||
|
||||
@@ -4,11 +4,11 @@
|
||||
package templates
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/assetfs"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
)
|
||||
|
||||
func AssetFS() *assetfs.LayeredFS {
|
||||
@@ -24,7 +24,7 @@ func ListWebTemplateAssetNames(assets *assetfs.LayeredFS) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return util.SliceRemoveAllFunc(files, func(file string) bool {
|
||||
return slices.DeleteFunc(files, func(file string) bool {
|
||||
return strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl")
|
||||
}), nil
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func ListMailTemplateAssetNames(assets *assetfs.LayeredFS) ([]string, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return util.SliceRemoveAllFunc(files, func(file string) bool {
|
||||
return slices.DeleteFunc(files, func(file string) bool {
|
||||
return !strings.HasPrefix(file, "mail/") || !strings.HasSuffix(file, ".tmpl")
|
||||
}), nil
|
||||
}
|
||||
|
||||
+7
-50
@@ -1,37 +1,21 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// Most of the functions in this file can have better implementations with "golang.org/x/exp/slices".
|
||||
// However, "golang.org/x/exp" is experimental and unreliable, we shouldn't use it.
|
||||
// So lets waiting for the "slices" has be promoted to the main repository one day.
|
||||
|
||||
package util
|
||||
|
||||
import "strings"
|
||||
|
||||
// SliceContains returns true if the target exists in the slice.
|
||||
func SliceContains[T comparable](slice []T, target T) bool {
|
||||
return SliceContainsFunc(slice, func(t T) bool { return t == target })
|
||||
}
|
||||
|
||||
// SliceContainsFunc returns true if any element in the slice satisfies the targetFunc.
|
||||
func SliceContainsFunc[T any](slice []T, targetFunc func(T) bool) bool {
|
||||
for _, v := range slice {
|
||||
if targetFunc(v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// SliceContainsString sequential searches if string exists in slice.
|
||||
func SliceContainsString(slice []string, target string, insensitive ...bool) bool {
|
||||
if len(insensitive) != 0 && insensitive[0] {
|
||||
target = strings.ToLower(target)
|
||||
return SliceContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
|
||||
return slices.ContainsFunc(slice, func(t string) bool { return strings.ToLower(t) == target })
|
||||
}
|
||||
|
||||
return SliceContains(slice, target)
|
||||
return slices.Contains(slice, target)
|
||||
}
|
||||
|
||||
// SliceSortedEqual returns true if the two slices will be equal when they get sorted.
|
||||
@@ -57,34 +41,7 @@ func SliceSortedEqual[T comparable](s1, s2 []T) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// SliceEqual returns true if the two slices are equal.
|
||||
func SliceEqual[T comparable](s1, s2 []T) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i, v := range s1 {
|
||||
if s2[i] != v {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// SliceRemoveAll removes all the target elements from the slice.
|
||||
func SliceRemoveAll[T comparable](slice []T, target T) []T {
|
||||
return SliceRemoveAllFunc(slice, func(t T) bool { return t == target })
|
||||
}
|
||||
|
||||
// SliceRemoveAllFunc removes all elements which satisfy the targetFunc from the slice.
|
||||
func SliceRemoveAllFunc[T comparable](slice []T, targetFunc func(T) bool) []T {
|
||||
idx := 0
|
||||
for _, v := range slice {
|
||||
if targetFunc(v) {
|
||||
continue
|
||||
}
|
||||
slice[idx] = v
|
||||
idx++
|
||||
}
|
||||
return slice[:idx]
|
||||
return slices.DeleteFunc(slice, func(t T) bool { return t == target })
|
||||
}
|
||||
|
||||
@@ -9,20 +9,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSliceContains(t *testing.T) {
|
||||
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 2))
|
||||
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 0))
|
||||
assert.True(t, SliceContains([]int{2, 0, 2, 3}, 3))
|
||||
|
||||
assert.True(t, SliceContains([]string{"2", "0", "2", "3"}, "0"))
|
||||
assert.True(t, SliceContains([]float64{2, 0, 2, 3}, 0))
|
||||
assert.True(t, SliceContains([]bool{false, true, false}, true))
|
||||
|
||||
assert.False(t, SliceContains([]int{2, 0, 2, 3}, 4))
|
||||
assert.False(t, SliceContains([]int{}, 4))
|
||||
assert.False(t, SliceContains(nil, 4))
|
||||
}
|
||||
|
||||
func TestSliceContainsString(t *testing.T) {
|
||||
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "a"))
|
||||
assert.True(t, SliceContainsString([]string{"c", "b", "a", "b"}, "b"))
|
||||
@@ -54,25 +40,6 @@ func TestSliceSortedEqual(t *testing.T) {
|
||||
assert.False(t, SliceSortedEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
|
||||
}
|
||||
|
||||
func TestSliceEqual(t *testing.T) {
|
||||
assert.True(t, SliceEqual([]int{2, 0, 2, 3}, []int{2, 0, 2, 3}))
|
||||
assert.True(t, SliceEqual([]int{}, []int{}))
|
||||
assert.True(t, SliceEqual([]int(nil), nil))
|
||||
assert.True(t, SliceEqual([]int(nil), []int{}))
|
||||
assert.True(t, SliceEqual([]int{}, []int{}))
|
||||
|
||||
assert.True(t, SliceEqual([]string{"2", "0", "2", "3"}, []string{"2", "0", "2", "3"}))
|
||||
assert.True(t, SliceEqual([]float64{2, 0, 2, 3}, []float64{2, 0, 2, 3}))
|
||||
assert.True(t, SliceEqual([]bool{false, true, false}, []bool{false, true, false}))
|
||||
|
||||
assert.False(t, SliceEqual([]int{3, 0, 2, 2}, []int{2, 0, 2, 3}))
|
||||
assert.False(t, SliceEqual([]int{2, 0, 2}, []int{2, 0, 2, 3}))
|
||||
assert.False(t, SliceEqual([]int{}, []int{2, 0, 2, 3}))
|
||||
assert.False(t, SliceEqual(nil, []int{2, 0, 2, 3}))
|
||||
assert.False(t, SliceEqual([]int{2, 0, 2, 4}, []int{2, 0, 2, 3}))
|
||||
assert.False(t, SliceEqual([]int{2, 0, 0, 3}, []int{2, 0, 2, 3}))
|
||||
}
|
||||
|
||||
func TestSliceRemoveAll(t *testing.T) {
|
||||
assert.ElementsMatch(t, []int{2, 2, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 0))
|
||||
assert.ElementsMatch(t, []int{0, 3}, SliceRemoveAll([]int{2, 0, 2, 3}, 2))
|
||||
|
||||
Reference in New Issue
Block a user