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

api.go 13 kB

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. }
  52. repo.Owner = owner
  53. if ctx.IsSigned && ctx.User.IsAdmin {
  54. ctx.Repo.AccessMode = models.AccessModeOwner
  55. } else {
  56. mode, err := models.AccessLevel(ctx.User, repo)
  57. if err != nil {
  58. ctx.Error(500, "AccessLevel", err)
  59. return
  60. }
  61. ctx.Repo.AccessMode = mode
  62. }
  63. if !ctx.Repo.HasAccess() {
  64. ctx.Status(404)
  65. return
  66. }
  67. ctx.Repo.Repository = repo
  68. }
  69. }
  70. // Contexter middleware already checks token for user sign in process.
  71. func reqToken() macaron.Handler {
  72. return func(ctx *context.Context) {
  73. if !ctx.IsSigned {
  74. ctx.Error(401)
  75. return
  76. }
  77. }
  78. }
  79. func reqBasicAuth() macaron.Handler {
  80. return func(ctx *context.Context) {
  81. if !ctx.IsBasicAuth {
  82. ctx.Error(401)
  83. return
  84. }
  85. }
  86. }
  87. func reqAdmin() macaron.Handler {
  88. return func(ctx *context.Context) {
  89. if !ctx.IsSigned || !ctx.User.IsAdmin {
  90. ctx.Error(403)
  91. return
  92. }
  93. }
  94. }
  95. func reqRepoWriter() macaron.Handler {
  96. return func(ctx *context.Context) {
  97. if !ctx.Repo.IsWriter() {
  98. ctx.Error(403)
  99. return
  100. }
  101. }
  102. }
  103. func reqOrgMembership() macaron.Handler {
  104. return func(ctx *context.APIContext) {
  105. var orgID int64
  106. if ctx.Org.Organization != nil {
  107. orgID = ctx.Org.Organization.ID
  108. } else if ctx.Org.Team != nil {
  109. orgID = ctx.Org.Team.OrgID
  110. } else {
  111. ctx.Error(500, "", "reqOrgMembership: unprepared context")
  112. return
  113. }
  114. if !models.IsOrganizationMember(orgID, ctx.User.ID) {
  115. if ctx.Org.Organization != nil {
  116. ctx.Error(403, "", "Must be an organization member")
  117. } else {
  118. ctx.Status(404)
  119. }
  120. return
  121. }
  122. }
  123. }
  124. func reqOrgOwnership() macaron.Handler {
  125. return func(ctx *context.APIContext) {
  126. var orgID int64
  127. if ctx.Org.Organization != nil {
  128. orgID = ctx.Org.Organization.ID
  129. } else if ctx.Org.Team != nil {
  130. orgID = ctx.Org.Team.OrgID
  131. } else {
  132. ctx.Error(500, "", "reqOrgOwnership: unprepared context")
  133. return
  134. }
  135. if !models.IsOrganizationOwner(orgID, ctx.User.ID) {
  136. if ctx.Org.Organization != nil {
  137. ctx.Error(403, "", "Must be an organization owner")
  138. } else {
  139. ctx.Status(404)
  140. }
  141. return
  142. }
  143. }
  144. }
  145. func orgAssignment(args ...bool) macaron.Handler {
  146. var (
  147. assignOrg bool
  148. assignTeam bool
  149. )
  150. if len(args) > 0 {
  151. assignOrg = args[0]
  152. }
  153. if len(args) > 1 {
  154. assignTeam = args[1]
  155. }
  156. return func(ctx *context.APIContext) {
  157. ctx.Org = new(context.APIOrganization)
  158. var err error
  159. if assignOrg {
  160. ctx.Org.Organization, err = models.GetUserByName(ctx.Params(":orgname"))
  161. if err != nil {
  162. if models.IsErrUserNotExist(err) {
  163. ctx.Status(404)
  164. } else {
  165. ctx.Error(500, "GetUserByName", err)
  166. }
  167. return
  168. }
  169. }
  170. if assignTeam {
  171. ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
  172. if err != nil {
  173. if models.IsErrUserNotExist(err) {
  174. ctx.Status(404)
  175. } else {
  176. ctx.Error(500, "GetTeamById", err)
  177. }
  178. return
  179. }
  180. }
  181. }
  182. }
  183. func mustEnableIssues(ctx *context.APIContext) {
  184. if !ctx.Repo.Repository.EnableUnit(models.UnitTypeIssues) {
  185. ctx.Status(404)
  186. return
  187. }
  188. }
  189. func mustAllowPulls(ctx *context.Context) {
  190. if !ctx.Repo.Repository.AllowsPulls() {
  191. ctx.Status(404)
  192. return
  193. }
  194. }
  195. // RegisterRoutes registers all v1 APIs routes to web application.
  196. // FIXME: custom form error response
  197. func RegisterRoutes(m *macaron.Macaron) {
  198. bind := binding.Bind
  199. m.Group("/v1", func() {
  200. // Miscellaneous
  201. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  202. m.Post("/markdown/raw", misc.MarkdownRaw)
  203. // Users
  204. m.Group("/users", func() {
  205. m.Get("/search", user.Search)
  206. m.Group("/:username", func() {
  207. m.Get("", user.GetInfo)
  208. m.Group("/tokens", func() {
  209. m.Combo("").Get(user.ListAccessTokens).
  210. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  211. }, reqBasicAuth())
  212. })
  213. })
  214. m.Group("/users", func() {
  215. m.Group("/:username", func() {
  216. m.Get("/keys", user.ListPublicKeys)
  217. m.Get("/followers", user.ListFollowers)
  218. m.Group("/following", func() {
  219. m.Get("", user.ListFollowing)
  220. m.Get("/:target", user.CheckFollowing)
  221. })
  222. m.Get("/starred", user.GetStarredRepos)
  223. m.Get("/subscriptions", user.GetWatchedRepos)
  224. })
  225. }, reqToken())
  226. m.Group("/user", func() {
  227. m.Get("", user.GetAuthenticatedUser)
  228. m.Combo("/emails").Get(user.ListEmails).
  229. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  230. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  231. m.Get("/followers", user.ListMyFollowers)
  232. m.Group("/following", func() {
  233. m.Get("", user.ListMyFollowing)
  234. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  235. })
  236. m.Group("/keys", func() {
  237. m.Combo("").Get(user.ListMyPublicKeys).
  238. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  239. m.Combo("/:id").Get(user.GetPublicKey).
  240. Delete(user.DeletePublicKey)
  241. })
  242. m.Group("/starred", func() {
  243. m.Get("", user.GetMyStarredRepos)
  244. m.Group("/:username/:reponame", func() {
  245. m.Get("", user.IsStarring)
  246. m.Put("", user.Star)
  247. m.Delete("", user.Unstar)
  248. }, repoAssignment())
  249. })
  250. m.Get("/subscriptions", user.GetMyWatchedRepos)
  251. }, reqToken())
  252. // Repositories
  253. m.Combo("/user/repos", reqToken()).Get(repo.ListMyRepos).
  254. Post(bind(api.CreateRepoOption{}), repo.Create)
  255. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  256. m.Group("/repos", func() {
  257. m.Get("/search", repo.Search)
  258. })
  259. m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID)
  260. m.Group("/repos", func() {
  261. m.Post("/migrate", bind(auth.MigrateRepoForm{}), repo.Migrate)
  262. m.Group("/:username/:reponame", func() {
  263. m.Combo("").Get(repo.Get).Delete(repo.Delete)
  264. m.Group("/hooks", func() {
  265. m.Combo("").Get(repo.ListHooks).
  266. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  267. m.Combo("/:id").Get(repo.GetHook).
  268. Patch(bind(api.EditHookOption{}), repo.EditHook).
  269. Delete(repo.DeleteHook)
  270. }, reqRepoWriter())
  271. m.Group("/collaborators", func() {
  272. m.Get("", repo.ListCollaborators)
  273. m.Combo("/:collaborator").Get(repo.IsCollaborator).
  274. Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  275. Delete(repo.DeleteCollaborator)
  276. })
  277. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  278. m.Get("/archive/*", repo.GetArchive)
  279. m.Combo("/forks").Get(repo.ListForks).
  280. Post(bind(api.CreateForkOption{}), repo.CreateFork)
  281. m.Group("/branches", func() {
  282. m.Get("", repo.ListBranches)
  283. m.Get("/:branchname", repo.GetBranch)
  284. })
  285. m.Group("/keys", func() {
  286. m.Combo("").Get(repo.ListDeployKeys).
  287. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  288. m.Combo("/:id").Get(repo.GetDeployKey).
  289. Delete(repo.DeleteDeploykey)
  290. })
  291. m.Group("/issues", func() {
  292. m.Combo("").Get(repo.ListIssues).Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  293. m.Group("/comments", func() {
  294. m.Get("", repo.ListRepoIssueComments)
  295. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  296. })
  297. m.Group("/:index", func() {
  298. m.Combo("").Get(repo.GetIssue).Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  299. m.Group("/comments", func() {
  300. m.Combo("").Get(repo.ListIssueComments).Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  301. m.Combo("/:id").Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  302. Delete(repo.DeleteIssueComment)
  303. })
  304. m.Group("/labels", func() {
  305. m.Combo("").Get(repo.ListIssueLabels).
  306. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  307. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  308. Delete(repo.ClearIssueLabels)
  309. m.Delete("/:id", repo.DeleteIssueLabel)
  310. })
  311. })
  312. }, mustEnableIssues)
  313. m.Group("/labels", func() {
  314. m.Combo("").Get(repo.ListLabels).
  315. Post(bind(api.CreateLabelOption{}), repo.CreateLabel)
  316. m.Combo("/:id").Get(repo.GetLabel).Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  317. Delete(repo.DeleteLabel)
  318. })
  319. m.Group("/milestones", func() {
  320. m.Combo("").Get(repo.ListMilestones).
  321. Post(reqRepoWriter(), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  322. m.Combo("/:id").Get(repo.GetMilestone).
  323. Patch(reqRepoWriter(), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  324. Delete(reqRepoWriter(), repo.DeleteMilestone)
  325. })
  326. m.Get("/stargazers", repo.ListStargazers)
  327. m.Get("/subscribers", repo.ListSubscribers)
  328. m.Group("/subscription", func() {
  329. m.Get("", user.IsWatching)
  330. m.Put("", user.Watch)
  331. m.Delete("", user.Unwatch)
  332. })
  333. m.Group("/releases", func() {
  334. m.Combo("").Get(repo.ListReleases).
  335. Post(bind(api.CreateReleaseOption{}), repo.CreateRelease)
  336. m.Combo("/:id").Get(repo.GetRelease).
  337. Patch(bind(api.EditReleaseOption{}), repo.EditRelease).
  338. Delete(repo.DeleteRelease)
  339. })
  340. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  341. m.Group("/pulls", func() {
  342. m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).Post(reqRepoWriter(), bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
  343. m.Group("/:index", func() {
  344. m.Combo("").Get(repo.GetPullRequest).Patch(reqRepoWriter(), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
  345. m.Combo("/merge").Get(repo.IsPullRequestMerged).Post(reqRepoWriter(), repo.MergePullRequest)
  346. })
  347. }, mustAllowPulls, context.ReferencesGitRepo())
  348. }, repoAssignment())
  349. }, reqToken())
  350. // Organizations
  351. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  352. m.Get("/users/:username/orgs", org.ListUserOrgs)
  353. m.Group("/orgs/:orgname", func() {
  354. m.Combo("").Get(org.Get).
  355. Patch(reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit)
  356. m.Group("/members", func() {
  357. m.Get("", org.ListMembers)
  358. m.Combo("/:username").Get(org.IsMember).
  359. Delete(reqOrgOwnership(), org.DeleteMember)
  360. })
  361. m.Group("/public_members", func() {
  362. m.Get("", org.ListPublicMembers)
  363. m.Combo("/:username").Get(org.IsPublicMember).
  364. Put(reqOrgMembership(), org.PublicizeMember).
  365. Delete(reqOrgMembership(), org.ConcealMember)
  366. })
  367. m.Combo("/teams", reqOrgMembership()).Get(org.ListTeams).
  368. Post(bind(api.CreateTeamOption{}), org.CreateTeam)
  369. m.Group("/hooks", func() {
  370. m.Combo("").Get(org.ListHooks).
  371. Post(bind(api.CreateHookOption{}), org.CreateHook)
  372. m.Combo("/:id").Get(org.GetHook).
  373. Patch(reqOrgOwnership(), bind(api.EditHookOption{}), org.EditHook).
  374. Delete(reqOrgOwnership(), org.DeleteHook)
  375. }, reqOrgMembership())
  376. }, orgAssignment(true))
  377. m.Group("/teams/:teamid", func() {
  378. m.Combo("").Get(org.GetTeam).
  379. Patch(reqOrgOwnership(), bind(api.EditTeamOption{}), org.EditTeam).
  380. Delete(reqOrgOwnership(), org.DeleteTeam)
  381. m.Group("/members", func() {
  382. m.Get("", org.GetTeamMembers)
  383. m.Combo("/:username").
  384. Put(reqOrgOwnership(), org.AddTeamMember).
  385. Delete(reqOrgOwnership(), org.RemoveTeamMember)
  386. })
  387. m.Group("/repos", func() {
  388. m.Get("", org.GetTeamRepos)
  389. m.Combo(":orgname/:reponame").
  390. Put(org.AddTeamRepository).
  391. Delete(org.RemoveTeamRepository)
  392. })
  393. }, reqOrgMembership(), orgAssignment(false, true))
  394. m.Any("/*", func(ctx *context.Context) {
  395. ctx.Error(404)
  396. })
  397. m.Group("/admin", func() {
  398. m.Group("/users", func() {
  399. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  400. m.Group("/:username", func() {
  401. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  402. Delete(admin.DeleteUser)
  403. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  404. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  405. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  406. })
  407. })
  408. }, reqAdmin())
  409. }, context.APIContexter())
  410. }