2020-09-11 08:14:48 -06:00
|
|
|
// Copyright 2020 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 repository
|
|
|
|
|
|
|
|
import (
|
2021-11-17 08:17:31 -07:00
|
|
|
"context"
|
2021-10-17 13:47:12 -06:00
|
|
|
"errors"
|
2020-09-11 08:14:48 -06:00
|
|
|
"fmt"
|
2021-11-17 08:17:31 -07:00
|
|
|
"strings"
|
2020-09-20 11:55:15 -06:00
|
|
|
"time"
|
2020-09-11 08:14:48 -06:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-12 09:51:54 -06:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-09 18:27:50 -07:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 02:49:20 -07:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2020-09-11 08:14:48 -06:00
|
|
|
"code.gitea.io/gitea/modules/cache"
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/graceful"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2022-01-19 16:26:57 -07:00
|
|
|
"code.gitea.io/gitea/modules/process"
|
2020-09-11 08:14:48 -06:00
|
|
|
"code.gitea.io/gitea/modules/queue"
|
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-11-17 08:17:31 -07:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-11-21 02:11:48 -07:00
|
|
|
issue_service "code.gitea.io/gitea/services/issue"
|
2020-09-11 08:14:48 -06:00
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
|
|
)
|
|
|
|
|
|
|
|
// pushQueue represents a queue to handle update pull request tests
|
|
|
|
var pushQueue queue.Queue
|
|
|
|
|
|
|
|
// handle passed PR IDs and test the PRs
|
2022-01-22 14:22:14 -07:00
|
|
|
func handle(data ...queue.Data) []queue.Data {
|
2020-09-11 08:14:48 -06:00
|
|
|
for _, datum := range data {
|
2020-10-30 15:59:02 -06:00
|
|
|
opts := datum.([]*repo_module.PushUpdateOptions)
|
2020-09-11 08:14:48 -06:00
|
|
|
if err := pushUpdates(opts); err != nil {
|
|
|
|
log.Error("pushUpdate failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2022-01-22 14:22:14 -07:00
|
|
|
return nil
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func initPushQueue() error {
|
2021-10-17 13:47:12 -06:00
|
|
|
pushQueue = queue.CreateQueue("push_update", handle, []*repo_module.PushUpdateOptions{})
|
2020-09-11 08:14:48 -06:00
|
|
|
if pushQueue == nil {
|
2021-10-17 13:47:12 -06:00
|
|
|
return errors.New("unable to create push_update Queue")
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
go graceful.GetManager().RunWithShutdownFns(pushQueue.Run)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushUpdate is an alias of PushUpdates for single push update options
|
2020-10-30 15:59:02 -06:00
|
|
|
func PushUpdate(opts *repo_module.PushUpdateOptions) error {
|
|
|
|
return PushUpdates([]*repo_module.PushUpdateOptions{opts})
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// PushUpdates adds a push update to push queue
|
2020-10-30 15:59:02 -06:00
|
|
|
func PushUpdates(opts []*repo_module.PushUpdateOptions) error {
|
2020-09-11 08:14:48 -06:00
|
|
|
if len(opts) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if opt.IsNewRef() && opt.IsDelRef() {
|
|
|
|
return fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pushQueue.Push(opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushUpdates generates push action history feeds for push updating multiple refs
|
2020-10-30 15:59:02 -06:00
|
|
|
func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
|
2020-09-11 08:14:48 -06:00
|
|
|
if len(optsList) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-19 16:26:57 -07:00
|
|
|
ctx, _, finished := process.GetManager().AddContext(graceful.GetManager().HammerContext(), fmt.Sprintf("PushUpdates: %s/%s", optsList[0].RepoUserName, optsList[0].RepoName))
|
|
|
|
defer finished()
|
|
|
|
|
2021-12-09 18:27:50 -07:00
|
|
|
repo, err := repo_model.GetRepositoryByOwnerAndName(optsList[0].RepoUserName, optsList[0].RepoName)
|
2020-09-11 08:14:48 -06:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetRepositoryByOwnerAndName failed: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
repoPath := repo.RepoPath()
|
2022-01-19 16:26:57 -07:00
|
|
|
|
2022-03-29 13:13:41 -06:00
|
|
|
gitRepo, err := git.OpenRepository(ctx, repoPath)
|
2020-09-11 08:14:48 -06:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("OpenRepository[%s]: %w", repoPath, err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
defer gitRepo.Close()
|
|
|
|
|
2022-06-06 02:01:49 -06:00
|
|
|
if err = repo_module.UpdateRepoSize(ctx, repo); err != nil {
|
2020-09-11 08:14:48 -06:00
|
|
|
log.Error("Failed to update size for repository: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addTags := make([]string, 0, len(optsList))
|
|
|
|
delTags := make([]string, 0, len(optsList))
|
2021-11-24 02:49:20 -07:00
|
|
|
var pusher *user_model.User
|
2020-09-11 08:14:48 -06:00
|
|
|
|
|
|
|
for _, opts := range optsList {
|
|
|
|
if opts.IsNewRef() && opts.IsDelRef() {
|
2022-01-19 16:26:57 -07:00
|
|
|
return fmt.Errorf("old and new revisions are both %s", git.EmptySHA)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
2020-12-16 05:41:21 -07:00
|
|
|
if opts.IsTag() { // If is tag reference
|
2020-12-11 10:27:00 -07:00
|
|
|
if pusher == nil || pusher.ID != opts.PusherID {
|
|
|
|
var err error
|
2021-11-24 02:49:20 -07:00
|
|
|
if pusher, err = user_model.GetUserByID(opts.PusherID); err != nil {
|
2020-12-11 10:27:00 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 08:14:48 -06:00
|
|
|
tagName := opts.TagName()
|
|
|
|
if opts.IsDelRef() {
|
2020-12-16 05:41:21 -07:00
|
|
|
notification.NotifyPushCommits(
|
|
|
|
pusher, repo,
|
|
|
|
&repo_module.PushUpdateOptions{
|
|
|
|
RefFullName: git.TagPrefix + tagName,
|
|
|
|
OldCommitID: opts.OldCommitID,
|
|
|
|
NewCommitID: git.EmptySHA,
|
|
|
|
}, repo_module.NewPushCommits())
|
|
|
|
|
2020-09-11 08:14:48 -06:00
|
|
|
delTags = append(delTags, tagName)
|
2020-12-07 19:23:18 -07:00
|
|
|
notification.NotifyDeleteRef(pusher, repo, "tag", opts.RefFullName)
|
2020-09-11 08:14:48 -06:00
|
|
|
} else { // is new tag
|
2021-12-29 04:40:57 -07:00
|
|
|
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("gitRepo.GetCommit: %w", err)
|
2021-12-29 04:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
commits := repo_module.NewPushCommits()
|
|
|
|
commits.HeadCommit = repo_module.CommitToPushCommit(newCommit)
|
|
|
|
commits.CompareURL = repo.ComposeCompareURL(git.EmptySHA, opts.NewCommitID)
|
|
|
|
|
2020-12-16 05:41:21 -07:00
|
|
|
notification.NotifyPushCommits(
|
|
|
|
pusher, repo,
|
|
|
|
&repo_module.PushUpdateOptions{
|
|
|
|
RefFullName: git.TagPrefix + tagName,
|
|
|
|
OldCommitID: git.EmptySHA,
|
|
|
|
NewCommitID: opts.NewCommitID,
|
2021-12-29 04:40:57 -07:00
|
|
|
}, commits)
|
2020-12-16 05:41:21 -07:00
|
|
|
|
2020-09-11 08:14:48 -06:00
|
|
|
addTags = append(addTags, tagName)
|
2022-01-19 16:26:57 -07:00
|
|
|
notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName, opts.NewCommitID)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
} else if opts.IsBranch() { // If is branch reference
|
|
|
|
if pusher == nil || pusher.ID != opts.PusherID {
|
|
|
|
var err error
|
2021-11-24 02:49:20 -07:00
|
|
|
if pusher, err = user_model.GetUserByID(opts.PusherID); err != nil {
|
2020-09-11 08:14:48 -06:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
branch := opts.BranchName()
|
|
|
|
if !opts.IsDelRef() {
|
2020-10-08 16:17:23 -06:00
|
|
|
log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
|
|
|
|
go pull_service.AddTestPullRequestTask(pusher, repo.ID, branch, true, opts.OldCommitID, opts.NewCommitID)
|
2020-09-11 08:14:48 -06:00
|
|
|
|
|
|
|
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("gitRepo.GetCommit: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
2020-12-07 19:23:18 -07:00
|
|
|
refName := opts.RefName()
|
|
|
|
|
2020-09-11 08:14:48 -06:00
|
|
|
// Push new branch.
|
2021-08-09 12:08:51 -06:00
|
|
|
var l []*git.Commit
|
2020-09-11 08:14:48 -06:00
|
|
|
if opts.IsNewRef() {
|
2020-12-07 19:23:18 -07:00
|
|
|
if repo.IsEmpty { // Change default branch and empty status only if pushed ref is non-empty branch.
|
|
|
|
repo.DefaultBranch = refName
|
|
|
|
repo.IsEmpty = false
|
|
|
|
if repo.DefaultBranch != setting.Repository.DefaultBranch {
|
|
|
|
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
|
|
|
|
if !git.IsErrUnsupportedVersion(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Update the is empty and default_branch columns
|
2022-05-20 08:08:52 -06:00
|
|
|
if err := repo_model.UpdateRepositoryCols(db.DefaultContext, repo, "default_branch", "is_empty"); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("UpdateRepositoryCols: %w", err)
|
2020-12-07 19:23:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:14:48 -06:00
|
|
|
l, err = newCommit.CommitsBeforeLimit(10)
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("newCommit.CommitsBeforeLimit: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
2022-01-19 16:26:57 -07:00
|
|
|
notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName, opts.NewCommitID)
|
2020-09-11 08:14:48 -06:00
|
|
|
} else {
|
|
|
|
l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("newCommit.CommitsBeforeUntil: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
2020-10-08 16:17:23 -06:00
|
|
|
|
2022-01-19 16:26:57 -07:00
|
|
|
isForce, err := repo_module.IsForcePush(ctx, opts)
|
2020-10-08 16:17:23 -06:00
|
|
|
if err != nil {
|
2020-10-31 09:42:54 -06:00
|
|
|
log.Error("isForcePush %s:%s failed: %v", repo.FullName(), branch, err)
|
2020-10-08 16:17:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if isForce {
|
|
|
|
log.Trace("Push %s is a force push", opts.NewCommitID)
|
|
|
|
|
|
|
|
cache.Remove(repo.GetCommitsCountCacheKey(opts.RefName(), true))
|
|
|
|
} else {
|
|
|
|
// TODO: increment update the commit count cache but not remove
|
|
|
|
cache.Remove(repo.GetCommitsCountCacheKey(opts.RefName(), true))
|
|
|
|
}
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
2021-08-09 12:08:51 -06:00
|
|
|
commits := repo_module.GitToPushCommits(l)
|
2021-06-29 07:34:03 -06:00
|
|
|
commits.HeadCommit = repo_module.CommitToPushCommit(newCommit)
|
2021-06-26 03:13:51 -06:00
|
|
|
|
2021-11-21 02:11:48 -07:00
|
|
|
if err := issue_service.UpdateIssuesCommit(pusher, repo, commits.Commits, refName); err != nil {
|
2021-06-26 03:13:51 -06:00
|
|
|
log.Error("updateIssuesCommit: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-03-23 07:40:12 -06:00
|
|
|
oldCommitID := opts.OldCommitID
|
|
|
|
if oldCommitID == git.EmptySHA && len(commits.Commits) > 0 {
|
|
|
|
oldCommit, err := gitRepo.GetCommit(commits.Commits[len(commits.Commits)-1].Sha1)
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
log.Error("unable to GetCommit %s from %-v: %v", oldCommitID, repo, err)
|
|
|
|
}
|
|
|
|
if oldCommit != nil {
|
|
|
|
for i := 0; i < oldCommit.ParentCount(); i++ {
|
|
|
|
commitID, _ := oldCommit.ParentID(i)
|
|
|
|
if !commitID.IsZero() {
|
|
|
|
oldCommitID = commitID.String()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldCommitID == git.EmptySHA && repo.DefaultBranch != branch {
|
|
|
|
oldCommitID = repo.DefaultBranch
|
|
|
|
}
|
|
|
|
|
|
|
|
if oldCommitID != git.EmptySHA {
|
|
|
|
commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID)
|
|
|
|
} else {
|
|
|
|
commits.CompareURL = ""
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:58:20 -06:00
|
|
|
if len(commits.Commits) > setting.UI.FeedMaxCommitNum {
|
|
|
|
commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:23:18 -07:00
|
|
|
notification.NotifyPushCommits(pusher, repo, opts, commits)
|
|
|
|
|
2022-06-12 09:51:54 -06:00
|
|
|
if err = git_model.RemoveDeletedBranchByName(repo.ID, branch); err != nil {
|
2020-09-11 08:14:48 -06:00
|
|
|
log.Error("models.RemoveDeletedBranch %s/%s failed: %v", repo.ID, branch, err)
|
|
|
|
}
|
|
|
|
|
2020-10-08 16:17:23 -06:00
|
|
|
// Cache for big repository
|
2021-11-17 08:17:31 -07:00
|
|
|
if err := CacheRef(graceful.GetManager().HammerContext(), repo, gitRepo, opts.RefFullName); err != nil {
|
2020-10-08 16:17:23 -06:00
|
|
|
log.Error("repo_module.CacheRef %s/%s failed: %v", repo.ID, branch, err)
|
|
|
|
}
|
2020-12-07 19:23:18 -07:00
|
|
|
} else {
|
|
|
|
notification.NotifyDeleteRef(pusher, repo, "branch", opts.RefFullName)
|
|
|
|
if err = pull_service.CloseBranchPulls(pusher, repo.ID, branch); err != nil {
|
|
|
|
// close all related pulls
|
|
|
|
log.Error("close related pull request failed: %v", err)
|
|
|
|
}
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even if user delete a branch on a repository which he didn't watch, he will be watch that.
|
2022-05-20 08:08:52 -06:00
|
|
|
if err = repo_model.WatchIfAuto(db.DefaultContext, opts.PusherID, repo.ID, true); err != nil {
|
2020-09-11 08:14:48 -06:00
|
|
|
log.Warn("Fail to perform auto watch on user %v for repo %v: %v", opts.PusherID, repo.ID, err)
|
|
|
|
}
|
2020-12-07 19:23:18 -07:00
|
|
|
} else {
|
|
|
|
log.Trace("Non-tag and non-branch commits pushed.")
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
}
|
2021-11-17 08:17:31 -07:00
|
|
|
if err := PushUpdateAddDeleteTags(repo, gitRepo, addTags, delTags); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("PushUpdateAddDeleteTags: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
2020-09-20 11:55:15 -06:00
|
|
|
// Change repository last updated time.
|
2021-12-12 08:48:20 -07:00
|
|
|
if err := repo_model.UpdateRepositoryUpdatedTime(repo.ID, time.Now()); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("UpdateRepositoryUpdatedTime: %w", err)
|
2020-09-11 08:14:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-11-17 08:17:31 -07:00
|
|
|
|
|
|
|
// PushUpdateAddDeleteTags updates a number of added and delete tags
|
2021-12-09 18:27:50 -07:00
|
|
|
func PushUpdateAddDeleteTags(repo *repo_model.Repository, gitRepo *git.Repository, addTags, delTags []string) error {
|
2021-11-17 08:17:31 -07:00
|
|
|
return db.WithTx(func(ctx context.Context) error {
|
2022-08-24 20:31:57 -06:00
|
|
|
if err := repo_model.PushUpdateDeleteTagsContext(ctx, repo, delTags); err != nil {
|
2021-11-17 08:17:31 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return pushUpdateAddTags(ctx, repo, gitRepo, addTags)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushUpdateAddTags updates a number of add tags
|
2021-12-09 18:27:50 -07:00
|
|
|
func pushUpdateAddTags(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, tags []string) error {
|
2021-11-17 08:17:31 -07:00
|
|
|
if len(tags) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerTags := make([]string, 0, len(tags))
|
|
|
|
for _, tag := range tags {
|
|
|
|
lowerTags = append(lowerTags, strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
releases, err := repo_model.GetReleasesByRepoIDAndNames(ctx, repo.ID, lowerTags)
|
2021-11-17 08:17:31 -07:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetReleasesByRepoIDAndNames: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
2022-08-24 20:31:57 -06:00
|
|
|
relMap := make(map[string]*repo_model.Release)
|
2021-11-17 08:17:31 -07:00
|
|
|
for _, rel := range releases {
|
|
|
|
relMap[rel.LowerTagName] = rel
|
|
|
|
}
|
|
|
|
|
2022-08-24 20:31:57 -06:00
|
|
|
newReleases := make([]*repo_model.Release, 0, len(lowerTags)-len(relMap))
|
2021-11-17 08:17:31 -07:00
|
|
|
|
2021-11-24 02:49:20 -07:00
|
|
|
emailToUser := make(map[string]*user_model.User)
|
2021-11-17 08:17:31 -07:00
|
|
|
|
|
|
|
for i, lowerTag := range lowerTags {
|
|
|
|
tag, err := gitRepo.GetTag(tags[i])
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetTag: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
2022-01-12 13:37:46 -07:00
|
|
|
commit, err := tag.Commit(gitRepo)
|
2021-11-17 08:17:31 -07:00
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("Commit: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sig := tag.Tagger
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Author
|
|
|
|
}
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Committer
|
|
|
|
}
|
2021-11-24 02:49:20 -07:00
|
|
|
var author *user_model.User
|
2022-01-20 10:46:10 -07:00
|
|
|
createdAt := time.Unix(1, 0)
|
2021-11-17 08:17:31 -07:00
|
|
|
|
|
|
|
if sig != nil {
|
|
|
|
var ok bool
|
|
|
|
author, ok = emailToUser[sig.Email]
|
|
|
|
if !ok {
|
2021-11-24 02:49:20 -07:00
|
|
|
author, err = user_model.GetUserByEmailContext(ctx, sig.Email)
|
|
|
|
if err != nil && !user_model.IsErrUserNotExist(err) {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("GetUserByEmail: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
if author != nil {
|
|
|
|
emailToUser[sig.Email] = author
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createdAt = sig.When
|
|
|
|
}
|
|
|
|
|
|
|
|
commitsCount, err := commit.CommitsCount()
|
|
|
|
if err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("CommitsCount: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rel, has := relMap[lowerTag]
|
|
|
|
|
|
|
|
if !has {
|
2022-08-24 20:31:57 -06:00
|
|
|
rel = &repo_model.Release{
|
2021-11-17 08:17:31 -07:00
|
|
|
RepoID: repo.ID,
|
|
|
|
Title: "",
|
|
|
|
TagName: tags[i],
|
|
|
|
LowerTagName: lowerTag,
|
|
|
|
Target: "",
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
NumCommits: commitsCount,
|
|
|
|
Note: "",
|
|
|
|
IsDraft: false,
|
|
|
|
IsPrerelease: false,
|
|
|
|
IsTag: true,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
|
|
|
|
}
|
|
|
|
if author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
newReleases = append(newReleases, rel)
|
|
|
|
} else {
|
|
|
|
rel.Sha1 = commit.ID.String()
|
|
|
|
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
|
|
|
|
rel.NumCommits = commitsCount
|
|
|
|
rel.IsDraft = false
|
|
|
|
if rel.IsTag && author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
2022-08-24 20:31:57 -06:00
|
|
|
if err = repo_model.UpdateRelease(ctx, rel); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("Update: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newReleases) > 0 {
|
2022-06-03 00:13:58 -06:00
|
|
|
if err = db.Insert(ctx, newReleases); err != nil {
|
2022-10-24 13:29:17 -06:00
|
|
|
return fmt.Errorf("Insert: %w", err)
|
2021-11-17 08:17:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|