@@ -184,14 +184,14 @@ func reqToken() macaron.Handler {
184
184
ctx .RequireCSRF ()
185
185
return
186
186
}
187
- ctx .Context . Error (http .StatusUnauthorized )
187
+ ctx .Error (http .StatusUnauthorized , "reqToken" , "token is required" )
188
188
}
189
189
}
190
190
191
191
func reqBasicAuth () macaron.Handler {
192
192
return func (ctx * context.APIContext ) {
193
193
if ! ctx .Context .IsBasicAuth {
194
- ctx .Context . Error (http .StatusUnauthorized )
194
+ ctx .Error (http .StatusUnauthorized , "reqBasicAuth" , "basic auth required" )
195
195
return
196
196
}
197
197
ctx .CheckForOTP ()
@@ -200,59 +200,59 @@ func reqBasicAuth() macaron.Handler {
200
200
201
201
// reqSiteAdmin user should be the site admin
202
202
func reqSiteAdmin () macaron.Handler {
203
- return func (ctx * context.Context ) {
203
+ return func (ctx * context.APIContext ) {
204
204
if ! ctx .IsUserSiteAdmin () {
205
- ctx .Error (http .StatusForbidden )
205
+ ctx .Error (http .StatusForbidden , "reqSiteAdmin" , "user should be the site admin" )
206
206
return
207
207
}
208
208
}
209
209
}
210
210
211
211
// reqOwner user should be the owner of the repo or site admin.
212
212
func reqOwner () macaron.Handler {
213
- return func (ctx * context.Context ) {
213
+ return func (ctx * context.APIContext ) {
214
214
if ! ctx .IsUserRepoOwner () && ! ctx .IsUserSiteAdmin () {
215
- ctx .Error (http .StatusForbidden )
215
+ ctx .Error (http .StatusForbidden , "reqOwner" , "user should be the owner of the repo" )
216
216
return
217
217
}
218
218
}
219
219
}
220
220
221
221
// reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
222
222
func reqAdmin () macaron.Handler {
223
- return func (ctx * context.Context ) {
223
+ return func (ctx * context.APIContext ) {
224
224
if ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
225
- ctx .Error (http .StatusForbidden )
225
+ ctx .Error (http .StatusForbidden , "reqAdmin" , "user should be an owner or a collaborator with admin write of a repository" )
226
226
return
227
227
}
228
228
}
229
229
}
230
230
231
231
// reqRepoWriter user should have a permission to write to a repo, or be a site admin
232
232
func reqRepoWriter (unitTypes ... models.UnitType ) macaron.Handler {
233
- return func (ctx * context.Context ) {
233
+ return func (ctx * context.APIContext ) {
234
234
if ! ctx .IsUserRepoWriter (unitTypes ) && ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
235
- ctx .Error (http .StatusForbidden )
235
+ ctx .Error (http .StatusForbidden , "reqRepoWriter" , "user should have a permission to write to a repo" )
236
236
return
237
237
}
238
238
}
239
239
}
240
240
241
241
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
242
242
func reqRepoReader (unitType models.UnitType ) macaron.Handler {
243
- return func (ctx * context.Context ) {
243
+ return func (ctx * context.APIContext ) {
244
244
if ! ctx .IsUserRepoReaderSpecific (unitType ) && ! ctx .IsUserRepoAdmin () && ! ctx .IsUserSiteAdmin () {
245
- ctx .Error (http .StatusForbidden )
245
+ ctx .Error (http .StatusForbidden , "reqRepoReader" , "user should have specific read permission or be a repo admin or a site admin" )
246
246
return
247
247
}
248
248
}
249
249
}
250
250
251
251
// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
252
252
func reqAnyRepoReader () macaron.Handler {
253
- return func (ctx * context.Context ) {
253
+ return func (ctx * context.APIContext ) {
254
254
if ! ctx .IsUserRepoReaderAny () && ! ctx .IsUserSiteAdmin () {
255
- ctx .Error (http .StatusForbidden )
255
+ ctx .Error (http .StatusForbidden , "reqAnyRepoReader" , "user should have any permission to read repository or permissions of site admin" )
256
256
return
257
257
}
258
258
}
@@ -495,7 +495,6 @@ func mustNotBeArchived(ctx *context.APIContext) {
495
495
}
496
496
497
497
// RegisterRoutes registers all v1 APIs routes to web application.
498
- // FIXME: custom form error response
499
498
func RegisterRoutes (m * macaron.Macaron ) {
500
499
bind := binding .Bind
501
500
@@ -628,7 +627,7 @@ func RegisterRoutes(m *macaron.Macaron) {
628
627
m .Group ("/:username/:reponame" , func () {
629
628
m .Combo ("" ).Get (reqAnyRepoReader (), repo .Get ).
630
629
Delete (reqToken (), reqOwner (), repo .Delete ).
631
- Patch (reqToken (), reqAdmin (), bind (api.EditRepoOption {}), context .RepoRef (), repo .Edit )
630
+ Patch (reqToken (), reqAdmin (), bind (api.EditRepoOption {}), context .RepoRefForAPI (), repo .Edit )
632
631
m .Post ("/transfer" , reqOwner (), bind (api.TransferRepoOption {}), repo .Transfer )
633
632
m .Combo ("/notifications" ).
634
633
Get (reqToken (), notify .ListRepoNotifications ).
@@ -640,7 +639,7 @@ func RegisterRoutes(m *macaron.Macaron) {
640
639
m .Combo ("" ).Get (repo .GetHook ).
641
640
Patch (bind (api.EditHookOption {}), repo .EditHook ).
642
641
Delete (repo .DeleteHook )
643
- m .Post ("/tests" , context .RepoRef (), repo .TestHook )
642
+ m .Post ("/tests" , context .RepoRefForAPI (), repo .TestHook )
644
643
})
645
644
m .Group ("/git" , func () {
646
645
m .Combo ("" ).Get (repo .ListGitHooks )
@@ -657,14 +656,14 @@ func RegisterRoutes(m *macaron.Macaron) {
657
656
Put (reqAdmin (), bind (api.AddCollaboratorOption {}), repo .AddCollaborator ).
658
657
Delete (reqAdmin (), repo .DeleteCollaborator )
659
658
}, reqToken ())
660
- m .Get ("/raw/*" , context .RepoRefByType ( context . RepoRefAny ), reqRepoReader (models .UnitTypeCode ), repo .GetRawFile )
659
+ m .Get ("/raw/*" , context .RepoRefForAPI ( ), reqRepoReader (models .UnitTypeCode ), repo .GetRawFile )
661
660
m .Get ("/archive/*" , reqRepoReader (models .UnitTypeCode ), repo .GetArchive )
662
661
m .Combo ("/forks" ).Get (repo .ListForks ).
663
662
Post (reqToken (), reqRepoReader (models .UnitTypeCode ), bind (api.CreateForkOption {}), repo .CreateFork )
664
663
m .Group ("/branches" , func () {
665
664
m .Get ("" , repo .ListBranches )
666
- m .Get ("/*" , context . RepoRefByType ( context . RepoRefBranch ), repo .GetBranch )
667
- m .Delete ("/*" , reqRepoWriter ( models . UnitTypeCode ), context . RepoRefByType ( context . RepoRefBranch ), repo .DeleteBranch )
665
+ m .Get ("/*" , repo .GetBranch )
666
+ m .Delete ("/*" , context . ReferencesGitRepo ( false ), reqRepoWriter ( models . UnitTypeCode ), repo .DeleteBranch )
668
667
}, reqRepoReader (models .UnitTypeCode ))
669
668
m .Group ("/branch_protections" , func () {
670
669
m .Get ("" , repo .ListBranchProtections )
@@ -785,7 +784,7 @@ func RegisterRoutes(m *macaron.Macaron) {
785
784
})
786
785
}, reqRepoReader (models .UnitTypeReleases ))
787
786
m .Post ("/mirror-sync" , reqToken (), reqRepoWriter (models .UnitTypeCode ), repo .MirrorSync )
788
- m .Get ("/editorconfig/:filename" , context .RepoRef (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
787
+ m .Get ("/editorconfig/:filename" , context .RepoRefForAPI (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
789
788
m .Group ("/pulls" , func () {
790
789
m .Combo ("" ).Get (bind (api.ListPullRequestsOptions {}), repo .ListPullRequests ).
791
790
Post (reqToken (), mustNotBeArchived , bind (api.CreatePullRequestOption {}), repo .CreatePullRequest )
@@ -827,9 +826,9 @@ func RegisterRoutes(m *macaron.Macaron) {
827
826
})
828
827
m .Get ("/refs" , repo .GetGitAllRefs )
829
828
m .Get ("/refs/*" , repo .GetGitRefs )
830
- m .Get ("/trees/:sha" , context .RepoRef (), repo .GetTree )
831
- m .Get ("/blobs/:sha" , context .RepoRef (), repo .GetBlob )
832
- m .Get ("/tags/:sha" , context .RepoRef (), repo .GetTag )
829
+ m .Get ("/trees/:sha" , context .RepoRefForAPI (), repo .GetTree )
830
+ m .Get ("/blobs/:sha" , context .RepoRefForAPI (), repo .GetBlob )
831
+ m .Get ("/tags/:sha" , context .RepoRefForAPI (), repo .GetTag )
833
832
}, reqRepoReader (models .UnitTypeCode ))
834
833
m .Group ("/contents" , func () {
835
834
m .Get ("" , repo .GetContentsList )
0 commit comments