Skip to content

Commit 558e3f3

Browse files
committed
Isolate pr create command
1 parent e024184 commit 558e3f3

7 files changed

Lines changed: 467 additions & 431 deletions

File tree

command/issue.go

Lines changed: 16 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,7 @@ func issueCreate(cmd *cobra.Command, args []string) error {
508508
if isWeb, err := cmd.Flags().GetBool("web"); err == nil && isWeb {
509509
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
510510
if title != "" || body != "" {
511-
milestone := ""
512-
if len(milestoneTitles) > 0 {
513-
milestone = milestoneTitles[0]
514-
}
515-
openURL, err = withPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestone)
511+
openURL, err = shared.WithPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestoneTitles)
516512
if err != nil {
517513
return err
518514
}
@@ -535,9 +531,9 @@ func issueCreate(cmd *cobra.Command, args []string) error {
535531
return fmt.Errorf("the '%s' repository has disabled issues", ghrepo.FullName(baseRepo))
536532
}
537533

538-
action := SubmitAction
539-
tb := issueMetadataState{
540-
Type: issueMetadata,
534+
action := shared.SubmitAction
535+
tb := shared.IssueMetadataState{
536+
Type: shared.IssueMetadata,
541537
Assignees: assignees,
542538
Labels: labelNames,
543539
Projects: projectNames,
@@ -558,14 +554,20 @@ func issueCreate(cmd *cobra.Command, args []string) error {
558554
legacyTemplateFile = githubtemplate.FindLegacy(rootDir, "ISSUE_TEMPLATE")
559555
}
560556
}
561-
err := titleBodySurvey(cmd, &tb, apiClient, baseRepo, title, body, defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage())
557+
558+
editorCommand, err := cmdutil.DetermineEditor(ctx.Config)
559+
if err != nil {
560+
return err
561+
}
562+
563+
err = shared.TitleBodySurvey(defaultStreams, editorCommand, &tb, apiClient, baseRepo, title, body, shared.Defaults{}, nonLegacyTemplateFiles, legacyTemplateFile, false, repo.ViewerCanTriage())
562564
if err != nil {
563565
return fmt.Errorf("could not collect title and/or body: %w", err)
564566
}
565567

566568
action = tb.Action
567569

568-
if tb.Action == CancelAction {
570+
if tb.Action == shared.CancelAction {
569571
fmt.Fprintln(cmd.ErrOrStderr(), "Discarding.")
570572

571573
return nil
@@ -583,26 +585,22 @@ func issueCreate(cmd *cobra.Command, args []string) error {
583585
}
584586
}
585587

586-
if action == PreviewAction {
588+
if action == shared.PreviewAction {
587589
openURL := ghrepo.GenerateRepoURL(baseRepo, "issues/new")
588-
milestone := ""
589-
if len(milestoneTitles) > 0 {
590-
milestone = milestoneTitles[0]
591-
}
592-
openURL, err = withPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestone)
590+
openURL, err = shared.WithPrAndIssueQueryParams(openURL, title, body, assignees, labelNames, projectNames, milestoneTitles)
593591
if err != nil {
594592
return err
595593
}
596594
// TODO could exceed max url length for explorer
597595
fmt.Fprintf(cmd.ErrOrStderr(), "Opening %s in your browser.\n", utils.DisplayURL(openURL))
598596
return utils.OpenInBrowser(openURL)
599-
} else if action == SubmitAction {
597+
} else if action == shared.SubmitAction {
600598
params := map[string]interface{}{
601599
"title": title,
602600
"body": body,
603601
}
604602

605-
err = addMetadataToIssueParams(apiClient, baseRepo, params, &tb)
603+
err = shared.AddMetadataToIssueParams(apiClient, baseRepo, params, &tb)
606604
if err != nil {
607605
return err
608606
}
@@ -620,82 +618,6 @@ func issueCreate(cmd *cobra.Command, args []string) error {
620618
return nil
621619
}
622620

623-
func addMetadataToIssueParams(client *api.Client, baseRepo ghrepo.Interface, params map[string]interface{}, tb *issueMetadataState) error {
624-
if !tb.HasMetadata() {
625-
return nil
626-
}
627-
628-
if tb.MetadataResult == nil {
629-
resolveInput := api.RepoResolveInput{
630-
Reviewers: tb.Reviewers,
631-
Assignees: tb.Assignees,
632-
Labels: tb.Labels,
633-
Projects: tb.Projects,
634-
Milestones: tb.Milestones,
635-
}
636-
637-
var err error
638-
tb.MetadataResult, err = api.RepoResolveMetadataIDs(client, baseRepo, resolveInput)
639-
if err != nil {
640-
return err
641-
}
642-
}
643-
644-
assigneeIDs, err := tb.MetadataResult.MembersToIDs(tb.Assignees)
645-
if err != nil {
646-
return fmt.Errorf("could not assign user: %w", err)
647-
}
648-
params["assigneeIds"] = assigneeIDs
649-
650-
labelIDs, err := tb.MetadataResult.LabelsToIDs(tb.Labels)
651-
if err != nil {
652-
return fmt.Errorf("could not add label: %w", err)
653-
}
654-
params["labelIds"] = labelIDs
655-
656-
projectIDs, err := tb.MetadataResult.ProjectsToIDs(tb.Projects)
657-
if err != nil {
658-
return fmt.Errorf("could not add to project: %w", err)
659-
}
660-
params["projectIds"] = projectIDs
661-
662-
if len(tb.Milestones) > 0 {
663-
milestoneID, err := tb.MetadataResult.MilestoneToID(tb.Milestones[0])
664-
if err != nil {
665-
return fmt.Errorf("could not add to milestone '%s': %w", tb.Milestones[0], err)
666-
}
667-
params["milestoneId"] = milestoneID
668-
}
669-
670-
if len(tb.Reviewers) == 0 {
671-
return nil
672-
}
673-
674-
var userReviewers []string
675-
var teamReviewers []string
676-
for _, r := range tb.Reviewers {
677-
if strings.ContainsRune(r, '/') {
678-
teamReviewers = append(teamReviewers, r)
679-
} else {
680-
userReviewers = append(userReviewers, r)
681-
}
682-
}
683-
684-
userReviewerIDs, err := tb.MetadataResult.MembersToIDs(userReviewers)
685-
if err != nil {
686-
return fmt.Errorf("could not request reviewer: %w", err)
687-
}
688-
params["userReviewerIds"] = userReviewerIDs
689-
690-
teamReviewerIDs, err := tb.MetadataResult.TeamsToIDs(teamReviewers)
691-
if err != nil {
692-
return fmt.Errorf("could not request reviewer: %w", err)
693-
}
694-
params["teamReviewerIds"] = teamReviewerIDs
695-
696-
return nil
697-
}
698-
699621
func printIssues(w io.Writer, prefix string, totalCount int, issues []api.Issue) {
700622
table := utils.NewTablePrinter(w)
701623
for _, issue := range issues {

command/pr.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func init() {
1919
prCmd.PersistentFlags().StringP("repo", "R", "", "Select another repository using the `OWNER/REPO` format")
2020

2121
RootCmd.AddCommand(prCmd)
22-
prCmd.AddCommand(prCreateCmd)
2322
prCmd.AddCommand(prCloseCmd)
2423
prCmd.AddCommand(prReopenCmd)
2524
prCmd.AddCommand(prReadyCmd)

command/root.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
apiCmd "github.com/cli/cli/pkg/cmd/api"
2525
gistCreateCmd "github.com/cli/cli/pkg/cmd/gist/create"
2626
prCheckoutCmd "github.com/cli/cli/pkg/cmd/pr/checkout"
27+
prCreateCmd "github.com/cli/cli/pkg/cmd/pr/create"
2728
prDiffCmd "github.com/cli/cli/pkg/cmd/pr/diff"
2829
prMergeCmd "github.com/cli/cli/pkg/cmd/pr/merge"
2930
prReviewCmd "github.com/cli/cli/pkg/cmd/pr/review"
@@ -179,6 +180,7 @@ func init() {
179180
prCmd.AddCommand(prViewCmd.NewCmdView(&repoResolvingCmdFactory, nil))
180181
prCmd.AddCommand(prMergeCmd.NewCmdMerge(&repoResolvingCmdFactory, nil))
181182
prCmd.AddCommand(prStatusCmd.NewCmdStatus(&repoResolvingCmdFactory, nil))
183+
prCmd.AddCommand(prCreateCmd.NewCmdCreate(&repoResolvingCmdFactory, nil))
182184

183185
RootCmd.AddCommand(creditsCmd.NewCmdCredits(cmdFactory, nil))
184186
}
@@ -398,41 +400,6 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co
398400
return baseRepo, nil
399401
}
400402

401-
// TODO there is a parallel implementation for isolated commands
402-
func formatRemoteURL(cmd *cobra.Command, repo ghrepo.Interface) string {
403-
ctx := contextForCommand(cmd)
404-
405-
var protocol string
406-
cfg, err := ctx.Config()
407-
if err != nil {
408-
fmt.Fprintf(colorableErr(cmd), "%s failed to load config: %s. using defaults\n", utils.Yellow("!"), err)
409-
} else {
410-
protocol, _ = cfg.Get(repo.RepoHost(), "git_protocol")
411-
}
412-
413-
if protocol == "ssh" {
414-
return fmt.Sprintf("git@%s:%s/%s.git", repo.RepoHost(), repo.RepoOwner(), repo.RepoName())
415-
}
416-
417-
return fmt.Sprintf("https://%s/%s/%s.git", repo.RepoHost(), repo.RepoOwner(), repo.RepoName())
418-
}
419-
420-
// TODO there is a parallel implementation for isolated commands
421-
func determineEditor(cmd *cobra.Command) (string, error) {
422-
editorCommand := os.Getenv("GH_EDITOR")
423-
if editorCommand == "" {
424-
ctx := contextForCommand(cmd)
425-
cfg, err := ctx.Config()
426-
if err != nil {
427-
return "", fmt.Errorf("could not read config: %w", err)
428-
}
429-
// TODO: consider supporting setting an editor per GHE host
430-
editorCommand, _ = cfg.Get(ghinstance.Default(), "editor")
431-
}
432-
433-
return editorCommand, nil
434-
}
435-
436403
func ExecuteShellAlias(args []string) error {
437404
externalCmd := exec.Command(args[0], args[1:]...)
438405
externalCmd.Stderr = os.Stderr

0 commit comments

Comments
 (0)