From 0f899ebaddcd052119f0fc9f4192696bd790558e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 26 Oct 2018 10:12:08 +0800 Subject: [PATCH 1/7] fix create team, update team missing units --- models/org_team.go | 26 +++++++++++++++++++++ models/unit.go | 14 +++++++++++ routers/api/v1/convert/convert.go | 1 + routers/api/v1/org/team.go | 27 ++++++++++++++++++++++ vendor/code.gitea.io/sdk/gitea/org_team.go | 7 ++++++ 5 files changed, 75 insertions(+) diff --git a/models/org_team.go b/models/org_team.go index ad11431a4954e..53c1ec34d8bdb 100644 --- a/models/org_team.go +++ b/models/org_team.go @@ -40,6 +40,14 @@ func (t *Team) getUnits(e Engine) (err error) { return err } +// GetUnitNames returns the team units names +func (t *Team) GetUnitNames() (res []string) { + for _, u := range t.Units { + res = append(res, Units[u.Type].NameKey) + } + return +} + // HasWriteAccess returns true if team has at least write level access mode. func (t *Team) HasWriteAccess() bool { return t.Authorize >= AccessModeWrite @@ -367,6 +375,24 @@ func UpdateTeam(t *Team, authChanged bool) (err error) { return fmt.Errorf("update: %v", err) } + // update units for team + if len(t.Units) > 0 { + for _, unit := range t.Units { + unit.TeamID = t.ID + } + // Delete team-unit. + if _, err := sess. + Where("team_id=?", t.ID). + Delete(new(TeamUnit)); err != nil { + return err + } + + if _, err = sess.Insert(&t.Units); err != nil { + sess.Rollback() + return err + } + } + // Update access for team members if needed. if authChanged { if err = t.getRepositories(sess); err != nil { diff --git a/models/unit.go b/models/unit.go index 1d263595280e2..e3c8762f0d805 100644 --- a/models/unit.go +++ b/models/unit.go @@ -4,6 +4,10 @@ package models +import ( + "strings" +) + // UnitType is Unit's Type type UnitType int @@ -137,3 +141,13 @@ var ( UnitTypeExternalWiki: UnitExternalWiki, } ) + +// FindUnitTypes give the unit key name and return unit +func FindUnitTypes(nameKeys ...string) (res []UnitType) { + for t, u := range Units { + if strings.EqualFold(u.NameKey, u.NameKey) { + res = append(res, t) + } + } + return +} diff --git a/routers/api/v1/convert/convert.go b/routers/api/v1/convert/convert.go index 1bfeae34bfe39..35416dea5181b 100644 --- a/routers/api/v1/convert/convert.go +++ b/routers/api/v1/convert/convert.go @@ -198,5 +198,6 @@ func ToTeam(team *models.Team) *api.Team { Name: team.Name, Description: team.Description, Permission: team.Authorize.String(), + Units: team.GetUnitNames(), } } diff --git a/routers/api/v1/org/team.go b/routers/api/v1/org/team.go index 2e319a18310ce..8b67eda42fdf5 100644 --- a/routers/api/v1/org/team.go +++ b/routers/api/v1/org/team.go @@ -90,6 +90,20 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) { Description: form.Description, Authorize: models.ParseAccessMode(form.Permission), } + + unitTypes := models.FindUnitTypes(form.Units...) + + if team.Authorize < models.AccessModeOwner { + var units = make([]*models.TeamUnit, 0, len(form.Units)) + for _, tp := range unitTypes { + units = append(units, &models.TeamUnit{ + OrgID: ctx.Org.Organization.ID, + Type: tp, + }) + } + team.Units = units + } + if err := models.NewTeam(team); err != nil { if models.IsErrTeamAlreadyExist(err) { ctx.Error(422, "", err) @@ -128,6 +142,19 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) { team.Name = form.Name team.Description = form.Description team.Authorize = models.ParseAccessMode(form.Permission) + unitTypes := models.FindUnitTypes(form.Units...) + + if team.Authorize < models.AccessModeOwner { + var units = make([]*models.TeamUnit, 0, len(form.Units)) + for _, tp := range unitTypes { + units = append(units, &models.TeamUnit{ + OrgID: ctx.Org.Organization.ID, + Type: tp, + }) + } + team.Units = units + } + if err := models.UpdateTeam(team, true); err != nil { ctx.Error(500, "EditTeam", err) return diff --git a/vendor/code.gitea.io/sdk/gitea/org_team.go b/vendor/code.gitea.io/sdk/gitea/org_team.go index 2fc6796d43ff9..f3e98b932e91c 100644 --- a/vendor/code.gitea.io/sdk/gitea/org_team.go +++ b/vendor/code.gitea.io/sdk/gitea/org_team.go @@ -1,4 +1,5 @@ // Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2018 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -11,6 +12,8 @@ type Team struct { Description string `json:"description"` // enum: none,read,write,admin,owner Permission string `json:"permission"` + // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki + Units []string `json:"units"` } // CreateTeamOption options for creating a team @@ -20,6 +23,8 @@ type CreateTeamOption struct { Description string `json:"description" binding:"MaxSize(255)"` // enum: read,write,admin Permission string `json:"permission"` + // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki + Units []string `json:"units"` } // EditTeamOption options for editing a team @@ -29,4 +34,6 @@ type EditTeamOption struct { Description string `json:"description" binding:"MaxSize(255)"` // enum: read,write,admin Permission string `json:"permission"` + // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki + Units []string `json:"units"` } From e3f4763882e0652d8ab283126f9bdec36b885bb1 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 27 Oct 2018 21:13:10 +0800 Subject: [PATCH 2/7] fix bug --- models/unit.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/models/unit.go b/models/unit.go index e3c8762f0d805..9619232cf4c1d 100644 --- a/models/unit.go +++ b/models/unit.go @@ -144,9 +144,12 @@ var ( // FindUnitTypes give the unit key name and return unit func FindUnitTypes(nameKeys ...string) (res []UnitType) { - for t, u := range Units { - if strings.EqualFold(u.NameKey, u.NameKey) { - res = append(res, t) + for _, key := range nameKeys { + for t, u := range Units { + if strings.EqualFold(key, u.NameKey) { + res = append(res, t) + break + } } } return From daad21ab953201bd990daa9d07de809b124e0f08 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 27 Oct 2018 22:46:45 +0800 Subject: [PATCH 3/7] update swagger --- templates/swagger/v1_json.tmpl | 50 +++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 56a169c295d52..32b96dcff5d63 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -6322,6 +6322,22 @@ "admin" ], "x-go-name": "Permission" + }, + "units": { + "type": "array", + "enum": [ + "repo.code", + "repo.issues", + "repo.ext_issues", + "repo.wiki", + "repo.pulls", + "repo.releases", + "repo.ext_wiki" + ], + "items": { + "type": "string" + }, + "x-go-name": "Units" } }, "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea" @@ -6697,6 +6713,22 @@ "admin" ], "x-go-name": "Permission" + }, + "units": { + "type": "array", + "enum": [ + "repo.code", + "repo.issues", + "repo.ext_issues", + "repo.wiki", + "repo.pulls", + "repo.releases", + "repo.ext_wiki" + ], + "items": { + "type": "string" + }, + "x-go-name": "Units" } }, "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea" @@ -7744,6 +7776,22 @@ "owner" ], "x-go-name": "Permission" + }, + "units": { + "type": "array", + "enum": [ + "repo.code", + "repo.issues", + "repo.ext_issues", + "repo.wiki", + "repo.pulls", + "repo.releases", + "repo.ext_wiki" + ], + "items": { + "type": "string" + }, + "x-go-name": "Units" } }, "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea" @@ -8311,4 +8359,4 @@ "SudoHeader": [] } ] -} \ No newline at end of file +} From 71d910ba2f27351202a8ae52e3e399e559ab9231 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 29 Oct 2018 09:33:07 +0800 Subject: [PATCH 4/7] fix swagger doc --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 32b96dcff5d63..6adb40568c82d 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -21,7 +21,7 @@ }, "version": "1.1.1" }, - "basePath": "{{AppSubUrl}}/api/v1", + "basePath": "/api/v1", "paths": { "/admin/users": { "post": { From c3f53f5fe618c14803a8eee60ea5b08ad4f46d55 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 31 Oct 2018 16:50:32 +0800 Subject: [PATCH 5/7] fix swagger doc --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 6adb40568c82d..32b96dcff5d63 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -21,7 +21,7 @@ }, "version": "1.1.1" }, - "basePath": "/api/v1", + "basePath": "{{AppSubUrl}}/api/v1", "paths": { "/admin/users": { "post": { From 5ef2eae735bb3b3bb13af193e4af22c6d483d738 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 9 Nov 2018 17:47:10 +0800 Subject: [PATCH 6/7] update dependecy --- Gopkg.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 2da402ba741a3..1a2b4b5f5bc60 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -11,11 +11,11 @@ [[projects]] branch = "master" - digest = "1:bf4f822f636b99ac7d4f8fa5210a7439bacaf8d1f15d5783956bdd5dd2069bdc" + digest = "1:b194da40b41ae99546dfeec5a85f1fec2a6c51350d438e511ef90f4293c6dcd7" name = "code.gitea.io/sdk" packages = ["gitea"] pruneopts = "NUT" - revision = "11c860c8e49a23be26e6d6c6a039a46ad2ae1d27" + revision = "4f96d9ac89886e78c50de8c835ebe87461578a5e" [[projects]] digest = "1:3fcef06a1a6561955c94af6c7757a6fa37605eb653f0d06ab960e5bb80092195" From 6605fff41a40f4634afb3dbc96be2d10ad97647f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 9 Nov 2018 19:00:46 +0800 Subject: [PATCH 7/7] remove newline at the end --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 32b96dcff5d63..5c8c666041f57 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8359,4 +8359,4 @@ "SudoHeader": [] } ] -} +} \ No newline at end of file