1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00
sip/cmd/pulls_status.go
John Olheiser 1956062791 Fix PR head nil panic (#7)
Fix PR head nil panic

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/7
2020-02-27 03:03:32 +00:00

62 lines
1.6 KiB
Go

package cmd
import (
"code.gitea.io/sdk/gitea"
"fmt"
"gitea.com/jolheiser/sip/modules/git"
"gitea.com/jolheiser/sip/modules/sdk"
"github.com/urfave/cli/v2"
"go.jolheiser.com/beaver/color"
"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 {
if pull.Head == nil {
continue
}
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
}