@@ -191,14 +191,14 @@ func reqToken() macaron.Handler {
191
191
ctx .RequireCSRF ()
192
192
return
193
193
}
194
- ctx .Context . Error (http .StatusUnauthorized )
194
+ ctx .Error (http .StatusUnauthorized , "reqToken" , "token is required" )
195
195
}
196
196
}
197
197
198
198
func reqBasicAuth () macaron.Handler {
199
199
return func (ctx * context.APIContext ) {
200
200
if ! ctx .Context .IsBasicAuth {
201
- ctx .Context . Error (http .StatusUnauthorized )
201
+ ctx .Error (http .StatusUnauthorized , "reqBasicAuth" , "basic auth required" )
202
202
return
203
203
}
204
204
ctx .CheckForOTP ()
@@ -207,59 +207,59 @@ func reqBasicAuth() macaron.Handler {
207
207
208
208
// reqSiteAdmin user should be the site admin
209
209
func reqSiteAdmin () macaron.Handler {
210
- return func (ctx * context.Context ) {
210
+ return func (ctx * context.APIContext ) {
211
211
if ! ctx .IsUserSiteAdmin () {
212
- ctx .Error (http .StatusForbidden )
212
+ ctx .Error (http .StatusForbidden , "reqSiteAdmin" , "user should be the site admin" )
213
213
return
214
214
}
215
215
}
216
216
}
217
217
218
218
// reqOwner user should be the owner of the repo or site admin.
219
219
func reqOwner () macaron.Handler {
220
- return func (ctx * context.Context ) {
220
+ return func (ctx * context.APIContext ) {
221
221
if ! ctx .IsUserRepoOwner () && ! ctx .IsUserSiteAdmin () {
222
- ctx .Error (http .StatusForbidden )
222
+ ctx .Error (http .StatusForbidden , "reqOwner" , "user should be the owner of the repo" )
223
223
return
224
224
}
225
225
}
226
226
}
227
227
228
228
// reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
229
229
func reqAdmin () macaron.Handler {
230
- return func (ctx * context.Context ) {
230
+ return func (ctx * context.APIContext ) {
231
231
if ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
232
- ctx .Error (http .StatusForbidden )
232
+ ctx .Error (http .StatusForbidden , "reqAdmin" , "user should be an owner or a collaborator with admin write of a repository" )
233
233
return
234
234
}
235
235
}
236
236
}
237
237
238
238
// reqRepoWriter user should have a permission to write to a repo, or be a site admin
239
239
func reqRepoWriter (unitTypes ... models.UnitType ) macaron.Handler {
240
- return func (ctx * context.Context ) {
240
+ return func (ctx * context.APIContext ) {
241
241
if ! ctx .IsUserRepoWriter (unitTypes ) && ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
242
- ctx .Error (http .StatusForbidden )
242
+ ctx .Error (http .StatusForbidden , "reqRepoWriter" , "user should have a permission to write to a repo" )
243
243
return
244
244
}
245
245
}
246
246
}
247
247
248
248
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
249
249
func reqRepoReader (unitType models.UnitType ) macaron.Handler {
250
- return func (ctx * context.Context ) {
250
+ return func (ctx * context.APIContext ) {
251
251
if ! ctx .IsUserRepoReaderSpecific (unitType ) && ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
252
- ctx .Error (http .StatusForbidden )
252
+ ctx .Error (http .StatusForbidden , "reqRepoReader" , "user should have specific read permission or be a repo admin or a site admin" )
253
253
return
254
254
}
255
255
}
256
256
}
257
257
258
258
// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
259
259
func reqAnyRepoReader () macaron.Handler {
260
- return func (ctx * context.Context ) {
260
+ return func (ctx * context.APIContext ) {
261
261
if ! ctx .IsUserRepoReaderAny () && ! ctx .IsUserSiteAdmin () {
262
- ctx .Error (http .StatusForbidden )
262
+ ctx .Error (http .StatusForbidden , "reqAnyRepoReader" , "user should have any permission to read repository or permissions of site admin" )
263
263
return
264
264
}
265
265
}
@@ -502,7 +502,6 @@ func mustNotBeArchived(ctx *context.APIContext) {
502
502
}
503
503
504
504
// RegisterRoutes registers all v1 APIs routes to web application.
505
- // FIXME: custom form error response
506
505
func RegisterRoutes (m * macaron.Macaron ) {
507
506
bind := binding .Bind
508
507
@@ -641,7 +640,7 @@ func RegisterRoutes(m *macaron.Macaron) {
641
640
m .Group ("/:username/:reponame" , func () {
642
641
m .Combo ("" ).Get (reqAnyRepoReader (), repo .Get ).
643
642
Delete (reqToken (), reqOwner (), repo .Delete ).
644
- Patch (reqToken (), reqAdmin (), bind (api.EditRepoOption {}), context .RepoRef (), repo .Edit )
643
+ Patch (reqToken (), reqAdmin (), bind (api.EditRepoOption {}), context .RepoRefForAPI (), repo .Edit )
645
644
m .Post ("/transfer" , reqOwner (), bind (api.TransferRepoOption {}), repo .Transfer )
646
645
m .Combo ("/notifications" ).
647
646
Get (reqToken (), notify .ListRepoNotifications ).
@@ -653,7 +652,7 @@ func RegisterRoutes(m *macaron.Macaron) {
653
652
m .Combo ("" ).Get (repo .GetHook ).
654
653
Patch (bind (api.EditHookOption {}), repo .EditHook ).
655
654
Delete (repo .DeleteHook )
656
- m .Post ("/tests" , context .RepoRef (), repo .TestHook )
655
+ m .Post ("/tests" , context .RepoRefForAPI (), repo .TestHook )
657
656
})
658
657
m .Group ("/git" , func () {
659
658
m .Combo ("" ).Get (repo .ListGitHooks )
@@ -670,14 +669,14 @@ func RegisterRoutes(m *macaron.Macaron) {
670
669
Put (reqAdmin (), bind (api.AddCollaboratorOption {}), repo .AddCollaborator ).
671
670
Delete (reqAdmin (), repo .DeleteCollaborator )
672
671
}, reqToken ())
673
- m .Get ("/raw/*" , context .RepoRefByType ( context . RepoRefAny ), reqRepoReader (models .UnitTypeCode ), repo .GetRawFile )
672
+ m .Get ("/raw/*" , context .RepoRefForAPI ( ), reqRepoReader (models .UnitTypeCode ), repo .GetRawFile )
674
673
m .Get ("/archive/*" , reqRepoReader (models .UnitTypeCode ), repo .GetArchive )
675
674
m .Combo ("/forks" ).Get (repo .ListForks ).
676
675
Post (reqToken (), reqRepoReader (models .UnitTypeCode ), bind (api.CreateForkOption {}), repo .CreateFork )
677
676
m .Group ("/branches" , func () {
678
677
m .Get ("" , repo .ListBranches )
679
- m .Get ("/*" , context . RepoRefByType ( context . RepoRefBranch ), repo .GetBranch )
680
- m .Delete ("/*" , reqRepoWriter ( models . UnitTypeCode ), context . RepoRefByType ( context . RepoRefBranch ), repo .DeleteBranch )
678
+ m .Get ("/*" , repo .GetBranch )
679
+ m .Delete ("/*" , context . ReferencesGitRepo ( false ), reqRepoWriter ( models . UnitTypeCode ), repo .DeleteBranch )
681
680
m .Post ("" , reqRepoWriter (models .UnitTypeCode ), bind (api.CreateBranchRepoOption {}), repo .CreateBranch )
682
681
}, reqRepoReader (models .UnitTypeCode ))
683
682
m .Group ("/branch_protections" , func () {
@@ -804,7 +803,7 @@ func RegisterRoutes(m *macaron.Macaron) {
804
803
})
805
804
}, reqRepoReader (models .UnitTypeReleases ))
806
805
m .Post ("/mirror-sync" , reqToken (), reqRepoWriter (models .UnitTypeCode ), repo .MirrorSync )
807
- m .Get ("/editorconfig/:filename" , context .RepoRef (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
806
+ m .Get ("/editorconfig/:filename" , context .RepoRefForAPI (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
808
807
m .Group ("/pulls" , func () {
809
808
m .Combo ("" ).Get (bind (api.ListPullRequestsOptions {}), repo .ListPullRequests ).
810
809
Post (reqToken (), mustNotBeArchived , bind (api.CreatePullRequestOption {}), repo .CreatePullRequest )
@@ -851,9 +850,9 @@ func RegisterRoutes(m *macaron.Macaron) {
851
850
})
852
851
m .Get ("/refs" , repo .GetGitAllRefs )
853
852
m .Get ("/refs/*" , repo .GetGitRefs )
854
- m .Get ("/trees/:sha" , context .RepoRef (), repo .GetTree )
855
- m .Get ("/blobs/:sha" , context .RepoRef (), repo .GetBlob )
856
- m .Get ("/tags/:sha" , context .RepoRef (), repo .GetTag )
853
+ m .Get ("/trees/:sha" , context .RepoRefForAPI (), repo .GetTree )
854
+ m .Get ("/blobs/:sha" , context .RepoRefForAPI (), repo .GetBlob )
855
+ m .Get ("/tags/:sha" , context .RepoRefForAPI (), repo .GetTag )
857
856
}, reqRepoReader (models .UnitTypeCode ))
858
857
m .Group ("/contents" , func () {
859
858
m .Get ("" , repo .GetContentsList )
0 commit comments