2020-02-14 21:46:02 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-16 06:51:14 +01:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-02-14 21:46:02 +01:00
|
|
|
"fmt"
|
|
|
|
"gitea.com/jolheiser/beaver/color"
|
2020-02-17 21:50:02 +01:00
|
|
|
"gitea.com/jolheiser/sip/modules/markdown"
|
|
|
|
"gitea.com/jolheiser/sip/modules/sdk"
|
|
|
|
"gitea.com/jolheiser/sip/modules/stdout"
|
2020-02-14 21:46:02 +01:00
|
|
|
"github.com/AlecAivazis/survey/v2"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Issues = cli.Command{
|
|
|
|
Name: "issues",
|
|
|
|
Aliases: []string{"issue"},
|
|
|
|
Usage: "Commands for interacting with issues",
|
2020-02-16 06:51:14 +01:00
|
|
|
Action: doIssuesSearch,
|
|
|
|
Subcommands: []*cli.Command{
|
2020-02-17 06:28:11 +01:00
|
|
|
&IssuesCreate,
|
2020-02-16 06:51:14 +01:00
|
|
|
},
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-17 06:28:11 +01:00
|
|
|
func doIssuesSearch(ctx *cli.Context) error {
|
|
|
|
if _, err := issuesSearch(ctx, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func issuesSearch(ctx *cli.Context, pulls bool) (*gitea.Issue, error) {
|
2020-02-16 06:51:14 +01:00
|
|
|
typ := "issues"
|
|
|
|
if pulls {
|
|
|
|
typ = "pulls"
|
|
|
|
}
|
2020-02-17 06:28:11 +01:00
|
|
|
issues, err := queryIssues(ctx, getClient(ctx), pulls)
|
2020-02-14 21:46:02 +01:00
|
|
|
if err != nil {
|
2020-02-17 06:28:11 +01:00
|
|
|
return nil, err
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(issues) == 0 {
|
2020-02-16 06:51:14 +01:00
|
|
|
stdout.Red("No " + typ + " found")
|
2020-02-17 06:28:11 +01:00
|
|
|
return nil, nil
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
issueMap := make(map[string]*gitea.Issue)
|
2020-02-14 21:46:02 +01:00
|
|
|
for _, issue := range issues {
|
2020-02-16 06:51:14 +01:00
|
|
|
index := color.New(color.FgCyan).Format("#" + strconv.Itoa(int(issue.Index)))
|
2020-02-14 21:46:02 +01:00
|
|
|
title := color.New(color.FgYellow).Format(issue.Title)
|
2020-02-16 06:51:14 +01:00
|
|
|
state := color.New(color.FgGreen).Format("[open]")
|
2020-02-16 23:31:01 +01:00
|
|
|
if issue.PullRequest != nil && issue.PullRequest.HasMerged {
|
|
|
|
state = color.New(color.FgMagenta).Format("[merged]")
|
|
|
|
} else if issue.State == gitea.StateClosed {
|
2020-02-16 06:51:14 +01:00
|
|
|
state = color.New(color.FgRed).Format("[closed]")
|
|
|
|
}
|
2020-02-14 21:46:02 +01:00
|
|
|
lbls := make([]string, len(issue.Labels))
|
|
|
|
for idx, label := range issue.Labels {
|
|
|
|
lbls[idx] = label.Name
|
|
|
|
}
|
|
|
|
var labels string
|
|
|
|
if len(lbls) > 0 {
|
2020-02-16 06:51:14 +01:00
|
|
|
labels = color.New(color.FgHiBlack).Format("(" + strings.Join(lbls, ", ") + ")")
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
2020-02-16 06:51:14 +01:00
|
|
|
issueMap[fmt.Sprintf("%s %s %s %s", index, state, title, labels)] = issue
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
list := make([]string, 0)
|
|
|
|
for key := range issueMap {
|
|
|
|
list = append(list, key)
|
|
|
|
}
|
2020-02-17 06:28:11 +01:00
|
|
|
sel := &survey.Select{Options: list, Message: "Matching " + typ}
|
2020-02-14 21:46:02 +01:00
|
|
|
var selection string
|
|
|
|
if err := survey.AskOne(sel, &selection); err != nil {
|
2020-02-17 06:28:11 +01:00
|
|
|
return nil, err
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
body, err := markdown.Render(issueMap[selection].Body)
|
|
|
|
if err != nil {
|
2020-02-17 06:28:11 +01:00
|
|
|
return nil, err
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(body)
|
2020-02-17 06:28:11 +01:00
|
|
|
return issueMap[selection], nil
|
2020-02-16 06:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func queryIssues(ctx *cli.Context, client *gitea.Client, pulls bool) ([]*gitea.Issue, error) {
|
2020-02-14 22:18:13 +01:00
|
|
|
questions := []*survey.Question{
|
|
|
|
{
|
|
|
|
Name: "repo",
|
2020-02-16 06:51:14 +01:00
|
|
|
Prompt: &survey.Input{Message: "Full repository name", Default: fullName(ctx)},
|
2020-02-14 22:18:13 +01:00
|
|
|
Validate: validateFullName,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "query",
|
|
|
|
Prompt: &survey.Input{Message: "Search query"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
answers := struct {
|
|
|
|
Repo string
|
|
|
|
Query string
|
|
|
|
}{}
|
|
|
|
|
|
|
|
if err := survey.Ask(questions, &answers); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ownerRepo := strings.Split(answers.Repo, "/")
|
2020-02-16 06:51:14 +01:00
|
|
|
opts := gitea.ListIssueOption{KeyWord: answers.Query, State: "all"}
|
|
|
|
issues, err := sdk.GetIssues(client, ownerRepo[0], ownerRepo[1], opts)
|
2020-02-14 21:46:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
filtered := make([]*gitea.Issue, 0)
|
|
|
|
for _, issue := range issues {
|
2020-02-17 06:28:11 +01:00
|
|
|
if pulls {
|
|
|
|
if issue.PullRequest != nil {
|
|
|
|
filtered = append(filtered, issue)
|
|
|
|
}
|
2020-02-16 06:51:14 +01:00
|
|
|
continue
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
2020-02-16 06:51:14 +01:00
|
|
|
filtered = append(filtered, issue)
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
2020-02-16 06:51:14 +01:00
|
|
|
|
|
|
|
return filtered, nil
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|