Skip to content

Commit

Permalink
support assign command (#11)
Browse files Browse the repository at this point in the history
Signed-off-by: wq1019 <2013855675@qq.com>
  • Loading branch information
sunl888 committed Mar 30, 2020
1 parent 70abd98 commit 53734a3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/pingcap-incubator/cherry-bot/pkg/types"

"github.com/pingcap-incubator/cherry-bot/pkg/providers/approve"
"github.com/pingcap-incubator/cherry-bot/pkg/providers/assign"
autoUpdate "github.com/pingcap-incubator/cherry-bot/pkg/providers/auto-update"
"github.com/pingcap-incubator/cherry-bot/pkg/providers/cherry"
"github.com/pingcap-incubator/cherry-bot/pkg/providers/contributor"
Expand Down Expand Up @@ -41,6 +42,7 @@ type Middleware struct {
Approve *approve.Approve
Contributor *contributor.Contributor
FileWatcher *fileWatcher.Watcher
Assign *assign.Assign
}

type bot struct {
Expand Down Expand Up @@ -87,6 +89,7 @@ func InitBot(repo *config.RepoConfig, opr *operator.Operator) Bot {
Approve: approve.Init(repo, opr),
Contributor: contributor.Init(repo, opr),
FileWatcher: fileWatcher.Init(repo, opr),
Assign: assign.Init(repo, opr),
},
}
}
Expand Down
2 changes: 2 additions & 0 deletions bot/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ func (b *bot) processIssueCommentEvent(event *github.IssueCommentEvent) {
b.Middleware.Approve.ProcessIssueCommentEvent(event)
}

b.Middleware.Assign.ProcessIssueCommentEvent(event)

b.Middleware.CommandRedeliver.ProcessIssueCommentEvent(event)
}

Expand Down
22 changes: 22 additions & 0 deletions pkg/providers/assign/assign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package assign

import (
"github.com/pingcap-incubator/cherry-bot/config"
"github.com/pingcap-incubator/cherry-bot/pkg/operator"
)

type Assign struct {
owner string
repo string
opr *operator.Operator
cfg *config.RepoConfig
}

func Init(repoCfg *config.RepoConfig, opr *operator.Operator) *Assign {
return &Assign{
owner: repoCfg.Owner,
repo: repoCfg.Repo,
opr: opr,
cfg: repoCfg,
}
}
54 changes: 54 additions & 0 deletions pkg/providers/assign/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package assign

import (
"context"
"github.com/google/go-github/v29/github"
"github.com/pingcap-incubator/cherry-bot/util"
"github.com/pkg/errors"
"strings"
)

const (
assignCommand = "/assign"
assignCancelCommand = "/assign cancel"
)

func (assign *Assign) ProcessIssueCommentEvent(event *github.IssueCommentEvent) {
if event.GetAction() != "created" {
return
}
opType := strings.TrimSpace(event.GetComment().GetBody())
comment := ""
if err := assign.do(event, opType); err != nil {
util.Error(err)
comment = "Assign failed."
}
if err := assign.addGithubComment(event.GetIssue().GetNumber(), comment); err != nil {
util.Error(err)
}
}

func (assign *Assign) do(event *github.IssueCommentEvent, opType string) (err error) {
assignees := []string{event.GetComment().GetUser().GetLogin()}
switch opType {
case assignCommand:
_, _, err = assign.opr.Github.Issues.AddAssignees(context.Background(), assign.owner, assign.repo,
event.GetIssue().GetNumber(), assignees)
case assignCancelCommand:
_, _, err = assign.opr.Github.Issues.RemoveAssignees(context.Background(), assign.owner, assign.repo,
event.GetIssue().GetNumber(), assignees)
}
return errors.Wrap(err, "send assign")
}

func (assign *Assign) addGithubComment(pullNumber int, commentBody string) error {
if commentBody == "" {
return nil
}
comment := &github.IssueComment{
Body: &commentBody,
}
_, _, err := assign.opr.Github.Issues.CreateComment(context.Background(),
assign.owner, assign.repo, pullNumber, comment)
return errors.Wrap(err, "add github comment")
}
6 changes: 3 additions & 3 deletions pkg/providers/redeliver-command/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ func (c *CommandRedeliver) ProcessIssueCommentEvent(event *github.IssueCommentEv
githubComment := &github.IssueComment{
Body: &comment,
}
IssueInfo := fmt.Sprintf("%s/%s #%d", c.repo.Owner, c.repo.Repo, event.GetIssue().GetNumber())
issueInfo := fmt.Sprintf("%s/%s #%d", c.repo.Owner, c.repo.Repo, event.GetIssue().GetNumber())
if _, _, err := c.opr.Github.Issues.CreateComment(context.Background(),
c.repo.Owner, c.repo.Repo, event.GetIssue().GetNumber(), githubComment); err != nil {
log.Println("error occured when redeliver command in %s, %s", IssueInfo, err)
log.Printf("error occured when redeliver command in %s, %s\n", issueInfo, err)
} else {
log.Println("redeliver command success, pull %s", IssueInfo)
log.Printf("redeliver command success, pull %s\n", issueInfo)
}
}

0 comments on commit 53734a3

Please sign in to comment.