Skip to content

Move some code into models/git #19879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 12, 2022
4 changes: 2 additions & 2 deletions cmd/migrate_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"fmt"
"strings"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/migrations"
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -88,7 +88,7 @@ func migrateAttachments(dstStorage storage.ObjectStorage) error {
}

func migrateLFS(dstStorage storage.ObjectStorage) error {
return models.IterateLFS(func(mo *models.LFSMetaObject) error {
return git_model.IterateLFS(func(mo *git_model.LFSMetaObject) error {
_, err := storage.Copy(dstStorage, mo.RelativePath(), storage.LFS, mo.RelativePath())
return err
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/serv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"strings"
"time"

"code.gitea.io/gitea/models"
asymkey_model "code.gitea.io/gitea/models/asymkey"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
Expand Down Expand Up @@ -276,7 +276,7 @@ func runServ(c *cli.Context) error {
return fail("Internal error", "Failed to sign JWT token: %v", err)
}

tokenAuthentication := &models.LFSTokenResponse{
tokenAuthentication := &git_model.LFSTokenResponse{
Header: make(map[string]string),
Href: url,
}
Expand Down
22 changes: 11 additions & 11 deletions integrations/api_repo_lfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"testing"

"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestAPILFSBatch(t *testing.T) {

content := []byte("dummy1")
oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)

session := loginUser(t, "user2")

Expand Down Expand Up @@ -260,9 +260,9 @@ func TestAPILFSBatch(t *testing.T) {
content := []byte("dummy0")
storeObjectInRepo(t, repo2.ID, &content)

meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.Nil(t, meta)
assert.Equal(t, models.ErrLFSObjectNotExist, err)
assert.Equal(t, git_model.ErrLFSObjectNotExist, err)

req := newRequest(t, &lfs.BatchRequest{
Operation: "upload",
Expand All @@ -275,7 +275,7 @@ func TestAPILFSBatch(t *testing.T) {
assert.Nil(t, br.Objects[0].Error)
assert.Empty(t, br.Objects[0].Actions)

meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
meta, err = git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err)
assert.NotNil(t, meta)

Expand Down Expand Up @@ -336,7 +336,7 @@ func TestAPILFSUpload(t *testing.T) {

content := []byte("dummy3")
oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)

session := loginUser(t, "user2")

Expand Down Expand Up @@ -365,9 +365,9 @@ func TestAPILFSUpload(t *testing.T) {
err = contentStore.Put(p, bytes.NewReader([]byte("dummy5")))
assert.NoError(t, err)

meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.Nil(t, meta)
assert.Equal(t, models.ErrLFSObjectNotExist, err)
assert.Equal(t, git_model.ErrLFSObjectNotExist, err)

t.Run("InvalidAccess", func(t *testing.T) {
req := newRequest(t, p, "invalid")
Expand All @@ -378,7 +378,7 @@ func TestAPILFSUpload(t *testing.T) {
req := newRequest(t, p, "dummy5")

session.MakeRequest(t, req, http.StatusOK)
meta, err = models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
meta, err = git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err)
assert.NotNil(t, meta)
})
Expand Down Expand Up @@ -426,7 +426,7 @@ func TestAPILFSUpload(t *testing.T) {
assert.NoError(t, err)
assert.True(t, exist)

meta, err := models.GetLFSMetaObjectByOid(repo.ID, p.Oid)
meta, err := git_model.GetLFSMetaObjectByOid(repo.ID, p.Oid)
assert.NoError(t, err)
assert.NotNil(t, meta)
})
Expand All @@ -441,7 +441,7 @@ func TestAPILFSVerify(t *testing.T) {

content := []byte("dummy3")
oid := storeObjectInRepo(t, repo.ID, &content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)

session := loginUser(t, "user2")

Expand Down
6 changes: 3 additions & 3 deletions integrations/lfs_getobject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"net/http/httptest"
"testing"

"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
Expand All @@ -28,7 +28,7 @@ func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string
pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
assert.NoError(t, err)

_, err = models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
_, err = git_model.NewLFSMetaObject(&git_model.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
assert.NoError(t, err)
contentStore := lfs.NewContentStore()
exist, err := contentStore.Exists(pointer)
Expand All @@ -44,7 +44,7 @@ func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, exp
repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1")
assert.NoError(t, err)
oid := storeObjectInRepo(t, repo.ID, content)
defer models.RemoveLFSMetaObjectByOid(repo.ID, oid)
defer git_model.RemoveLFSMetaObjectByOid(repo.ID, oid)

session := loginUser(t, "user2")

Expand Down
9 changes: 5 additions & 4 deletions integrations/repo_tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"code.gitea.io/gitea/models"
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
Expand All @@ -32,12 +33,12 @@ func TestCreateNewTagProtected(t *testing.T) {
err := release.CreateNewTag(git.DefaultContext, owner, repo, "master", "v-1", "first tag")
assert.NoError(t, err)

err = models.InsertProtectedTag(&models.ProtectedTag{
err = git_model.InsertProtectedTag(&git_model.ProtectedTag{
RepoID: repo.ID,
NamePattern: "v-*",
})
assert.NoError(t, err)
err = models.InsertProtectedTag(&models.ProtectedTag{
err = git_model.InsertProtectedTag(&git_model.ProtectedTag{
RepoID: repo.ID,
NamePattern: "v-1.1",
AllowlistUserIDs: []int64{repo.OwnerID},
Expand Down Expand Up @@ -87,11 +88,11 @@ func TestCreateNewTagProtected(t *testing.T) {
assert.NoError(t, err)
}

protectedTags, err := models.GetProtectedTags(repo.ID)
protectedTags, err := git_model.GetProtectedTags(repo.ID)
assert.NoError(t, err)

for _, protectedTag := range protectedTags {
err = models.DeleteProtectedTag(protectedTag)
err = git_model.DeleteProtectedTag(protectedTag)
assert.NoError(t, err)
}
}
80 changes: 80 additions & 0 deletions models/branch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
"context"

"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/modules/log"
)

// HasEnoughApprovals returns true if pr has enough granted approvals.
func HasEnoughApprovals(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if protectBranch.RequiredApprovals == 0 {
return true
}
return GetGrantedApprovalsCount(ctx, protectBranch, pr) >= protectBranch.RequiredApprovals
}

// GetGrantedApprovalsCount returns the number of granted approvals for pr. A granted approval must be authored by a user in an approval whitelist.
func GetGrantedApprovalsCount(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) int64 {
sess := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeApprove).
And("official = ?", true).
And("dismissed = ?", false)
if protectBranch.DismissStaleApprovals {
sess = sess.And("stale = ?", false)
}
approvals, err := sess.Count(new(Review))
if err != nil {
log.Error("GetGrantedApprovalsCount: %v", err)
return 0
}

return approvals
}

// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
func MergeBlockedByRejectedReview(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if !protectBranch.BlockOnRejectedReviews {
return false
}
rejectExist, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeReject).
And("official = ?", true).
And("dismissed = ?", false).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByRejectedReview: %v", err)
return true
}

return rejectExist
}

// MergeBlockedByOfficialReviewRequests block merge because of some review request to official reviewer
// of from official review
func MergeBlockedByOfficialReviewRequests(ctx context.Context, protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
if !protectBranch.BlockOnOfficialReviewRequests {
return false
}
has, err := db.GetEngine(ctx).Where("issue_id = ?", pr.IssueID).
And("type = ?", ReviewTypeRequest).
And("official = ?", true).
Exist(new(Review))
if err != nil {
log.Error("MergeBlockedByOfficialReviewRequests: %v", err)
return true
}

return has
}

// MergeBlockedByOutdatedBranch returns true if merge is blocked by an outdated head branch
func MergeBlockedByOutdatedBranch(protectBranch *git_model.ProtectedBranch, pr *PullRequest) bool {
return protectBranch.BlockOnOutdatedBranch && pr.CommitsBehind > 0
}
26 changes: 0 additions & 26 deletions models/commit.go

This file was deleted.

78 changes: 0 additions & 78 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package models
import (
"fmt"

"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/git"
)
Expand Down Expand Up @@ -145,83 +144,6 @@ func (err ErrAccessTokenEmpty) Error() string {
return "access token is empty"
}

//.____ ____________________
//| | \_ _____/ _____/
//| | | __) \_____ \
//| |___| \ / \
//|_______ \___ / /_______ /
// \/ \/ \/

// ErrLFSLockNotExist represents a "LFSLockNotExist" kind of error.
type ErrLFSLockNotExist struct {
ID int64
RepoID int64
Path string
}

// IsErrLFSLockNotExist checks if an error is a ErrLFSLockNotExist.
func IsErrLFSLockNotExist(err error) bool {
_, ok := err.(ErrLFSLockNotExist)
return ok
}

func (err ErrLFSLockNotExist) Error() string {
return fmt.Sprintf("lfs lock does not exist [id: %d, rid: %d, path: %s]", err.ID, err.RepoID, err.Path)
}

// ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error.
type ErrLFSUnauthorizedAction struct {
RepoID int64
UserName string
Mode perm.AccessMode
}

// IsErrLFSUnauthorizedAction checks if an error is a ErrLFSUnauthorizedAction.
func IsErrLFSUnauthorizedAction(err error) bool {
_, ok := err.(ErrLFSUnauthorizedAction)
return ok
}

func (err ErrLFSUnauthorizedAction) Error() string {
if err.Mode == perm.AccessModeWrite {
return fmt.Sprintf("User %s doesn't have write access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}
return fmt.Sprintf("User %s doesn't have read access for lfs lock [rid: %d]", err.UserName, err.RepoID)
}

// ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error.
type ErrLFSLockAlreadyExist struct {
RepoID int64
Path string
}

// IsErrLFSLockAlreadyExist checks if an error is a ErrLFSLockAlreadyExist.
func IsErrLFSLockAlreadyExist(err error) bool {
_, ok := err.(ErrLFSLockAlreadyExist)
return ok
}

func (err ErrLFSLockAlreadyExist) Error() string {
return fmt.Sprintf("lfs lock already exists [rid: %d, path: %s]", err.RepoID, err.Path)
}

// ErrLFSFileLocked represents a "LFSFileLocked" kind of error.
type ErrLFSFileLocked struct {
RepoID int64
Path string
UserName string
}

// IsErrLFSFileLocked checks if an error is a ErrLFSFileLocked.
func IsErrLFSFileLocked(err error) bool {
_, ok := err.(ErrLFSFileLocked)
return ok
}

func (err ErrLFSFileLocked) Error() string {
return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path)
}

// ErrNoPendingRepoTransfer is an error type for repositories without a pending
// transfer request
type ErrNoPendingRepoTransfer struct {
Expand Down
Loading