-
Notifications
You must be signed in to change notification settings - Fork 51
/
comment.go
89 lines (73 loc) · 2.23 KB
/
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
package comment
import (
"context"
"fmt"
"strconv"
"github.com/jenkins-x/jx-helpers/v3/pkg/scmhelpers"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"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(`
Adds a comment to the current pull request
`)
cmdExample = templates.Examples(`
# add comment
%s pr comment -c "message from Jenkins X pipeline"
`)
)
// Options the options for the command
type Options struct {
scmhelpers.PullRequestOptions
Comment string
Result *scm.PullRequest
}
// NewCmdPullRequestComment creates a command object for the command
func NewCmdPullRequestComment() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "comment",
Short: "Add comment to the pull request",
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().StringVarP(&o.Comment, "comment", "c", "", "comment to add")
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.commentPullRequest(pr)
}
func (o *Options) commentPullRequest(pr *scm.PullRequest) error {
o.Result = pr
ctx := context.Background()
comment := &scm.CommentInput{Body: o.Comment}
_, _, err := o.ScmClient.PullRequests.CreateComment(ctx, o.FullRepositoryName, o.Number, comment)
prName := "#" + strconv.Itoa(o.Number)
if err != nil {
return errors.Wrapf(err, "failed to comment on pull request %s on repository %s", prName, o.FullRepositoryName)
}
log.Logger().Infof("commented on pull request %s on repository %s", prName, o.FullRepositoryName)
return nil
}