-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_pr_comment.go
113 lines (94 loc) · 2.57 KB
/
step_pr_comment.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd
import (
"io"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"strconv"
"fmt"
"github.com/jenkins-x/jx/pkg/gits"
)
// GetOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type StepPRCommentOptions struct {
StepPROptions
Flags StepPRCommentFlags
}
type StepPRCommentFlags struct {
Comment string
URL string
Owner string
Repository string
PR string
}
// NewCmdStep Steps a command object for the "step" command
func NewCmdStepPRComment(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &StepPRCommentOptions{
StepPROptions: StepPROptions{
StepOptions: StepOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "comment",
Short: "pipeline step pr comment",
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Flags.Comment, "comment", "c", "", "comment to add to the Pull Request")
cmd.Flags().StringVarP(&options.Flags.Owner, "owner", "o", "", "Git organisation / owner")
cmd.Flags().StringVarP(&options.Flags.Repository, "repository", "r", "", "Git repository")
cmd.Flags().StringVarP(&options.Flags.PR, "pull-request", "p", "", "Git Pull Request number")
options.addCommonFlags(cmd)
return cmd
}
// Run implements this command
func (o *StepPRCommentOptions) Run() error {
if o.Flags.PR == "" {
return fmt.Errorf("no Pull Request number provided")
}
if o.Flags.Owner == "" {
return fmt.Errorf("no Git owner provided")
}
if o.Flags.Repository == "" {
return fmt.Errorf("no Git repository provided")
}
if o.Flags.Comment == "" {
return fmt.Errorf("no comment provided")
}
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
gitInfo, err := o.Git().Info("")
if err != nil {
return err
}
gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return err
}
provider, err := gitInfo.PickOrCreateProvider(authConfigSvc, "user name to submit comment as", o.BatchMode, gitKind, o.Git(), o.In, o.Out, o.Err)
if err != nil {
return err
}
prNumber, err := strconv.Atoi(o.Flags.PR)
if err != nil {
return err
}
pr := gits.GitPullRequest{
Repo: o.Flags.Repository,
Owner: o.Flags.Owner,
Number: &prNumber,
}
return provider.AddPRComment(&pr, o.Flags.Comment)
}