@@ -124,17 +124,17 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
124
124
repo , err := models .GetRepositoryByOwnerAndName (ownerName , repoName )
125
125
if err != nil {
126
126
log .Error ("Unable to get repository: %s/%s Error: %v" , ownerName , repoName , err )
127
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
128
- "err" : err .Error (),
127
+ ctx .JSON (http .StatusInternalServerError , private. Response {
128
+ Err : err .Error (),
129
129
})
130
130
return
131
131
}
132
132
repo .OwnerName = ownerName
133
133
gitRepo , err := git .OpenRepository (repo .RepoPath ())
134
134
if err != nil {
135
135
log .Error ("Unable to get git repository for: %s/%s Error: %v" , ownerName , repoName , err )
136
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
137
- "err" : err .Error (),
136
+ ctx .JSON (http .StatusInternalServerError , private. Response {
137
+ Err : err .Error (),
138
138
})
139
139
return
140
140
}
@@ -164,17 +164,17 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
164
164
branchName := strings .TrimPrefix (refFullName , git .BranchPrefix )
165
165
if branchName == repo .DefaultBranch && newCommitID == git .EmptySHA {
166
166
log .Warn ("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted" , branchName , repo )
167
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
168
- "err" : fmt .Sprintf ("branch %s is the default branch and cannot be deleted" , branchName ),
167
+ ctx .JSON (http .StatusForbidden , private. Response {
168
+ Err : fmt .Sprintf ("branch %s is the default branch and cannot be deleted" , branchName ),
169
169
})
170
170
return
171
171
}
172
172
173
173
protectBranch , err := models .GetProtectedBranchBy (repo .ID , branchName )
174
174
if err != nil {
175
175
log .Error ("Unable to get protected branch: %s in %-v Error: %v" , branchName , repo , err )
176
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
177
- "err" : err .Error (),
176
+ ctx .JSON (http .StatusInternalServerError , private. Response {
177
+ Err : err .Error (),
178
178
})
179
179
return
180
180
}
@@ -191,8 +191,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
191
191
// 1. Detect and prevent deletion of the branch
192
192
if newCommitID == git .EmptySHA {
193
193
log .Warn ("Forbidden: Branch: %s in %-v is protected from deletion" , branchName , repo )
194
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
195
- "err" : fmt .Sprintf ("branch %s is protected from deletion" , branchName ),
194
+ ctx .JSON (http .StatusForbidden , private. Response {
195
+ Err : fmt .Sprintf ("branch %s is protected from deletion" , branchName ),
196
196
})
197
197
return
198
198
}
@@ -202,14 +202,14 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
202
202
output , err := git .NewCommand ("rev-list" , "--max-count=1" , oldCommitID , "^" + newCommitID ).RunInDirWithEnv (repo .RepoPath (), env )
203
203
if err != nil {
204
204
log .Error ("Unable to detect force push between: %s and %s in %-v Error: %v" , oldCommitID , newCommitID , repo , err )
205
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
206
- "err" : fmt .Sprintf ("Fail to detect force push: %v" , err ),
205
+ ctx .JSON (http .StatusInternalServerError , private. Response {
206
+ Err : fmt .Sprintf ("Fail to detect force push: %v" , err ),
207
207
})
208
208
return
209
209
} else if len (output ) > 0 {
210
210
log .Warn ("Forbidden: Branch: %s in %-v is protected from force push" , branchName , repo )
211
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
212
- "err" : fmt .Sprintf ("branch %s is protected from force push" , branchName ),
211
+ ctx .JSON (http .StatusForbidden , private. Response {
212
+ Err : fmt .Sprintf ("branch %s is protected from force push" , branchName ),
213
213
})
214
214
return
215
215
@@ -222,15 +222,15 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
222
222
if err != nil {
223
223
if ! isErrUnverifiedCommit (err ) {
224
224
log .Error ("Unable to check commits from %s to %s in %-v: %v" , oldCommitID , newCommitID , repo , err )
225
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
226
- "err" : fmt .Sprintf ("Unable to check commits from %s to %s: %v" , oldCommitID , newCommitID , err ),
225
+ ctx .JSON (http .StatusInternalServerError , private. Response {
226
+ Err : fmt .Sprintf ("Unable to check commits from %s to %s: %v" , oldCommitID , newCommitID , err ),
227
227
})
228
228
return
229
229
}
230
230
unverifiedCommit := err .(* errUnverifiedCommit ).sha
231
231
log .Warn ("Forbidden: Branch: %s in %-v is protected from unverified commit %s" , branchName , repo , unverifiedCommit )
232
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
233
- "err" : fmt .Sprintf ("branch %s is protected from unverified commit %s" , branchName , unverifiedCommit ),
232
+ ctx .JSON (http .StatusForbidden , private. Response {
233
+ Err : fmt .Sprintf ("branch %s is protected from unverified commit %s" , branchName , unverifiedCommit ),
234
234
})
235
235
return
236
236
}
@@ -248,8 +248,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
248
248
if err != nil {
249
249
if ! models .IsErrFilePathProtected (err ) {
250
250
log .Error ("Unable to check file protection for commits from %s to %s in %-v: %v" , oldCommitID , newCommitID , repo , err )
251
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
252
- "err" : fmt .Sprintf ("Unable to check file protection for commits from %s to %s: %v" , oldCommitID , newCommitID , err ),
251
+ ctx .JSON (http .StatusInternalServerError , private. Response {
252
+ Err : fmt .Sprintf ("Unable to check file protection for commits from %s to %s: %v" , oldCommitID , newCommitID , err ),
253
253
})
254
254
return
255
255
}
@@ -270,49 +270,49 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
270
270
// 6. If we're not allowed to push directly
271
271
if ! canPush {
272
272
// Is this is a merge from the UI/API?
273
- if opts .ProtectedBranchID == 0 {
273
+ if opts .PullRequestID == 0 {
274
274
// 6a. If we're not merging from the UI/API then there are two ways we got here:
275
275
//
276
276
// We are changing a protected file and we're not allowed to do that
277
277
if changedProtectedfiles {
278
278
log .Warn ("Forbidden: Branch: %s in %-v is protected from changing file %s" , branchName , repo , protectedFilePath )
279
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
280
- "err" : fmt .Sprintf ("branch %s is protected from changing file %s" , branchName , protectedFilePath ),
279
+ ctx .JSON (http .StatusForbidden , private. Response {
280
+ Err : fmt .Sprintf ("branch %s is protected from changing file %s" , branchName , protectedFilePath ),
281
281
})
282
282
return
283
283
}
284
284
285
285
// Or we're simply not able to push to this protected branch
286
286
log .Warn ("Forbidden: User %d is not allowed to push to protected branch: %s in %-v" , opts .UserID , branchName , repo )
287
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
288
- "err" : fmt .Sprintf ("Not allowed to push to protected branch %s" , branchName ),
287
+ ctx .JSON (http .StatusForbidden , private. Response {
288
+ Err : fmt .Sprintf ("Not allowed to push to protected branch %s" , branchName ),
289
289
})
290
290
return
291
291
}
292
292
// 6b. Merge (from UI or API)
293
293
294
294
// Get the PR, user and permissions for the user in the repository
295
- pr , err := models .GetPullRequestByID (opts .ProtectedBranchID )
295
+ pr , err := models .GetPullRequestByID (opts .PullRequestID )
296
296
if err != nil {
297
- log .Error ("Unable to get PullRequest %d Error: %v" , opts .ProtectedBranchID , err )
298
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
299
- "err" : fmt .Sprintf ("Unable to get PullRequest %d Error: %v" , opts .ProtectedBranchID , err ),
297
+ log .Error ("Unable to get PullRequest %d Error: %v" , opts .PullRequestID , err )
298
+ ctx .JSON (http .StatusInternalServerError , private. Response {
299
+ Err : fmt .Sprintf ("Unable to get PullRequest %d Error: %v" , opts .PullRequestID , err ),
300
300
})
301
301
return
302
302
}
303
303
user , err := models .GetUserByID (opts .UserID )
304
304
if err != nil {
305
305
log .Error ("Unable to get User id %d Error: %v" , opts .UserID , err )
306
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
307
- "err" : fmt .Sprintf ("Unable to get User id %d Error: %v" , opts .UserID , err ),
306
+ ctx .JSON (http .StatusInternalServerError , private. Response {
307
+ Err : fmt .Sprintf ("Unable to get User id %d Error: %v" , opts .UserID , err ),
308
308
})
309
309
return
310
310
}
311
311
perm , err := models .GetUserRepoPermission (repo , user )
312
312
if err != nil {
313
313
log .Error ("Unable to get Repo permission of repo %s/%s of User %s" , repo .OwnerName , repo .Name , user .Name , err )
314
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
315
- "err" : fmt .Sprintf ("Unable to get Repo permission of repo %s/%s of User %s: %v" , repo .OwnerName , repo .Name , user .Name , err ),
314
+ ctx .JSON (http .StatusInternalServerError , private. Response {
315
+ Err : fmt .Sprintf ("Unable to get Repo permission of repo %s/%s of User %s: %v" , repo .OwnerName , repo .Name , user .Name , err ),
316
316
})
317
317
return
318
318
}
@@ -321,16 +321,16 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
321
321
allowedMerge , err := pull_service .IsUserAllowedToMerge (pr , perm , user )
322
322
if err != nil {
323
323
log .Error ("Error calculating if allowed to merge: %v" , err )
324
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
325
- "err" : fmt .Sprintf ("Error calculating if allowed to merge: %v" , err ),
324
+ ctx .JSON (http .StatusInternalServerError , private. Response {
325
+ Err : fmt .Sprintf ("Error calculating if allowed to merge: %v" , err ),
326
326
})
327
327
return
328
328
}
329
329
330
330
if ! allowedMerge {
331
331
log .Warn ("Forbidden: User %d is not allowed to push to protected branch: %s in %-v and is not allowed to merge pr #%d" , opts .UserID , branchName , repo , pr .Index )
332
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
333
- "err" : fmt .Sprintf ("Not allowed to push to protected branch %s" , branchName ),
332
+ ctx .JSON (http .StatusForbidden , private. Response {
333
+ Err : fmt .Sprintf ("Not allowed to push to protected branch %s" , branchName ),
334
334
})
335
335
return
336
336
}
@@ -343,8 +343,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
343
343
// Now if we're not an admin - we can't overwrite protected files so fail now
344
344
if changedProtectedfiles {
345
345
log .Warn ("Forbidden: Branch: %s in %-v is protected from changing file %s" , branchName , repo , protectedFilePath )
346
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
347
- "err" : fmt .Sprintf ("branch %s is protected from changing file %s" , branchName , protectedFilePath ),
346
+ ctx .JSON (http .StatusForbidden , private. Response {
347
+ Err : fmt .Sprintf ("branch %s is protected from changing file %s" , branchName , protectedFilePath ),
348
348
})
349
349
return
350
350
}
@@ -353,14 +353,14 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
353
353
if err := pull_service .CheckPRReadyToMerge (pr , true ); err != nil {
354
354
if models .IsErrNotAllowedToMerge (err ) {
355
355
log .Warn ("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s" , opts .UserID , branchName , repo , pr .Index , err .Error ())
356
- ctx .JSON (http .StatusForbidden , map [ string ] interface {} {
357
- "err" : fmt .Sprintf ("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s" , branchName , opts .ProtectedBranchID , err .Error ()),
356
+ ctx .JSON (http .StatusForbidden , private. Response {
357
+ Err : fmt .Sprintf ("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s" , branchName , opts .PullRequestID , err .Error ()),
358
358
})
359
359
return
360
360
}
361
361
log .Error ("Unable to check if mergable: protected branch %s in %-v and pr #%d. Error: %v" , opts .UserID , branchName , repo , pr .Index , err )
362
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
363
- "err" : fmt .Sprintf ("Unable to get status of pull request %d. Error: %v" , opts .ProtectedBranchID , err ),
362
+ ctx .JSON (http .StatusInternalServerError , private. Response {
363
+ Err : fmt .Sprintf ("Unable to get status of pull request %d. Error: %v" , opts .PullRequestID , err ),
364
364
})
365
365
return
366
366
}
@@ -549,8 +549,8 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
549
549
repo , err := models .GetRepositoryByOwnerAndName (ownerName , repoName )
550
550
if err != nil {
551
551
log .Error ("Failed to get repository: %s/%s Error: %v" , ownerName , repoName , err )
552
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
553
- " Err" : fmt .Sprintf ("Failed to get repository: %s/%s Error: %v" , ownerName , repoName , err ),
552
+ ctx .JSON (http .StatusInternalServerError , private. Response {
553
+ Err : fmt .Sprintf ("Failed to get repository: %s/%s Error: %v" , ownerName , repoName , err ),
554
554
})
555
555
return
556
556
}
@@ -561,27 +561,27 @@ func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
561
561
repo .DefaultBranch = branch
562
562
gitRepo , err := git .OpenRepository (repo .RepoPath ())
563
563
if err != nil {
564
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
565
- " Err" : fmt .Sprintf ("Failed to get git repository: %s/%s Error: %v" , ownerName , repoName , err ),
564
+ ctx .JSON (http .StatusInternalServerError , private. Response {
565
+ Err : fmt .Sprintf ("Failed to get git repository: %s/%s Error: %v" , ownerName , repoName , err ),
566
566
})
567
567
return
568
568
}
569
569
if err := gitRepo .SetDefaultBranch (repo .DefaultBranch ); err != nil {
570
570
if ! git .IsErrUnsupportedVersion (err ) {
571
571
gitRepo .Close ()
572
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
573
- " Err" : fmt .Sprintf ("Unable to set default branch on repository: %s/%s Error: %v" , ownerName , repoName , err ),
572
+ ctx .JSON (http .StatusInternalServerError , private. Response {
573
+ Err : fmt .Sprintf ("Unable to set default branch on repository: %s/%s Error: %v" , ownerName , repoName , err ),
574
574
})
575
575
return
576
576
}
577
577
}
578
578
gitRepo .Close ()
579
579
580
580
if err := repo .UpdateDefaultBranch (); err != nil {
581
- ctx .JSON (http .StatusInternalServerError , map [ string ] interface {} {
582
- " Err" : fmt .Sprintf ("Unable to set default branch on repository: %s/%s Error: %v" , ownerName , repoName , err ),
581
+ ctx .JSON (http .StatusInternalServerError , private. Response {
582
+ Err : fmt .Sprintf ("Unable to set default branch on repository: %s/%s Error: %v" , ownerName , repoName , err ),
583
583
})
584
584
return
585
585
}
586
- ctx .PlainText (200 , []byte ("success" ))
586
+ ctx .PlainText (http . StatusOK , []byte ("success" ))
587
587
}
0 commit comments