diff --git a/README.md b/README.md index ef56176..502c136 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,28 @@ add Issue or Pull Request to Project(v2) gh ap ``` +### Optional Args + +```bash +gh ap --help + -issue int + Issue Number + -pr int + PullRequest Number +``` + +- Specified Issue Number(Optional) + +```bash +gh ap -issue ${issueNumber} +``` + +- Specified PullRequest Number(Optional) + +```bash +gh ap -pr ${pullRequestNumber} +``` + ## Demo ![demo](demo.gif) diff --git a/cli.go b/cli.go index 9cda5af..70a0808 100644 --- a/cli.go +++ b/cli.go @@ -54,3 +54,24 @@ func ghContent(contentType string, number string) Content { return content } + +func ghContentList(contentType string) []Content { + var subCommand string + if contentType == "Issue" { + subCommand = "issue" + } else { + subCommand = "pr" + } + args := []string{subCommand, "list", "--limit", "50", "--json", "id,number,title"} + stdOut, _, err := gh.Exec(args...) + if err != nil { + log.Fatal(err) + } + var contents []Content + + if err := json.Unmarshal(stdOut.Bytes(), &contents); err != nil { + panic(err) + } + + return contents +} diff --git a/main.go b/main.go index 4ba605d..7083326 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "flag" "github.com/cli/go-gh" "github.com/cli/go-gh/pkg/api" "log" + "strconv" ) func getProjects(gqlclient api.GQLClient) []Project { @@ -54,6 +56,14 @@ func getProjects(gqlclient api.GQLClient) []Project { } func main() { + var options struct { + issueNo int + prNo int + } + flag.IntVar(&options.issueNo, "issue", 0, "Issue Number") + flag.IntVar(&options.prNo, "pr", 0, "PullRequest Number") + flag.Parse() + gqlclient, err := gh.GQLClient(nil) if err != nil { log.Fatal(err) @@ -63,18 +73,28 @@ func main() { projectId := askOneProjectId(projects) fields := getProjectFields(gqlclient, projectId) - itemTypes := []string{"Current PullRequest", "PullRequest", "Issue"} - selectedType := askOneContentType(itemTypes) - var itemId string - if selectedType == "Current PullRequest" { - currentPR := ghCurrentPullRequest() - itemId = addProject(gqlclient, projectId, currentPR.Id) + var content Content + + if options.issueNo != 0 || options.prNo != 0 { + if options.issueNo != 0 { + content = ghContent("Issue", strconv.Itoa(options.issueNo)) + } else { + content = ghContent("PullRequest", strconv.Itoa(options.prNo)) + } } else { - number := askContentNumber(selectedType) - content := ghContent(selectedType, number) - itemId = addProject(gqlclient, projectId, content.Id) + itemTypes := []string{"Current PullRequest", "PullRequest", "Issue"} + selectedType := askOneContentType(itemTypes) + + if selectedType == "Current PullRequest" { + content = ghCurrentPullRequest() + } else { + contentList := ghContentList(selectedType) + number := askContentNumber(selectedType, contentList) + content = ghContent(selectedType, number) + } } + itemId = addProject(gqlclient, projectId, content.Id) for _, field := range fields { if field.DataType == "TEXT" { diff --git a/survey.go b/survey.go index d40b001..0a94c20 100644 --- a/survey.go +++ b/survey.go @@ -55,19 +55,26 @@ func askOneContentType(itemTypes []string) string { return selectedType } -func askContentNumber(contentType string) string { +func askContentNumber(contentType string, contents []Content) string { + var numbers = make([]string, len(contents)) + for i, c := range contents { + numbers[i] = strconv.Itoa(c.Number) + } + name := contentType + " Number" qs := []*survey.Question{ { - Name: "number", - Prompt: &survey.Input{Message: name}, - Validate: func(v interface{}) error { - strValue := v.(string) - _, err := strconv.Atoi(strValue) - if err != nil { - return errors.New("Value is Int.") - } - return nil + Name: "number", + Prompt: &survey.Select{ + Message: name, + Options: numbers, + Description: func(value string, index int) string { + return contents[index].Title + }, + Filter: func(filterValue string, optValue string, optIndex int) bool { + return strings.Contains(contents[optIndex].Title, filterValue) + }, + PageSize: 50, }, }, } @@ -76,8 +83,9 @@ func askContentNumber(contentType string) string { if err != nil { log.Fatal(err.Error()) } + optionAnswer := answers["number"].(survey.OptionAnswer) - return answers["number"].(string) + return optionAnswer.Value } func askTextFieldValue(fieldName string) string {