api.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package v1
  5. import (
  6. "strings"
  7. "github.com/go-macaron/binding"
  8. "gopkg.in/macaron.v1"
  9. api "github.com/gogits/go-gogs-client"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/context"
  12. "github.com/gogits/gogs/modules/form"
  13. "github.com/gogits/gogs/routers/api/v1/admin"
  14. "github.com/gogits/gogs/routers/api/v1/misc"
  15. "github.com/gogits/gogs/routers/api/v1/org"
  16. "github.com/gogits/gogs/routers/api/v1/repo"
  17. "github.com/gogits/gogs/routers/api/v1/user"
  18. )
  19. func repoAssignment() macaron.Handler {
  20. return func(ctx *context.APIContext) {
  21. userName := ctx.Params(":username")
  22. repoName := ctx.Params(":reponame")
  23. var (
  24. owner *models.User
  25. err error
  26. )
  27. // Check if the user is the same as the repository owner.
  28. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  29. owner = ctx.User
  30. } else {
  31. owner, err = models.GetUserByName(userName)
  32. if err != nil {
  33. if models.IsErrUserNotExist(err) {
  34. ctx.Status(404)
  35. } else {
  36. ctx.Error(500, "GetUserByName", err)
  37. }
  38. return
  39. }
  40. }
  41. ctx.Repo.Owner = owner
  42. // Get repository.
  43. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  44. if err != nil {
  45. if models.IsErrRepoNotExist(err) {
  46. ctx.Status(404)
  47. } else {
  48. ctx.Error(500, "GetRepositoryByName", err)
  49. }
  50. return
  51. } else if err = repo.GetOwner(); err != nil {
  52. ctx.Error(500, "GetOwner", err)
  53. return
  54. }
  55. if ctx.IsSigned && ctx.User.IsAdmin {
  56. ctx.Repo.AccessMode = models.ACCESS_MODE_OWNER
  57. } else {
  58. mode, err := models.AccessLevel(ctx.User.ID, repo)
  59. if err != nil {
  60. ctx.Error(500, "AccessLevel", err)
  61. return
  62. }
  63. ctx.Repo.AccessMode = mode
  64. }
  65. if !ctx.Repo.HasAccess() {
  66. ctx.Status(404)
  67. return
  68. }
  69. ctx.Repo.Repository = repo
  70. }
  71. }
  72. // Contexter middleware already checks token for user sign in process.
  73. func reqToken() macaron.Handler {
  74. return func(ctx *context.Context) {
  75. if !ctx.IsSigned {
  76. ctx.Error(401)
  77. return
  78. }
  79. }
  80. }
  81. func reqBasicAuth() macaron.Handler {
  82. return func(ctx *context.Context) {
  83. if !ctx.IsBasicAuth {
  84. ctx.Error(401)
  85. return
  86. }
  87. }
  88. }
  89. func reqAdmin() macaron.Handler {
  90. return func(ctx *context.Context) {
  91. if !ctx.IsSigned || !ctx.User.IsAdmin {
  92. ctx.Error(403)
  93. return
  94. }
  95. }
  96. }
  97. func reqRepoWriter() macaron.Handler {
  98. return func(ctx *context.Context) {
  99. if !ctx.Repo.IsWriter() {
  100. ctx.Error(403)
  101. return
  102. }
  103. }
  104. }
  105. func orgAssignment(args ...bool) macaron.Handler {
  106. var (
  107. assignOrg bool
  108. assignTeam bool
  109. )
  110. if len(args) > 0 {
  111. assignOrg = args[0]
  112. }
  113. if len(args) > 1 {
  114. assignTeam = args[1]
  115. }
  116. return func(ctx *context.APIContext) {
  117. ctx.Org = new(context.APIOrganization)
  118. var err error
  119. if assignOrg {
  120. ctx.Org.Organization, err = models.GetUserByName(ctx.Params(":orgname"))
  121. if err != nil {
  122. if models.IsErrUserNotExist(err) {
  123. ctx.Status(404)
  124. } else {
  125. ctx.Error(500, "GetUserByName", err)
  126. }
  127. return
  128. }
  129. }
  130. if assignTeam {
  131. ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
  132. if err != nil {
  133. if models.IsErrUserNotExist(err) {
  134. ctx.Status(404)
  135. } else {
  136. ctx.Error(500, "GetTeamById", err)
  137. }
  138. return
  139. }
  140. }
  141. }
  142. }
  143. func mustEnableIssues(ctx *context.APIContext) {
  144. if !ctx.Repo.Repository.EnableIssues || ctx.Repo.Repository.EnableExternalTracker {
  145. ctx.Status(404)
  146. return
  147. }
  148. }
  149. // RegisterRoutes registers all v1 APIs routes to web application.
  150. // FIXME: custom form error response
  151. func RegisterRoutes(m *macaron.Macaron) {
  152. bind := binding.Bind
  153. m.Group("/v1", func() {
  154. // Handle preflight OPTIONS request
  155. m.Options("/*", func() {})
  156. // Miscellaneous
  157. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  158. m.Post("/markdown/raw", misc.MarkdownRaw)
  159. // Users
  160. m.Group("/users", func() {
  161. m.Get("/search", user.Search)
  162. m.Group("/:username", func() {
  163. m.Get("", user.GetInfo)
  164. m.Group("/tokens", func() {
  165. m.Combo("").Get(user.ListAccessTokens).
  166. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  167. }, reqBasicAuth())
  168. })
  169. })
  170. m.Group("/users", func() {
  171. m.Group("/:username", func() {
  172. m.Get("/keys", user.ListPublicKeys)
  173. m.Get("/followers", user.ListFollowers)
  174. m.Group("/following", func() {
  175. m.Get("", user.ListFollowing)
  176. m.Get("/:target", user.CheckFollowing)
  177. })
  178. })
  179. }, reqToken())
  180. m.Group("/user", func() {
  181. m.Get("", user.GetAuthenticatedUser)
  182. m.Combo("/emails").Get(user.ListEmails).
  183. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  184. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  185. m.Get("/followers", user.ListMyFollowers)
  186. m.Group("/following", func() {
  187. m.Get("", user.ListMyFollowing)
  188. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  189. })
  190. m.Group("/keys", func() {
  191. m.Combo("").Get(user.ListMyPublicKeys).
  192. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  193. m.Combo("/:id").Get(user.GetPublicKey).
  194. Delete(user.DeletePublicKey)
  195. })
  196. m.Combo("/issues").Get(repo.ListUserIssues)
  197. }, reqToken())
  198. // Repositories
  199. m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
  200. m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
  201. m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
  202. Post(bind(api.CreateRepoOption{}), repo.Create)
  203. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  204. m.Group("/repos", func() {
  205. m.Get("/search", repo.Search)
  206. })
  207. m.Group("/repos", func() {
  208. m.Post("/migrate", bind(form.MigrateRepo{}), repo.Migrate)
  209. m.Combo("/:username/:reponame").Get(repo.Get).
  210. Delete(repo.Delete)
  211. m.Group("/:username/:reponame", func() {
  212. m.Group("/hooks", func() {
  213. m.Combo("").Get(repo.ListHooks).
  214. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  215. m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
  216. Delete(repo.DeleteHook)
  217. })
  218. m.Group("/collaborators", func() {
  219. m.Get("", repo.ListCollaborators)
  220. m.Combo("/:collaborator").Get(repo.IsCollaborator).Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  221. Delete(repo.DeleteCollaborator)
  222. })
  223. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  224. m.Get("/archive/*", repo.GetArchive)
  225. m.Get("/forks", repo.ListForks)
  226. m.Group("/branches", func() {
  227. m.Get("", repo.ListBranches)
  228. m.Get("/*", repo.GetBranch)
  229. })
  230. m.Group("/keys", func() {
  231. m.Combo("").Get(repo.ListDeployKeys).
  232. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  233. m.Combo("/:id").Get(repo.GetDeployKey).
  234. Delete(repo.DeleteDeploykey)
  235. })
  236. m.Group("/issues", func() {
  237. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  238. m.Group("/comments", func() {
  239. m.Get("", repo.ListRepoIssueComments)
  240. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  241. })
  242. m.Group("/:index", func() {
  243. m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  244. m.Group("/comments", func() {
  245. m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  246. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  247. Delete(repo.DeleteIssueComment)
  248. })
  249. m.Group("/labels", func() {
  250. m.Combo("").Get(repo.ListIssueLabels).
  251. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  252. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  253. Delete(repo.ClearIssueLabels)
  254. m.Delete("/:id", repo.DeleteIssueLabel)
  255. })
  256. })
  257. }, mustEnableIssues)
  258. m.Group("/labels", func() {
  259. m.Combo("").Get(repo.ListLabels).
  260. Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
  261. m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  262. Delete(repo.DeleteLabel)
  263. })
  264. m.Group("/milestones", func() {
  265. m.Combo("").Get(repo.ListMilestones).
  266. Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  267. m.Combo("/:id").Get(repo.GetMilestone).
  268. Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  269. Delete(reqRepoWriter(), repo.DeleteMilestone)
  270. })
  271. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  272. }, repoAssignment())
  273. }, reqToken())
  274. m.Get("/issues", reqToken(), repo.ListUserIssues)
  275. // Organizations
  276. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  277. m.Get("/users/:username/orgs", org.ListUserOrgs)
  278. m.Group("/orgs/:orgname", func() {
  279. m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  280. m.Combo("/teams").Get(org.ListTeams)
  281. }, orgAssignment(true))
  282. m.Any("/*", func(ctx *context.Context) {
  283. ctx.Error(404)
  284. })
  285. m.Group("/admin", func() {
  286. m.Group("/users", func() {
  287. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  288. m.Group("/:username", func() {
  289. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  290. Delete(admin.DeleteUser)
  291. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  292. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  293. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  294. })
  295. })
  296. m.Group("/orgs/:orgname", func() {
  297. m.Group("/teams", func() {
  298. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  299. })
  300. })
  301. m.Group("/teams", func() {
  302. m.Group("/:teamid", func() {
  303. m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
  304. m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
  305. }, orgAssignment(false, true))
  306. })
  307. }, reqAdmin())
  308. }, context.APIContexter())
  309. }