From b44ed7433dbe02a3d209ad93b9b9c9b9b0a87291 Mon Sep 17 00:00:00 2001 From: benebsiny Date: Fri, 26 Jan 2024 00:21:52 +0800 Subject: [PATCH] Refactor `OrganizationTeam` --- api/queries_org.go | 6 +++--- pkg/cmd/project/link/link.go | 21 +++++++++++++++------ pkg/cmd/project/link/link_test.go | 15 +++++++++++---- pkg/cmd/project/unlink/unlink.go | 21 +++++++++++++++------ pkg/cmd/project/unlink/unlink_test.go | 15 +++++++++++---- 5 files changed, 55 insertions(+), 23 deletions(-) diff --git a/api/queries_org.go b/api/queries_org.go index 7366b8bc4..f2e93342e 100644 --- a/api/queries_org.go +++ b/api/queries_org.go @@ -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 } diff --git a/pkg/cmd/project/link/link.go b/pkg/cmd/project/link/link.go index aeba64830..933fdee29 100644 --- a/pkg/cmd/project/link/link.go +++ b/pkg/cmd/project/link/link.go @@ -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 } diff --git a/pkg/cmd/project/link/link_test.go b/pkg/cmd/project/link/link_test.go index 49601547a..c5d6acca3 100644 --- a/pkg/cmd/project/link/link_test.go +++ b/pkg/cmd/project/link/link_test.go @@ -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, diff --git a/pkg/cmd/project/unlink/unlink.go b/pkg/cmd/project/unlink/unlink.go index 2b43bcdea..cdcdc3d06 100644 --- a/pkg/cmd/project/unlink/unlink.go +++ b/pkg/cmd/project/unlink/unlink.go @@ -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 } diff --git a/pkg/cmd/project/unlink/unlink_test.go b/pkg/cmd/project/unlink/unlink_test.go index 6cadd617d..6932f8d4e 100644 --- a/pkg/cmd/project/unlink/unlink_test.go +++ b/pkg/cmd/project/unlink/unlink_test.go @@ -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,