api.go 9.8 KB

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