This commit is contained in:
Chris Westra 2022-09-01 16:36:48 -04:00
parent 711591c92b
commit 152b861001
2 changed files with 145 additions and 9 deletions

View file

@ -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
}

View file

@ -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
}