You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
9.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  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 "code.gitea.io/sdk/gitea"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/auth"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/routers/api/v1/admin"
  14. "code.gitea.io/gitea/routers/api/v1/misc"
  15. "code.gitea.io/gitea/routers/api/v1/org"
  16. "code.gitea.io/gitea/routers/api/v1/repo"
  17. "code.gitea.io/gitea/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.AccessModeOwner
  57. } else {
  58. mode, err := models.AccessLevel(ctx.User, 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. // Miscellaneous
  155. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  156. m.Post("/markdown/raw", misc.MarkdownRaw)
  157. // Users
  158. m.Group("/users", func() {
  159. m.Get("/search", user.Search)
  160. m.Group("/:username", func() {
  161. m.Get("", user.GetInfo)
  162. m.Group("/tokens", func() {
  163. m.Combo("").Get(user.ListAccessTokens).
  164. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  165. }, reqBasicAuth())
  166. })
  167. })
  168. m.Group("/users", func() {
  169. m.Group("/:username", func() {
  170. m.Get("/keys", user.ListPublicKeys)
  171. m.Get("/followers", user.ListFollowers)
  172. m.Group("/following", func() {
  173. m.Get("", user.ListFollowing)
  174. m.Get("/:target", user.CheckFollowing)
  175. })
  176. m.Get("/starred", user.GetStarredRepos)
  177. })
  178. }, reqToken())
  179. m.Group("/user", func() {
  180. m.Get("", user.GetAuthenticatedUser)
  181. m.Combo("/emails").Get(user.ListEmails).
  182. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  183. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  184. m.Get("/followers", user.ListMyFollowers)
  185. m.Group("/following", func() {
  186. m.Get("", user.ListMyFollowing)
  187. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  188. })
  189. m.Group("/keys", func() {
  190. m.Combo("").Get(user.ListMyPublicKeys).
  191. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  192. m.Combo("/:id").Get(user.GetPublicKey).
  193. Delete(user.DeletePublicKey)
  194. })
  195. m.Group("/starred", func() {
  196. m.Get("", user.GetMyStarredRepos)
  197. m.Group("/:username/:reponame", func() {
  198. m.Get("", user.IsStarring)
  199. m.Put("", user.Star)
  200. m.Delete("", user.Unstar)
  201. }, context.ExtractOwnerAndRepo())
  202. })
  203. }, reqToken())
  204. // Repositories
  205. m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
  206. Post(bind(api.CreateRepoOption{}), repo.Create)
  207. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  208. m.Group("/repos", func() {
  209. m.Get("/search", repo.Search)
  210. })
  211. m.Group("/repos", func() {
  212. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  213. m.Combo("/:username/:reponame", context.ExtractOwnerAndRepo()).
  214. Get(repo.Get).
  215. Delete(repo.Delete)
  216. m.Group("/:username/:reponame", func() {
  217. m.Group("/hooks", func() {
  218. m.Combo("").Get(repo.ListHooks).
  219. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  220. m.Combo("/:id").Patch(bind(api.EditHookOption{}), repo.EditHook).
  221. Delete(repo.DeleteHook)
  222. })
  223. m.Put("/collaborators/:collaborator", bind(api.AddCollaboratorOption{}), repo.AddCollaborator)
  224. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  225. m.Get("/archive/*", repo.GetArchive)
  226. m.Group("/branches", func() {
  227. m.Get("", repo.ListBranches)
  228. m.Get("/:branchname", 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("/:index", func() {
  239. m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  240. m.Group("/comments", func() {
  241. m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  242. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  243. })
  244. m.Group("/labels", func() {
  245. m.Combo("").Get(repo.ListIssueLabels).
  246. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  247. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  248. Delete(repo.ClearIssueLabels)
  249. m.Delete("/:id", repo.DeleteIssueLabel)
  250. })
  251. })
  252. }, mustEnableIssues)
  253. m.Group("/labels", func() {
  254. m.Combo("").Get(repo.ListLabels).
  255. Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
  256. m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  257. Delete(repo.DeleteLabel)
  258. })
  259. m.Group("/milestones", func() {
  260. m.Combo("").Get(repo.ListMilestones).
  261. Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  262. m.Combo("/:id").Get(repo.GetMilestone).
  263. Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  264. Delete(reqRepoWriter(), repo.DeleteMilestone)
  265. })
  266. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  267. }, repoAssignment())
  268. }, reqToken())
  269. // Organizations
  270. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  271. m.Get("/users/:username/orgs", org.ListUserOrgs)
  272. m.Group("/orgs/:orgname", func() {
  273. m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  274. m.Combo("/teams").Get(org.ListTeams)
  275. }, orgAssignment(true))
  276. m.Any("/*", func(ctx *context.Context) {
  277. ctx.Error(404)
  278. })
  279. m.Group("/admin", func() {
  280. m.Group("/users", func() {
  281. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  282. m.Group("/:username", func() {
  283. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  284. Delete(admin.DeleteUser)
  285. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  286. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  287. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  288. })
  289. })
  290. m.Group("/orgs/:orgname", func() {
  291. m.Group("/teams", func() {
  292. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  293. })
  294. })
  295. m.Group("/teams", func() {
  296. m.Group("/:teamid", func() {
  297. m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
  298. m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
  299. }, orgAssignment(false, true))
  300. })
  301. }, reqAdmin())
  302. }, context.APIContexter())
  303. }