@@ -159,112 +159,132 @@ type CommitRepoActionOptions struct {
159
159
160
160
// CommitRepoAction adds new commit action to the repository, and prepare
161
161
// corresponding webhooks.
162
- func CommitRepoAction (opts CommitRepoActionOptions ) error {
163
- pusher , err := models .GetUserByName (opts .PusherName )
164
- if err != nil {
165
- return fmt .Errorf ("GetUserByName [%s]: %v" , opts .PusherName , err )
166
- }
167
-
168
- repo , err := models .GetRepositoryByName (opts .RepoOwnerID , opts .RepoName )
169
- if err != nil {
170
- return fmt .Errorf ("GetRepositoryByName [owner_id: %d, name: %s]: %v" , opts .RepoOwnerID , opts .RepoName , err )
171
- }
172
-
173
- refName := git .RefEndName (opts .RefFullName )
174
-
175
- // Change default branch and empty status only if pushed ref is non-empty branch.
176
- if repo .IsEmpty && opts .NewCommitID != git .EmptySHA && strings .HasPrefix (opts .RefFullName , git .BranchPrefix ) {
177
- repo .DefaultBranch = refName
178
- repo .IsEmpty = false
179
- if refName != "master" {
180
- gitRepo , err := git .OpenRepository (repo .RepoPath ())
162
+ func CommitRepoAction (optsList ... CommitRepoActionOptions ) error {
163
+ var pusher * models.User
164
+ var repo * models.Repository
165
+ actions := make ([]* models.Action , len (optsList ))
166
+
167
+ for i , opts := range optsList {
168
+ if pusher == nil || pusher .Name != opts .PusherName {
169
+ var err error
170
+ pusher , err = models .GetUserByName (opts .PusherName )
181
171
if err != nil {
182
- return err
172
+ return fmt . Errorf ( "GetUserByName [%s]: %v" , opts . PusherName , err )
183
173
}
184
- if err := gitRepo .SetDefaultBranch (repo .DefaultBranch ); err != nil {
185
- if ! git .IsErrUnsupportedVersion (err ) {
186
- gitRepo .Close ()
174
+ }
175
+
176
+ if repo == nil || repo .OwnerID != opts .RepoOwnerID || repo .Name != opts .RepoName {
177
+ var err error
178
+ if repo != nil {
179
+ // Change repository empty status and update last updated time.
180
+ if err := models .UpdateRepository (repo , false ); err != nil {
181
+ return fmt .Errorf ("UpdateRepository: %v" , err )
182
+ }
183
+ repo , err = models .GetRepositoryByName (opts .RepoOwnerID , opts .RepoName )
184
+ if err != nil {
185
+ return fmt .Errorf ("GetRepositoryByName [owner_id: %d, name: %s]: %v" , opts .RepoOwnerID , opts .RepoName , err )
186
+ }
187
+ }
188
+ }
189
+ refName := git .RefEndName (opts .RefFullName )
190
+
191
+ // Change default branch and empty status only if pushed ref is non-empty branch.
192
+ if repo .IsEmpty && opts .NewCommitID != git .EmptySHA && strings .HasPrefix (opts .RefFullName , git .BranchPrefix ) {
193
+ repo .DefaultBranch = refName
194
+ repo .IsEmpty = false
195
+ if refName != "master" {
196
+ gitRepo , err := git .OpenRepository (repo .RepoPath ())
197
+ if err != nil {
187
198
return err
188
199
}
200
+ if err := gitRepo .SetDefaultBranch (repo .DefaultBranch ); err != nil {
201
+ if ! git .IsErrUnsupportedVersion (err ) {
202
+ gitRepo .Close ()
203
+ return err
204
+ }
205
+ }
206
+ gitRepo .Close ()
189
207
}
190
- gitRepo .Close ()
191
208
}
192
- }
193
209
194
- // Change repository empty status and update last updated time.
195
- if err = models .UpdateRepository (repo , false ); err != nil {
196
- return fmt .Errorf ("UpdateRepository: %v" , err )
197
- }
210
+ isNewBranch := false
211
+ opType := models .ActionCommitRepo
198
212
199
- isNewBranch := false
200
- opType := models .ActionCommitRepo
201
- // Check it's tag push or branch.
202
- if strings .HasPrefix (opts .RefFullName , git .TagPrefix ) {
203
- opType = models .ActionPushTag
204
- if opts .NewCommitID == git .EmptySHA {
205
- opType = models .ActionDeleteTag
206
- }
207
- opts .Commits = & models.PushCommits {}
208
- } else if opts .NewCommitID == git .EmptySHA {
209
- opType = models .ActionDeleteBranch
210
- opts .Commits = & models.PushCommits {}
211
- } else {
212
- // if not the first commit, set the compare URL.
213
- if opts .OldCommitID == git .EmptySHA {
214
- isNewBranch = true
213
+ // Check it's tag push or branch.
214
+ if strings .HasPrefix (opts .RefFullName , git .TagPrefix ) {
215
+ opType = models .ActionPushTag
216
+ if opts .NewCommitID == git .EmptySHA {
217
+ opType = models .ActionDeleteTag
218
+ }
219
+ opts .Commits = & models.PushCommits {}
220
+ } else if opts .NewCommitID == git .EmptySHA {
221
+ opType = models .ActionDeleteBranch
222
+ opts .Commits = & models.PushCommits {}
215
223
} else {
216
- opts .Commits .CompareURL = repo .ComposeCompareURL (opts .OldCommitID , opts .NewCommitID )
217
- }
224
+ // if not the first commit, set the compare URL.
225
+ if opts .OldCommitID == git .EmptySHA {
226
+ isNewBranch = true
227
+ } else {
228
+ opts .Commits .CompareURL = repo .ComposeCompareURL (opts .OldCommitID , opts .NewCommitID )
229
+ }
218
230
219
- if err = UpdateIssuesCommit (pusher , repo , opts .Commits .Commits , refName ); err != nil {
220
- log .Error ("updateIssuesCommit: %v" , err )
231
+ if err := UpdateIssuesCommit (pusher , repo , opts .Commits .Commits , refName ); err != nil {
232
+ log .Error ("updateIssuesCommit: %v" , err )
233
+ }
221
234
}
222
- }
223
235
224
- if len (opts .Commits .Commits ) > setting .UI .FeedMaxCommitNum {
225
- opts .Commits .Commits = opts .Commits .Commits [:setting .UI .FeedMaxCommitNum ]
226
- }
227
-
228
- data , err := json .Marshal (opts .Commits )
229
- if err != nil {
230
- return fmt .Errorf ("Marshal: %v" , err )
231
- }
236
+ if len (opts .Commits .Commits ) > setting .UI .FeedMaxCommitNum {
237
+ opts .Commits .Commits = opts .Commits .Commits [:setting .UI .FeedMaxCommitNum ]
238
+ }
232
239
233
- if err = models .NotifyWatchers (& models.Action {
234
- ActUserID : pusher .ID ,
235
- ActUser : pusher ,
236
- OpType : opType ,
237
- Content : string (data ),
238
- RepoID : repo .ID ,
239
- Repo : repo ,
240
- RefName : refName ,
241
- IsPrivate : repo .IsPrivate ,
242
- }); err != nil {
243
- return fmt .Errorf ("NotifyWatchers: %v" , err )
244
- }
240
+ data , err := json .Marshal (opts .Commits )
241
+ if err != nil {
242
+ return fmt .Errorf ("Marshal: %v" , err )
243
+ }
245
244
246
- var isHookEventPush = true
247
- switch opType {
248
- case models .ActionCommitRepo : // Push
249
- if isNewBranch {
250
- notification .NotifyCreateRef (pusher , repo , "branch" , opts .RefFullName )
245
+ actions [i ] = & models.Action {
246
+ ActUserID : pusher .ID ,
247
+ ActUser : pusher ,
248
+ OpType : opType ,
249
+ Content : string (data ),
250
+ RepoID : repo .ID ,
251
+ Repo : repo ,
252
+ RefName : refName ,
253
+ IsPrivate : repo .IsPrivate ,
251
254
}
252
255
253
- case models .ActionDeleteBranch : // Delete Branch
254
- notification .NotifyDeleteRef (pusher , repo , "branch" , opts .RefFullName )
256
+ var isHookEventPush = true
257
+ switch opType {
258
+ case models .ActionCommitRepo : // Push
259
+ if isNewBranch {
260
+ notification .NotifyCreateRef (pusher , repo , "branch" , opts .RefFullName )
261
+ }
262
+ case models .ActionDeleteBranch : // Delete Branch
263
+ notification .NotifyDeleteRef (pusher , repo , "branch" , opts .RefFullName )
264
+
265
+ case models .ActionPushTag : // Create
266
+ notification .NotifyCreateRef (pusher , repo , "tag" , opts .RefFullName )
255
267
256
- case models .ActionPushTag : // Create
257
- notification .NotifyCreateRef (pusher , repo , "tag" , opts .RefFullName )
268
+ case models .ActionDeleteTag : // Delete Tag
269
+ notification .NotifyDeleteRef (pusher , repo , "tag" , opts .RefFullName )
270
+ default :
271
+ isHookEventPush = false
272
+ }
258
273
259
- case models .ActionDeleteTag : // Delete Tag
260
- notification .NotifyDeleteRef (pusher , repo , "tag" , opts .RefFullName )
261
- default :
262
- isHookEventPush = false
274
+ if isHookEventPush {
275
+ notification .NotifyPushCommits (pusher , repo , opts .RefFullName , opts .OldCommitID , opts .NewCommitID , opts .Commits )
276
+ }
263
277
}
264
278
265
- if isHookEventPush {
266
- notification .NotifyPushCommits (pusher , repo , opts .RefFullName , opts .OldCommitID , opts .NewCommitID , opts .Commits )
279
+ if repo != nil {
280
+ // Change repository empty status and update last updated time.
281
+ if err := models .UpdateRepository (repo , false ); err != nil {
282
+ return fmt .Errorf ("UpdateRepository: %v" , err )
283
+ }
267
284
}
268
285
286
+ if err := models .NotifyWatchers (actions ... ); err != nil {
287
+ return fmt .Errorf ("NotifyWatchers: %v" , err )
288
+ }
269
289
return nil
270
290
}
0 commit comments