diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-01-10 17:34:21 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-10 17:34:21 +0800 |
commit | 99d869fa63e07780f1a17d1a9599187b9b689d9b (patch) | |
tree | ecadeba077cf0ae58d0a21f232d280fec60c779a /modules/repository/repo.go | |
parent | 384c2b342ec01fadb520572666127cb5564e1050 (diff) | |
download | gitea-99d869fa63e07780f1a17d1a9599187b9b689d9b.tar.gz gitea-99d869fa63e07780f1a17d1a9599187b9b689d9b.zip |
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 <art27@cantab.net>
Diffstat (limited to 'modules/repository/repo.go')
-rw-r--r-- | modules/repository/repo.go | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/modules/repository/repo.go b/modules/repository/repo.go index 9351ab397e..b0b118e038 100644 --- a/modules/repository/repo.go +++ b/modules/repository/repo.go @@ -214,10 +214,61 @@ func SyncReleasesWithTags(repo *models.Repository, gitRepo *git.Repository) erro } for _, tagName := range tags { if _, ok := existingRelTags[strings.ToLower(tagName)]; !ok { - if err := models.PushUpdateAddTag(repo, gitRepo, tagName); err != nil { - return fmt.Errorf("pushUpdateAddTag: %s: %v", tagName, err) + if err := PushUpdateAddTag(repo, gitRepo, tagName); err != nil { + return fmt.Errorf("pushUpdateAddTag: %v", err) } } } return nil } + +// PushUpdateAddTag must be called for any push actions to add tag +func PushUpdateAddTag(repo *models.Repository, gitRepo *git.Repository, tagName string) error { + tag, err := gitRepo.GetTag(tagName) + if err != nil { + return fmt.Errorf("GetTag: %v", err) + } + commit, err := tag.Commit() + if err != nil { + return fmt.Errorf("Commit: %v", err) + } + + sig := tag.Tagger + if sig == nil { + sig = commit.Author + } + if sig == nil { + sig = commit.Committer + } + + var author *models.User + var createdAt = time.Unix(1, 0) + + if sig != nil { + author, err = models.GetUserByEmail(sig.Email) + if err != nil && !models.IsErrUserNotExist(err) { + return fmt.Errorf("GetUserByEmail: %v", err) + } + createdAt = sig.When + } + + commitsCount, err := commit.CommitsCount() + if err != nil { + return fmt.Errorf("CommitsCount: %v", err) + } + + var rel = models.Release{ + RepoID: repo.ID, + TagName: tagName, + LowerTagName: strings.ToLower(tagName), + Sha1: commit.ID.String(), + NumCommits: commitsCount, + CreatedUnix: timeutil.TimeStamp(createdAt.Unix()), + IsTag: true, + } + if author != nil { + rel.PublisherID = author.ID + } + + return models.SaveOrUpdateTag(repo, &rel) +} |