Merge branch 'main' into limit-repo-size

This commit is contained in:
zeripath
2022-12-03 22:20:18 +00:00
committed by GitHub
231 changed files with 1109 additions and 1001 deletions
+1 -1
View File
@@ -35,7 +35,7 @@ func TestActivityPubSignedPost(t *testing.T) {
body, err := io.ReadAll(r.Body)
assert.NoError(t, err)
assert.Equal(t, expected, string(body))
fmt.Fprintf(w, expected)
fmt.Fprint(w, expected)
}))
defer srv.Close()
+1 -1
View File
@@ -139,7 +139,7 @@ func (ctx *Context) IsUserRepoReaderAny() bool {
// RedirectToUser redirect to a differently-named user
func RedirectToUser(ctx *Context, userName string, redirectUserID int64) {
user, err := user_model.GetUserByID(redirectUserID)
user, err := user_model.GetUserByID(ctx, redirectUserID)
if err != nil {
ctx.ServerError("GetUserByID", err)
return
+1
View File
@@ -13,6 +13,7 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// SPDX-License-Identifier: Apache-2.0
// a middleware that generates and validates CSRF tokens.
+3 -3
View File
@@ -264,7 +264,7 @@ func (r *Repository) GetEditorconfig(optCommit ...*git.Commit) (*editorconfig.Ed
// RetrieveBaseRepo retrieves base repository
func RetrieveBaseRepo(ctx *Context, repo *repo_model.Repository) {
// Non-fork repository will not return error in this method.
if err := repo.GetBaseRepo(); err != nil {
if err := repo.GetBaseRepo(ctx); err != nil {
if repo_model.IsErrRepoNotExist(err) {
repo.IsFork = false
repo.ForkID = 0
@@ -335,7 +335,7 @@ func RedirectToRepo(ctx *Context, redirectRepoID int64) {
ownerName := ctx.Params(":username")
previousRepoName := ctx.Params(":reponame")
repo, err := repo_model.GetRepositoryByID(redirectRepoID)
repo, err := repo_model.GetRepositoryByID(ctx, redirectRepoID)
if err != nil {
ctx.ServerError("GetRepositoryByID", err)
return
@@ -410,7 +410,7 @@ func RepoIDAssignment() func(ctx *Context) {
repoID := ctx.ParamsInt64(":repoid")
// Get repository.
repo, err := repo_model.GetRepositoryByID(repoID)
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
if err != nil {
if repo_model.IsErrRepoNotExist(err) {
ctx.NotFound("GetRepositoryByID", nil)
+1
View File
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SPDX-License-Identifier: Apache-2.0
package context
+1
View File
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// SPDX-License-Identifier: Apache-2.0
package context
+3 -2
View File
@@ -5,6 +5,7 @@
package convert
import (
"context"
"fmt"
"strconv"
"strings"
@@ -408,8 +409,8 @@ func ToOAuth2Application(app *auth.OAuth2Application) *api.OAuth2Application {
}
// ToLFSLock convert a LFSLock to api.LFSLock
func ToLFSLock(l *git_model.LFSLock) *api.LFSLock {
u, err := user_model.GetUserByID(l.OwnerID)
func ToLFSLock(ctx context.Context, l *git_model.LFSLock) *api.LFSLock {
u, err := user_model.GetUserByID(ctx, l.OwnerID)
if err != nil {
return nil
}
+1 -1
View File
@@ -149,7 +149,7 @@ func ToStopWatches(sws []*issues_model.Stopwatch) (api.StopWatches, error) {
}
repo, ok = repoCache[issue.RepoID]
if !ok {
repo, err = repo_model.GetRepositoryByID(issue.RepoID)
repo, err = repo_model.GetRepositoryByID(db.DefaultContext, issue.RepoID)
if err != nil {
return nil, err
}
+3 -3
View File
@@ -138,15 +138,15 @@ func ToTimelineComment(ctx context.Context, c *issues_model.Comment, doer *user_
var repo *repo_model.Repository
if c.Label.BelongsToOrg() {
var err error
org, err = user_model.GetUserByIDCtx(ctx, c.Label.OrgID)
org, err = user_model.GetUserByID(ctx, c.Label.OrgID)
if err != nil {
log.Error("GetUserByIDCtx(%d): %v", c.Label.OrgID, err)
log.Error("GetUserByID(%d): %v", c.Label.OrgID, err)
return nil
}
}
if c.Label.BelongsToRepo() {
var err error
repo, err = repo_model.GetRepositoryByIDCtx(ctx, c.Label.RepoID)
repo, err = repo_model.GetRepositoryByID(ctx, c.Label.RepoID)
if err != nil {
log.Error("GetRepositoryByIDCtx(%d): %v", c.Label.RepoID, err)
return nil
+2 -1
View File
@@ -7,6 +7,7 @@ import (
"net/url"
activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
api "code.gitea.io/gitea/modules/structs"
)
@@ -23,7 +24,7 @@ func ToNotificationThread(n *activities_model.Notification) *api.NotificationThr
// since user only get notifications when he has access to use minimal access mode
if n.Repository != nil {
result.Repository = ToRepo(n.Repository, perm.AccessModeRead)
result.Repository = ToRepo(db.DefaultContext, n.Repository, perm.AccessModeRead)
// This permission is not correct and we should not be reporting it
for repository := result.Repository; repository != nil; repository = repository.Parent {
+1 -1
View File
@@ -22,7 +22,7 @@ func ToPackage(ctx context.Context, pd *packages.PackageDescriptor, doer *user_m
}
if permission.HasAccess() {
repo = ToRepo(pd.Repository, permission.AccessMode)
repo = ToRepo(ctx, pd.Repository, permission.AccessMode)
}
}
+2 -2
View File
@@ -79,7 +79,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
Name: pr.BaseBranch,
Ref: pr.BaseBranch,
RepoID: pr.BaseRepoID,
Repository: ToRepo(pr.BaseRepo, p.AccessMode),
Repository: ToRepo(ctx, pr.BaseRepo, p.AccessMode),
},
Head: &api.PRBranchInfo{
Name: pr.HeadBranch,
@@ -139,7 +139,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
}
apiPullRequest.Head.RepoID = pr.HeadRepo.ID
apiPullRequest.Head.Repository = ToRepo(pr.HeadRepo, p.AccessMode)
apiPullRequest.Head.Repository = ToRepo(ctx, pr.HeadRepo, p.AccessMode)
headGitRepo, err := git.OpenRepository(ctx, pr.HeadRepo.RepoPath())
if err != nil {
+1 -1
View File
@@ -31,7 +31,7 @@ func TestPullRequest_APIFormat(t *testing.T) {
Ref: "refs/pull/2/head",
Sha: "4a357436d925b5c974181ff12a994538ddc5a269",
RepoID: 1,
Repository: ToRepo(headRepo, perm.AccessModeRead),
Repository: ToRepo(db.DefaultContext, headRepo, perm.AccessModeRead),
}, apiPullRequest.Head)
// withOut HeadRepo
+6 -5
View File
@@ -4,6 +4,7 @@
package convert
import (
"context"
"time"
"code.gitea.io/gitea/models"
@@ -16,11 +17,11 @@ import (
)
// ToRepo converts a Repository to api.Repository
func ToRepo(repo *repo_model.Repository, mode perm.AccessMode) *api.Repository {
return innerToRepo(repo, mode, false)
func ToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.AccessMode) *api.Repository {
return innerToRepo(ctx, repo, mode, false)
}
func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
func innerToRepo(ctx context.Context, repo *repo_model.Repository, mode perm.AccessMode, isParent bool) *api.Repository {
var parent *api.Repository
cloneLink := repo.CloneLink()
@@ -30,12 +31,12 @@ func innerToRepo(repo *repo_model.Repository, mode perm.AccessMode, isParent boo
Pull: mode >= perm.AccessModeRead,
}
if !isParent {
err := repo.GetBaseRepo()
err := repo.GetBaseRepo(ctx)
if err != nil {
return nil
}
if repo.BaseRepo != nil {
parent = innerToRepo(repo.BaseRepo, mode, true)
parent = innerToRepo(ctx, repo.BaseRepo, mode, true)
}
}
+6 -4
View File
@@ -4,13 +4,15 @@
package convert
import (
"context"
git_model "code.gitea.io/gitea/models/git"
user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
)
// ToCommitStatus converts git_model.CommitStatus to api.CommitStatus
func ToCommitStatus(status *git_model.CommitStatus) *api.CommitStatus {
func ToCommitStatus(ctx context.Context, status *git_model.CommitStatus) *api.CommitStatus {
apiStatus := &api.CommitStatus{
Created: status.CreatedUnix.AsTime(),
Updated: status.CreatedUnix.AsTime(),
@@ -23,7 +25,7 @@ func ToCommitStatus(status *git_model.CommitStatus) *api.CommitStatus {
}
if status.CreatorID != 0 {
creator, _ := user_model.GetUserByID(status.CreatorID)
creator, _ := user_model.GetUserByID(ctx, status.CreatorID)
apiStatus.Creator = ToUser(creator, nil)
}
@@ -31,7 +33,7 @@ func ToCommitStatus(status *git_model.CommitStatus) *api.CommitStatus {
}
// ToCombinedStatus converts List of CommitStatus to a CombinedStatus
func ToCombinedStatus(statuses []*git_model.CommitStatus, repo *api.Repository) *api.CombinedStatus {
func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, repo *api.Repository) *api.CombinedStatus {
if len(statuses) == 0 {
return nil
}
@@ -45,7 +47,7 @@ func ToCombinedStatus(statuses []*git_model.CommitStatus, repo *api.Repository)
retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
for _, status := range statuses {
retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(status))
retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
if status.State.NoBetterThan(retStatus.State) {
retStatus.State = status.State
}
+40 -23
View File
@@ -55,40 +55,57 @@ func (ref *Reference) Commit() (*Commit, error) {
// ShortName returns the short name of the reference
func (ref *Reference) ShortName() string {
if ref == nil {
return ""
}
if strings.HasPrefix(ref.Name, BranchPrefix) {
return strings.TrimPrefix(ref.Name, BranchPrefix)
}
if strings.HasPrefix(ref.Name, TagPrefix) {
return strings.TrimPrefix(ref.Name, TagPrefix)
}
if strings.HasPrefix(ref.Name, RemotePrefix) {
return strings.TrimPrefix(ref.Name, RemotePrefix)
}
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen]
}
return ref.Name
return RefName(ref.Name).ShortName()
}
// RefGroup returns the group type of the reference
func (ref *Reference) RefGroup() string {
if ref == nil {
return ""
return RefName(ref.Name).RefGroup()
}
// RefName represents a git reference name
type RefName string
func (ref RefName) IsBranch() bool {
return strings.HasPrefix(string(ref), BranchPrefix)
}
func (ref RefName) IsTag() bool {
return strings.HasPrefix(string(ref), TagPrefix)
}
// ShortName returns the short name of the reference name
func (ref RefName) ShortName() string {
refName := string(ref)
if strings.HasPrefix(refName, BranchPrefix) {
return strings.TrimPrefix(refName, BranchPrefix)
}
if strings.HasPrefix(ref.Name, BranchPrefix) {
if strings.HasPrefix(refName, TagPrefix) {
return strings.TrimPrefix(refName, TagPrefix)
}
if strings.HasPrefix(refName, RemotePrefix) {
return strings.TrimPrefix(refName, RemotePrefix)
}
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
return refName[pullLen : strings.IndexByte(refName[pullLen:], '/')+pullLen]
}
return refName
}
// RefGroup returns the group type of the reference
func (ref RefName) RefGroup() string {
refName := string(ref)
if strings.HasPrefix(refName, BranchPrefix) {
return "heads"
}
if strings.HasPrefix(ref.Name, TagPrefix) {
if strings.HasPrefix(refName, TagPrefix) {
return "tags"
}
if strings.HasPrefix(ref.Name, RemotePrefix) {
if strings.HasPrefix(refName, RemotePrefix) {
return "remotes"
}
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
return "pull"
}
return ""
+1 -1
View File
@@ -84,7 +84,7 @@ type IndexerData struct {
var indexerQueue queue.UniqueQueue
func index(ctx context.Context, indexer Indexer, repoID int64) error {
repo, err := repo_model.GetRepositoryByID(repoID)
repo, err := repo_model.GetRepositoryByID(ctx, repoID)
if repo_model.IsErrRepoNotExist(err) {
return indexer.Delete(repoID)
}
+1 -1
View File
@@ -21,7 +21,7 @@ func (db *DBIndexer) Index(id int64) error {
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().ShutdownContext(), fmt.Sprintf("Stats.DB Index Repo[%d]", id))
defer finished()
repo, err := repo_model.GetRepositoryByID(id)
repo, err := repo_model.GetRepositoryByID(ctx, id)
if err != nil {
return err
}
+1 -1
View File
@@ -36,7 +36,7 @@ func TestRepoStatsIndex(t *testing.T) {
err := Init()
assert.NoError(t, err)
repo, err := repo_model.GetRepositoryByID(1)
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 1)
assert.NoError(t, err)
err = UpdateRepoIndexer(repo)
+3 -3
View File
@@ -6,7 +6,7 @@ package template
import (
"fmt"
"io"
"path/filepath"
"path"
"strconv"
"code.gitea.io/gitea/modules/git"
@@ -43,7 +43,7 @@ func Unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
// UnmarshalFromEntry parses out a valid template from the blob in entry
func UnmarshalFromEntry(entry *git.TreeEntry, dir string) (*api.IssueTemplate, error) {
return unmarshalFromEntry(entry, filepath.Join(dir, entry.Name()))
return unmarshalFromEntry(entry, path.Join(dir, entry.Name())) // Filepaths in Git are ALWAYS '/' separated do not use filepath here
}
// UnmarshalFromCommit parses out a valid template from the commit
@@ -108,7 +108,7 @@ func unmarshal(filename string, content []byte) (*api.IssueTemplate, error) {
// It could be a valid markdown with two horizontal lines, or an invalid markdown with wrong metadata.
it.Content = string(content)
it.Name = filepath.Base(it.FileName)
it.Name = path.Base(it.FileName) // paths in Git are always '/' separated - do not use filepath!
it.About, _ = util.SplitStringAtByteN(it.Content, 80)
} else {
it.Content = templateBody
+1 -1
View File
@@ -1,5 +1,5 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// SPDX-License-Identifier: MIT
package json
+37 -37
View File
@@ -58,7 +58,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user
Action: api.HookIssueLabelCleared,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
} else {
@@ -66,7 +66,7 @@ func (m *webhookNotifier) NotifyIssueClearLabels(ctx context.Context, doer *user
Action: api.HookIssueLabelCleared,
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
}
@@ -81,8 +81,8 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m
// forked webhook
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: oldRepo}, webhook.HookEventFork, &api.ForkPayload{
Forkee: convert.ToRepo(oldRepo, oldMode),
Repo: convert.ToRepo(repo, mode),
Forkee: convert.ToRepo(ctx, oldRepo, oldMode),
Repo: convert.ToRepo(ctx, repo, mode),
Sender: convert.ToUser(doer, nil),
}); err != nil {
log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
@@ -94,7 +94,7 @@ func (m *webhookNotifier) NotifyForkRepository(ctx context.Context, doer *user_m
if u.IsOrganization() {
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Organization: convert.ToUser(u, nil),
Sender: convert.ToUser(doer, nil),
}); err != nil {
@@ -107,7 +107,7 @@ func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u
// Add to hook queue for created repo after session commit.
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Organization: convert.ToUser(u, nil),
Sender: convert.ToUser(doer, nil),
}); err != nil {
@@ -118,7 +118,7 @@ func (m *webhookNotifier) NotifyCreateRepository(ctx context.Context, doer, u *u
func (m *webhookNotifier) NotifyDeleteRepository(ctx context.Context, doer *user_model.User, repo *repo_model.Repository) {
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoDeleted,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Organization: convert.ToUser(repo.MustOwner(ctx), nil),
Sender: convert.ToUser(doer, nil),
}); err != nil {
@@ -130,7 +130,7 @@ func (m *webhookNotifier) NotifyMigrateRepository(ctx context.Context, doer, u *
// Add to hook queue for created repo after session commit.
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventRepository, &api.RepositoryPayload{
Action: api.HookRepoCreated,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Organization: convert.ToUser(u, nil),
Sender: convert.ToUser(doer, nil),
}); err != nil {
@@ -150,7 +150,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u
apiPullRequest := &api.PullRequestPayload{
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
if removed {
@@ -168,7 +168,7 @@ func (m *webhookNotifier) NotifyIssueChangeAssignee(ctx context.Context, doer *u
apiIssue := &api.IssuePayload{
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
if removed {
@@ -202,7 +202,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user
},
},
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
} else {
@@ -215,7 +215,7 @@ func (m *webhookNotifier) NotifyIssueChangeTitle(ctx context.Context, doer *user
},
},
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
}
@@ -237,7 +237,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use
apiPullRequest := &api.PullRequestPayload{
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
if isClosed {
@@ -250,7 +250,7 @@ func (m *webhookNotifier) NotifyIssueChangeStatus(ctx context.Context, doer *use
apiIssue := &api.IssuePayload{
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}
if isClosed {
@@ -280,7 +280,7 @@ func (m *webhookNotifier) NotifyNewIssue(ctx context.Context, issue *issues_mode
Action: api.HookIssueOpened,
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(issue.Poster, nil),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
@@ -306,7 +306,7 @@ func (m *webhookNotifier) NotifyNewPullRequest(ctx context.Context, pull *issues
Action: api.HookIssueOpened,
Index: pull.Issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, pull, nil),
Repository: convert.ToRepo(pull.Issue.Repo, mode),
Repository: convert.ToRepo(ctx, pull.Issue.Repo, mode),
Sender: convert.ToUser(pull.Issue.Poster, nil),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
@@ -327,7 +327,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us
},
},
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
} else {
@@ -340,7 +340,7 @@ func (m *webhookNotifier) NotifyIssueChangeContent(ctx context.Context, doer *us
},
},
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
}
@@ -381,7 +381,7 @@ func (m *webhookNotifier) NotifyUpdateComment(ctx context.Context, doer *user_mo
From: oldContent,
},
},
Repository: convert.ToRepo(c.Issue.Repo, mode),
Repository: convert.ToRepo(ctx, c.Issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
IsPull: c.Issue.IsPull,
}); err != nil {
@@ -404,7 +404,7 @@ func (m *webhookNotifier) NotifyCreateIssueComment(ctx context.Context, doer *us
Action: api.HookIssueCommentCreated,
Issue: convert.ToAPIIssue(ctx, issue),
Comment: convert.ToComment(comment),
Repository: convert.ToRepo(repo, mode),
Repository: convert.ToRepo(ctx, repo, mode),
Sender: convert.ToUser(doer, nil),
IsPull: issue.IsPull,
}); err != nil {
@@ -441,7 +441,7 @@ func (m *webhookNotifier) NotifyDeleteComment(ctx context.Context, doer *user_mo
Action: api.HookIssueCommentDeleted,
Issue: convert.ToAPIIssue(ctx, comment.Issue),
Comment: convert.ToComment(comment),
Repository: convert.ToRepo(comment.Issue.Repo, mode),
Repository: convert.ToRepo(ctx, comment.Issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
IsPull: comment.Issue.IsPull,
}); err != nil {
@@ -453,7 +453,7 @@ func (m *webhookNotifier) NotifyNewWikiPage(ctx context.Context, doer *user_mode
// Add to hook queue for created wiki page.
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
Action: api.HookWikiCreated,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Sender: convert.ToUser(doer, nil),
Page: page,
Comment: comment,
@@ -466,7 +466,7 @@ func (m *webhookNotifier) NotifyEditWikiPage(ctx context.Context, doer *user_mod
// Add to hook queue for edit wiki page.
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
Action: api.HookWikiEdited,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Sender: convert.ToUser(doer, nil),
Page: page,
Comment: comment,
@@ -479,7 +479,7 @@ func (m *webhookNotifier) NotifyDeleteWikiPage(ctx context.Context, doer *user_m
// Add to hook queue for edit wiki page.
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventWiki, &api.WikiPayload{
Action: api.HookWikiDeleted,
Repository: convert.ToRepo(repo, perm.AccessModeOwner),
Repository: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Sender: convert.ToUser(doer, nil),
Page: page,
}); err != nil {
@@ -516,7 +516,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use
Action: api.HookIssueLabelUpdated,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, perm.AccessModeNone),
Repository: convert.ToRepo(ctx, issue.Repo, perm.AccessModeNone),
Sender: convert.ToUser(doer, nil),
})
} else {
@@ -524,7 +524,7 @@ func (m *webhookNotifier) NotifyIssueChangeLabels(ctx context.Context, doer *use
Action: api.HookIssueLabelUpdated,
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
}
@@ -558,7 +558,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer *
Action: hookAction,
Index: issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, issue.PullRequest, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
} else {
@@ -566,7 +566,7 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(ctx context.Context, doer *
Action: hookAction,
Index: issue.Index,
Issue: convert.ToAPIIssue(ctx, issue),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
})
}
@@ -591,7 +591,7 @@ func (m *webhookNotifier) NotifyPushCommits(ctx context.Context, pusher *user_mo
Commits: apiCommits,
TotalCommits: commits.Len,
HeadCommit: apiHeadCommit,
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
Repo: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Pusher: apiPusher,
Sender: apiPusher,
}); err != nil {
@@ -631,7 +631,7 @@ func (*webhookNotifier) NotifyMergePullRequest(ctx context.Context, doer *user_m
apiPullRequest := &api.PullRequestPayload{
Index: pr.Issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(pr.Issue.Repo, mode),
Repository: convert.ToRepo(ctx, pr.Issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
Action: api.HookIssueClosed,
}
@@ -659,7 +659,7 @@ func (m *webhookNotifier) NotifyPullRequestChangeTargetBranch(ctx context.Contex
},
},
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(issue.Repo, mode),
Repository: convert.ToRepo(ctx, issue.Repo, mode),
Sender: convert.ToUser(doer, nil),
}); err != nil {
log.Error("PrepareWebhooks [pr: %d]: %v", pr.ID, err)
@@ -696,7 +696,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue
Action: api.HookIssueReviewed,
Index: review.Issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(review.Issue.Repo, mode),
Repository: convert.ToRepo(ctx, review.Issue.Repo, mode),
Sender: convert.ToUser(review.Reviewer, nil),
Review: &api.ReviewPayload{
Type: string(reviewHookType),
@@ -709,7 +709,7 @@ func (m *webhookNotifier) NotifyPullRequestReview(ctx context.Context, pr *issue
func (m *webhookNotifier) NotifyCreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName, refID string) {
apiPusher := convert.ToUser(pusher, nil)
apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone)
refName := git.RefEndName(refFullName)
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventCreate, &api.CreatePayload{
@@ -737,7 +737,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe
Action: api.HookIssueSynchronized,
Index: pr.Issue.Index,
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
Repository: convert.ToRepo(pr.Issue.Repo, perm.AccessModeNone),
Repository: convert.ToRepo(ctx, pr.Issue.Repo, perm.AccessModeNone),
Sender: convert.ToUser(doer, nil),
}); err != nil {
log.Error("PrepareWebhooks [pull_id: %v]: %v", pr.ID, err)
@@ -746,7 +746,7 @@ func (m *webhookNotifier) NotifyPullRequestSynchronized(ctx context.Context, doe
func (m *webhookNotifier) NotifyDeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refType, refFullName string) {
apiPusher := convert.ToUser(pusher, nil)
apiRepo := convert.ToRepo(repo, perm.AccessModeNone)
apiRepo := convert.ToRepo(ctx, repo, perm.AccessModeNone)
refName := git.RefEndName(refFullName)
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: repo}, webhook.HookEventDelete, &api.DeletePayload{
@@ -770,7 +770,7 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model
if err := webhook_services.PrepareWebhooks(ctx, webhook_services.EventSource{Repository: rel.Repo}, webhook.HookEventRelease, &api.ReleasePayload{
Action: action,
Release: convert.ToRelease(rel),
Repository: convert.ToRepo(rel.Repo, mode),
Repository: convert.ToRepo(ctx, rel.Repo, mode),
Sender: convert.ToUser(doer, nil),
}); err != nil {
log.Error("PrepareWebhooks: %v", err)
@@ -805,7 +805,7 @@ func (m *webhookNotifier) NotifySyncPushCommits(ctx context.Context, pusher *use
Commits: apiCommits,
TotalCommits: commits.Len,
HeadCommit: apiHeadCommit,
Repo: convert.ToRepo(repo, perm.AccessModeOwner),
Repo: convert.ToRepo(ctx, repo, perm.AccessModeOwner),
Pusher: apiPusher,
Sender: apiPusher,
}); err != nil {
+1
View File
@@ -1,5 +1,6 @@
// Copyright 2022 The Gitea Authors.
// Copyright 2015 https://github.com/unknwon. Licensed under the Apache License, Version 2.0
// SPDX-License-Identifier: Apache-2.0
package paginator
+1
View File
@@ -1,5 +1,6 @@
// Copyright 2022 The Gitea Authors.
// Copyright 2015 https://github.com/unknwon. Licensed under the Apache License, Version 2.0
// SPDX-License-Identifier: Apache-2.0
package paginator
+3 -3
View File
@@ -25,7 +25,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
testTeamRepositories := func(teamID int64, repoIds []int64) {
team := unittest.AssertExistsAndLoadBean(t, &organization.Team{ID: teamID})
assert.NoError(t, team.GetRepositoriesCtx(db.DefaultContext), "%s: GetRepositories", team.Name)
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 {
@@ -36,7 +36,7 @@ func TestIncludesAllRepositoriesTeams(t *testing.T) {
}
// Get an admin user.
user, err := user_model.GetUserByID(1)
user, err := user_model.GetUserByID(db.DefaultContext, 1)
assert.NoError(t, err, "GetUserByID")
// Create org.
@@ -153,7 +153,7 @@ func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
// Get sample repo and change visibility
repo, err := repo_model.GetRepositoryByID(9)
repo, err := repo_model.GetRepositoryByID(db.DefaultContext, 9)
assert.NoError(t, err)
repo.IsPrivate = true
+1 -1
View File
@@ -243,7 +243,7 @@ func generateGitContent(ctx context.Context, repo, templateRepo, generateRepo *r
}
// re-fetch repo
if repo, err = repo_model.GetRepositoryByIDCtx(ctx, repo.ID); err != nil {
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
return fmt.Errorf("getRepositoryByID: %w", err)
}
+1 -1
View File
@@ -414,7 +414,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
// 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.GetRepositoryByIDCtx(ctx, repo.ID); err != nil {
if repo, err = repo_model.GetRepositoryByID(ctx, repo.ID); err != nil {
return fmt.Errorf("getRepositoryByID: %w", err)
}
+1
View File
@@ -13,6 +13,7 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// SPDX-License-Identifier: Apache-2.0
package session
+32
View File
@@ -4,6 +4,10 @@
package storage
import (
"fmt"
"io"
"net/url"
"os"
"reflect"
"code.gitea.io/gitea/modules/json"
@@ -61,3 +65,31 @@ func toConfig(exemplar, cfg interface{}) (interface{}, error) {
}
return newVal.Elem().Interface(), nil
}
var uninitializedStorage = discardStorage("uninitialized storage")
type discardStorage string
func (s discardStorage) Open(_ string) (Object, error) {
return nil, fmt.Errorf("%s", s)
}
func (s discardStorage) Save(_ string, _ io.Reader, _ int64) (int64, error) {
return 0, fmt.Errorf("%s", s)
}
func (s discardStorage) Stat(_ string) (os.FileInfo, error) {
return nil, fmt.Errorf("%s", s)
}
func (s discardStorage) Delete(_ string) error {
return fmt.Errorf("%s", s)
}
func (s discardStorage) URL(_, _ string) (*url.URL, error) {
return nil, fmt.Errorf("%s", s)
}
func (s discardStorage) IterateObjects(_ func(string, Object) error) error {
return fmt.Errorf("%s", s)
}
+50
View File
@@ -0,0 +1,50 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package storage
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_discardStorage(t *testing.T) {
tests := []discardStorage{
uninitializedStorage,
discardStorage("empty"),
}
for _, tt := range tests {
t.Run(string(tt), func(t *testing.T) {
{
got, err := tt.Open("path")
assert.Nil(t, got)
assert.Error(t, err, string(tt))
}
{
got, err := tt.Save("path", bytes.NewReader([]byte{0}), 1)
assert.Equal(t, int64(0), got)
assert.Error(t, err, string(tt))
}
{
got, err := tt.Stat("path")
assert.Nil(t, got)
assert.Error(t, err, string(tt))
}
{
err := tt.Delete("path")
assert.Error(t, err, string(tt))
}
{
got, err := tt.URL("path", "name")
assert.Nil(t, got)
assert.Errorf(t, err, string(tt))
}
{
err := tt.IterateObjects(func(_ string, _ Object) error { return nil })
assert.Error(t, err, string(tt))
}
})
}
}
+30 -26
View File
@@ -110,46 +110,38 @@ func SaveFrom(objStorage ObjectStorage, p string, callback func(w io.Writer) err
var (
// Attachments represents attachments storage
Attachments ObjectStorage
Attachments ObjectStorage = uninitializedStorage
// LFS represents lfs storage
LFS ObjectStorage
LFS ObjectStorage = uninitializedStorage
// Avatars represents user avatars storage
Avatars ObjectStorage
Avatars ObjectStorage = uninitializedStorage
// RepoAvatars represents repository avatars storage
RepoAvatars ObjectStorage
RepoAvatars ObjectStorage = uninitializedStorage
// RepoArchives represents repository archives storage
RepoArchives ObjectStorage
RepoArchives ObjectStorage = uninitializedStorage
// Packages represents packages storage
Packages ObjectStorage
Packages ObjectStorage = uninitializedStorage
)
// Init init the stoarge
func Init() error {
if err := initAttachments(); err != nil {
return err
for _, f := range []func() error{
initAttachments,
initAvatars,
initRepoAvatars,
initLFS,
initRepoArchives,
initPackages,
} {
if err := f(); err != nil {
return err
}
}
if err := initAvatars(); err != nil {
return err
}
if err := initRepoAvatars(); err != nil {
return err
}
if err := initLFS(); err != nil {
return err
}
if err := initRepoArchives(); err != nil {
return err
}
return initPackages()
return nil
}
// NewStorage takes a storage type and some config and returns an ObjectStorage or an error
@@ -172,12 +164,20 @@ func initAvatars() (err error) {
}
func initAttachments() (err error) {
if !setting.Attachment.Enabled {
Attachments = discardStorage("Attachment isn't enabled")
return nil
}
log.Info("Initialising Attachment storage with type: %s", setting.Attachment.Storage.Type)
Attachments, err = NewStorage(setting.Attachment.Storage.Type, &setting.Attachment.Storage)
return err
}
func initLFS() (err error) {
if !setting.LFS.StartServer {
LFS = discardStorage("LFS isn't enabled")
return nil
}
log.Info("Initialising LFS storage with type: %s", setting.LFS.Storage.Type)
LFS, err = NewStorage(setting.LFS.Storage.Type, &setting.LFS.Storage)
return err
@@ -196,6 +196,10 @@ func initRepoArchives() (err error) {
}
func initPackages() (err error) {
if !setting.Packages.Enabled {
Packages = discardStorage("Packages isn't enabled")
return nil
}
log.Info("Initialising Packages storage with type: %s", setting.Packages.Storage.Type)
Packages, err = NewStorage(setting.Packages.Storage.Type, &setting.Packages.Storage)
return err
+2 -1
View File
@@ -25,6 +25,7 @@ import (
activities_model "code.gitea.io/gitea/models/activities"
"code.gitea.io/gitea/models/avatars"
"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/organization"
repo_model "code.gitea.io/gitea/models/repo"
@@ -631,7 +632,7 @@ func Avatar(item interface{}, others ...interface{}) template.HTML {
// AvatarByAction renders user avatars from action. args: action, size (int), class (string)
func AvatarByAction(action *activities_model.Action, others ...interface{}) template.HTML {
action.LoadActUser()
action.LoadActUser(db.DefaultContext)
return Avatar(action.ActUser, others...)
}
+1 -1
View File
@@ -57,7 +57,7 @@ func LoadRepo(t *testing.T, ctx *context.Context, repoID int64) {
ctx.Repo = &context.Repository{}
ctx.Repo.Repository = unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: repoID})
var err error
ctx.Repo.Owner, err = user_model.GetUserByID(ctx.Repo.Repository.OwnerID)
ctx.Repo.Owner, err = user_model.GetUserByID(ctx, ctx.Repo.Repository.OwnerID)
assert.NoError(t, err)
ctx.Repo.RepoLink = ctx.Repo.Repository.Link()
ctx.Repo.Permission, err = access_model.GetUserRepoPermission(ctx, ctx.Repo.Repository, ctx.Doer)