Skip to content

Commit

Permalink
feat: skip the invalid pr with message sending (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
LinuxSuRen authored Sep 26, 2023
1 parent 82e5d1c commit 5e9c409
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newPullRequestCmd() (c *cobra.Command) {
flags.BoolVarP(&opt.printAssignee, "assignee", "", false, "Print the assignees of the pull request")
flags.StringVarP(&opt.msg, "msg", "", "", "The message of the pull request")
flags.StringSliceVarP(&opt.dingdingTokenPairs, "dingding-tokens", "", []string{}, "The dingding token pairs of the pull request, format: login=token")
flags.BoolVarP(&opt.skipInvalidPR, "skip-invalid-pr", "", true, "Skip the invalid pull request")
return
}

Expand All @@ -49,6 +50,13 @@ func (o *pullRequestOption) preRunE(c *cobra.Command, args []string) (err error)
}

func (o *pullRequestOption) runE(c *cobra.Command, args []string) (err error) {
if o.pr <= 0 {
if !o.skipInvalidPR {
err = fmt.Errorf("invalid pr number %d", o.pr)
}
return
}

var scmClient *scm.Client
if scmClient, err = o.getClient(); err != nil {
return
Expand Down Expand Up @@ -156,4 +164,5 @@ type pullRequestOption struct {
msg string
dingdingTokenPairs []string
dingdingTokenMap map[string]string
skipInvalidPR bool
}
19 changes: 19 additions & 0 deletions cmd/pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ func TestPullRequestCmd(t *testing.T) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unsupported")
})

t.Run("invalid pr number, do not skip", func(t *testing.T) {
c := NewRootCommand()
c.SetOut(io.Discard)

c.SetArgs([]string{"pr", "--pr=-1", "--repo=xxx/xxx", "--token=token", "--username=xxx", "--skip-invalid-pr=false"})
err := c.Execute()
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid pr number")
})

t.Run("skip invalid pr number", func(t *testing.T) {
c := NewRootCommand()
c.SetOut(io.Discard)

c.SetArgs([]string{"pr", "--pr=-1", "--repo=xxx/xxx", "--token=token", "--username=xxx"})
err := c.Execute()
assert.NoError(t, err)
})
}

func TestFormatMessage(t *testing.T) {
Expand Down

0 comments on commit 5e9c409

Please sign in to comment.