From 099b5d7015b11d019679e1610458f5f13f6b39e0 Mon Sep 17 00:00:00 2001 From: hcxm <149076749@qq.com> Date: Fri, 15 Jan 2021 10:30:24 +0800 Subject: [PATCH] =?UTF-8?q?C:/Program=20Files/Git/orgs=20=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E7=BB=84=E7=BB=87=E8=A1=A5=E5=85=A8=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=9B=A2=E9=98=9F=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/convert/convert_ext.go | 25 ++++++++++++ modules/structs/org_ext.go | 17 ++++++++ routers/api/v1/api.go | 6 ++- routers/api/v1/org/org_ext.go | 73 ++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 modules/convert/convert_ext.go diff --git a/modules/convert/convert_ext.go b/modules/convert/convert_ext.go new file mode 100644 index 000000000..f717b812b --- /dev/null +++ b/modules/convert/convert_ext.go @@ -0,0 +1,25 @@ +// Copyright 2015 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package convert + +import ( + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" +) + +// ToOrganization convert models.User to api.Organization +func ToOrganizationExt(org *models.User) *api.OrganizationExt { + return &api.OrganizationExt{ + ID: org.ID, + AvatarURL: org.AvatarLink(), + UserName: org.Name, + FullName: org.FullName, + Description: org.Description, + Website: org.Website, + Location: org.Location, + Visibility: org.Visibility.String(), + RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess, + } +} diff --git a/modules/structs/org_ext.go b/modules/structs/org_ext.go index b43a7248e..cd304318b 100644 --- a/modules/structs/org_ext.go +++ b/modules/structs/org_ext.go @@ -5,6 +5,23 @@ package structs +// Organization represents an organization + +type OrganizationExt struct { + ID int64 `json:"id"` + UserName string `json:"username"` + FullName string `json:"full_name"` + AvatarURL string `json:"avatar_url"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` + Visibility string `json:"visibility"` + RepoAdminChangeTeamAccess bool `json:"repo_admin_change_team_access"` + OwnerTeam interface{} `json:"owner_team"` //团队关系; +} + + + // EditOrgOption options for editing an organization type EditOrgOptionExt struct { Name string `json:"name"` // 添加对name的修改,lower_name 其值跟随name变化; diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 33130b37b..fbdc0ddec 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -931,7 +931,11 @@ func RegisterRoutes(m *macaron.Macaron) { // Organizations m.Get("/user/orgs", reqToken(), org.ListMyOrgs) m.Get("/users/:username/orgs", org.ListUserOrgs) - m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create) + // modified on 2021-01-14 begin 创建组织时返回默认的团队 begin + //m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create) + m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.CreateExt) + // modified on 2021-01-14 begin 创建组织时返回默认的团队 end + m.Get("/orgs", org.GetAll) m.Group("/orgs/:org", func() { m.Combo("").Get(org.Get). diff --git a/routers/api/v1/org/org_ext.go b/routers/api/v1/org/org_ext.go index a05b84ae7..bc098cece 100644 --- a/routers/api/v1/org/org_ext.go +++ b/routers/api/v1/org/org_ext.go @@ -85,3 +85,76 @@ func Edit_Ext(ctx *context.APIContext, form api.EditOrgOptionExt) { ctx.JSON(http.StatusOK, convert.ToOrganization(org)) } + + +// Create api for create organization ext inferface +func CreateExt(ctx *context.APIContext, form api.CreateOrgOption) { + // swagger:operation POST /orgs organization orgCreate + // --- + // summary: Create an organization + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: organization + // in: body + // required: true + // schema: { "$ref": "#/definitions/CreateOrgOption" } + // responses: + // "201": + // "$ref": "#/responses/Organization" + // "403": + // "$ref": "#/responses/forbidden" + // "422": + // "$ref": "#/responses/validationError" + + if !ctx.User.CanCreateOrganization() { + ctx.Error(http.StatusForbidden, "Create organization not allowed", nil) + return + } + + visibility := api.VisibleTypePublic + if form.Visibility != "" { + visibility = api.VisibilityModes[form.Visibility] + } + + org := &models.User{ + Name: form.UserName, + FullName: form.FullName, + Description: form.Description, + Website: form.Website, + Location: form.Location, + IsActive: true, + Type: models.UserTypeOrganization, + Visibility: visibility, + RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, + } + if err := models.CreateOrganization(org, ctx.User); err != nil { + if models.IsErrUserAlreadyExist(err) || + models.IsErrNameReserved(err) || + models.IsErrNameCharsNotAllowed(err) || + models.IsErrNamePatternNotAllowed(err) { + ctx.Error(http.StatusUnprocessableEntity, "", err) + } else { + ctx.Error(http.StatusInternalServerError, "CreateOrganization", err) + } + return + } + + // 根据业务需要 自定义创建组织时将默认团队同时返回. + Team, err := models.GetTeam(org.ID,"") + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetTeam", err) + } + return + } + + apiOrg:=convert.ToOrganizationExt(org) + apiOrg.OwnerTeam=convert.ToTeam(Team) + ctx.JSON(http.StatusCreated, apiOrg) + +}