Skip to content

Commit 7907786

Browse files
ethantkoeniglunny
authored andcommitted
Trigger sync webhooks on UI commit (#2302)
* Trigger sync webhooks on UI commit * Also fix UI upload/delete
1 parent 951fb57 commit 7907786

File tree

3 files changed

+80
-61
lines changed

3 files changed

+80
-61
lines changed

models/repo_editor.go

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,29 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
155155
}
156156

157157
// Simulate push event.
158-
pushCommits := &PushCommits{
159-
Len: 1,
160-
Commits: []*PushCommit{CommitToPushCommit(commit)},
161-
}
162158
oldCommitID := opts.LastCommitID
163159
if opts.NewBranch != opts.OldBranch {
164160
oldCommitID = git.EmptySHA
165161
}
166-
if err := CommitRepoAction(CommitRepoActionOptions{
167-
PusherName: doer.Name,
168-
RepoOwnerID: repo.MustOwner().ID,
169-
RepoName: repo.Name,
170-
RefFullName: git.BranchPrefix + opts.NewBranch,
171-
OldCommitID: oldCommitID,
172-
NewCommitID: commit.ID.String(),
173-
Commits: pushCommits,
174-
}); err != nil {
175-
log.Error(4, "CommitRepoAction: %v", err)
176-
return nil
177-
}
178162

163+
if err = repo.GetOwner(); err != nil {
164+
return fmt.Errorf("GetOwner: %v", err)
165+
}
166+
err = PushUpdate(
167+
opts.NewBranch,
168+
PushUpdateOptions{
169+
PusherID: doer.ID,
170+
PusherName: doer.Name,
171+
RepoUserName: repo.Owner.Name,
172+
RepoName: repo.Name,
173+
RefFullName: git.BranchPrefix + opts.NewBranch,
174+
OldCommitID: oldCommitID,
175+
NewCommitID: commit.ID.String(),
176+
},
177+
)
178+
if err != nil {
179+
return fmt.Errorf("PushUpdate: %v", err)
180+
}
179181
return nil
180182
}
181183

@@ -295,23 +297,29 @@ func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (
295297
}
296298

297299
// Simulate push event.
298-
pushCommits := &PushCommits{
299-
Len: 1,
300-
Commits: []*PushCommit{CommitToPushCommit(commit)},
301-
}
302-
if err := CommitRepoAction(CommitRepoActionOptions{
303-
PusherName: doer.Name,
304-
RepoOwnerID: repo.MustOwner().ID,
305-
RepoName: repo.Name,
306-
RefFullName: git.BranchPrefix + opts.NewBranch,
307-
OldCommitID: opts.LastCommitID,
308-
NewCommitID: commit.ID.String(),
309-
Commits: pushCommits,
310-
}); err != nil {
311-
log.Error(4, "CommitRepoAction: %v", err)
312-
return nil
300+
oldCommitID := opts.LastCommitID
301+
if opts.NewBranch != opts.OldBranch {
302+
oldCommitID = git.EmptySHA
313303
}
314304

305+
if err = repo.GetOwner(); err != nil {
306+
return fmt.Errorf("GetOwner: %v", err)
307+
}
308+
err = PushUpdate(
309+
opts.NewBranch,
310+
PushUpdateOptions{
311+
PusherID: doer.ID,
312+
PusherName: doer.Name,
313+
RepoUserName: repo.Owner.Name,
314+
RepoName: repo.Name,
315+
RefFullName: git.BranchPrefix + opts.NewBranch,
316+
OldCommitID: oldCommitID,
317+
NewCommitID: commit.ID.String(),
318+
},
319+
)
320+
if err != nil {
321+
return fmt.Errorf("PushUpdate: %v", err)
322+
}
315323
return nil
316324
}
317325

@@ -534,21 +542,28 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
534542
}
535543

536544
// Simulate push event.
537-
pushCommits := &PushCommits{
538-
Len: 1,
539-
Commits: []*PushCommit{CommitToPushCommit(commit)},
540-
}
541-
if err := CommitRepoAction(CommitRepoActionOptions{
542-
PusherName: doer.Name,
543-
RepoOwnerID: repo.MustOwner().ID,
544-
RepoName: repo.Name,
545-
RefFullName: git.BranchPrefix + opts.NewBranch,
546-
OldCommitID: opts.LastCommitID,
547-
NewCommitID: commit.ID.String(),
548-
Commits: pushCommits,
549-
}); err != nil {
550-
log.Error(4, "CommitRepoAction: %v", err)
551-
return nil
545+
oldCommitID := opts.LastCommitID
546+
if opts.NewBranch != opts.OldBranch {
547+
oldCommitID = git.EmptySHA
548+
}
549+
550+
if err = repo.GetOwner(); err != nil {
551+
return fmt.Errorf("GetOwner: %v", err)
552+
}
553+
err = PushUpdate(
554+
opts.NewBranch,
555+
PushUpdateOptions{
556+
PusherID: doer.ID,
557+
PusherName: doer.Name,
558+
RepoUserName: repo.Owner.Name,
559+
RepoName: repo.Name,
560+
RefFullName: git.BranchPrefix + opts.NewBranch,
561+
OldCommitID: oldCommitID,
562+
NewCommitID: commit.ID.String(),
563+
},
564+
)
565+
if err != nil {
566+
return fmt.Errorf("PushUpdate: %v", err)
552567
}
553568

554569
return DeleteUploads(uploads...)

models/update.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,24 @@ type PushUpdateOptions struct {
6464

6565
// PushUpdate must be called for any push actions in order to
6666
// generates necessary push action history feeds.
67-
func PushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
67+
func PushUpdate(branch string, opt PushUpdateOptions) error {
68+
repo, err := pushUpdate(opt)
69+
if err != nil {
70+
return err
71+
}
72+
73+
pusher, err := GetUserByID(opt.PusherID)
74+
if err != nil {
75+
return err
76+
}
77+
78+
log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
79+
80+
go AddTestPullRequestTask(pusher, repo.ID, branch, true)
81+
return nil
82+
}
83+
84+
func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
6885
isNewRef := opts.OldCommitID == git.EmptySHA
6986
isDelRef := opts.NewCommitID == git.EmptySHA
7087
if isNewRef && isDelRef {

routers/private/push_update.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,7 @@ func PushUpdate(ctx *macaron.Context) {
3232
return
3333
}
3434

35-
repo, err := models.PushUpdate(opt)
36-
if err != nil {
37-
ctx.JSON(500, map[string]interface{}{
38-
"err": err.Error(),
39-
})
40-
return
41-
}
42-
43-
pusher, err := models.GetUserByID(opt.PusherID)
35+
err := models.PushUpdate(branch, opt)
4436
if err != nil {
4537
if models.IsErrUserNotExist(err) {
4638
ctx.Error(404)
@@ -51,10 +43,5 @@ func PushUpdate(ctx *macaron.Context) {
5143
}
5244
return
5345
}
54-
55-
log.Trace("TriggerTask '%s/%s' by %s", repo.Name, branch, pusher.Name)
56-
57-
go models.HookQueue.Add(repo.ID)
58-
go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
5946
ctx.Status(202)
6047
}

0 commit comments

Comments
 (0)