Skip to content

Commit 99d869f

Browse files
lunnyzeripath
andauthored
Move push commits from models to modules/repository (#9370)
* Move push commits from models to modules/repository * fix test * fix test * fix test * fix test * fix test Co-authored-by: zeripath <[email protected]>
1 parent 384c2b3 commit 99d869f

File tree

20 files changed

+482
-436
lines changed

20 files changed

+482
-436
lines changed

models/action.go

Lines changed: 0 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ import (
1313
"time"
1414

1515
"code.gitea.io/gitea/modules/base"
16-
"code.gitea.io/gitea/modules/git"
1716
"code.gitea.io/gitea/modules/log"
1817
"code.gitea.io/gitea/modules/setting"
19-
api "code.gitea.io/gitea/modules/structs"
2018
"code.gitea.io/gitea/modules/timeutil"
2119

2220
"github.com/unknwon/com"
@@ -284,130 +282,6 @@ func (a *Action) GetIssueContent() string {
284282
return issue.Content
285283
}
286284

287-
// PushCommit represents a commit in a push operation.
288-
type PushCommit struct {
289-
Sha1 string
290-
Message string
291-
AuthorEmail string
292-
AuthorName string
293-
CommitterEmail string
294-
CommitterName string
295-
Timestamp time.Time
296-
}
297-
298-
// PushCommits represents list of commits in a push operation.
299-
type PushCommits struct {
300-
Len int
301-
Commits []*PushCommit
302-
CompareURL string
303-
304-
avatars map[string]string
305-
emailUsers map[string]*User
306-
}
307-
308-
// NewPushCommits creates a new PushCommits object.
309-
func NewPushCommits() *PushCommits {
310-
return &PushCommits{
311-
avatars: make(map[string]string),
312-
emailUsers: make(map[string]*User),
313-
}
314-
}
315-
316-
// ToAPIPayloadCommits converts a PushCommits object to
317-
// api.PayloadCommit format.
318-
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
319-
commits := make([]*api.PayloadCommit, len(pc.Commits))
320-
321-
if pc.emailUsers == nil {
322-
pc.emailUsers = make(map[string]*User)
323-
}
324-
var err error
325-
for i, commit := range pc.Commits {
326-
authorUsername := ""
327-
author, ok := pc.emailUsers[commit.AuthorEmail]
328-
if !ok {
329-
author, err = GetUserByEmail(commit.AuthorEmail)
330-
if err == nil {
331-
authorUsername = author.Name
332-
pc.emailUsers[commit.AuthorEmail] = author
333-
}
334-
} else {
335-
authorUsername = author.Name
336-
}
337-
338-
committerUsername := ""
339-
committer, ok := pc.emailUsers[commit.CommitterEmail]
340-
if !ok {
341-
committer, err = GetUserByEmail(commit.CommitterEmail)
342-
if err == nil {
343-
// TODO: check errors other than email not found.
344-
committerUsername = committer.Name
345-
pc.emailUsers[commit.CommitterEmail] = committer
346-
}
347-
} else {
348-
committerUsername = committer.Name
349-
}
350-
351-
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
352-
if err != nil {
353-
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
354-
}
355-
356-
commits[i] = &api.PayloadCommit{
357-
ID: commit.Sha1,
358-
Message: commit.Message,
359-
URL: fmt.Sprintf("%s/commit/%s", repoLink, commit.Sha1),
360-
Author: &api.PayloadUser{
361-
Name: commit.AuthorName,
362-
Email: commit.AuthorEmail,
363-
UserName: authorUsername,
364-
},
365-
Committer: &api.PayloadUser{
366-
Name: commit.CommitterName,
367-
Email: commit.CommitterEmail,
368-
UserName: committerUsername,
369-
},
370-
Added: fileStatus.Added,
371-
Removed: fileStatus.Removed,
372-
Modified: fileStatus.Modified,
373-
Timestamp: commit.Timestamp,
374-
}
375-
}
376-
return commits, nil
377-
}
378-
379-
// AvatarLink tries to match user in database with e-mail
380-
// in order to show custom avatar, and falls back to general avatar link.
381-
func (pc *PushCommits) AvatarLink(email string) string {
382-
if pc.avatars == nil {
383-
pc.avatars = make(map[string]string)
384-
}
385-
avatar, ok := pc.avatars[email]
386-
if ok {
387-
return avatar
388-
}
389-
390-
u, ok := pc.emailUsers[email]
391-
if !ok {
392-
var err error
393-
u, err = GetUserByEmail(email)
394-
if err != nil {
395-
pc.avatars[email] = base.AvatarLink(email)
396-
if !IsErrUserNotExist(err) {
397-
log.Error("GetUserByEmail: %v", err)
398-
return ""
399-
}
400-
} else {
401-
pc.emailUsers[email] = u
402-
}
403-
}
404-
if u != nil {
405-
pc.avatars[email] = u.RelAvatarLink()
406-
}
407-
408-
return pc.avatars[email]
409-
}
410-
411285
// GetFeedsOptions options for retrieving feeds
412286
type GetFeedsOptions struct {
413287
RequestedUser *User

models/action_test.go

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -27,106 +27,6 @@ func TestAction_GetRepoLink(t *testing.T) {
2727
assert.Equal(t, expected, action.GetRepoLink())
2828
}
2929

30-
func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
31-
pushCommits := NewPushCommits()
32-
pushCommits.Commits = []*PushCommit{
33-
{
34-
Sha1: "69554a6",
35-
CommitterEmail: "[email protected]",
36-
CommitterName: "User2",
37-
AuthorEmail: "[email protected]",
38-
AuthorName: "User2",
39-
Message: "not signed commit",
40-
},
41-
{
42-
Sha1: "27566bd",
43-
CommitterEmail: "[email protected]",
44-
CommitterName: "User2",
45-
AuthorEmail: "[email protected]",
46-
AuthorName: "User2",
47-
Message: "good signed commit (with not yet validated email)",
48-
},
49-
{
50-
Sha1: "5099b81",
51-
CommitterEmail: "[email protected]",
52-
CommitterName: "User2",
53-
AuthorEmail: "[email protected]",
54-
AuthorName: "User2",
55-
Message: "good signed commit",
56-
},
57-
}
58-
pushCommits.Len = len(pushCommits.Commits)
59-
60-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 16}).(*Repository)
61-
payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
62-
assert.NoError(t, err)
63-
assert.EqualValues(t, 3, len(payloadCommits))
64-
65-
assert.Equal(t, "69554a6", payloadCommits[0].ID)
66-
assert.Equal(t, "not signed commit", payloadCommits[0].Message)
67-
assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
68-
assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
69-
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
70-
assert.Equal(t, "User2", payloadCommits[0].Author.Name)
71-
assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
72-
assert.EqualValues(t, []string{}, payloadCommits[0].Added)
73-
assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
74-
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
75-
76-
assert.Equal(t, "27566bd", payloadCommits[1].ID)
77-
assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
78-
assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
79-
assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
80-
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
81-
assert.Equal(t, "User2", payloadCommits[1].Author.Name)
82-
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
83-
assert.EqualValues(t, []string{}, payloadCommits[1].Added)
84-
assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
85-
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
86-
87-
assert.Equal(t, "5099b81", payloadCommits[2].ID)
88-
assert.Equal(t, "good signed commit", payloadCommits[2].Message)
89-
assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
90-
assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
91-
assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
92-
assert.Equal(t, "User2", payloadCommits[2].Author.Name)
93-
assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
94-
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
95-
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
96-
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
97-
}
98-
99-
func TestPushCommits_AvatarLink(t *testing.T) {
100-
pushCommits := NewPushCommits()
101-
pushCommits.Commits = []*PushCommit{
102-
{
103-
Sha1: "abcdef1",
104-
CommitterEmail: "[email protected]",
105-
CommitterName: "User Two",
106-
AuthorEmail: "[email protected]",
107-
AuthorName: "User Four",
108-
Message: "message1",
109-
},
110-
{
111-
Sha1: "abcdef2",
112-
CommitterEmail: "[email protected]",
113-
CommitterName: "User Two",
114-
AuthorEmail: "[email protected]",
115-
AuthorName: "User Two",
116-
Message: "message2",
117-
},
118-
}
119-
pushCommits.Len = len(pushCommits.Commits)
120-
121-
assert.Equal(t,
122-
"/suburl/user/avatar/user2/-1",
123-
pushCommits.AvatarLink("[email protected]"))
124-
125-
assert.Equal(t,
126-
"https://secure.gravatar.com/avatar/19ade630b94e1e0535b3df7387434154?d=identicon",
127-
pushCommits.AvatarLink("[email protected]"))
128-
}
129-
13030
func TestGetFeeds(t *testing.T) {
13131
// test with an individual user
13232
assert.NoError(t, PrepareTestDatabase())

models/update.go

Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package models
66

77
import (
8-
"container/list"
98
"fmt"
109
"strings"
1110
"time"
@@ -27,33 +26,6 @@ const (
2726
EnvIsInternal = "GITEA_INTERNAL_PUSH"
2827
)
2928

30-
// CommitToPushCommit transforms a git.Commit to PushCommit type.
31-
func CommitToPushCommit(commit *git.Commit) *PushCommit {
32-
return &PushCommit{
33-
Sha1: commit.ID.String(),
34-
Message: commit.Message(),
35-
AuthorEmail: commit.Author.Email,
36-
AuthorName: commit.Author.Name,
37-
CommitterEmail: commit.Committer.Email,
38-
CommitterName: commit.Committer.Name,
39-
Timestamp: commit.Author.When,
40-
}
41-
}
42-
43-
// ListToPushCommits transforms a list.List to PushCommits type.
44-
func ListToPushCommits(l *list.List) *PushCommits {
45-
var commits []*PushCommit
46-
var actEmail string
47-
for e := l.Front(); e != nil; e = e.Next() {
48-
commit := e.Value.(*git.Commit)
49-
if actEmail == "" {
50-
actEmail = commit.Committer.Email
51-
}
52-
commits = append(commits, CommitToPushCommit(commit))
53-
}
54-
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
55-
}
56-
5729
// PushUpdateAddDeleteTags updates a number of added and delete tags
5830
func PushUpdateAddDeleteTags(repo *Repository, gitRepo *git.Repository, addTags, delTags []string) error {
5931
sess := x.NewSession()
@@ -258,75 +230,25 @@ func pushUpdateAddTags(e Engine, repo *Repository, gitRepo *git.Repository, tags
258230
return nil
259231
}
260232

261-
// PushUpdateAddTag must be called for any push actions to add tag
262-
func PushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
263-
rel, err := GetRelease(repo.ID, tagName)
233+
// SaveOrUpdateTag must be called for any push actions to add tag
234+
func SaveOrUpdateTag(repo *Repository, newRel *Release) error {
235+
rel, err := GetRelease(repo.ID, newRel.TagName)
264236
if err != nil && !IsErrReleaseNotExist(err) {
265237
return fmt.Errorf("GetRelease: %v", err)
266238
}
267239

268-
tag, err := gitRepo.GetTag(tagName)
269-
if err != nil {
270-
return fmt.Errorf("GetTag: %v", err)
271-
}
272-
commit, err := tag.Commit()
273-
if err != nil {
274-
return fmt.Errorf("Commit: %v", err)
275-
}
276-
277-
sig := tag.Tagger
278-
if sig == nil {
279-
sig = commit.Author
280-
}
281-
if sig == nil {
282-
sig = commit.Committer
283-
}
284-
285-
var author *User
286-
var createdAt = time.Unix(1, 0)
287-
288-
if sig != nil {
289-
author, err = GetUserByEmail(sig.Email)
290-
if err != nil && !IsErrUserNotExist(err) {
291-
return fmt.Errorf("GetUserByEmail: %v", err)
292-
}
293-
createdAt = sig.When
294-
}
295-
296-
commitsCount, err := commit.CommitsCount()
297-
if err != nil {
298-
return fmt.Errorf("CommitsCount: %v", err)
299-
}
300-
301240
if rel == nil {
302-
rel = &Release{
303-
RepoID: repo.ID,
304-
Title: "",
305-
TagName: tagName,
306-
LowerTagName: strings.ToLower(tagName),
307-
Target: "",
308-
Sha1: commit.ID.String(),
309-
NumCommits: commitsCount,
310-
Note: "",
311-
IsDraft: false,
312-
IsPrerelease: false,
313-
IsTag: true,
314-
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
315-
}
316-
if author != nil {
317-
rel.PublisherID = author.ID
318-
}
319-
320-
if _, err = x.InsertOne(rel); err != nil {
241+
rel = newRel
242+
if _, err = x.Insert(rel); err != nil {
321243
return fmt.Errorf("InsertOne: %v", err)
322244
}
323245
} else {
324-
rel.Sha1 = commit.ID.String()
325-
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
326-
rel.NumCommits = commitsCount
246+
rel.Sha1 = newRel.Sha1
247+
rel.CreatedUnix = newRel.CreatedUnix
248+
rel.NumCommits = newRel.NumCommits
327249
rel.IsDraft = false
328-
if rel.IsTag && author != nil {
329-
rel.PublisherID = author.ID
250+
if rel.IsTag && newRel.PublisherID > 0 {
251+
rel.PublisherID = newRel.PublisherID
330252
}
331253
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
332254
return fmt.Errorf("Update: %v", err)

0 commit comments

Comments
 (0)