From b81847d03d3f5dc546807e179f32b604370141ab Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 13 Aug 2021 23:56:50 +0800 Subject: [PATCH] Add Repo Team Management Functions (#537) close #526 * [x] functions * [x] tests Reviewed-on: https://gitea.com/gitea/go-sdk/pulls/537 Reviewed-by: Andrew Thornton Reviewed-by: Lunny Xiao Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de> --- gitea/org_team_test.go | 25 ++++++++++++++ gitea/org_test.go | 49 +++++++++++++++++++++++++++ gitea/repo_team.go | 65 ++++++++++++++++++++++++++++++++++++ gitea/repo_team_test.go | 74 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 gitea/org_team_test.go create mode 100644 gitea/org_test.go create mode 100644 gitea/repo_team.go create mode 100644 gitea/repo_team_test.go diff --git a/gitea/org_team_test.go b/gitea/org_team_test.go new file mode 100644 index 0000000..53da9af --- /dev/null +++ b/gitea/org_team_test.go @@ -0,0 +1,25 @@ +// Copyright 2021 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. + +package gitea + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func createTestOrgTeams(t *testing.T, c *Client, org, name string, accessMode AccessMode, units []RepoUnitType) (*Team, error) { + team, _, e := c.CreateTeam(org, CreateTeamOption{ + Name: name, + Description: name + "'s team desc", + Permission: accessMode, + CanCreateOrgRepo: false, + IncludesAllRepositories: false, + Units: units, + }) + assert.NoError(t, e) + assert.NotNil(t, team) + return team, e +} diff --git a/gitea/org_test.go b/gitea/org_test.go new file mode 100644 index 0000000..93a1ba7 --- /dev/null +++ b/gitea/org_test.go @@ -0,0 +1,49 @@ +// Copyright 2021 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. + +package gitea + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func createTestOrgRepo(t *testing.T, c *Client, name string) (func(), *Repository, error) { + _, _, err := c.GetOrg(name) + if err == nil { + _, _ = c.DeleteOrg(name) + } + _, _, err = c.CreateOrg(CreateOrgOption{ + Name: name, + Visibility: VisibleTypePublic, + RepoAdminChangeTeamAccess: true, + }) + if !assert.NoError(t, err) { + return nil, nil, err + } + + _, _, err = c.GetRepo(name, name) + if err == nil { + _, _ = c.DeleteRepo(name, name) + } + + repo, _, err := c.CreateOrgRepo(name, CreateRepoOption{ + Name: name, + Description: "A test Repo: " + name, + AutoInit: true, + Gitignores: "C,C++", + License: "MIT", + Readme: "Default", + IssueLabels: "Default", + Private: false, + }) + assert.NoError(t, err) + assert.NotNil(t, repo) + + return func() { + _, _ = c.DeleteRepo(name, name) + _, _ = c.DeleteOrg(name) + }, repo, err +} diff --git a/gitea/repo_team.go b/gitea/repo_team.go new file mode 100644 index 0000000..b983d87 --- /dev/null +++ b/gitea/repo_team.go @@ -0,0 +1,65 @@ +// Copyright 2021 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. + +package gitea + +import ( + "fmt" + "net/http" +) + +// GetRepoTeams return teams from a repository +func (c *Client) GetRepoTeams(user, repo string) ([]*Team, *Response, error) { + if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil { + return nil, nil, err + } + if err := escapeValidatePathSegments(&user, &repo); err != nil { + return nil, nil, err + } + teams := make([]*Team, 0, 5) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/teams", user, repo), nil, nil, &teams) + return teams, resp, err +} + +// AddRepoTeam add a team to a repository +func (c *Client) AddRepoTeam(user, repo, team string) (*Response, error) { + if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil { + return nil, err + } + if err := escapeValidatePathSegments(&user, &repo, &team); err != nil { + return nil, err + } + _, resp, err := c.getResponse("PUT", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil) + return resp, err +} + +// RemoveRepoTeam delete a team from a repository +func (c *Client) RemoveRepoTeam(user, repo, team string) (*Response, error) { + if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil { + return nil, err + } + if err := escapeValidatePathSegments(&user, &repo, &team); err != nil { + return nil, err + } + _, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil) + return resp, err +} + +// CheckRepoTeam check if team is assigned to repo by name and return it. +// If not assigned, it will return nil. +func (c *Client) CheckRepoTeam(user, repo, team string) (*Team, *Response, error) { + if err := c.checkServerVersionGreaterThanOrEqual(version1_15_0); err != nil { + return nil, nil, err + } + if err := escapeValidatePathSegments(&user, &repo, &team); err != nil { + return nil, nil, err + } + t := new(Team) + resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/teams/%s", user, repo, team), nil, nil, &t) + if resp != nil && resp.StatusCode == http.StatusNotFound { + // if not found it's not an error, it indicates it's not assigned + return nil, resp, nil + } + return t, resp, err +} diff --git a/gitea/repo_team_test.go b/gitea/repo_team_test.go new file mode 100644 index 0000000..b205a9f --- /dev/null +++ b/gitea/repo_team_test.go @@ -0,0 +1,74 @@ +// Copyright 2021 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. + +package gitea + +import ( + "log" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRepoTeamManagement(t *testing.T) { + log.Println("== TestRepoTeamManagement ==") + c := newTestClient() + + // prepare for test + clean, repo, err := createTestOrgRepo(t, c, "RepoTeamManagement") + if err != nil { + return + } + defer clean() + if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "Admins", AccessModeAdmin, []RepoUnitType{RepoUnitCode, RepoUnitIssues, RepoUnitPulls, RepoUnitReleases}); err != nil { + return + } + if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "CodeManager", AccessModeWrite, []RepoUnitType{RepoUnitCode}); err != nil { + return + } + if _, err = createTestOrgTeams(t, c, repo.Owner.UserName, "IssueManager", AccessModeWrite, []RepoUnitType{RepoUnitIssues, RepoUnitPulls}); err != nil { + return + } + + // test + teams, _, err := c.GetRepoTeams(repo.Owner.UserName, repo.Name) + assert.NoError(t, err) + if !assert.Len(t, teams, 1) { + return + } + assert.EqualValues(t, AccessModeOwner, teams[0].Permission) + + team, _, err := c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins") + assert.NoError(t, err) + assert.Nil(t, team) + + resp, err := c.AddRepoTeam(repo.Owner.UserName, repo.Name, "Admins") + assert.NoError(t, err) + assert.EqualValues(t, 204, resp.StatusCode) + resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "CodeManager") + assert.NoError(t, err) + assert.EqualValues(t, 204, resp.StatusCode) + resp, err = c.AddRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager") + assert.NoError(t, err) + assert.EqualValues(t, 204, resp.StatusCode) + + team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "Admins") + assert.NoError(t, err) + if assert.NotNil(t, team) { + assert.EqualValues(t, "Admins", team.Name) + assert.EqualValues(t, AccessModeAdmin, team.Permission) + } + + teams, _, err = c.GetRepoTeams(repo.Owner.UserName, repo.Name) + assert.NoError(t, err) + assert.Len(t, teams, 4) + + resp, err = c.RemoveRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager") + assert.NoError(t, err) + assert.EqualValues(t, 204, resp.StatusCode) + + team, _, err = c.CheckRepoTeam(repo.Owner.UserName, repo.Name, "IssueManager") + assert.NoError(t, err) + assert.Nil(t, team) +}