@@ -191,14 +191,14 @@ func reqToken() macaron.Handler {
191191 ctx .RequireCSRF ()
192192 return
193193 }
194- ctx .Context . Error (http .StatusUnauthorized )
194+ ctx .Error (http .StatusUnauthorized , "reqToken" , "token is required" )
195195 }
196196}
197197
198198func reqBasicAuth () macaron.Handler {
199199 return func (ctx * context.APIContext ) {
200200 if ! ctx .Context .IsBasicAuth {
201- ctx .Context . Error (http .StatusUnauthorized )
201+ ctx .Error (http .StatusUnauthorized , "reqBasicAuth" , "basic auth required" )
202202 return
203203 }
204204 ctx .CheckForOTP ()
@@ -207,59 +207,59 @@ func reqBasicAuth() macaron.Handler {
207207
208208// reqSiteAdmin user should be the site admin
209209func reqSiteAdmin () macaron.Handler {
210- return func (ctx * context.Context ) {
210+ return func (ctx * context.APIContext ) {
211211 if ! ctx .IsUserSiteAdmin () {
212- ctx .Error (http .StatusForbidden )
212+ ctx .Error (http .StatusForbidden , "reqSiteAdmin" , "user should be the site admin" )
213213 return
214214 }
215215 }
216216}
217217
218218// reqOwner user should be the owner of the repo or site admin.
219219func reqOwner () macaron.Handler {
220- return func (ctx * context.Context ) {
220+ return func (ctx * context.APIContext ) {
221221 if ! ctx .IsUserRepoOwner () && ! ctx .IsUserSiteAdmin () {
222- ctx .Error (http .StatusForbidden )
222+ ctx .Error (http .StatusForbidden , "reqOwner" , "user should be the owner of the repo" )
223223 return
224224 }
225225 }
226226}
227227
228228// reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
229229func reqAdmin () macaron.Handler {
230- return func (ctx * context.Context ) {
230+ return func (ctx * context.APIContext ) {
231231 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" )
233233 return
234234 }
235235 }
236236}
237237
238238// reqRepoWriter user should have a permission to write to a repo, or be a site admin
239239func reqRepoWriter (unitTypes ... models.UnitType ) macaron.Handler {
240- return func (ctx * context.Context ) {
240+ return func (ctx * context.APIContext ) {
241241 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" )
243243 return
244244 }
245245 }
246246}
247247
248248// reqRepoReader user should have specific read permission or be a repo admin or a site admin
249249func reqRepoReader (unitType models.UnitType ) macaron.Handler {
250- return func (ctx * context.Context ) {
250+ return func (ctx * context.APIContext ) {
251251 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" )
253253 return
254254 }
255255 }
256256}
257257
258258// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
259259func reqAnyRepoReader () macaron.Handler {
260- return func (ctx * context.Context ) {
260+ return func (ctx * context.APIContext ) {
261261 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" )
263263 return
264264 }
265265 }
@@ -502,7 +502,6 @@ func mustNotBeArchived(ctx *context.APIContext) {
502502}
503503
504504// RegisterRoutes registers all v1 APIs routes to web application.
505- // FIXME: custom form error response
506505func RegisterRoutes (m * macaron.Macaron ) {
507506 bind := binding .Bind
508507
@@ -641,7 +640,7 @@ func RegisterRoutes(m *macaron.Macaron) {
641640 m .Group ("/:username/:reponame" , func () {
642641 m .Combo ("" ).Get (reqAnyRepoReader (), repo .Get ).
643642 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 )
645644 m .Post ("/transfer" , reqOwner (), bind (api.TransferRepoOption {}), repo .Transfer )
646645 m .Combo ("/notifications" ).
647646 Get (reqToken (), notify .ListRepoNotifications ).
@@ -653,7 +652,7 @@ func RegisterRoutes(m *macaron.Macaron) {
653652 m .Combo ("" ).Get (repo .GetHook ).
654653 Patch (bind (api.EditHookOption {}), repo .EditHook ).
655654 Delete (repo .DeleteHook )
656- m .Post ("/tests" , context .RepoRef (), repo .TestHook )
655+ m .Post ("/tests" , context .RepoRefForAPI (), repo .TestHook )
657656 })
658657 m .Group ("/git" , func () {
659658 m .Combo ("" ).Get (repo .ListGitHooks )
@@ -670,14 +669,14 @@ func RegisterRoutes(m *macaron.Macaron) {
670669 Put (reqAdmin (), bind (api.AddCollaboratorOption {}), repo .AddCollaborator ).
671670 Delete (reqAdmin (), repo .DeleteCollaborator )
672671 }, 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 )
674673 m .Get ("/archive/*" , reqRepoReader (models .UnitTypeCode ), repo .GetArchive )
675674 m .Combo ("/forks" ).Get (repo .ListForks ).
676675 Post (reqToken (), reqRepoReader (models .UnitTypeCode ), bind (api.CreateForkOption {}), repo .CreateFork )
677676 m .Group ("/branches" , func () {
678677 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 )
681680 m .Post ("" , reqRepoWriter (models .UnitTypeCode ), bind (api.CreateBranchRepoOption {}), repo .CreateBranch )
682681 }, reqRepoReader (models .UnitTypeCode ))
683682 m .Group ("/branch_protections" , func () {
@@ -802,7 +801,7 @@ func RegisterRoutes(m *macaron.Macaron) {
802801 })
803802 }, reqRepoReader (models .UnitTypeReleases ))
804803 m .Post ("/mirror-sync" , reqToken (), reqRepoWriter (models .UnitTypeCode ), repo .MirrorSync )
805- m .Get ("/editorconfig/:filename" , context .RepoRef (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
804+ m .Get ("/editorconfig/:filename" , context .RepoRefForAPI (), reqRepoReader (models .UnitTypeCode ), repo .GetEditorconfig )
806805 m .Group ("/pulls" , func () {
807806 m .Combo ("" ).Get (bind (api.ListPullRequestsOptions {}), repo .ListPullRequests ).
808807 Post (reqToken (), mustNotBeArchived , bind (api.CreatePullRequestOption {}), repo .CreatePullRequest )
@@ -847,9 +846,9 @@ func RegisterRoutes(m *macaron.Macaron) {
847846 })
848847 m .Get ("/refs" , repo .GetGitAllRefs )
849848 m .Get ("/refs/*" , repo .GetGitRefs )
850- m .Get ("/trees/:sha" , context .RepoRef (), repo .GetTree )
851- m .Get ("/blobs/:sha" , context .RepoRef (), repo .GetBlob )
852- m .Get ("/tags/:sha" , context .RepoRef (), repo .GetTag )
849+ m .Get ("/trees/:sha" , context .RepoRefForAPI (), repo .GetTree )
850+ m .Get ("/blobs/:sha" , context .RepoRefForAPI (), repo .GetBlob )
851+ m .Get ("/tags/:sha" , context .RepoRefForAPI (), repo .GetTag )
853852 }, reqRepoReader (models .UnitTypeCode ))
854853 m .Group ("/contents" , func () {
855854 m .Get ("" , repo .GetContentsList )
0 commit comments