Skip to content

Commit

Permalink
Label handling in PR Describe section
Browse files Browse the repository at this point in the history
  • Loading branch information
pavan187 committed Feb 28, 2019
1 parent 9abbbb2 commit cb19dfe
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 43 deletions.
34 changes: 18 additions & 16 deletions handlers/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/golang/glog"
"github.com/google/go-github/github"

)

type GithubIssue github.Issue
Expand All @@ -25,25 +24,28 @@ func (s *Server) handleIssueCommentEvent(body []byte, client *github.Client) {
// Unmarshal
err := json.Unmarshal(body, &commentEvent)
if err != nil {
glog.Errorf("Failed to unmarshal: %v", err)
}

// assign
err = assign.Handle(client, commentEvent)
if err != nil {
glog.Errorf("Failed to handle: %v", err)
glog.Errorf("Failed to unmarshal commentEvent: %v", err)
}

// label
err = label.Handle(client, commentEvent)
if err != nil {
glog.Errorf("Failed to handle: %v", err)
if label.RegAddLabel.MatchString(*commentEvent.Comment.Body) || label.RegRemoveLabel.MatchString(*commentEvent.Comment.Body) {
err = label.Handle(client, commentEvent)
if err != nil {
glog.Errorf("Failed to handle label: %v", err)
}
}
// assign
if AssignOrUnassing.MatchString(*commentEvent.Comment.Body) {
err = assign.Handle(client, commentEvent)
if err != nil {
glog.Errorf("Failed to handle assign: %v", err)
}
}

// retest
err = retest.Handle(client, commentEvent, s.Config.TravisCIToken, s.Config.TravisRepoName)
if err != nil {
glog.Errorf("Failed to handle: %v", err)
if TestReg.MatchString(*commentEvent.Comment.Body) || RetestReg.MatchString(*commentEvent.Comment.Body) {
err = retest.Handle(client, commentEvent, s.Config.TravisCIToken, s.Config.TravisRepoName)
if err != nil {
glog.Errorf("Failed to handle retest: %v", err)
}
}

}
30 changes: 15 additions & 15 deletions handlers/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

var (
// regular expression to add label
regAddLabel = regexp.MustCompile(`(?mi)^/(kind|priority)\s*(.*)$`)
RegAddLabel = regexp.MustCompile(`(?mi)^/(kind|priority)\s*(.*)$`)
// regular expression to remove label
regRemoveLabel = regexp.MustCompile(`(?mi)^/remove-(kind|priority)\s*(.*)$`)
RegRemoveLabel = regexp.MustCompile(`(?mi)^/remove-(kind|priority)\s*(.*)$`)
)

// Handle event with label
Expand All @@ -23,19 +23,19 @@ func Handle(client *github.Client, event github.IssueCommentEvent) error {
glog.Infof("receive event with label. comment: %s", comment)

// add labels
if regAddLabel.MatchString(comment) {
return add(client, event)
if RegAddLabel.MatchString(comment) {
return Add(client, event)
}
// remove labels
if regRemoveLabel.MatchString(comment) {
return remove(client, event)
if RegRemoveLabel.MatchString(comment) {
return Remove(client, event)
}

return nil
}

// add labels
func add(client *github.Client, event github.IssueCommentEvent) error {
func Add(client *github.Client, event github.IssueCommentEvent) error {
// get basic params
ctx := context.Background()
comment := *event.Comment.Body
Expand All @@ -45,7 +45,7 @@ func add(client *github.Client, event github.IssueCommentEvent) error {
glog.Infof("add label started. comment: %s owner: %s repo: %s number: %d", comment, owner, repo, number)

// map of add labels
mapOfAddLabels := getLabelsMap(comment)
mapOfAddLabels := GetLabelsMap(comment)
glog.Infof("map of add labels: %v", mapOfAddLabels)

// list labels in current github repository
Expand All @@ -65,7 +65,7 @@ func add(client *github.Client, event github.IssueCommentEvent) error {
glog.Infof("list of issue labels: %v", listofIssueLabels)

// list of add labels
listOfAddLabels := getListOfAddLabels(mapOfAddLabels, listofRepoLabels, listofIssueLabels)
listOfAddLabels := GetListOfAddLabels(mapOfAddLabels, listofRepoLabels, listofIssueLabels)
glog.Infof("list of add labels: %v", listOfAddLabels)

// invoke github api to add labels
Expand All @@ -84,7 +84,7 @@ func add(client *github.Client, event github.IssueCommentEvent) error {
}

// remove labels
func remove(client *github.Client, event github.IssueCommentEvent) error {
func Remove(client *github.Client, event github.IssueCommentEvent) error {
// get basic params
ctx := context.Background()
comment := *event.Comment.Body
Expand All @@ -94,7 +94,7 @@ func remove(client *github.Client, event github.IssueCommentEvent) error {
glog.Infof("remove label started. comment: %s owner: %s repo: %s number: %d", comment, owner, repo, number)

// map of add labels
mapOfRemoveLabels := getLabelsMap(comment)
mapOfRemoveLabels := GetLabelsMap(comment)
glog.Infof("map of remove labels: %v", mapOfRemoveLabels)

// list labels in current issue
Expand All @@ -106,7 +106,7 @@ func remove(client *github.Client, event github.IssueCommentEvent) error {
glog.Infof("list of issue labels: %v", listofIssueLabels)

// list of remove labels
listOfRemoveLabels := getListOfRemoveLabels(mapOfRemoveLabels, listofIssueLabels)
listOfRemoveLabels := GetListOfRemoveLabels(mapOfRemoveLabels, listofIssueLabels)
glog.Infof("list of remove labels: %v", listOfRemoveLabels)

// invoke github api to remove labels
Expand All @@ -126,7 +126,7 @@ func remove(client *github.Client, event github.IssueCommentEvent) error {
}

// getListOfAddLabels return the exact list of add labels
func getListOfAddLabels(mapOfAddLabels map[string]string, listofRepoLabels []*github.Label, listofIssueLabels []*github.Label) []string {
func GetListOfAddLabels(mapOfAddLabels map[string]string, listofRepoLabels []*github.Label, listofIssueLabels []*github.Label) []string {
// init
listOfAddLabels := make([]string, 0)
// range over the map to filter the list of labels
Expand Down Expand Up @@ -166,7 +166,7 @@ func getListOfAddLabels(mapOfAddLabels map[string]string, listofRepoLabels []*gi
}

// getListOfRemoveLabels return the exact list of remove labels
func getListOfRemoveLabels(mapOfRemoveLabels map[string]string, listofIssueLabels []*github.Label) []string {
func GetListOfRemoveLabels(mapOfRemoveLabels map[string]string, listofIssueLabels []*github.Label) []string {
// init
listOfRemoveLabels := make([]string, 0)
// range over the map to filter the list of labels
Expand All @@ -192,7 +192,7 @@ func getListOfRemoveLabels(mapOfRemoveLabels map[string]string, listofIssueLabel
}

// getLabelsMap for add or remove labels
func getLabelsMap(comment string) map[string]string {
func GetLabelsMap(comment string) map[string]string {
// init labels map
mapOfLabels := map[string]string{}
// split with blank space
Expand Down
58 changes: 56 additions & 2 deletions handlers/pullrequest.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,68 @@
package handlers

import (
"context"
"encoding/json"
"strings"

"github.com/Huawei-PaaS/ci-bot/handlers/label"

"github.com/golang/glog"
"github.com/google/go-github/github"
)

type GithubPR github.PullRequest
const (
kind = "/kind"
)

type GithubPR github.PullRequestEvent

func (s *Server) handlePullRequestEvent(body []byte) {
func (s *Server) handlePullRequestEvent(body []byte, client *github.Client) {
glog.Infof("Received an PullRequest Event")
// get basic params
ctx := context.Background()
var Label []string
var prEvent github.PullRequestEvent
listOfAddLabels := make([]string, 0)
// Unmarshal
err := json.Unmarshal(body, &prEvent)
if err != nil {
glog.Errorf("Failed to unmarshal prEvent: %v", err)
}
//Get the Lable to add
eventBody := strings.SplitAfter(*prEvent.PullRequest.Body, kind)

if strings.Contains(eventBody[0], kind) {
if len(eventBody) >= 1 {
//Trimming /kind lable operation
labelOp := strings.Split(eventBody[1], "\r\n")
Op := strings.Trim(labelOp[0], " ")
Label = append(Label, kind, Op)
lableBody := strings.Join(Label, " ")

mapOfAddLabels := label.GetLabelsMap(lableBody)
listofRepoLabels, _, err := client.Issues.ListLabels(ctx, *prEvent.Repo.Owner.Login, *prEvent.Repo.Name, nil)
if err != nil {
glog.Fatalf("Unable to list repository labels. err: %v", err)
}
// list labels in current issue
listofIssueLabels, _, err := client.Issues.ListLabelsByIssue(ctx, *prEvent.Repo.Owner.Login, *prEvent.Repo.Name, *prEvent.Number, nil)
if err != nil {
glog.Fatalf("Unable to list issue labels. err: %v", err)
}
glog.Infof("List of issue labels: %v", listofIssueLabels)
//Get the list of add labels
listOfAddLabels = label.GetListOfAddLabels(mapOfAddLabels, listofRepoLabels, listofIssueLabels)
_, _, err = client.Issues.AddLabelsToIssue(ctx, *prEvent.Repo.Owner.Login, *prEvent.Repo.Name, *prEvent.Number, listOfAddLabels)
if err != nil {
glog.Fatalf("Unable to add labels: %v err: %v", listOfAddLabels, err)
} else {
glog.Infof("Add labels successfully: %v", listOfAddLabels)
}
}
} else {
glog.Infof("No /kind is added to this PR !!")
}

}

Expand Down
14 changes: 13 additions & 1 deletion handlers/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
"golang.org/x/oauth2"
)

//Syncronization Flag for IssueComment and PR event Handling
//A Comment in PR section will lead Github to throw 2 webhook events which are
//1.The Webhook event name is "issue_comment"
//2.The Webhook event name is "pull_request" sequentially.
//let the handling only be with issue_comment event not pull_request event
var IsIssueCommentHandling = false

//Github client
var ClientRepo *github.Client
var c = Config{}
Expand Down Expand Up @@ -84,9 +91,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
go s.handleIssueEvent(payload)
case *github.IssueCommentEvent:
// Comments on PRs belong to IssueCommentEvent
IsIssueCommentHandling = true
go s.handleIssueCommentEvent(payload, ClientRepo)
case *github.PullRequestEvent:
go s.handlePullRequestEvent(payload)
if !IsIssueCommentHandling{
go s.handlePullRequestEvent(payload, ClientRepo)
}
//Fall Back to original state
IsIssueCommentHandling = false
case *github.PullRequestComment:
go s.handlePullRequestCommentEvent(payload)
}
Expand Down
21 changes: 12 additions & 9 deletions handlers/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import (

var (
// label
labelReg = regexp.MustCompile("^/[Ll][Aa][Bb][Ee][Ll]")
labelCancelReg = regexp.MustCompile("^/[Rr][Ee][Mm][Oo][Vv][Ee]-[Ll][Aa][Bb][Ee][Ll]")
LabelReg = regexp.MustCompile("^/[Ll][Aa][Bb][Ee][Ll]")
LabelCancelReg = regexp.MustCompile("^/[Rr][Ee][Mm][Oo][Vv][Ee]-[Ll][Aa][Bb][Ee][Ll]")

// test
okToTestReg = regexp.MustCompile("^/[Oo][Kk]-[Tt][Oo]-[Tt][Ee][Ss][Tt]")
retestReg = regexp.MustCompile("^/[Rr][Ee][Tt][Ee][Ss][Tt]")
testReg = regexp.MustCompile("^/[Tt][Ee][Ss][Tt]")
OkToTestReg = regexp.MustCompile("^/[Oo][Kk]-[Tt][Oo]-[Tt][Ee][Ss][Tt]")
RetestReg = regexp.MustCompile("^/[Rr][Ee][Tt][Ee][Ss][Tt]")
TestReg = regexp.MustCompile("^/[Tt][Ee][Ss][Tt]")

// review and approve
lgtmReg = regexp.MustCompile("^/[Ll][Gg][Tt][Mm]")
lgtmCancelReg = regexp.MustCompile("^/[Ll][Gg][Tt][Mm] [Cc][Aa][Nn][Cc][Ee][Ll]")
approveReg = regexp.MustCompile("^/[Aa][Pp][Pp][Rr][Oo][Vv][Ee]")
approveCancelReg = regexp.MustCompile("^/[Aa][Pp][Pp][Rr][Oo][Vv][Ee] [Cc][Aa][Nn][Cc][Ee][Ll]")
LgtmReg = regexp.MustCompile("^/[Ll][Gg][Tt][Mm]")
LgtmCancelReg = regexp.MustCompile("^/[Ll][Gg][Tt][Mm] [Cc][Aa][Nn][Cc][Ee][Ll]")
ApproveReg = regexp.MustCompile("^/[Aa][Pp][Pp][Rr][Oo][Vv][Ee]")
ApproveCancelReg = regexp.MustCompile("^/[Aa][Pp][Pp][Rr][Oo][Vv][Ee] [Cc][Aa][Nn][Cc][Ee][Ll]")

//assign/unassign
AssignOrUnassing = regexp.MustCompile("(?mi)^/(un)?assign(( @?[-\\w]+?)*)\\s*$")
)

const (
Expand Down

0 comments on commit cb19dfe

Please sign in to comment.