Refactor OrganizationTeam

This commit is contained in:
benebsiny 2024-01-26 00:21:52 +08:00
parent 7abf1c05bc
commit b44ed7433d
5 changed files with 55 additions and 23 deletions

View file

@ -49,7 +49,7 @@ type OrgTeam struct {
}
// OrganizationTeam fetch the team in an organization with the given slug
func OrganizationTeam(client *Client, repo ghrepo.Interface, teamSlug string) (*OrgTeam, error) {
func OrganizationTeam(client *Client, hostname string, org string, teamSlug string) (*OrgTeam, error) {
type responseData struct {
Organization struct {
Team OrgTeam `graphql:"team(slug: $teamSlug)"`
@ -57,12 +57,12 @@ func OrganizationTeam(client *Client, repo ghrepo.Interface, teamSlug string) (*
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"owner": githubv4.String(org),
"teamSlug": githubv4.String(teamSlug),
}
var query responseData
err := client.Query(repo.RepoHost(), "OrganizationTeam", &query, variables)
err := client.Query(hostname, "OrganizationTeam", &query, variables)
if err != nil {
return nil, err
}

View file

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/project/shared/client"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
@ -28,6 +29,7 @@ type linkOpts struct {
type linkConfig struct {
httpClient func() (*http.Client, error)
config func() (config.Config, error)
client *queries.Client
opts linkOpts
io *iostreams.IOStreams
@ -61,6 +63,7 @@ func NewCmdLink(f *cmdutil.Factory, runF func(config linkConfig) error) *cobra.C
config := linkConfig{
httpClient: f.HttpClient,
config: f.Config,
client: client,
opts: opts,
io: f.IOStreams,
@ -108,16 +111,22 @@ func runLink(config linkConfig) error {
}
c := api.NewClientFromHTTP(httpClient)
cfg, err := config.config()
if err != nil {
return err
}
host, _ := cfg.Authentication().DefaultHost()
if config.opts.repo != "" {
return linkRepo(c, owner, config)
return linkRepo(c, owner, host, config)
} else if config.opts.team != "" {
return linkTeam(c, owner, config)
return linkTeam(c, owner, host, config)
}
return nil
}
func linkRepo(c *api.Client, owner *queries.Owner, config linkConfig) error {
repo, err := api.GitHubRepo(c, ghrepo.New(owner.Login, config.opts.repo))
func linkRepo(c *api.Client, owner *queries.Owner, host string, config linkConfig) error {
repo, err := api.GitHubRepo(c, ghrepo.NewWithHost(owner.Login, config.opts.repo, host))
if err != nil {
return err
}
@ -134,8 +143,8 @@ func linkRepo(c *api.Client, owner *queries.Owner, config linkConfig) error {
return printResults(config, result.URL)
}
func linkTeam(c *api.Client, owner *queries.Owner, config linkConfig) error {
team, err := api.OrganizationTeam(c, ghrepo.New(owner.Login, ""), config.opts.team)
func linkTeam(c *api.Client, owner *queries.Owner, host string, config linkConfig) error {
team, err := api.OrganizationTeam(c, host, owner.Login, config.opts.team)
if err != nil {
return err
}

View file

@ -1,6 +1,7 @@
package link
import (
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
@ -209,7 +210,7 @@ func TestRunLink_Repo(t *testing.T) {
ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := linkConfig{
cfg := linkConfig{
opts: linkOpts{
number: 1,
repo: "my-repo",
@ -219,10 +220,13 @@ func TestRunLink_Repo(t *testing.T) {
httpClient: func() (*http.Client, error) {
return http.DefaultClient, nil
},
config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
io: ios,
}
err := runLink(config)
err := runLink(cfg)
assert.NoError(t, err)
assert.Equal(
t,
@ -322,7 +326,7 @@ func TestRunLink_Team(t *testing.T) {
ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := linkConfig{
cfg := linkConfig{
opts: linkOpts{
number: 1,
team: "my-team",
@ -332,10 +336,13 @@ func TestRunLink_Team(t *testing.T) {
httpClient: func() (*http.Client, error) {
return http.DefaultClient, nil
},
config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
io: ios,
}
err := runLink(config)
err := runLink(cfg)
assert.NoError(t, err)
assert.Equal(
t,

View file

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/project/shared/client"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
@ -28,6 +29,7 @@ type unlinkOpts struct {
type unlinkConfig struct {
httpClient func() (*http.Client, error)
config func() (config.Config, error)
client *queries.Client
opts unlinkOpts
io *iostreams.IOStreams
@ -61,6 +63,7 @@ func NewCmdUnlink(f *cmdutil.Factory, runF func(config unlinkConfig) error) *cob
config := unlinkConfig{
httpClient: f.HttpClient,
config: f.Config,
client: client,
opts: opts,
io: f.IOStreams,
@ -108,16 +111,22 @@ func runUnlink(config unlinkConfig) error {
}
c := api.NewClientFromHTTP(httpClient)
cfg, err := config.config()
if err != nil {
return err
}
host, _ := cfg.Authentication().DefaultHost()
if config.opts.repo != "" {
return unlinkRepo(c, owner, config)
return unlinkRepo(c, owner, host, config)
} else if config.opts.team != "" {
return unlinkTeam(c, owner, config)
return unlinkTeam(c, owner, host, config)
}
return nil
}
func unlinkRepo(c *api.Client, owner *queries.Owner, config unlinkConfig) error {
repo, err := api.GitHubRepo(c, ghrepo.New(owner.Login, config.opts.repo))
func unlinkRepo(c *api.Client, owner *queries.Owner, host string, config unlinkConfig) error {
repo, err := api.GitHubRepo(c, ghrepo.NewWithHost(owner.Login, config.opts.repo, host))
if err != nil {
return err
}
@ -134,8 +143,8 @@ func unlinkRepo(c *api.Client, owner *queries.Owner, config unlinkConfig) error
return printResults(config, result.URL)
}
func unlinkTeam(c *api.Client, owner *queries.Owner, config unlinkConfig) error {
team, err := api.OrganizationTeam(c, ghrepo.New(owner.Login, ""), config.opts.team)
func unlinkTeam(c *api.Client, owner *queries.Owner, host string, config unlinkConfig) error {
team, err := api.OrganizationTeam(c, host, owner.Login, config.opts.team)
if err != nil {
return err
}

View file

@ -1,6 +1,7 @@
package unlink
import (
"github.com/cli/cli/v2/internal/config"
"github.com/cli/cli/v2/pkg/cmd/project/shared/queries"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
@ -209,7 +210,7 @@ func TestRunUnlink_Repo(t *testing.T) {
ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := unlinkConfig{
cfg := unlinkConfig{
opts: unlinkOpts{
number: 1,
repo: "my-repo",
@ -219,10 +220,13 @@ func TestRunUnlink_Repo(t *testing.T) {
httpClient: func() (*http.Client, error) {
return http.DefaultClient, nil
},
config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
io: ios,
}
err := runUnlink(config)
err := runUnlink(cfg)
assert.NoError(t, err)
assert.Equal(
t,
@ -322,7 +326,7 @@ func TestRunUnlink_Team(t *testing.T) {
ios, _, stdout, _ := iostreams.Test()
ios.SetStdoutTTY(true)
config := unlinkConfig{
cfg := unlinkConfig{
opts: unlinkOpts{
number: 1,
team: "my-team",
@ -332,10 +336,13 @@ func TestRunUnlink_Team(t *testing.T) {
httpClient: func() (*http.Client, error) {
return http.DefaultClient, nil
},
config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
io: ios,
}
err := runUnlink(config)
err := runUnlink(cfg)
assert.NoError(t, err)
assert.Equal(
t,