1
0

api.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package v1
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/go-macaron/binding"
  6. "gopkg.in/macaron.v1"
  7. api "github.com/gogs/go-gogs-client"
  8. "gogs.io/gogs/internal/context"
  9. "gogs.io/gogs/internal/database"
  10. "gogs.io/gogs/internal/form"
  11. "gogs.io/gogs/internal/route/api/v1/admin"
  12. "gogs.io/gogs/internal/route/api/v1/misc"
  13. "gogs.io/gogs/internal/route/api/v1/org"
  14. "gogs.io/gogs/internal/route/api/v1/repo"
  15. "gogs.io/gogs/internal/route/api/v1/user"
  16. )
  17. // repoAssignment extracts information from URL parameters to retrieve the repository,
  18. // and makes sure the context user has at least the read access to the repository.
  19. func repoAssignment() macaron.Handler {
  20. return func(c *context.APIContext) {
  21. username := c.Params(":username")
  22. reponame := c.Params(":reponame")
  23. var err error
  24. var owner *database.User
  25. // Check if the context user is the repository owner.
  26. if c.IsLogged && c.User.LowerName == strings.ToLower(username) {
  27. owner = c.User
  28. } else {
  29. owner, err = database.Handle.Users().GetByUsername(c.Req.Context(), username)
  30. if err != nil {
  31. c.NotFoundOrError(err, "get user by name")
  32. return
  33. }
  34. }
  35. c.Repo.Owner = owner
  36. repo, err := database.Handle.Repositories().GetByName(c.Req.Context(), owner.ID, reponame)
  37. if err != nil {
  38. c.NotFoundOrError(err, "get repository by name")
  39. return
  40. } else if err = repo.GetOwner(); err != nil {
  41. c.Error(err, "get owner")
  42. return
  43. }
  44. if c.IsTokenAuth && c.User.IsAdmin {
  45. c.Repo.AccessMode = database.AccessModeOwner
  46. } else {
  47. c.Repo.AccessMode = database.Handle.Permissions().AccessMode(c.Req.Context(), c.UserID(), repo.ID,
  48. database.AccessModeOptions{
  49. OwnerID: repo.OwnerID,
  50. Private: repo.IsPrivate,
  51. },
  52. )
  53. }
  54. if !c.Repo.HasAccess() {
  55. c.NotFound()
  56. return
  57. }
  58. c.Repo.Repository = repo
  59. }
  60. }
  61. // orgAssignment extracts information from URL parameters to retrieve the organization or team.
  62. func orgAssignment(args ...bool) macaron.Handler {
  63. var (
  64. assignOrg bool
  65. assignTeam bool
  66. )
  67. if len(args) > 0 {
  68. assignOrg = args[0]
  69. }
  70. if len(args) > 1 {
  71. assignTeam = args[1]
  72. }
  73. return func(c *context.APIContext) {
  74. c.Org = new(context.APIOrganization)
  75. var err error
  76. if assignOrg {
  77. c.Org.Organization, err = database.Handle.Users().GetByUsername(c.Req.Context(), c.Params(":orgname"))
  78. if err != nil {
  79. c.NotFoundOrError(err, "get organization by name")
  80. return
  81. }
  82. }
  83. if assignTeam {
  84. c.Org.Team, err = database.GetTeamByID(c.ParamsInt64(":teamid"))
  85. if err != nil {
  86. c.NotFoundOrError(err, "get team by ID")
  87. return
  88. }
  89. }
  90. }
  91. }
  92. // reqToken makes sure the context user is authorized via access token.
  93. func reqToken() macaron.Handler {
  94. return func(c *context.Context) {
  95. if !c.IsTokenAuth {
  96. c.Status(http.StatusUnauthorized)
  97. return
  98. }
  99. }
  100. }
  101. // reqBasicAuth makes sure the context user is authorized via HTTP Basic Auth.
  102. func reqBasicAuth() macaron.Handler {
  103. return func(c *context.Context) {
  104. if !c.IsBasicAuth {
  105. c.Status(http.StatusUnauthorized)
  106. return
  107. }
  108. }
  109. }
  110. // reqAdmin makes sure the context user is a site admin.
  111. func reqAdmin() macaron.Handler {
  112. return func(c *context.Context) {
  113. if !c.IsLogged || !c.User.IsAdmin {
  114. c.Status(http.StatusForbidden)
  115. return
  116. }
  117. }
  118. }
  119. // reqRepoWriter makes sure the context user has at least write access to the repository.
  120. func reqRepoWriter() macaron.Handler {
  121. return func(c *context.Context) {
  122. if !c.Repo.IsWriter() {
  123. c.Status(http.StatusForbidden)
  124. return
  125. }
  126. }
  127. }
  128. // reqRepoWriter makes sure the context user has at least admin access to the repository.
  129. func reqRepoAdmin() macaron.Handler {
  130. return func(c *context.Context) {
  131. if !c.Repo.IsAdmin() {
  132. c.Status(http.StatusForbidden)
  133. return
  134. }
  135. }
  136. }
  137. func mustEnableIssues(c *context.APIContext) {
  138. if !c.Repo.Repository.EnableIssues || c.Repo.Repository.EnableExternalTracker {
  139. c.NotFound()
  140. return
  141. }
  142. }
  143. // RegisterRoutes registers all route in API v1 to the web application.
  144. // FIXME: custom form error response
  145. func RegisterRoutes(m *macaron.Macaron) {
  146. bind := binding.Bind
  147. m.Group("/v1", func() {
  148. // Handle preflight OPTIONS request
  149. m.Options("/*", func() {})
  150. // Miscellaneous
  151. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  152. m.Post("/markdown/raw", misc.MarkdownRaw)
  153. // Users
  154. m.Group("/users", func() {
  155. m.Get("/search", user.Search)
  156. m.Group("/:username", func() {
  157. m.Get("", user.GetInfo)
  158. m.Group("/tokens", func() {
  159. accessTokensHandler := user.NewAccessTokensHandler(user.NewAccessTokensStore())
  160. m.Combo("").
  161. Get(accessTokensHandler.List()).
  162. Post(bind(api.CreateAccessTokenOption{}), accessTokensHandler.Create())
  163. }, reqBasicAuth())
  164. })
  165. })
  166. m.Group("/users", func() {
  167. m.Group("/:username", func() {
  168. m.Get("/keys", user.ListPublicKeys)
  169. m.Get("/followers", user.ListFollowers)
  170. m.Group("/following", func() {
  171. m.Get("", user.ListFollowing)
  172. m.Get("/:target", user.CheckFollowing)
  173. })
  174. })
  175. }, reqToken())
  176. m.Group("/user", func() {
  177. m.Get("", user.GetAuthenticatedUser)
  178. m.Combo("/emails").
  179. Get(user.ListEmails).
  180. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  181. Delete(bind(api.CreateEmailOption{}), user.DeleteEmail)
  182. m.Get("/followers", user.ListMyFollowers)
  183. m.Group("/following", func() {
  184. m.Get("", user.ListMyFollowing)
  185. m.Combo("/:username").
  186. Get(user.CheckMyFollowing).
  187. Put(user.Follow).
  188. Delete(user.Unfollow)
  189. })
  190. m.Group("/keys", func() {
  191. m.Combo("").
  192. Get(user.ListMyPublicKeys).
  193. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  194. m.Combo("/:id").
  195. Get(user.GetPublicKey).
  196. Delete(user.DeletePublicKey)
  197. })
  198. m.Get("/issues", repo.ListUserIssues)
  199. }, reqToken())
  200. // Repositories
  201. m.Get("/users/:username/repos", reqToken(), repo.ListUserRepositories)
  202. m.Get("/orgs/:org/repos", reqToken(), repo.ListOrgRepositories)
  203. m.Combo("/user/repos", reqToken()).
  204. Get(repo.ListMyRepos).
  205. Post(bind(api.CreateRepoOption{}), repo.Create)
  206. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  207. m.Group("/repos", func() {
  208. m.Get("/search", repo.Search)
  209. m.Get("/:username/:reponame", repoAssignment(), repo.Get)
  210. m.Get("/:username/:reponame/releases", repoAssignment(), repo.Releases)
  211. })
  212. m.Group("/repos", func() {
  213. m.Post("/migrate", bind(form.MigrateRepo{}), repo.Migrate)
  214. m.Delete("/:username/:reponame", repoAssignment(), repo.Delete)
  215. m.Group("/:username/:reponame", func() {
  216. m.Group("/hooks", func() {
  217. m.Combo("").
  218. Get(repo.ListHooks).
  219. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  220. m.Combo("/:id").
  221. Patch(bind(api.EditHookOption{}), repo.EditHook).
  222. Delete(repo.DeleteHook)
  223. }, reqRepoAdmin())
  224. m.Group("/collaborators", func() {
  225. m.Get("", repo.ListCollaborators)
  226. m.Combo("/:collaborator").
  227. Get(repo.IsCollaborator).
  228. Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  229. Delete(repo.DeleteCollaborator)
  230. }, reqRepoAdmin())
  231. m.Get("/raw/*", context.RepoRef(), repo.GetRawFile)
  232. m.Group("/contents", func() {
  233. m.Get("", repo.GetContents)
  234. m.Combo("/*").
  235. Get(repo.GetContents).
  236. Put(bind(repo.PutContentsRequest{}), repo.PutContents)
  237. })
  238. m.Get("/archive/*", repo.GetArchive)
  239. m.Group("/git", func() {
  240. m.Group("/trees", func() {
  241. m.Get("/:sha", repo.GetRepoGitTree)
  242. })
  243. m.Group("/blobs", func() {
  244. m.Get("/:sha", repo.RepoGitBlob)
  245. })
  246. })
  247. m.Get("/forks", repo.ListForks)
  248. m.Get("/tags", repo.ListTags)
  249. m.Group("/branches", func() {
  250. m.Get("", repo.ListBranches)
  251. m.Get("/*", repo.GetBranch)
  252. })
  253. m.Group("/commits", func() {
  254. m.Get("/:sha", repo.GetSingleCommit)
  255. m.Get("", repo.GetAllCommits)
  256. m.Get("/*", repo.GetReferenceSHA)
  257. })
  258. m.Group("/keys", func() {
  259. m.Combo("").
  260. Get(repo.ListDeployKeys).
  261. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  262. m.Combo("/:id").
  263. Get(repo.GetDeployKey).
  264. Delete(repo.DeleteDeploykey)
  265. }, reqRepoAdmin())
  266. m.Group("/issues", func() {
  267. m.Combo("").
  268. Get(repo.ListIssues).
  269. Post(bind(api.CreateIssueOption{}), repo.CreateIssue)
  270. m.Group("/comments", func() {
  271. m.Get("", repo.ListRepoIssueComments)
  272. m.Patch("/:id", bind(api.EditIssueCommentOption{}), repo.EditIssueComment)
  273. })
  274. m.Group("/:index", func() {
  275. m.Combo("").
  276. Get(repo.GetIssue).
  277. Patch(bind(api.EditIssueOption{}), repo.EditIssue)
  278. m.Group("/comments", func() {
  279. m.Combo("").
  280. Get(repo.ListIssueComments).
  281. Post(bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  282. m.Combo("/:id").
  283. Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  284. Delete(repo.DeleteIssueComment)
  285. })
  286. m.Get("/labels", repo.ListIssueLabels)
  287. m.Group("/labels", func() {
  288. m.Combo("").
  289. Post(bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  290. Put(bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  291. Delete(repo.ClearIssueLabels)
  292. m.Delete("/:id", repo.DeleteIssueLabel)
  293. }, reqRepoWriter())
  294. })
  295. }, mustEnableIssues)
  296. m.Group("/labels", func() {
  297. m.Get("", repo.ListLabels)
  298. m.Get("/:id", repo.GetLabel)
  299. })
  300. m.Group("/labels", func() {
  301. m.Post("", bind(api.CreateLabelOption{}), repo.CreateLabel)
  302. m.Combo("/:id").
  303. Patch(bind(api.EditLabelOption{}), repo.EditLabel).
  304. Delete(repo.DeleteLabel)
  305. }, reqRepoWriter())
  306. m.Group("/milestones", func() {
  307. m.Get("", repo.ListMilestones)
  308. m.Get("/:id", repo.GetMilestone)
  309. })
  310. m.Group("/milestones", func() {
  311. m.Post("", bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  312. m.Combo("/:id").
  313. Patch(bind(api.EditMilestoneOption{}), repo.EditMilestone).
  314. Delete(repo.DeleteMilestone)
  315. }, reqRepoWriter())
  316. m.Patch("/issue-tracker", reqRepoWriter(), bind(api.EditIssueTrackerOption{}), repo.IssueTracker)
  317. m.Patch("/wiki", reqRepoWriter(), bind(api.EditWikiOption{}), repo.Wiki)
  318. m.Post("/mirror-sync", reqRepoWriter(), repo.MirrorSync)
  319. m.Get("/editorconfig/:filename", context.RepoRef(), repo.GetEditorconfig)
  320. }, repoAssignment())
  321. }, reqToken())
  322. m.Get("/issues", reqToken(), repo.ListUserIssues)
  323. // Organizations
  324. m.Combo("/user/orgs", reqToken()).
  325. Get(org.ListMyOrgs).
  326. Post(bind(api.CreateOrgOption{}), org.CreateMyOrg)
  327. m.Get("/users/:username/orgs", org.ListUserOrgs)
  328. m.Group("/orgs/:orgname", func() {
  329. m.Combo("").
  330. Get(org.Get).
  331. Patch(bind(api.EditOrgOption{}), org.Edit)
  332. m.Get("/teams", org.ListTeams)
  333. }, orgAssignment(true))
  334. m.Group("/admin", func() {
  335. m.Group("/users", func() {
  336. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  337. m.Group("/:username", func() {
  338. m.Combo("").
  339. Patch(bind(api.EditUserOption{}), admin.EditUser).
  340. Delete(admin.DeleteUser)
  341. m.Post("/keys", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  342. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  343. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  344. })
  345. })
  346. m.Group("/orgs/:orgname", func() {
  347. m.Group("/teams", func() {
  348. m.Post("", orgAssignment(true), bind(api.CreateTeamOption{}), admin.CreateTeam)
  349. })
  350. })
  351. m.Group("/teams", func() {
  352. m.Group("/:teamid", func() {
  353. m.Get("/members", admin.ListTeamMembers)
  354. m.Combo("/members/:username").
  355. Put(admin.AddTeamMember).
  356. Delete(admin.RemoveTeamMember)
  357. m.Combo("/repos/:reponame").
  358. Put(admin.AddTeamRepository).
  359. Delete(admin.RemoveTeamRepository)
  360. }, orgAssignment(false, true))
  361. })
  362. }, reqAdmin())
  363. m.Any("/*", func(c *context.Context) {
  364. c.NotFound()
  365. })
  366. }, context.APIContexter())
  367. }