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.

196 lines
4.9 KiB

  1. // Copyright 2016 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 org
  5. import (
  6. api "code.gitea.io/sdk/gitea"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/routers/api/v1/convert"
  10. "code.gitea.io/gitea/routers/api/v1/user"
  11. )
  12. // ListTeams list all the teams of an organization
  13. func ListTeams(ctx *context.APIContext) {
  14. org := ctx.Org.Organization
  15. if err := org.GetTeams(); err != nil {
  16. ctx.Error(500, "GetTeams", err)
  17. return
  18. }
  19. apiTeams := make([]*api.Team, len(org.Teams))
  20. for i := range org.Teams {
  21. apiTeams[i] = convert.ToTeam(org.Teams[i])
  22. }
  23. ctx.JSON(200, apiTeams)
  24. }
  25. // GetTeam api for get a team
  26. func GetTeam(ctx *context.APIContext) {
  27. ctx.JSON(200, convert.ToTeam(ctx.Org.Team))
  28. }
  29. // CreateTeam api for create a team
  30. func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
  31. team := &models.Team{
  32. OrgID: ctx.Org.Organization.ID,
  33. Name: form.Name,
  34. Description: form.Description,
  35. Authorize: models.ParseAccessMode(form.Permission),
  36. }
  37. if err := models.NewTeam(team); err != nil {
  38. if models.IsErrTeamAlreadyExist(err) {
  39. ctx.Error(422, "", err)
  40. } else {
  41. ctx.Error(500, "NewTeam", err)
  42. }
  43. return
  44. }
  45. ctx.JSON(201, convert.ToTeam(team))
  46. }
  47. // EditTeam api for edit a team
  48. func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
  49. team := &models.Team{
  50. ID: ctx.Org.Team.ID,
  51. OrgID: ctx.Org.Team.OrgID,
  52. Name: form.Name,
  53. Description: form.Description,
  54. Authorize: models.ParseAccessMode(form.Permission),
  55. }
  56. if err := models.UpdateTeam(team, true); err != nil {
  57. ctx.Error(500, "EditTeam", err)
  58. return
  59. }
  60. ctx.JSON(200, convert.ToTeam(team))
  61. }
  62. // DeleteTeam api for delete a team
  63. func DeleteTeam(ctx *context.APIContext) {
  64. if err := models.DeleteTeam(ctx.Org.Team); err != nil {
  65. ctx.Error(500, "DeleteTeam", err)
  66. return
  67. }
  68. ctx.Status(204)
  69. }
  70. // GetTeamMembers api for get a team's members
  71. func GetTeamMembers(ctx *context.APIContext) {
  72. if !models.IsOrganizationMember(ctx.Org.Team.OrgID, ctx.User.ID) {
  73. ctx.Status(404)
  74. return
  75. }
  76. team := ctx.Org.Team
  77. if err := team.GetMembers(); err != nil {
  78. ctx.Error(500, "GetTeamMembers", err)
  79. return
  80. }
  81. members := make([]*api.User, len(team.Members))
  82. for i, member := range team.Members {
  83. members[i] = member.APIFormat()
  84. }
  85. ctx.JSON(200, members)
  86. }
  87. // AddTeamMember api for add a member to a team
  88. func AddTeamMember(ctx *context.APIContext) {
  89. u := user.GetUserByParams(ctx)
  90. if ctx.Written() {
  91. return
  92. }
  93. if err := ctx.Org.Team.AddMember(u.ID); err != nil {
  94. ctx.Error(500, "AddMember", err)
  95. return
  96. }
  97. ctx.Status(204)
  98. }
  99. // RemoveTeamMember api for remove one member from a team
  100. func RemoveTeamMember(ctx *context.APIContext) {
  101. u := user.GetUserByParams(ctx)
  102. if ctx.Written() {
  103. return
  104. }
  105. if err := ctx.Org.Team.RemoveMember(u.ID); err != nil {
  106. ctx.Error(500, "RemoveMember", err)
  107. return
  108. }
  109. ctx.Status(204)
  110. }
  111. // GetTeamRepos api for get a team's repos
  112. func GetTeamRepos(ctx *context.APIContext) {
  113. team := ctx.Org.Team
  114. if err := team.GetRepositories(); err != nil {
  115. ctx.Error(500, "GetTeamRepos", err)
  116. }
  117. repos := make([]*api.Repository, len(team.Repos))
  118. for i, repo := range team.Repos {
  119. access, err := models.AccessLevel(ctx.User.ID, repo)
  120. if err != nil {
  121. ctx.Error(500, "GetTeamRepos", err)
  122. return
  123. }
  124. repos[i] = repo.APIFormat(access)
  125. }
  126. ctx.JSON(200, repos)
  127. }
  128. // getRepositoryByParams get repository by a team's organization ID and repo name
  129. func getRepositoryByParams(ctx *context.APIContext) *models.Repository {
  130. repo, err := models.GetRepositoryByName(ctx.Org.Team.OrgID, ctx.Params(":reponame"))
  131. if err != nil {
  132. if models.IsErrRepoNotExist(err) {
  133. ctx.Status(404)
  134. } else {
  135. ctx.Error(500, "GetRepositoryByName", err)
  136. }
  137. return nil
  138. }
  139. return repo
  140. }
  141. // AddTeamRepository api for adding a repository to a team
  142. func AddTeamRepository(ctx *context.APIContext) {
  143. repo := getRepositoryByParams(ctx)
  144. if ctx.Written() {
  145. return
  146. }
  147. if access, err := models.AccessLevel(ctx.User.ID, repo); err != nil {
  148. ctx.Error(500, "AccessLevel", err)
  149. return
  150. } else if access < models.AccessModeAdmin {
  151. ctx.Error(403, "", "Must have admin-level access to the repository")
  152. return
  153. }
  154. if err := ctx.Org.Team.AddRepository(repo); err != nil {
  155. ctx.Error(500, "AddRepository", err)
  156. return
  157. }
  158. ctx.Status(204)
  159. }
  160. // RemoveTeamRepository api for removing a repository from a team
  161. func RemoveTeamRepository(ctx *context.APIContext) {
  162. repo := getRepositoryByParams(ctx)
  163. if ctx.Written() {
  164. return
  165. }
  166. if access, err := models.AccessLevel(ctx.User.ID, repo); err != nil {
  167. ctx.Error(500, "AccessLevel", err)
  168. return
  169. } else if access < models.AccessModeAdmin {
  170. ctx.Error(403, "", "Must have admin-level access to the repository")
  171. return
  172. }
  173. if err := ctx.Org.Team.RemoveRepository(repo.ID); err != nil {
  174. ctx.Error(500, "RemoveRepository", err)
  175. return
  176. }
  177. ctx.Status(204)
  178. }