api.go 12 KB

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