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.

api.go 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Package v1 Gitea API.
  6. //
  7. // This documentation describes the Gitea API.
  8. //
  9. // Schemes: http, https
  10. // BasePath: /api/v1
  11. // Version: 1.1.1
  12. // License: MIT http://opensource.org/licenses/MIT
  13. //
  14. // Consumes:
  15. // - application/json
  16. // - text/plain
  17. //
  18. // Produces:
  19. // - application/json
  20. // - text/html
  21. //
  22. // Security:
  23. // - BasicAuth :
  24. // - Token :
  25. // - AccessToken :
  26. // - AuthorizationHeaderToken :
  27. // - SudoParam :
  28. // - SudoHeader :
  29. //
  30. // SecurityDefinitions:
  31. // BasicAuth:
  32. // type: basic
  33. // Token:
  34. // type: apiKey
  35. // name: token
  36. // in: query
  37. // AccessToken:
  38. // type: apiKey
  39. // name: access_token
  40. // in: query
  41. // AuthorizationHeaderToken:
  42. // type: apiKey
  43. // name: Authorization
  44. // in: header
  45. // SudoParam:
  46. // type: apiKey
  47. // name: sudo
  48. // in: query
  49. // description: Sudo API request as the user provided as the key. Admin privileges are required.
  50. // SudoHeader:
  51. // type: apiKey
  52. // name: Sudo
  53. // in: header
  54. // description: Sudo API request as the user provided as the key. Admin privileges are required.
  55. //
  56. // swagger:meta
  57. package v1
  58. import (
  59. "strings"
  60. "code.gitea.io/gitea/models"
  61. "code.gitea.io/gitea/modules/auth"
  62. "code.gitea.io/gitea/modules/context"
  63. "code.gitea.io/gitea/modules/log"
  64. "code.gitea.io/gitea/modules/setting"
  65. "code.gitea.io/gitea/routers/api/v1/admin"
  66. "code.gitea.io/gitea/routers/api/v1/misc"
  67. "code.gitea.io/gitea/routers/api/v1/org"
  68. "code.gitea.io/gitea/routers/api/v1/repo"
  69. _ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
  70. "code.gitea.io/gitea/routers/api/v1/user"
  71. api "code.gitea.io/sdk/gitea"
  72. "github.com/go-macaron/binding"
  73. "gopkg.in/macaron.v1"
  74. )
  75. func sudo() macaron.Handler {
  76. return func(ctx *context.APIContext) {
  77. sudo := ctx.Query("sudo")
  78. if len(sudo) == 0 {
  79. sudo = ctx.Req.Header.Get("Sudo")
  80. }
  81. if len(sudo) > 0 {
  82. if ctx.IsSigned && ctx.User.IsAdmin {
  83. user, err := models.GetUserByName(sudo)
  84. if err != nil {
  85. if models.IsErrUserNotExist(err) {
  86. ctx.NotFound()
  87. } else {
  88. ctx.Error(500, "GetUserByName", err)
  89. }
  90. return
  91. }
  92. log.Trace("Sudo from (%s) to: %s", ctx.User.Name, user.Name)
  93. ctx.User = user
  94. } else {
  95. ctx.JSON(403, map[string]string{
  96. "message": "Only administrators allowed to sudo.",
  97. })
  98. return
  99. }
  100. }
  101. }
  102. }
  103. func repoAssignment() macaron.Handler {
  104. return func(ctx *context.APIContext) {
  105. userName := ctx.Params(":username")
  106. repoName := ctx.Params(":reponame")
  107. var (
  108. owner *models.User
  109. err error
  110. )
  111. // Check if the user is the same as the repository owner.
  112. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  113. owner = ctx.User
  114. } else {
  115. owner, err = models.GetUserByName(userName)
  116. if err != nil {
  117. if models.IsErrUserNotExist(err) {
  118. ctx.NotFound()
  119. } else {
  120. ctx.Error(500, "GetUserByName", err)
  121. }
  122. return
  123. }
  124. }
  125. ctx.Repo.Owner = owner
  126. // Get repository.
  127. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  128. if err != nil {
  129. if models.IsErrRepoNotExist(err) {
  130. redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
  131. if err == nil {
  132. context.RedirectToRepo(ctx.Context, redirectRepoID)
  133. } else if models.IsErrRepoRedirectNotExist(err) {
  134. ctx.NotFound()
  135. } else {
  136. ctx.Error(500, "LookupRepoRedirect", err)
  137. }
  138. } else {
  139. ctx.Error(500, "GetRepositoryByName", err)
  140. }
  141. return
  142. }
  143. repo.Owner = owner
  144. ctx.Repo.Repository = repo
  145. ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.User)
  146. if err != nil {
  147. ctx.Error(500, "GetUserRepoPermission", err)
  148. return
  149. }
  150. if !ctx.Repo.HasAccess() {
  151. ctx.NotFound()
  152. return
  153. }
  154. }
  155. }
  156. // Contexter middleware already checks token for user sign in process.
  157. func reqToken() macaron.Handler {
  158. return func(ctx *context.APIContext) {
  159. if true == ctx.Data["IsApiToken"] {
  160. return
  161. }
  162. if ctx.IsSigned {
  163. ctx.RequireCSRF()
  164. return
  165. }
  166. ctx.Context.Error(401)
  167. }
  168. }
  169. func reqBasicAuth() macaron.Handler {
  170. return func(ctx *context.Context) {
  171. if !ctx.IsBasicAuth {
  172. ctx.Error(401)
  173. return
  174. }
  175. }
  176. }
  177. // reqSiteAdmin user should be the site admin
  178. func reqSiteAdmin() macaron.Handler {
  179. return func(ctx *context.Context) {
  180. if !ctx.IsUserSiteAdmin() {
  181. ctx.Error(403)
  182. return
  183. }
  184. }
  185. }
  186. // reqOwner user should be the owner of the repo or site admin.
  187. func reqOwner() macaron.Handler {
  188. return func(ctx *context.Context) {
  189. if !ctx.IsUserRepoOwner() && !ctx.IsUserSiteAdmin() {
  190. ctx.Error(403)
  191. return
  192. }
  193. }
  194. }
  195. // reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
  196. func reqAdmin() macaron.Handler {
  197. return func(ctx *context.Context) {
  198. if !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  199. ctx.Error(403)
  200. return
  201. }
  202. }
  203. }
  204. // reqRepoWriter user should have a permission to write to a repo, or be a site admin
  205. func reqRepoWriter(unitTypes ...models.UnitType) macaron.Handler {
  206. return func(ctx *context.Context) {
  207. if !ctx.IsUserRepoWriter(unitTypes) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  208. ctx.Error(403)
  209. return
  210. }
  211. }
  212. }
  213. // reqRepoReader user should have specific read permission or be a repo admin or a site admin
  214. func reqRepoReader(unitType models.UnitType) macaron.Handler {
  215. return func(ctx *context.Context) {
  216. if !ctx.IsUserRepoReaderSpecific(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  217. ctx.Error(403)
  218. return
  219. }
  220. }
  221. }
  222. // reqAnyRepoReader user should have any permission to read repository or permissions of site admin
  223. func reqAnyRepoReader() macaron.Handler {
  224. return func(ctx *context.Context) {
  225. if !ctx.IsUserRepoReaderAny() && !ctx.IsUserSiteAdmin() {
  226. ctx.Error(403)
  227. return
  228. }
  229. }
  230. }
  231. // reqOrgOwnership user should be an organization owner, or a site admin
  232. func reqOrgOwnership() macaron.Handler {
  233. return func(ctx *context.APIContext) {
  234. if ctx.Context.IsUserSiteAdmin() {
  235. return
  236. }
  237. var orgID int64
  238. if ctx.Org.Organization != nil {
  239. orgID = ctx.Org.Organization.ID
  240. } else if ctx.Org.Team != nil {
  241. orgID = ctx.Org.Team.OrgID
  242. } else {
  243. ctx.Error(500, "", "reqOrgOwnership: unprepared context")
  244. return
  245. }
  246. isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
  247. if err != nil {
  248. ctx.Error(500, "IsOrganizationOwner", err)
  249. return
  250. } else if !isOwner {
  251. if ctx.Org.Organization != nil {
  252. ctx.Error(403, "", "Must be an organization owner")
  253. } else {
  254. ctx.NotFound()
  255. }
  256. return
  257. }
  258. }
  259. }
  260. // reqOrgMembership user should be an organization member, or a site admin
  261. func reqOrgMembership() macaron.Handler {
  262. return func(ctx *context.APIContext) {
  263. if ctx.Context.IsUserSiteAdmin() {
  264. return
  265. }
  266. var orgID int64
  267. if ctx.Org.Organization != nil {
  268. orgID = ctx.Org.Organization.ID
  269. } else if ctx.Org.Team != nil {
  270. orgID = ctx.Org.Team.OrgID
  271. } else {
  272. ctx.Error(500, "", "reqOrgMembership: unprepared context")
  273. return
  274. }
  275. if isMember, err := models.IsOrganizationMember(orgID, ctx.User.ID); err != nil {
  276. ctx.Error(500, "IsOrganizationMember", err)
  277. return
  278. } else if !isMember {
  279. if ctx.Org.Organization != nil {
  280. ctx.Error(403, "", "Must be an organization member")
  281. } else {
  282. ctx.NotFound()
  283. }
  284. return
  285. }
  286. }
  287. }
  288. func orgAssignment(args ...bool) macaron.Handler {
  289. var (
  290. assignOrg bool
  291. assignTeam bool
  292. )
  293. if len(args) > 0 {
  294. assignOrg = args[0]
  295. }
  296. if len(args) > 1 {
  297. assignTeam = args[1]
  298. }
  299. return func(ctx *context.APIContext) {
  300. ctx.Org = new(context.APIOrganization)
  301. var err error
  302. if assignOrg {
  303. ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":orgname"))
  304. if err != nil {
  305. if models.IsErrOrgNotExist(err) {
  306. ctx.NotFound()
  307. } else {
  308. ctx.Error(500, "GetOrgByName", err)
  309. }
  310. return
  311. }
  312. }
  313. if assignTeam {
  314. ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
  315. if err != nil {
  316. if models.IsErrUserNotExist(err) {
  317. ctx.NotFound()
  318. } else {
  319. ctx.Error(500, "GetTeamById", err)
  320. }
  321. return
  322. }
  323. }
  324. }
  325. }
  326. func mustEnableIssues(ctx *context.APIContext) {
  327. if !ctx.Repo.CanRead(models.UnitTypeIssues) {
  328. ctx.NotFound()
  329. return
  330. }
  331. }
  332. func mustAllowPulls(ctx *context.APIContext) {
  333. if !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
  334. ctx.NotFound()
  335. return
  336. }
  337. }
  338. func mustEnableIssuesOrPulls(ctx *context.APIContext) {
  339. if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
  340. !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
  341. ctx.NotFound()
  342. return
  343. }
  344. }
  345. func mustEnableUserHeatmap(ctx *context.APIContext) {
  346. if !setting.Service.EnableUserHeatmap {
  347. ctx.NotFound()
  348. return
  349. }
  350. }
  351. func mustNotBeArchived(ctx *context.APIContext) {
  352. if ctx.Repo.Repository.IsArchived {
  353. ctx.NotFound()
  354. return
  355. }
  356. }
  357. // RegisterRoutes registers all v1 APIs routes to web application.
  358. // FIXME: custom form error response
  359. func RegisterRoutes(m *macaron.Macaron) {
  360. bind := binding.Bind
  361. if setting.API.EnableSwagger {
  362. m.Get("/swagger", misc.Swagger) //Render V1 by default
  363. }
  364. m.Group("/v1", func() {
  365. // Miscellaneous
  366. if setting.API.EnableSwagger {
  367. m.Get("/swagger", misc.Swagger)
  368. }
  369. m.Get("/version", misc.Version)
  370. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  371. m.Post("/markdown/raw", misc.MarkdownRaw)
  372. // Users
  373. m.Group("/users", func() {
  374. m.Get("/search", user.Search)
  375. m.Group("/:username", func() {
  376. m.Get("", user.GetInfo)
  377. m.Get("/heatmap", mustEnableUserHeatmap, user.GetUserHeatmapData)
  378. m.Get("/repos", user.ListUserRepos)
  379. m.Group("/tokens", func() {
  380. m.Combo("").Get(user.ListAccessTokens).
  381. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  382. m.Combo("/:id").Delete(user.DeleteAccessToken)
  383. }, reqBasicAuth())
  384. })
  385. })
  386. m.Group("/users", func() {
  387. m.Group("/:username", func() {
  388. m.Get("/keys", user.ListPublicKeys)
  389. m.Get("/gpg_keys", user.ListGPGKeys)
  390. m.Get("/followers", user.ListFollowers)
  391. m.Group("/following", func() {
  392. m.Get("", user.ListFollowing)
  393. m.Get("/:target", user.CheckFollowing)
  394. })
  395. m.Get("/starred", user.GetStarredRepos)
  396. m.Get("/subscriptions", user.GetWatchedRepos)
  397. })
  398. }, reqToken())
  399. m.Group("/user", func() {
  400. m.Get("", user.GetAuthenticatedUser)
  401. m.Combo("/emails").Get(user.ListEmails).
  402. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  403. Delete(bind(api.DeleteEmailOption{}), user.DeleteEmail)
  404. m.Get("/followers", user.ListMyFollowers)
  405. m.Group("/following", func() {
  406. m.Get("", user.ListMyFollowing)
  407. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  408. })
  409. m.Group("/keys", func() {
  410. m.Combo("").Get(user.ListMyPublicKeys).
  411. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  412. m.Combo("/:id").Get(user.GetPublicKey).
  413. Delete(user.DeletePublicKey)
  414. })
  415. m.Group("/gpg_keys", func() {
  416. m.Combo("").Get(user.ListMyGPGKeys).
  417. Post(bind(api.CreateGPGKeyOption{}), user.CreateGPGKey)
  418. m.Combo("/:id").Get(user.GetGPGKey).
  419. Delete(user.DeleteGPGKey)
  420. })
  421. m.Combo("/repos").Get(user.ListMyRepos).
  422. Post(bind(api.CreateRepoOption{}), repo.Create)
  423. m.Group("/starred", func() {
  424. m.Get("", user.GetMyStarredRepos)
  425. m.Group("/:username/:reponame", func() {
  426. m.Get("", user.IsStarring)
  427. m.Put("", user.Star)
  428. m.Delete("", user.Unstar)
  429. }, repoAssignment())
  430. })
  431. m.Get("/times", repo.ListMyTrackedTimes)
  432. m.Get("/subscriptions", user.GetMyWatchedRepos)
  433. m.Get("/teams", org.ListUserTeams)
  434. }, reqToken())
  435. // Repositories
  436. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  437. m.Group("/repos", func() {
  438. m.Get("/search", repo.Search)
  439. })
  440. m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID)
  441. m.Group("/repos", func() {
  442. m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate)
  443. m.Group("/:username/:reponame", func() {
  444. m.Combo("").Get(reqAnyRepoReader(), repo.Get).
  445. Delete(reqToken(), reqOwner(), repo.Delete)
  446. m.Group("/hooks", func() {
  447. m.Combo("").Get(repo.ListHooks).
  448. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  449. m.Group("/:id", func() {
  450. m.Combo("").Get(repo.GetHook).
  451. Patch(bind(api.EditHookOption{}), repo.EditHook).
  452. Delete(repo.DeleteHook)
  453. m.Post("/tests", context.RepoRef(), repo.TestHook)
  454. })
  455. }, reqToken(), reqAdmin())
  456. m.Group("/collaborators", func() {
  457. m.Get("", repo.ListCollaborators)
  458. m.Combo("/:collaborator").Get(repo.IsCollaborator).
  459. Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  460. Delete(repo.DeleteCollaborator)
  461. }, reqToken(), reqAdmin())
  462. m.Get("/raw/*", context.RepoRefByType(context.RepoRefAny), reqRepoReader(models.UnitTypeCode), repo.GetRawFile)
  463. m.Get("/archive/*", reqRepoReader(models.UnitTypeCode), repo.GetArchive)
  464. m.Combo("/forks").Get(repo.ListForks).
  465. Post(reqToken(), reqRepoReader(models.UnitTypeCode), bind(api.CreateForkOption{}), repo.CreateFork)
  466. m.Group("/branches", func() {
  467. m.Get("", repo.ListBranches)
  468. m.Get("/*", context.RepoRefByType(context.RepoRefBranch), repo.GetBranch)
  469. }, reqRepoReader(models.UnitTypeCode))
  470. m.Group("/tags", func() {
  471. m.Get("", repo.ListTags)
  472. }, reqRepoReader(models.UnitTypeCode))
  473. m.Group("/keys", func() {
  474. m.Combo("").Get(repo.ListDeployKeys).
  475. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  476. m.Combo("/:id").Get(repo.GetDeployKey).
  477. Delete(repo.DeleteDeploykey)
  478. }, reqToken(), reqAdmin())
  479. m.Group("/times", func() {
  480. m.Combo("").Get(repo.ListTrackedTimesByRepository)
  481. m.Combo("/:timetrackingusername").Get(repo.ListTrackedTimesByUser)
  482. }, mustEnableIssues)
  483. m.Group("/issues", func() {
  484. m.Combo("").Get(repo.ListIssues).
  485. Post(reqToken(), mustNotBeArchived, bind(api.CreateIssueOption{}), repo.CreateIssue)
  486. m.Group("/comments", func() {
  487. m.Get("", repo.ListRepoIssueComments)
  488. m.Combo("/:id", reqToken()).
  489. Patch(mustNotBeArchived, bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  490. Delete(repo.DeleteIssueComment)
  491. })
  492. m.Group("/:index", func() {
  493. m.Combo("").Get(repo.GetIssue).
  494. Patch(reqToken(), bind(api.EditIssueOption{}), repo.EditIssue)
  495. m.Group("/comments", func() {
  496. m.Combo("").Get(repo.ListIssueComments).
  497. Post(reqToken(), mustNotBeArchived, bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  498. m.Combo("/:id", reqToken()).Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueCommentDeprecated).
  499. Delete(repo.DeleteIssueCommentDeprecated)
  500. })
  501. m.Group("/labels", func() {
  502. m.Combo("").Get(repo.ListIssueLabels).
  503. Post(reqToken(), bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  504. Put(reqToken(), bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  505. Delete(reqToken(), repo.ClearIssueLabels)
  506. m.Delete("/:id", reqToken(), repo.DeleteIssueLabel)
  507. })
  508. m.Group("/times", func() {
  509. m.Combo("").Get(repo.ListTrackedTimes).
  510. Post(reqToken(), bind(api.AddTimeOption{}), repo.AddTime)
  511. })
  512. m.Combo("/deadline").Post(reqToken(), bind(api.EditDeadlineOption{}), repo.UpdateIssueDeadline)
  513. m.Group("/stopwatch", func() {
  514. m.Post("/start", reqToken(), repo.StartIssueStopwatch)
  515. m.Post("/stop", reqToken(), repo.StopIssueStopwatch)
  516. })
  517. })
  518. }, mustEnableIssuesOrPulls)
  519. m.Group("/labels", func() {
  520. m.Combo("").Get(repo.ListLabels).
  521. Post(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.CreateLabelOption{}), repo.CreateLabel)
  522. m.Combo("/:id").Get(repo.GetLabel).
  523. Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.EditLabelOption{}), repo.EditLabel).
  524. Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), repo.DeleteLabel)
  525. })
  526. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  527. m.Post("/markdown/raw", misc.MarkdownRaw)
  528. m.Group("/milestones", func() {
  529. m.Combo("").Get(repo.ListMilestones).
  530. Post(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  531. m.Combo("/:id").Get(repo.GetMilestone).
  532. Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  533. Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), repo.DeleteMilestone)
  534. })
  535. m.Get("/stargazers", repo.ListStargazers)
  536. m.Get("/subscribers", repo.ListSubscribers)
  537. m.Group("/subscription", func() {
  538. m.Get("", user.IsWatching)
  539. m.Put("", reqToken(), user.Watch)
  540. m.Delete("", reqToken(), user.Unwatch)
  541. })
  542. m.Group("/releases", func() {
  543. m.Combo("").Get(repo.ListReleases).
  544. Post(reqToken(), reqRepoWriter(models.UnitTypeReleases), context.ReferencesGitRepo(), bind(api.CreateReleaseOption{}), repo.CreateRelease)
  545. m.Group("/:id", func() {
  546. m.Combo("").Get(repo.GetRelease).
  547. Patch(reqToken(), reqRepoWriter(models.UnitTypeReleases), context.ReferencesGitRepo(), bind(api.EditReleaseOption{}), repo.EditRelease).
  548. Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteRelease)
  549. m.Group("/assets", func() {
  550. m.Combo("").Get(repo.ListReleaseAttachments).
  551. Post(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.CreateReleaseAttachment)
  552. m.Combo("/:asset").Get(repo.GetReleaseAttachment).
  553. Patch(reqToken(), reqRepoWriter(models.UnitTypeReleases), bind(api.EditAttachmentOptions{}), repo.EditReleaseAttachment).
  554. Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseAttachment)
  555. })
  556. })
  557. }, reqRepoReader(models.UnitTypeReleases))
  558. m.Post("/mirror-sync", reqToken(), reqRepoWriter(models.UnitTypeCode), repo.MirrorSync)
  559. m.Get("/editorconfig/:filename", context.RepoRef(), reqRepoReader(models.UnitTypeCode), repo.GetEditorconfig)
  560. m.Group("/pulls", func() {
  561. m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).
  562. Post(reqToken(), mustNotBeArchived, bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
  563. m.Group("/:index", func() {
  564. m.Combo("").Get(repo.GetPullRequest).
  565. Patch(reqToken(), reqRepoWriter(models.UnitTypePullRequests), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
  566. m.Combo("/merge").Get(repo.IsPullRequestMerged).
  567. Post(reqToken(), mustNotBeArchived, reqRepoWriter(models.UnitTypePullRequests), bind(auth.MergePullRequestForm{}), repo.MergePullRequest)
  568. })
  569. }, mustAllowPulls, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo())
  570. m.Group("/statuses", func() {
  571. m.Combo("/:sha").Get(repo.GetCommitStatuses).
  572. Post(reqToken(), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
  573. }, reqRepoReader(models.UnitTypeCode))
  574. m.Group("/commits/:ref", func() {
  575. m.Get("/status", repo.GetCombinedCommitStatusByRef)
  576. m.Get("/statuses", repo.GetCommitStatusesByRef)
  577. }, reqRepoReader(models.UnitTypeCode))
  578. m.Group("/git", func() {
  579. m.Group("/commits", func() {
  580. m.Get("/:sha", repo.GetSingleCommit)
  581. })
  582. m.Get("/refs", repo.GetGitAllRefs)
  583. m.Get("/refs/*", repo.GetGitRefs)
  584. m.Combo("/trees/:sha", context.RepoRef()).Get(repo.GetTree)
  585. }, reqRepoReader(models.UnitTypeCode))
  586. }, repoAssignment())
  587. })
  588. // Organizations
  589. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  590. m.Get("/users/:username/orgs", org.ListUserOrgs)
  591. m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create)
  592. m.Group("/orgs/:orgname", func() {
  593. m.Get("/repos", user.ListOrgRepos)
  594. m.Combo("").Get(org.Get).
  595. Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
  596. Delete(reqToken(), reqOrgOwnership(), org.Delete)
  597. m.Group("/members", func() {
  598. m.Get("", org.ListMembers)
  599. m.Combo("/:username").Get(org.IsMember).
  600. Delete(reqToken(), reqOrgOwnership(), org.DeleteMember)
  601. })
  602. m.Group("/public_members", func() {
  603. m.Get("", org.ListPublicMembers)
  604. m.Combo("/:username").Get(org.IsPublicMember).
  605. Put(reqToken(), reqOrgMembership(), org.PublicizeMember).
  606. Delete(reqToken(), reqOrgMembership(), org.ConcealMember)
  607. })
  608. m.Combo("/teams", reqToken(), reqOrgMembership()).Get(org.ListTeams).
  609. Post(bind(api.CreateTeamOption{}), org.CreateTeam)
  610. m.Group("/hooks", func() {
  611. m.Combo("").Get(org.ListHooks).
  612. Post(bind(api.CreateHookOption{}), org.CreateHook)
  613. m.Combo("/:id").Get(org.GetHook).
  614. Patch(reqOrgOwnership(), bind(api.EditHookOption{}), org.EditHook).
  615. Delete(reqOrgOwnership(), org.DeleteHook)
  616. }, reqToken(), reqOrgMembership())
  617. }, orgAssignment(true))
  618. m.Group("/teams/:teamid", func() {
  619. m.Combo("").Get(org.GetTeam).
  620. Patch(reqOrgOwnership(), bind(api.EditTeamOption{}), org.EditTeam).
  621. Delete(reqOrgOwnership(), org.DeleteTeam)
  622. m.Group("/members", func() {
  623. m.Get("", org.GetTeamMembers)
  624. m.Combo("/:username").
  625. Get(org.GetTeamMember).
  626. Put(reqOrgOwnership(), org.AddTeamMember).
  627. Delete(reqOrgOwnership(), org.RemoveTeamMember)
  628. })
  629. m.Group("/repos", func() {
  630. m.Get("", org.GetTeamRepos)
  631. m.Combo("/:orgname/:reponame").
  632. Put(org.AddTeamRepository).
  633. Delete(org.RemoveTeamRepository)
  634. })
  635. }, orgAssignment(false, true), reqToken(), reqOrgMembership())
  636. m.Any("/*", func(ctx *context.APIContext) {
  637. ctx.NotFound()
  638. })
  639. m.Group("/admin", func() {
  640. m.Get("/orgs", admin.GetAllOrgs)
  641. m.Group("/users", func() {
  642. m.Get("", admin.GetAllUsers)
  643. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  644. m.Group("/:username", func() {
  645. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  646. Delete(admin.DeleteUser)
  647. m.Group("/keys", func() {
  648. m.Post("", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  649. m.Delete("/:id", admin.DeleteUserPublicKey)
  650. })
  651. m.Get("/orgs", org.ListUserOrgs)
  652. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  653. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  654. })
  655. })
  656. }, reqToken(), reqSiteAdmin())
  657. m.Group("/topics", func() {
  658. m.Get("/search", repo.TopicSearch)
  659. })
  660. }, context.APIContexter(), sudo())
  661. }