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.

65 lines
1.8 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. // see https://github.com/gogits/go-gogs-client/wiki/Organizations#list-your-organizations
  25. func ListMyOrgs(ctx *context.APIContext) {
  26. listUserOrgs(ctx, ctx.User, true)
  27. }
  28. // ListUserOrgs list user's orgs
  29. // see https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations
  30. func ListUserOrgs(ctx *context.APIContext) {
  31. u := user.GetUserByParams(ctx)
  32. if ctx.Written() {
  33. return
  34. }
  35. listUserOrgs(ctx, u, false)
  36. }
  37. // Get get an organization
  38. // see https://github.com/gogits/go-gogs-client/wiki/Organizations#get-an-organization
  39. func Get(ctx *context.APIContext) {
  40. ctx.JSON(200, convert.ToOrganization(ctx.Org.Organization))
  41. }
  42. // Edit change an organization's information
  43. // see https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization
  44. func Edit(ctx *context.APIContext, form api.EditOrgOption) {
  45. org := ctx.Org.Organization
  46. org.FullName = form.FullName
  47. org.Description = form.Description
  48. org.Website = form.Website
  49. org.Location = form.Location
  50. if err := models.UpdateUser(org); err != nil {
  51. ctx.Error(500, "UpdateUser", err)
  52. return
  53. }
  54. ctx.JSON(200, convert.ToOrganization(org))
  55. }