api.go 9.8 KB

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