Browse Source

Trigger sync webhooks on UI commit (#2302)

* Trigger sync webhooks on UI commit

* Also fix UI upload/delete
Ethan Koenig 2 years ago
parent
commit
7907786040
3 changed files with 80 additions and 61 deletions
  1. 61 46
      models/repo_editor.go
  2. 18 1
      models/update.go
  3. 1 14
      routers/private/push_update.go

+ 61 - 46
models/repo_editor.go

@@ -155,27 +155,29 @@ func (repo *Repository) UpdateRepoFile(doer *User, opts UpdateRepoFileOptions) (
155 155
 	}
156 156
 
157 157
 	// Simulate push event.
158
-	pushCommits := &PushCommits{
159
-		Len:     1,
160
-		Commits: []*PushCommit{CommitToPushCommit(commit)},
161
-	}
162 158
 	oldCommitID := opts.LastCommitID
163 159
 	if opts.NewBranch != opts.OldBranch {
164 160
 		oldCommitID = git.EmptySHA
165 161
 	}
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
-	}
178 162
 
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
+	}
179 181
 	return nil
180 182
 }
181 183
 
@@ -295,23 +297,29 @@ func (repo *Repository) DeleteRepoFile(doer *User, opts DeleteRepoFileOptions) (
295 297
 	}
296 298
 
297 299
 	// 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
313 303
 	}
314 304
 
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
+	}
315 323
 	return nil
316 324
 }
317 325
 
@@ -534,21 +542,28 @@ func (repo *Repository) UploadRepoFiles(doer *User, opts UploadRepoFileOptions)
534 542
 	}
535 543
 
536 544
 	// 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)
552 567
 	}
553 568
 
554 569
 	return DeleteUploads(uploads...)

+ 18 - 1
models/update.go

@@ -64,7 +64,24 @@ type PushUpdateOptions struct {
64 64
 
65 65
 // PushUpdate must be called for any push actions in order to
66 66
 // 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) {
68 85
 	isNewRef := opts.OldCommitID == git.EmptySHA
69 86
 	isDelRef := opts.NewCommitID == git.EmptySHA
70 87
 	if isNewRef && isDelRef {

+ 1 - 14
routers/private/push_update.go

@@ -32,15 +32,7 @@ func PushUpdate(ctx *macaron.Context) {
32 32
 		return
33 33
 	}
34 34
 
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)
44 36
 	if err != nil {
45 37
 		if models.IsErrUserNotExist(err) {
46 38
 			ctx.Error(404)
@@ -51,10 +43,5 @@ func PushUpdate(ctx *macaron.Context) {
51 43
 		}
52 44
 		return
53 45
 	}
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)
59 46
 	ctx.Status(202)
60 47
 }