@@ -184,14 +184,14 @@ func reqToken() macaron.Handler {
184184 ctx .RequireCSRF ()
185185 return
186186 }
187- ctx .Context . Error (http .StatusUnauthorized )
187+ ctx .Error (http .StatusUnauthorized , "reqToken" , "token is required" )
188188 }
189189}
190190
191191func reqBasicAuth () macaron.Handler {
192192 return func (ctx * context.APIContext ) {
193193 if ! ctx .Context .IsBasicAuth {
194- ctx .Context . Error (http .StatusUnauthorized )
194+ ctx .Error (http .StatusUnauthorized , "reqBasicAuth" , "basic auth required" )
195195 return
196196 }
197197 ctx .CheckForOTP ()
@@ -200,59 +200,59 @@ func reqBasicAuth() macaron.Handler {
200200
201201// reqSiteAdmin user should be the site admin
202202func reqSiteAdmin () macaron.Handler {
203- return func (ctx * context.Context ) {
203+ return func (ctx * context.APIContext ) {
204204 if ! ctx .IsUserSiteAdmin () {
205- ctx .Error (http .StatusForbidden )
205+ ctx .Error (http .StatusForbidden , "reqSiteAdmin" , "user should be the site admin" )
206206 return
207207 }
208208 }
209209}
210210
211211// reqOwner user should be the owner of the repo or site admin.
212212func reqOwner () macaron.Handler {
213- return func (ctx * context.Context ) {
213+ return func (ctx * context.APIContext ) {
214214 if ! ctx .IsUserRepoOwner () && ! ctx .IsUserSiteAdmin () {
215- ctx .Error (http .StatusForbidden )
215+ ctx .Error (http .StatusForbidden , "reqOwner" , "user should be the owner of the repo" )
216216 return
217217 }
218218 }
219219}
220220
221221// reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
222222func reqAdmin () macaron.Handler {
223- return func (ctx * context.Context ) {
223+ return func (ctx * context.APIContext ) {
224224 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" )
226226 return
227227 }
228228 }
229229}
230230
231231// reqRepoWriter user should have a permission to write to a repo, or be a site admin
232232func reqRepoWriter (unitTypes ... models.UnitType ) macaron.Handler {
233- return func (ctx * context.Context ) {
233+ return func (ctx * context.APIContext ) {
234234 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" )
236236 return
237237 }
238238 }
239239}
240240
241241// reqRepoReader user should have specific read permission or be a repo admin or a site admin
242242func reqRepoReader (unitType models.UnitType ) macaron.Handler {
243- return func (ctx * context.Context ) {
243+ return func (ctx * context.APIContext ) {
244244 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" )
246246 return
247247 }
248248 }
249249}
250250
251251// reqAnyRepoReader user should have any permission to read repository or permissions of site admin
252252func reqAnyRepoReader () macaron.Handler {
253- return func (ctx * context.Context ) {
253+ return func (ctx * context.APIContext ) {
254254 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" )
256256 return
257257 }
258258 }
@@ -495,7 +495,6 @@ func mustNotBeArchived(ctx *context.APIContext) {
495495}
496496
497497// RegisterRoutes registers all v1 APIs routes to web application.
498- // FIXME: custom form error response
499498func RegisterRoutes (m * macaron.Macaron ) {
500499 bind := binding .Bind
501500
@@ -628,7 +627,7 @@ func RegisterRoutes(m *macaron.Macaron) {
628627 m .Group ("/:username/:reponame" , func () {
629628 m .Combo ("" ).Get (reqAnyRepoReader (), repo .Get ).
630629 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 )
632631 m .Post ("/transfer" , reqOwner (), bind (api.TransferRepoOption {}), repo .Transfer )
633632 m .Combo ("/notifications" ).
634633 Get (reqToken (), notify .ListRepoNotifications ).
@@ -640,7 +639,7 @@ func RegisterRoutes(m *macaron.Macaron) {
640639 m .Combo ("" ).Get (repo .GetHook ).
641640 Patch (bind (api.EditHookOption {}), repo .EditHook ).
642641 Delete (repo .DeleteHook )
643- m .Post ("/tests" , context .RepoRef (), repo .TestHook )
642+ m .Post ("/tests" , context .RepoRefForAPI (), repo .TestHook )
644643 })
645644 m .Group ("/git" , func () {
646645 m .Combo ("" ).Get (repo .ListGitHooks )
@@ -657,14 +656,14 @@ func RegisterRoutes(m *macaron.Macaron) {
657656 Put (reqAdmin (), bind (api.AddCollaboratorOption {}), repo .AddCollaborator ).
658657 Delete (reqAdmin (), repo .DeleteCollaborator )
659658 }, 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 )
661660 m .Get ("/archive/*" , reqRepoReader (models .UnitTypeCode ), repo .GetArchive )
662661 m .Combo ("/forks" ).Get (repo .ListForks ).
663662 Post (reqToken (), reqRepoReader (models .UnitTypeCode ), bind (api.CreateForkOption {}), repo .CreateFork )
664663 m .Group ("/branches" , func () {
665664 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 )
668667 }, reqRepoReader (models .UnitTypeCode ))
669668 m .Group ("/branch_protections" , func () {
670669 m .Get ("" , repo .ListBranchProtections )
@@ -785,7 +784,7 @@ func RegisterRoutes(m *macaron.Macaron) {
785784 })
786785 }, reqRepoReader (models .UnitTypeReleases ))
787786 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 )
789788 m .Group ("/pulls" , func () {
790789 m .Combo ("" ).Get (bind (api.ListPullRequestsOptions {}), repo .ListPullRequests ).
791790 Post (reqToken (), mustNotBeArchived , bind (api.CreatePullRequestOption {}), repo .CreatePullRequest )
@@ -827,9 +826,9 @@ func RegisterRoutes(m *macaron.Macaron) {
827826 })
828827 m .Get ("/refs" , repo .GetGitAllRefs )
829828 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 )
833832 }, reqRepoReader (models .UnitTypeCode ))
834833 m .Group ("/contents" , func () {
835834 m .Get ("" , repo .GetContentsList )
0 commit comments