diff --git a/cmd/issues.go b/cmd/issues.go index 99ccd18..dd517d0 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -25,27 +25,7 @@ func doSearchIssues(ctx *cli.Context) error { auth := getAuth(ctx.String("login")) client := getClient(ctx.String("url")) - questions := []*survey.Question{ - { - Name: "repo", - Prompt: &survey.Input{Message: "Full repository name", Default: fullName()}, - 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 err - } - ownerRepo := strings.Split(answers.Repo, "/") - issues, err := getAllIssues(auth, client, ownerRepo[0], ownerRepo[1], &gitea.IssueListIssuesOpts{Q: optional.NewString(answers.Query)}) + issues, err := queryIssues(auth, client, false) if err != nil { return err } @@ -89,6 +69,39 @@ func doSearchIssues(ctx *cli.Context) error { return nil } +func queryIssues(auth context.Context, client *gitea.APIClient, pulls bool) ([]gitea.Issue, error) { + questions := []*survey.Question{ + { + Name: "repo", + Prompt: &survey.Input{Message: "Full repository name", Default: fullName()}, + 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, "/") + opts := &gitea.IssueListIssuesOpts{Q: optional.NewString(answers.Query), Type_: optional.NewString("issues")} + if pulls { + opts.Type_ = optional.NewString("pulls") + } + issues, err := getAllIssues(auth, client, ownerRepo[0], ownerRepo[1], opts) + if err != nil { + return nil, err + } + + return issues, nil +} + func getAllIssues(auth context.Context, client *gitea.APIClient, owner, repo string, opts *gitea.IssueListIssuesOpts) ([]gitea.Issue, error) { opts.State = optional.NewString("open") issuesOpen, err := getIssues(auth, client, owner, repo, opts) @@ -107,7 +120,6 @@ func getAllIssues(auth context.Context, client *gitea.APIClient, owner, repo str func getIssues(auth context.Context, client *gitea.APIClient, owner, repo string, opts *gitea.IssueListIssuesOpts) ([]gitea.Issue, error) { issues := make([]gitea.Issue, 0) - opts.Type_ = optional.NewString("issues") var p int32 = 1 for { opts.Page = optional.NewInt32(p) diff --git a/cmd/pulls.go b/cmd/pulls.go index 9cf580f..74cccd3 100644 --- a/cmd/pulls.go +++ b/cmd/pulls.go @@ -2,10 +2,89 @@ package cmd import ( "context" + "fmt" + "gitea.com/jolheiser/beaver/color" "gitea.com/jolheiser/gitea-sdk" + "gitea.com/jolheiser/tea/modules/markdown" + "gitea.com/jolheiser/tea/modules/stdout" + "github.com/AlecAivazis/survey/v2" "github.com/antihax/optional" + "github.com/urfave/cli/v2" + "strconv" + "strings" ) +var Pulls = cli.Command{ + Name: "pulls", + Aliases: []string{"pr"}, + Usage: "Commands for interacting with pull requests", + Action: doSearchPulls, +} + +func doSearchPulls(ctx *cli.Context) error { + auth := getAuth(ctx.String("login")) + client := getClient(ctx.String("url")) + + issues, err := queryIssues(auth, client, true) + if err != nil { + return err + } + + if len(issues) == 0 { + stdout.Red("No pulls found") + return nil + } + + issueMap := make(map[string]gitea.Issue) + for _, issue := range issues { + index := color.New(color.FgCyan).Format("#" + strconv.Itoa(int(issue.Number))) + title := color.New(color.FgYellow).Format(issue.Title) + lbls := make([]string, len(issue.Labels)) + for idx, label := range issue.Labels { + lbls[idx] = label.Name + } + var labels string + if len(lbls) > 0 { + labels = color.New(color.FgHiBlack).Format("(" + strings.Join(lbls, ",") + ")") + } + issueMap[fmt.Sprintf("%s %s %s", index, title, labels)] = issue + } + + list := make([]string, 0) + for key := range issueMap { + list = append(list, key) + } + sel := &survey.Select{Options: list, Message: "Matching issues, select one to see more details"} + var selection string + if err := survey.AskOne(sel, &selection); err != nil { + return err + } + + body, err := markdown.Render(issueMap[selection].Body) + if err != nil { + return err + } + + fmt.Println(body) + return nil +} + +func getAllPulls(auth context.Context, client *gitea.APIClient, owner, repo string, opts *gitea.RepoListPullRequestsOpts) ([]gitea.PullRequest, error) { + opts.State = optional.NewString("open") + pullsOpen, err := getPulls(auth, client, owner, repo, opts) + if err == nil { + return nil, err + } + + opts.State = optional.NewString("closed") + pullsClosed, err := getPulls(auth, client, owner, repo, opts) + if err == nil { + return nil, err + } + + return append(pullsOpen, pullsClosed...), nil +} + func getPulls(auth context.Context, client *gitea.APIClient, owner, repo string, opts *gitea.RepoListPullRequestsOpts) ([]gitea.PullRequest, error) { pulls := make([]gitea.PullRequest, 0) var p int32 = 1 diff --git a/go.mod b/go.mod index bc40b14..e8e7102 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,7 @@ require ( github.com/golang/protobuf v1.3.3 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/kr/pty v1.1.8 // indirect + github.com/kyokomi/emoji v2.1.0+incompatible github.com/mitchellh/go-homedir v1.1.0 github.com/sergi/go-diff v1.1.0 // indirect github.com/urfave/cli/v2 v2.1.1 diff --git a/go.sum b/go.sum index 258e86d..4cdf9bc 100644 --- a/go.sum +++ b/go.sum @@ -65,6 +65,8 @@ github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI= github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kyokomi/emoji v2.1.0+incompatible h1:+DYU2RgpI6OHG4oQkM5KlqD3Wd3UPEsX8jamTo1Mp6o= +github.com/kyokomi/emoji v2.1.0+incompatible/go.mod h1:mZ6aGCD7yk8j6QY6KICwnZ2pxoszVseX1DNoGtU2tBA= github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23 h1:Wp7NjqGKGN9te9N/rvXYRhlVcrulGdxnz8zadXWs7fc= github.com/logrusorgru/aurora v0.0.0-20191116043053-66b7ad493a23/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac= diff --git a/main.go b/main.go index bfb7ee6..a436de4 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ func main() { &cmd.Logout, &cmd.Repo, &cmd.Issues, + &cmd.Pulls, } app.Flags = cmd.Flags app.EnableBashCompletion = true diff --git a/modules/markdown/markdown.go b/modules/markdown/markdown.go index d146f66..1336f74 100644 --- a/modules/markdown/markdown.go +++ b/modules/markdown/markdown.go @@ -2,6 +2,7 @@ package markdown import ( "github.com/charmbracelet/glamour" + "github.com/kyokomi/emoji" "strings" ) @@ -12,5 +13,5 @@ func normalizeNewlines(s string) string { } func Render(text string) (string, error) { - return glamour.Render(normalizeNewlines(text), "dark") + return glamour.Render(emoji.Sprint(normalizeNewlines(text)), "dark") }