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.

117 lines
2.9 KiB

  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 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. func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) {
  13. if err := u.GetOrganizations(all); err != nil {
  14. ctx.Error(500, "GetOrganizations", err)
  15. return
  16. }
  17. apiOrgs := make([]*api.Organization, len(u.Orgs))
  18. for i := range u.Orgs {
  19. apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
  20. }
  21. ctx.JSON(200, &apiOrgs)
  22. }
  23. // ListMyOrgs list all my orgs
  24. func ListMyOrgs(ctx *context.APIContext) {
  25. // swagger:operation GET /user/orgs organization orgListCurrentUserOrgs
  26. // ---
  27. // summary: List the current user's organizations
  28. // produces:
  29. // - application/json
  30. // responses:
  31. // "200":
  32. // "$ref": "#/responses/OrganizationList"
  33. listUserOrgs(ctx, ctx.User, true)
  34. }
  35. // ListUserOrgs list user's orgs
  36. func ListUserOrgs(ctx *context.APIContext) {
  37. // swagger:operation GET /user/{username}/orgs organization orgListUserOrgs
  38. // ---
  39. // summary: List a user's organizations
  40. // produces:
  41. // - application/json
  42. // parameters:
  43. // - name: username
  44. // in: path
  45. // description: username of user
  46. // type: string
  47. // required: true
  48. // responses:
  49. // "200":
  50. // "$ref": "#/responses/OrganizationList"
  51. u := user.GetUserByParams(ctx)
  52. if ctx.Written() {
  53. return
  54. }
  55. listUserOrgs(ctx, u, false)
  56. }
  57. // Get get an organization
  58. func Get(ctx *context.APIContext) {
  59. // swagger:operation GET /orgs/{org} organization orgGet
  60. // ---
  61. // summary: Get an organization
  62. // produces:
  63. // - application/json
  64. // parameters:
  65. // - name: org
  66. // in: path
  67. // description: name of the organization to get
  68. // type: string
  69. // required: true
  70. // responses:
  71. // "200":
  72. // "$ref": "#/responses/Organization"
  73. ctx.JSON(200, convert.ToOrganization(ctx.Org.Organization))
  74. }
  75. // Edit change an organization's information
  76. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  77. // swagger:operation PATCH /orgs/{org} organization orgEdit
  78. // ---
  79. // summary: Edit an organization
  80. // consumes:
  81. // - application/json
  82. // produces:
  83. // - application/json
  84. // parameters:
  85. // - name: org
  86. // in: path
  87. // description: name of the organization to edit
  88. // type: string
  89. // required: true
  90. // - name: body
  91. // in: body
  92. // schema:
  93. // "$ref": "#/definitions/EditOrgOption"
  94. // responses:
  95. // "200":
  96. // "$ref": "#/responses/Organization"
  97. org := ctx.Org.Organization
  98. org.FullName = form.FullName
  99. org.Description = form.Description
  100. org.Website = form.Website
  101. org.Location = form.Location
  102. if err := models.UpdateUserCols(org, "full_name", "description", "website", "location"); err != nil {
  103. ctx.Error(500, "UpdateUser", err)
  104. return
  105. }
  106. ctx.JSON(200, convert.ToOrganization(org))
  107. }