Skip to content

Commit e7d4895

Browse files
authored
Move commit repo action from models to repofiles package (#7645)
* move commit repo action from models to repofiles package * fix unit tests
1 parent 4d643a5 commit e7d4895

File tree

6 files changed

+348
-310
lines changed

6 files changed

+348
-310
lines changed

models/action.go

+3-195
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"unicode"
1818

1919
"code.gitea.io/gitea/modules/base"
20-
"code.gitea.io/gitea/modules/git"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/setting"
2322
api "code.gitea.io/gitea/modules/structs"
@@ -441,6 +440,9 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
441440
// AvatarLink tries to match user in database with e-mail
442441
// in order to show custom avatar, and falls back to general avatar link.
443442
func (pc *PushCommits) AvatarLink(email string) string {
443+
if pc.avatars == nil {
444+
pc.avatars = make(map[string]string)
445+
}
444446
avatar, ok := pc.avatars[email]
445447
if ok {
446448
return avatar
@@ -655,200 +657,6 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit, bra
655657
return nil
656658
}
657659

658-
// CommitRepoActionOptions represent options of a new commit action.
659-
type CommitRepoActionOptions struct {
660-
PusherName string
661-
RepoOwnerID int64
662-
RepoName string
663-
RefFullName string
664-
OldCommitID string
665-
NewCommitID string
666-
Commits *PushCommits
667-
}
668-
669-
// CommitRepoAction adds new commit action to the repository, and prepare
670-
// corresponding webhooks.
671-
func CommitRepoAction(opts CommitRepoActionOptions) error {
672-
pusher, err := GetUserByName(opts.PusherName)
673-
if err != nil {
674-
return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
675-
}
676-
677-
repo, err := GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
678-
if err != nil {
679-
return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
680-
}
681-
682-
refName := git.RefEndName(opts.RefFullName)
683-
684-
// Change default branch and empty status only if pushed ref is non-empty branch.
685-
if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
686-
repo.DefaultBranch = refName
687-
repo.IsEmpty = false
688-
if refName != "master" {
689-
gitRepo, err := git.OpenRepository(repo.RepoPath())
690-
if err != nil {
691-
return err
692-
}
693-
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
694-
if !git.IsErrUnsupportedVersion(err) {
695-
return err
696-
}
697-
}
698-
}
699-
}
700-
701-
// Change repository empty status and update last updated time.
702-
if err = UpdateRepository(repo, false); err != nil {
703-
return fmt.Errorf("UpdateRepository: %v", err)
704-
}
705-
706-
isNewBranch := false
707-
opType := ActionCommitRepo
708-
// Check it's tag push or branch.
709-
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
710-
opType = ActionPushTag
711-
if opts.NewCommitID == git.EmptySHA {
712-
opType = ActionDeleteTag
713-
}
714-
opts.Commits = &PushCommits{}
715-
} else if opts.NewCommitID == git.EmptySHA {
716-
opType = ActionDeleteBranch
717-
opts.Commits = &PushCommits{}
718-
} else {
719-
// if not the first commit, set the compare URL.
720-
if opts.OldCommitID == git.EmptySHA {
721-
isNewBranch = true
722-
} else {
723-
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
724-
}
725-
726-
if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
727-
log.Error("updateIssuesCommit: %v", err)
728-
}
729-
}
730-
731-
if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
732-
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
733-
}
734-
735-
data, err := json.Marshal(opts.Commits)
736-
if err != nil {
737-
return fmt.Errorf("Marshal: %v", err)
738-
}
739-
740-
if err = NotifyWatchers(&Action{
741-
ActUserID: pusher.ID,
742-
ActUser: pusher,
743-
OpType: opType,
744-
Content: string(data),
745-
RepoID: repo.ID,
746-
Repo: repo,
747-
RefName: refName,
748-
IsPrivate: repo.IsPrivate,
749-
}); err != nil {
750-
return fmt.Errorf("NotifyWatchers: %v", err)
751-
}
752-
753-
defer func() {
754-
go HookQueue.Add(repo.ID)
755-
}()
756-
757-
apiPusher := pusher.APIFormat()
758-
apiRepo := repo.APIFormat(AccessModeNone)
759-
760-
var shaSum string
761-
var isHookEventPush = false
762-
switch opType {
763-
case ActionCommitRepo: // Push
764-
isHookEventPush = true
765-
766-
if isNewBranch {
767-
gitRepo, err := git.OpenRepository(repo.RepoPath())
768-
if err != nil {
769-
log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
770-
}
771-
772-
shaSum, err = gitRepo.GetBranchCommitID(refName)
773-
if err != nil {
774-
log.Error("GetBranchCommitID[%s]: %v", opts.RefFullName, err)
775-
}
776-
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
777-
Ref: refName,
778-
Sha: shaSum,
779-
RefType: "branch",
780-
Repo: apiRepo,
781-
Sender: apiPusher,
782-
}); err != nil {
783-
return fmt.Errorf("PrepareWebhooks: %v", err)
784-
}
785-
}
786-
787-
case ActionDeleteBranch: // Delete Branch
788-
isHookEventPush = true
789-
790-
if err = PrepareWebhooks(repo, HookEventDelete, &api.DeletePayload{
791-
Ref: refName,
792-
RefType: "branch",
793-
PusherType: api.PusherTypeUser,
794-
Repo: apiRepo,
795-
Sender: apiPusher,
796-
}); err != nil {
797-
return fmt.Errorf("PrepareWebhooks.(delete branch): %v", err)
798-
}
799-
800-
case ActionPushTag: // Create
801-
isHookEventPush = true
802-
803-
gitRepo, err := git.OpenRepository(repo.RepoPath())
804-
if err != nil {
805-
log.Error("OpenRepository[%s]: %v", repo.RepoPath(), err)
806-
}
807-
shaSum, err = gitRepo.GetTagCommitID(refName)
808-
if err != nil {
809-
log.Error("GetTagCommitID[%s]: %v", opts.RefFullName, err)
810-
}
811-
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
812-
Ref: refName,
813-
Sha: shaSum,
814-
RefType: "tag",
815-
Repo: apiRepo,
816-
Sender: apiPusher,
817-
}); err != nil {
818-
return fmt.Errorf("PrepareWebhooks: %v", err)
819-
}
820-
case ActionDeleteTag: // Delete Tag
821-
isHookEventPush = true
822-
823-
if err = PrepareWebhooks(repo, HookEventDelete, &api.DeletePayload{
824-
Ref: refName,
825-
RefType: "tag",
826-
PusherType: api.PusherTypeUser,
827-
Repo: apiRepo,
828-
Sender: apiPusher,
829-
}); err != nil {
830-
return fmt.Errorf("PrepareWebhooks.(delete tag): %v", err)
831-
}
832-
}
833-
834-
if isHookEventPush {
835-
if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
836-
Ref: opts.RefFullName,
837-
Before: opts.OldCommitID,
838-
After: opts.NewCommitID,
839-
CompareURL: setting.AppURL + opts.Commits.CompareURL,
840-
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
841-
Repo: apiRepo,
842-
Pusher: apiPusher,
843-
Sender: apiPusher,
844-
}); err != nil {
845-
return fmt.Errorf("PrepareWebhooks: %v", err)
846-
}
847-
}
848-
849-
return nil
850-
}
851-
852660
func transferRepoAction(e Engine, doer, oldOwner *User, repo *Repository) (err error) {
853661
if err = notifyWatchers(e, &Action{
854662
ActUserID: doer.ID,

models/action_test.go

-114
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77
"testing"
88

9-
"code.gitea.io/gitea/modules/git"
109
"code.gitea.io/gitea/modules/setting"
1110

1211
"github.com/stretchr/testify/assert"
@@ -409,119 +408,6 @@ func TestUpdateIssuesCommit_AnotherRepoNoPermission(t *testing.T) {
409408
CheckConsistencyFor(t, &Action{})
410409
}
411410

412-
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
413-
AssertNotExistsBean(t, actionBean)
414-
assert.NoError(t, CommitRepoAction(opts))
415-
AssertExistsAndLoadBean(t, actionBean)
416-
CheckConsistencyFor(t, &Action{})
417-
}
418-
419-
func TestCommitRepoAction(t *testing.T) {
420-
samples := []struct {
421-
userID int64
422-
repositoryID int64
423-
commitRepoActionOptions CommitRepoActionOptions
424-
action Action
425-
}{
426-
{
427-
userID: 2,
428-
repositoryID: 2,
429-
commitRepoActionOptions: CommitRepoActionOptions{
430-
RefFullName: "refName",
431-
OldCommitID: "oldCommitID",
432-
NewCommitID: "newCommitID",
433-
Commits: &PushCommits{
434-
avatars: make(map[string]string),
435-
Commits: []*PushCommit{
436-
{
437-
Sha1: "abcdef1",
438-
CommitterEmail: "[email protected]",
439-
CommitterName: "User Two",
440-
AuthorEmail: "[email protected]",
441-
AuthorName: "User Four",
442-
Message: "message1",
443-
},
444-
{
445-
Sha1: "abcdef2",
446-
CommitterEmail: "[email protected]",
447-
CommitterName: "User Two",
448-
AuthorEmail: "[email protected]",
449-
AuthorName: "User Two",
450-
Message: "message2",
451-
},
452-
},
453-
Len: 2,
454-
},
455-
},
456-
action: Action{
457-
OpType: ActionCommitRepo,
458-
RefName: "refName",
459-
},
460-
},
461-
{
462-
userID: 2,
463-
repositoryID: 1,
464-
commitRepoActionOptions: CommitRepoActionOptions{
465-
RefFullName: git.TagPrefix + "v1.1",
466-
OldCommitID: git.EmptySHA,
467-
NewCommitID: "newCommitID",
468-
Commits: &PushCommits{},
469-
},
470-
action: Action{
471-
OpType: ActionPushTag,
472-
RefName: "v1.1",
473-
},
474-
},
475-
{
476-
userID: 2,
477-
repositoryID: 1,
478-
commitRepoActionOptions: CommitRepoActionOptions{
479-
RefFullName: git.TagPrefix + "v1.1",
480-
OldCommitID: "oldCommitID",
481-
NewCommitID: git.EmptySHA,
482-
Commits: &PushCommits{},
483-
},
484-
action: Action{
485-
OpType: ActionDeleteTag,
486-
RefName: "v1.1",
487-
},
488-
},
489-
{
490-
userID: 2,
491-
repositoryID: 1,
492-
commitRepoActionOptions: CommitRepoActionOptions{
493-
RefFullName: git.BranchPrefix + "feature/1",
494-
OldCommitID: "oldCommitID",
495-
NewCommitID: git.EmptySHA,
496-
Commits: &PushCommits{},
497-
},
498-
action: Action{
499-
OpType: ActionDeleteBranch,
500-
RefName: "feature/1",
501-
},
502-
},
503-
}
504-
505-
for _, s := range samples {
506-
PrepareTestEnv(t)
507-
508-
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
509-
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
510-
repo.Owner = user
511-
512-
s.commitRepoActionOptions.PusherName = user.Name
513-
s.commitRepoActionOptions.RepoOwnerID = user.ID
514-
s.commitRepoActionOptions.RepoName = repo.Name
515-
516-
s.action.ActUserID = user.ID
517-
s.action.RepoID = repo.ID
518-
s.action.Repo = repo
519-
s.action.IsPrivate = repo.IsPrivate
520-
521-
testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
522-
}
523-
}
524-
525411
func TestTransferRepoAction(t *testing.T) {
526412
assert.NoError(t, PrepareTestDatabase())
527413

models/unit_tests.go

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ func MainTest(m *testing.M, pathToGiteaRoot string) {
6565
fatalTestError("url.Parse: %v\n", err)
6666
}
6767

68+
if err = removeAllWithRetry(setting.RepoRootPath); err != nil {
69+
fatalTestError("os.RemoveAll: %v\n", err)
70+
}
71+
if err = com.CopyDir(filepath.Join(pathToGiteaRoot, "integrations", "gitea-repositories-meta"), setting.RepoRootPath); err != nil {
72+
fatalTestError("com.CopyDir: %v\n", err)
73+
}
74+
6875
exitStatus := m.Run()
6976
if err = removeAllWithRetry(setting.RepoRootPath); err != nil {
7077
fatalTestError("os.RemoveAll: %v\n", err)

0 commit comments

Comments
 (0)