Skip to content
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

コマンドラインオプションでIssue,PRの番号をしてい可能にする、指定がない場合はIssue,PRのリストを選択式にする #8

Merged
merged 4 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
38 changes: 29 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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" {
Expand Down
30 changes: 19 additions & 11 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
}
Expand All @@ -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 {
Expand Down