1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-23 04:12:00 +01:00
sip/cmd/pulls_status.go

59 lines
1.6 KiB
Go
Raw Normal View History

package cmd
import (
"code.gitea.io/sdk/gitea"
"fmt"
"gitea.com/jolheiser/beaver/color"
"gitea.com/jolheiser/sip/modules/git"
"gitea.com/jolheiser/sip/modules/sdk"
"github.com/urfave/cli/v2"
"strconv"
)
var PullsStatus = cli.Command{
Name: "status",
Usage: "View the status of a pull request",
Action: doPullStatus,
}
func doPullStatus(ctx *cli.Context) error {
client := getClient(ctx)
head := fmt.Sprintf("%s:%s", getOriginRepo()[1], git.Branch())
pulls, err := sdk.GetPulls(client, getUpstreamRepo()[1], getUpstreamRepo()[2], gitea.ListPullRequestsOptions{State: "all"})
if err != nil {
return err
}
var pr *gitea.PullRequest
for _, pull := range pulls {
compare := fmt.Sprintf("%s:%s", pull.Head.Repository.Owner.UserName, pull.Head.Name)
if compare == head {
pr = pull
break
}
}
if pr == nil {
return fmt.Errorf("no pull request found with head target %s", color.New(color.FgMagenta).Format(head))
}
index := color.New(color.FgCyan).Format("#" + strconv.Itoa(int(pr.Index)))
title := color.New(color.FgYellow).Format(pr.Title)
state := color.New(color.FgGreen).Format("[open]")
if pr.HasMerged {
state = color.New(color.FgMagenta).Format("[merged]")
} else if pr.State == gitea.StateClosed {
state = color.New(color.FgRed).Format("[closed]")
}
lbls := make([]string, len(pr.Labels))
for idx, label := range pr.Labels {
lbls[idx] = label.Name
}
fmt.Println(index, title, state)
fmt.Println(color.New(color.FgCyan).Format(fmt.Sprintf("%d comments", pr.Comments)))
fmt.Println(color.New(color.FgYellow).Format(pr.HTMLURL))
return nil
}