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

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