-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Link Project to Repository or Team Command #8595
Merged
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
617ec61
Link Project to Repository or Team Command
benebsiny bb1ab90
Remove support for team
benebsiny e209d14
Support for unlink
benebsiny f98acb2
Support for team
benebsiny 918443f
Add testing for link and unlink
benebsiny aaa41ca
Fix wording
benebsiny c9184d2
Merge branch 'trunk' into cli-7849
benebsiny 7fd91f5
fix wording, move conditional blocks to functions, and add api to ret…
benebsiny b57f402
handle flags with `cmdutil.MutuallyExclusive`, and move mutation meth…
benebsiny 7abf1c0
Replace `httpmock` with `gock` in testing
benebsiny b44ed74
Refactor `OrganizationTeam`
benebsiny 0ce6634
Change the output of link` and `unlink`
benebsiny adc0abe
Support for directory based repo and `GH_REPO` env variable
benebsiny 28c8376
Fix printed project number
benebsiny f9a54f6
Remove `--format` flag and add description for not specifying `--repo…
benebsiny 9225102
Merge branch 'trunk' of github.com:cli/cli into trunk
benebsiny 874da37
Replace `assert` with `require` in testing
benebsiny 7dfaa32
Support for `[HOST/]OWNER/REPO` format
benebsiny 66fb573
chore: simplify parameter `owner`
benebsiny File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package link | ||
|
||
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" | ||
"github.com/cli/cli/v2/pkg/cmdutil" | ||
"github.com/cli/cli/v2/pkg/iostreams" | ||
"github.com/spf13/cobra" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type linkOpts struct { | ||
number int32 | ||
owner string | ||
repo string | ||
team string | ||
projectID string | ||
projectTitle string | ||
repoID string | ||
teamID string | ||
} | ||
|
||
type linkConfig struct { | ||
httpClient func() (*http.Client, error) | ||
config func() (config.Config, error) | ||
client *queries.Client | ||
opts linkOpts | ||
io *iostreams.IOStreams | ||
} | ||
|
||
func NewCmdLink(f *cmdutil.Factory, runF func(config linkConfig) error) *cobra.Command { | ||
opts := linkOpts{} | ||
linkCmd := &cobra.Command{ | ||
Short: "Link a project to a repository or a team", | ||
Use: "link [<number>] [flag]", | ||
Example: heredoc.Doc(` | ||
# link monalisa's project 1 to her repository "my_repo" | ||
gh project link 1 --owner monalisa --repo my_repo | ||
|
||
# link monalisa's organization's project 1 to her team "my_team" | ||
gh project link 1 --owner my_organization --team my_team | ||
|
||
# link monalisa's project 1 to the repository of current directory if neither --repo nor --team is specified | ||
gh project link 1 | ||
`), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := client.New(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(args) == 1 { | ||
num, err := strconv.ParseInt(args[0], 10, 32) | ||
if err != nil { | ||
return cmdutil.FlagErrorf("invalid number: %v", args[0]) | ||
} | ||
opts.number = int32(num) | ||
} | ||
|
||
if opts.repo == "" && opts.team == "" { | ||
repo, err := f.BaseRepo() | ||
if err != nil { | ||
return err | ||
} | ||
opts.repo = repo.RepoName() | ||
if opts.owner == "" { | ||
opts.owner = repo.RepoOwner() | ||
} | ||
} | ||
|
||
if err := cmdutil.MutuallyExclusive("specify only one of `--repo` or `--team`", opts.repo != "", opts.team != ""); err != nil { | ||
return err | ||
} | ||
|
||
config := linkConfig{ | ||
httpClient: f.HttpClient, | ||
config: f.Config, | ||
client: client, | ||
opts: opts, | ||
io: f.IOStreams, | ||
} | ||
|
||
// allow testing of the command without actually running it | ||
if runF != nil { | ||
return runF(config) | ||
} | ||
return runLink(config) | ||
}, | ||
} | ||
|
||
cmdutil.EnableRepoOverride(linkCmd, f) | ||
linkCmd.Flags().StringVar(&opts.owner, "owner", "", "Login of the owner. Use \"@me\" for the current user.") | ||
linkCmd.Flags().StringVarP(&opts.repo, "repo", "R", "", "The repository to be linked to this project") | ||
linkCmd.Flags().StringVarP(&opts.team, "team", "T", "", "The team to be linked to this project") | ||
|
||
return linkCmd | ||
} | ||
|
||
func runLink(config linkConfig) error { | ||
canPrompt := config.io.CanPrompt() | ||
owner, err := config.client.NewOwner(canPrompt, config.opts.owner) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
project, err := config.client.NewProject(canPrompt, owner, config.opts.number, false) | ||
if err != nil { | ||
return err | ||
} | ||
config.opts.projectTitle = project.Title | ||
config.opts.projectID = project.ID | ||
if config.opts.number == 0 { | ||
config.opts.number = project.Number | ||
} | ||
|
||
httpClient, err := config.httpClient() | ||
if err != nil { | ||
return err | ||
} | ||
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, host, config) | ||
} else if config.opts.team != "" { | ||
return linkTeam(c, owner, host, config) | ||
} | ||
return nil | ||
} | ||
|
||
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 | ||
} | ||
config.opts.repoID = repo.ID | ||
|
||
err = config.client.LinkProjectToRepository(config.opts.projectID, config.opts.repoID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return printResults(config, owner, config.opts.repo) | ||
} | ||
|
||
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 | ||
} | ||
config.opts.teamID = team.ID | ||
|
||
err = config.client.LinkProjectToTeam(config.opts.projectID, config.opts.teamID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return printResults(config, owner, config.opts.team) | ||
} | ||
|
||
func printResults(config linkConfig, owner *queries.Owner, linkedTarget string) error { | ||
if !config.io.IsStdoutTTY() { | ||
return nil | ||
} | ||
|
||
_, err := fmt.Fprintf(config.io.Out, "Linked '%s/%s' to project #%d '%s'\n", owner.Login, linkedTarget, config.opts.number, config.opts.projectTitle) | ||
return err | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generally want to default to lowercase flags to avoid of extra keystrokes but also
-R,--repo [HOST/]OWNER/REPO
is a standard flag on nearly every other command.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andyfeller There is already a shorthand
t
for--template
😱There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, I went through this discovery as well,
-R
and-T
are fine for me.This comment was marked as spam.
Sorry, something went wrong.