-
Notifications
You must be signed in to change notification settings - Fork 51
/
get.go
88 lines (73 loc) · 2.04 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package get
import (
"fmt"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"sigs.k8s.io/yaml"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
cmdLong = templates.LongDesc(`
Gets a pull request and displays fields from it
`)
cmdExample = templates.Examples(`
# display the head source URL
%s pr get --head-url
`)
)
// KptOptions the options for the command
type Options struct {
scmhelpers.PullRequestOptions
ShowHeadURL bool
Result *scm.PullRequest
}
// NewCmdPullRequestPush creates a command object for the command
func NewCmdPullRequestGet() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "get",
Short: "Gets a pull request and displays fields from it",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
o.PullRequestOptions.AddFlags(cmd)
cmd.Flags().BoolVarP(&o.ShowHeadURL, "head-url", "", false, "show the head clone URL of the PR")
return cmd, o
}
// Run implements the command
func (o *Options) Run() error {
err := o.PullRequestOptions.Validate()
if err != nil {
return errors.Wrapf(err, "failed to ")
}
pr, err := o.DiscoverPullRequest()
if err != nil {
return errors.Wrapf(err, "failed to discover the pull request")
}
if pr == nil {
return errors.Errorf("no Pull Request could be found for %d in repository %s", o.Number, o.Repository)
}
return o.displayPullRequest(pr)
}
func (o *Options) displayPullRequest(pr *scm.PullRequest) error {
o.Result = pr
if o.ShowHeadURL {
log.Logger().Info(pr.Head.Repo.Clone)
return nil
}
data, err := yaml.Marshal(pr)
if err != nil {
return errors.Wrapf(err, "failed to marshal PullRequest as YAML")
}
log.Logger().Info(string(data))
return nil
}