From 152b8610019a50d18226178e87781019a4314e93 Mon Sep 17 00:00:00 2001 From: Chris Westra Date: Thu, 1 Sep 2022 16:36:48 -0400 Subject: [PATCH] WIP --- api/queries_branch_issue_reference.go | 93 +++++++++++++++++++++++++++ pkg/cmd/issue/develop/develop.go | 61 +++++++++++++++--- 2 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 api/queries_branch_issue_reference.go diff --git a/api/queries_branch_issue_reference.go b/api/queries_branch_issue_reference.go new file mode 100644 index 000000000..bb168bb74 --- /dev/null +++ b/api/queries_branch_issue_reference.go @@ -0,0 +1,93 @@ +package api + +type BranchIssueReference struct { + ID int + BranchName string + BranchRepositoryId int + IssueId int + IssueRepositoryId int +} + +func CreateBranchIssueReference(client *Client, repo *Repository, params map[string]interface{}) (*BranchIssueReference, error) { + query := ` + mutation BranchIssueReferenceCreate($input: CreateBranchIssueReferenceInput!) { + mutation($issueId: ID!, $oid: GitObjectID!, $name: String, $repositoryId: ID) { + createLinkedBranch(input: { + issueId: $issueId, + name: $name, + oid: $oid + repositoryId: $repositoryId + }) { + linkedBranch { + ref { + name + repository { + name + } + } + } + } + } + }` + + inputParams := map[string]interface{}{ + "repositoryId": repo.ID, + } + for key, val := range params { + switch key { + case "issueId", "name", "oid": + inputParams[key] = val + } + } + variables := map[string]interface{}{ + "input": inputParams, + } + + result := struct { + createLinkedBranch struct { + BranchIssueReference BranchIssueReference + } + }{} + + err := client.GraphQL(repo.RepoHost(), query, variables, &result) + if err != nil { + return nil, err + } + ref := &result.createLinkedBranch.BranchIssueReference + return ref, nil + +} + +func FindBaseOid(client *Client, repo *Repository, ref string) (string, error) { + query := ` + query BranchIssueReferenceFindBaseOid($repositoryId: ID!, $ref: String!) { + repository(id: $repositoryId) { + ref(qualifiedName: $ref) { + target { + oid + } + } + } + }` + + variables := map[string]interface{}{ + "repositoryId": repo.ID, + "ref": ref, + } + + result := struct { + Repository struct { + Ref struct { + Target struct { + Oid string + } + } + } + }{} + + err := client.GraphQL(repo.RepoHost(), query, variables, &result) + if err != nil { + return "", err + } + return result.Repository.Ref.Target.Oid, nil +} diff --git a/pkg/cmd/issue/develop/develop.go b/pkg/cmd/issue/develop/develop.go index e4fae8066..96fdb4a0c 100644 --- a/pkg/cmd/issue/develop/develop.go +++ b/pkg/cmd/issue/develop/develop.go @@ -5,9 +5,11 @@ import ( "net/http" "github.com/MakeNowJust/heredoc" + "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/internal/browser" "github.com/cli/cli/v2/internal/config" "github.com/cli/cli/v2/internal/ghrepo" + "github.com/cli/cli/v2/pkg/cmd/issue/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" "github.com/spf13/cobra" @@ -20,10 +22,11 @@ type DevelopOptions struct { BaseRepo func() (ghrepo.Interface, error) Browser browser.Browser - IssueRepo string - Name string - BaseBranch string - Checkout bool + IssueRepo string + IssueNumber string + Name string + BaseBranch string + Checkout bool } func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra.Command { @@ -59,10 +62,50 @@ func NewCmdDevelop(f *cmdutil.Factory, runF func(*DevelopOptions) error) *cobra. } func developRun(opts *DevelopOptions) (err error) { - // httpClient, err := opts.HttpClient() - // if err != nil { - // return err - // } - fmt.Fprintf(opts.IO.ErrOut, "hello world") + httpClient, err := opts.HttpClient() + if err != nil { + return err + } + apiClient := api.NewClientFromHTTP(httpClient) + baseRepo, err := opts.BaseRepo() + if err != nil { + return err + } + opts.IO.StartProgressIndicator() + fmt.Fprintf(opts.IO.ErrOut, "running") + repo, err := api.GitHubRepo(apiClient, baseRepo) + fmt.Fprintf(opts.IO.ErrOut, "found your repo %s\n", repo.Name) + if err != nil { + return err + } + + oid, err := api.FindBaseOid(apiClient, repo, opts.BaseBranch) + if err != nil { + return err + } + + fmt.Fprintf(opts.IO.ErrOut, "found oid for %s", oid) + // get the id of the issue repo + issue, _, err := shared.IssueFromArgWithFields(httpClient, opts.BaseRepo, opts.IssueNumber, []string{"id", "number", "title", "state"}) + if err != nil { + return err + } + + // get the oid of the branch from the base repo + params := map[string]interface{}{ + "issueId": issue.ID, + "name": opts.Name, + "oid": oid, + "repositoryId": repo.ID, + } + + ref, err := api.CreateBranchIssueReference(apiClient, repo, params) + opts.IO.StopProgressIndicator() + if ref != nil { + fmt.Fprintf(opts.IO.Out, "Created %s\n", ref.BranchName) + } + if err != nil { + return err + } return }