Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

api.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. func mustAllowPulls(ctx *context.Context) {
  150. if !ctx.Repo.Repository.AllowsPulls() {
  151. ctx.Status(404)
  152. return
  153. }
  154. }
  155. // RegisterRoutes registers all v1 APIs routes to web application.
  156. // FIXME: custom form error response
  157. func RegisterRoutes(m *macaron.Macaron) {
  158. bind := binding.Bind
  159. m.Group("/v1", func() {
  160. // Miscellaneous
  161. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  162. m.Post("/markdown/raw", misc.MarkdownRaw)
  163. // Users
  164. m.Group("/users", func() {
  165. m.Get("/search", user.Search)
  166. m.Group("/:username", func() {
  167. m.Get("", user.GetInfo)
  168. m.Group("/tokens", func() {
  169. m.Combo("").Get(user.ListAccessTokens).
  170. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  171. }, reqBasicAuth())
  172. })
  173. })
  174. m.Group("/users", func() {
  175. m.Group("/:username", func() {
  176. m.Get("/keys", user.ListPublicKeys)
  177. m.Get("/followers", user.ListFollowers)
  178. m.Group("/following", func() {
  179. m.Get("", user.ListFollowing)
  180. m.Get("/:target", user.CheckFollowing)
  181. })
  182. m.Get("/starred", user.GetStarredRepos)
  183. m.Get("/subscriptions", user.GetWatchedRepos)
  184. })
  185. }, reqToken())
  186. m.Group("/user", func() {
  187. m.Get("", user.GetAuthenticatedUser)
  188. m.Combo("/emails").Get(user.ListEmails).
  189. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  190. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  191. m.Get("/followers", user.ListMyFollowers)
  192. m.Group("/following", func() {
  193. m.Get("", user.ListMyFollowing)
  194. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  195. })
  196. m.Group("/keys", func() {
  197. m.Combo("").Get(user.ListMyPublicKeys).
  198. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  199. m.Combo("/:id").Get(user.GetPublicKey).
  200. Delete(user.DeletePublicKey)
  201. })
  202. m.Group("/starred", func() {
  203. m.Get("", user.GetMyStarredRepos)
  204. m.Group("/:username/:reponame", func() {
  205. m.Get("", user.IsStarring)
  206. m.Put("", user.Star)
  207. m.Delete("", user.Unstar)
  208. }, context.ExtractOwnerAndRepo())
  209. })
  210. m.Get("/subscriptions", user.GetMyWatchedRepos)
  211. }, reqToken())
  212. // Repositories
  213. m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
  214. Post(bind(api.CreateRepoOption{}), repo.Create)
  215. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  216. m.Group("/repos", func() {
  217. m.Get("/search", repo.Search)
  218. })
  219. m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID)
  220. m.Group("/repos", func() {
  221. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  222. m.Combo("/:username/:reponame", context.ExtractOwnerAndRepo()).
  223. Get(repo.Get).
  224. Delete(repo.Delete)
  225. m.Group("/:username/:reponame", func() {
  226. m.Group("/hooks", func() {
  227. m.Combo("").Get(repo.ListHooks).
  228. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  229. m.Combo("/:id").Get(repo.GetHook).
  230. Patch(bind(api.EditHookOption{}), repo.EditHook).
  231. Delete(repo.DeleteHook)
  232. })
  233. m.Put("/collaborators/:collaborator", bind(api.AddCollaboratorOption{}), repo.AddCollaborator)
  234. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  235. m.Get("/archive/*", repo.GetArchive)
  236. m.Group("/branches", func() {
  237. m.Get("", repo.ListBranches)
  238. m.Get("/:branchname", repo.GetBranch)
  239. })
  240. m.Group("/keys", func() {
  241. m.Combo("").Get(repo.ListDeployKeys).
  242. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  243. m.Combo("/:id").Get(repo.GetDeployKey).
  244. Delete(repo.DeleteDeploykey)
  245. })
  246. m.Group("/issues", func() {
  247. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  248. m.Group("/comments", func() {
  249. m.Get("", repo.ListRepoIssueComments)
  250. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  251. })
  252. m.Group("/:index", func() {
  253. m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  254. m.Group("/comments", func() {
  255. m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  256. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  257. Delete(repo.DeleteIssueComment)
  258. })
  259. m.Group("/labels", func() {
  260. m.Combo("").Get(repo.ListIssueLabels).
  261. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  262. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  263. Delete(repo.ClearIssueLabels)
  264. m.Delete("/:id", repo.DeleteIssueLabel)
  265. })
  266. })
  267. }, mustEnableIssues)
  268. m.Group("/labels", func() {
  269. m.Combo("").Get(repo.ListLabels).
  270. Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
  271. m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  272. Delete(repo.DeleteLabel)
  273. })
  274. m.Group("/milestones", func() {
  275. m.Combo("").Get(repo.ListMilestones).
  276. Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  277. m.Combo("/:id").Get(repo.GetMilestone).
  278. Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  279. Delete(reqRepoWriter(), repo.DeleteMilestone)
  280. })
  281. m.Group("/subscription", func() {
  282. m.Get("", user.IsWatching)
  283. m.Put("", user.Watch)
  284. m.Delete("", user.Unwatch)
  285. }, context.ExtractOwnerAndRepo())
  286. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  287. m.Group("/pulls", func() {
  288. m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
  289. m.Group("/:index", func() {
  290. m.Combo("").Get(repo.GetPullRequest).Patch(reqRepoWriter(), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
  291. m.Combo("/merge").Get(repo.IsPullRequestMerged).Post(reqRepoWriter(), repo.MergePullRequest)
  292. })
  293. }, mustAllowPulls, context.ReferencesGitRepo())
  294. }, repoAssignment())
  295. }, reqToken())
  296. // Organizations
  297. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  298. m.Get("/users/:username/orgs", org.ListUserOrgs)
  299. m.Group("/orgs/:orgname", func() {
  300. m.Combo("").Get(org.Get).Patch(bind(api.EditOrgOption{}), org.Edit)
  301. m.Combo("/teams").Get(org.ListTeams)
  302. m.Group("/hooks", func() {
  303. m.Combo("").Get(org.ListHooks).
  304. Post(bind(api.CreateHookOption{}), org.CreateHook)
  305. m.Combo("/:id").Get(org.GetHook).
  306. Patch(bind(api.EditHookOption{}), org.EditHook).
  307. Delete(org.DeleteHook)
  308. })
  309. }, orgAssignment(true))
  310. m.Any("/*", func(ctx *context.Context) {
  311. ctx.Error(404)
  312. })
  313. m.Group("/admin", func() {
  314. m.Group("/users", func() {
  315. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  316. m.Group("/:username", func() {
  317. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  318. Delete(admin.DeleteUser)
  319. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  320. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  321. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  322. })
  323. })
  324. m.Group("/orgs/:orgname", func() {
  325. m.Group("/teams", func() {
  326. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  327. })
  328. })
  329. m.Group("/teams", func() {
  330. m.Group("/:teamid", func() {
  331. m.Combo("/members/:username").Put(admin.AddTeamMember).Delete(admin.RemoveTeamMember)
  332. m.Combo("/repos/:reponame").Put(admin.AddTeamRepository).Delete(admin.RemoveTeamRepository)
  333. }, orgAssignment(false, true))
  334. })
  335. }, reqAdmin())
  336. }, context.APIContexter())
  337. }