Skip to content

Commit

Permalink
Move PushUpdate dependency from models to repofiles (go-gitea#6763)
Browse files Browse the repository at this point in the history
* remove push_update

* move models.PushUpdate to repofiles.PushUpdate
  • Loading branch information
lunny authored and jeffliu27 committed Jul 18, 2019
1 parent 132995e commit 4fc886f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
16 changes: 10 additions & 6 deletions modules/repofiles/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
)
Expand Down Expand Up @@ -424,11 +423,16 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
return file, nil
}

// PushUpdate push updates
func PushUpdate(repo *models.Repository, branch string, opt models.PushUpdateOptions) error {
if err := models.PushUpdate(branch, opt); err != nil {
return err
// PushUpdate must be called for any push actions in order to
// generates necessary push action history feeds and other operations
func PushUpdate(repo *models.Repository, branch string, opts models.PushUpdateOptions) error {
err := models.PushUpdate(branch, opts)
if err != nil {
return fmt.Errorf("PushUpdate: %v", err)
}

if opts.RefFullName == git.BranchPrefix+repo.DefaultBranch {
models.UpdateRepoIndexer(repo)
}
notification.NotifyPushCommits(repo, branch, opt)
return nil
}
11 changes: 10 additions & 1 deletion routers/private/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/repofiles"
"code.gitea.io/gitea/modules/util"

macaron "gopkg.in/macaron.v1"
Expand Down Expand Up @@ -117,7 +118,15 @@ func HookPostReceive(ctx *macaron.Context) {
// or other less-standard refs spaces are ignored since there
// may be a very large number of them).
if strings.HasPrefix(refFullName, git.BranchPrefix) || strings.HasPrefix(refFullName, git.TagPrefix) {
if err := models.PushUpdate(branch, models.PushUpdateOptions{
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
if err != nil {
log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"err": fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
})
return
}
if err := repofiles.PushUpdate(repo, branch, models.PushUpdateOptions{
RefFullName: refFullName,
OldCommitID: oldCommitID,
NewCommitID: newCommitID,
Expand Down
14 changes: 7 additions & 7 deletions routers/private/push_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ func PushUpdate(ctx *macaron.Context) {
return
}

branch := strings.TrimPrefix(opt.RefFullName, git.BranchPrefix)
if len(branch) == 0 || opt.PusherID <= 0 {
ctx.Error(404)
log.Trace("PushUpdate: branch or secret is empty, or pusher ID is not valid")
return
}

repo, err := models.GetRepositoryByOwnerAndName(opt.RepoUserName, opt.RepoName)
if err != nil {
ctx.JSON(500, map[string]interface{}{
Expand All @@ -34,13 +41,6 @@ func PushUpdate(ctx *macaron.Context) {
return
}

branch := strings.TrimPrefix(opt.RefFullName, git.BranchPrefix)
if len(branch) == 0 || opt.PusherID <= 0 {
ctx.Error(404)
log.Trace("PushUpdate: branch or secret is empty, or pusher ID is not valid")
return
}

err = repofiles.PushUpdate(repo, branch, opt)
if err != nil {
if models.IsErrUserNotExist(err) {
Expand Down

0 comments on commit 4fc886f

Please sign in to comment.