aboutsummaryrefslogtreecommitdiffstats
path: root/modules/repofiles/action.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2019-12-26 11:29:45 +0000
committerLauris BH <lauris@nix.lv>2019-12-26 13:29:45 +0200
commit7bfb83e0642530183cc15f3c9208d95f88fdc79a (patch)
tree7e3670ea97ac6e9304a8df5488fed6839d1f3dc4 /modules/repofiles/action.go
parent114d474f02f8e4148b9fd65c8f2bc7b47f924c17 (diff)
downloadgitea-7bfb83e0642530183cc15f3c9208d95f88fdc79a.tar.gz
gitea-7bfb83e0642530183cc15f3c9208d95f88fdc79a.zip
Batch hook pre- and post-receive calls (#8602)
* make notifyWatchers work on multiple actions * more efficient multiple notifyWatchers * Make CommitRepoAction take advantage of multiple actions * Batch post and pre-receive results * Set batch to 30 * Auto adjust timeout & add logging * adjust processing message * Add some messages to pre-receive * Make any non-200 status code from pre-receive an error * Add missing hookPrintResults * Remove shortcut for single action * mistaken merge fix * oops * Move master branch to the front * If repo was empty and the master branch is pushed ensure that that is set as the default branch * fixup * fixup * Missed HookOptions in setdefaultbranch * Batch PushUpdateAddTag and PushUpdateDelTag Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/repofiles/action.go')
-rw-r--r--modules/repofiles/action.go188
1 files changed, 104 insertions, 84 deletions
diff --git a/modules/repofiles/action.go b/modules/repofiles/action.go
index a5a5e151cb..d207247114 100644
--- a/modules/repofiles/action.go
+++ b/modules/repofiles/action.go
@@ -159,112 +159,132 @@ type CommitRepoActionOptions struct {
// CommitRepoAction adds new commit action to the repository, and prepare
// corresponding webhooks.
-func CommitRepoAction(opts CommitRepoActionOptions) error {
- pusher, err := models.GetUserByName(opts.PusherName)
- if err != nil {
- return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
- }
-
- repo, err := models.GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
- if err != nil {
- return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
- }
-
- refName := git.RefEndName(opts.RefFullName)
+func CommitRepoAction(optsList ...*CommitRepoActionOptions) error {
+ var pusher *models.User
+ var repo *models.Repository
+ actions := make([]*models.Action, len(optsList))
+
+ for i, opts := range optsList {
+ if pusher == nil || pusher.Name != opts.PusherName {
+ var err error
+ pusher, err = models.GetUserByName(opts.PusherName)
+ if err != nil {
+ return fmt.Errorf("GetUserByName [%s]: %v", opts.PusherName, err)
+ }
+ }
- // Change default branch and empty status only if pushed ref is non-empty branch.
- if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
- repo.DefaultBranch = refName
- repo.IsEmpty = false
- if refName != "master" {
- gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if repo == nil || repo.OwnerID != opts.RepoOwnerID || repo.Name != opts.RepoName {
+ var err error
+ if repo != nil {
+ // Change repository empty status and update last updated time.
+ if err := models.UpdateRepository(repo, false); err != nil {
+ return fmt.Errorf("UpdateRepository: %v", err)
+ }
+ }
+ repo, err = models.GetRepositoryByName(opts.RepoOwnerID, opts.RepoName)
if err != nil {
- return err
+ return fmt.Errorf("GetRepositoryByName [owner_id: %d, name: %s]: %v", opts.RepoOwnerID, opts.RepoName, err)
}
- if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
- if !git.IsErrUnsupportedVersion(err) {
- gitRepo.Close()
+ }
+ refName := git.RefEndName(opts.RefFullName)
+
+ // Change default branch and empty status only if pushed ref is non-empty branch.
+ if repo.IsEmpty && opts.NewCommitID != git.EmptySHA && strings.HasPrefix(opts.RefFullName, git.BranchPrefix) {
+ repo.DefaultBranch = refName
+ repo.IsEmpty = false
+ if refName != "master" {
+ gitRepo, err := git.OpenRepository(repo.RepoPath())
+ if err != nil {
return err
}
+ if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
+ if !git.IsErrUnsupportedVersion(err) {
+ gitRepo.Close()
+ return err
+ }
+ }
+ gitRepo.Close()
}
- gitRepo.Close()
}
- }
- // Change repository empty status and update last updated time.
- if err = models.UpdateRepository(repo, false); err != nil {
- return fmt.Errorf("UpdateRepository: %v", err)
- }
+ isNewBranch := false
+ opType := models.ActionCommitRepo
- isNewBranch := false
- opType := models.ActionCommitRepo
- // Check it's tag push or branch.
- if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
- opType = models.ActionPushTag
- if opts.NewCommitID == git.EmptySHA {
- opType = models.ActionDeleteTag
- }
- opts.Commits = &models.PushCommits{}
- } else if opts.NewCommitID == git.EmptySHA {
- opType = models.ActionDeleteBranch
- opts.Commits = &models.PushCommits{}
- } else {
- // if not the first commit, set the compare URL.
- if opts.OldCommitID == git.EmptySHA {
- isNewBranch = true
+ // Check it's tag push or branch.
+ if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
+ opType = models.ActionPushTag
+ if opts.NewCommitID == git.EmptySHA {
+ opType = models.ActionDeleteTag
+ }
+ opts.Commits = &models.PushCommits{}
+ } else if opts.NewCommitID == git.EmptySHA {
+ opType = models.ActionDeleteBranch
+ opts.Commits = &models.PushCommits{}
} else {
- opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
- }
+ // if not the first commit, set the compare URL.
+ if opts.OldCommitID == git.EmptySHA {
+ isNewBranch = true
+ } else {
+ opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
+ }
- if err = UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
- log.Error("updateIssuesCommit: %v", err)
+ if err := UpdateIssuesCommit(pusher, repo, opts.Commits.Commits, refName); err != nil {
+ log.Error("updateIssuesCommit: %v", err)
+ }
}
- }
- if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
- opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
- }
-
- data, err := json.Marshal(opts.Commits)
- if err != nil {
- return fmt.Errorf("Marshal: %v", err)
- }
+ if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
+ opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
+ }
- if err = models.NotifyWatchers(&models.Action{
- ActUserID: pusher.ID,
- ActUser: pusher,
- OpType: opType,
- Content: string(data),
- RepoID: repo.ID,
- Repo: repo,
- RefName: refName,
- IsPrivate: repo.IsPrivate,
- }); err != nil {
- return fmt.Errorf("NotifyWatchers: %v", err)
- }
+ data, err := json.Marshal(opts.Commits)
+ if err != nil {
+ return fmt.Errorf("Marshal: %v", err)
+ }
- var isHookEventPush = true
- switch opType {
- case models.ActionCommitRepo: // Push
- if isNewBranch {
- notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
+ actions[i] = &models.Action{
+ ActUserID: pusher.ID,
+ ActUser: pusher,
+ OpType: opType,
+ Content: string(data),
+ RepoID: repo.ID,
+ Repo: repo,
+ RefName: refName,
+ IsPrivate: repo.IsPrivate,
}
- case models.ActionDeleteBranch: // Delete Branch
- notification.NotifyDeleteRef(pusher, repo, "branch", opts.RefFullName)
+ var isHookEventPush = true
+ switch opType {
+ case models.ActionCommitRepo: // Push
+ if isNewBranch {
+ notification.NotifyCreateRef(pusher, repo, "branch", opts.RefFullName)
+ }
+ case models.ActionDeleteBranch: // Delete Branch
+ notification.NotifyDeleteRef(pusher, repo, "branch", opts.RefFullName)
+
+ case models.ActionPushTag: // Create
+ notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName)
- case models.ActionPushTag: // Create
- notification.NotifyCreateRef(pusher, repo, "tag", opts.RefFullName)
+ case models.ActionDeleteTag: // Delete Tag
+ notification.NotifyDeleteRef(pusher, repo, "tag", opts.RefFullName)
+ default:
+ isHookEventPush = false
+ }
- case models.ActionDeleteTag: // Delete Tag
- notification.NotifyDeleteRef(pusher, repo, "tag", opts.RefFullName)
- default:
- isHookEventPush = false
+ if isHookEventPush {
+ notification.NotifyPushCommits(pusher, repo, opts.RefFullName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
+ }
}
- if isHookEventPush {
- notification.NotifyPushCommits(pusher, repo, opts.RefFullName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
+ if repo != nil {
+ // Change repository empty status and update last updated time.
+ if err := models.UpdateRepository(repo, false); err != nil {
+ return fmt.Errorf("UpdateRepository: %v", err)
+ }
}
+ if err := models.NotifyWatchers(actions...); err != nil {
+ return fmt.Errorf("NotifyWatchers: %v", err)
+ }
return nil
}