diff --git a/.gitignore b/.gitignore index 97d405761fee688b085e6f3966b88a475e5916f2..1e89d74236ad86e345bd5153dc6e19e2ceb80545 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ ci-bot # Output of the go coverage tool, specifically when used with LiteIDE *.out +*.bat # Dependency directories (remove the comment below to include it) # vendor/ diff --git a/README.md b/README.md index 29f0a944e7b12aa09f67e5242ebe0d872571da48..fb811e4616178c716d8f87fc02d5510716d7eb49 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ This is an example to create Database instance. The information of database instance will be used in the following Installation. ## Config +### environment variables Some sensitive configurations items support reading from environment variables. you can set the following environment variables in your OS: * GITEE_TOKEN @@ -30,6 +31,14 @@ you can set the following environment variables in your OS: * DATABASE_PORT * DATABASE_USERNAME * DATABASE_PASSWORD +### label config +If you want to clear some tags when the pull request source branch changes, + you can configure it in the configuration file(config.yaml). + for example see config.yaml delLabels fields.Description: + * kind,sig,openeuler-cla,priority Delete labels beginning with kind,sig,openeuler-cla or priority. + * lgtm Delete labels lgtm or beginning with lgtm-. + * Except for the above description items, other labels will be judged as equal. + ## Getting Started diff --git a/config.yaml b/config.yaml index 74546881f100cbbd22c69c2dd1e4c338ce7e2230..e30216b427fa26d8b00a3a2ce56cae4aca165639 100644 --- a/config.yaml +++ b/config.yaml @@ -6,6 +6,16 @@ databasePort: 3306 databaseName: "cibot" databaseUserName: "root" databasePassword: "******" +#the flag that pull request commit_id has changed +prUpdateLabelFlag: "source_branch_changed" +#List of labels that need to be deleted after the commit_id is changed +delLabels: + - lgtm + - approve + - priority + - openeuler-cla + - sig + - kind lgtmCountsRequired: 1 requiringLabels: - openeuler-cla/yes diff --git a/pkg/cibot/config/config.go b/pkg/cibot/config/config.go index af251f5e0c50f413baeeccb15a5c9a005a3fc7f4..b3f4951896e11d928ba40cd412cab36363890ea9 100644 --- a/pkg/cibot/config/config.go +++ b/pkg/cibot/config/config.go @@ -9,6 +9,8 @@ type Config struct { DataBaseName string `yaml:"databaseName"` DataBaseUserName string `yaml:"databaseUserName" envVariable:"DATABASE_USERNAME"` DataBasePassword string `yaml:"databasePassword" envVariable:"DATABASE_PASSWORD"` + PrUpdateLabelFlag string `yaml:"prUpdateLabelFlag"` + DelLabels []string `yaml:"delLabels"` WatchProjectFiles []WatchProjectFile `yaml:"watchProjectFiles"` WatchProjectFileDuration int `yaml:"watchProjectFileDuration"` WatchSigFiles []WatchSigFile `yaml:"watchSigFiles"` diff --git a/pkg/cibot/label.go b/pkg/cibot/label.go index 6ffa6062247aa873ffaaf3140ed6ccb40f1af28a..2f4403007f020916d29b01f1086ecef99935e5e9 100644 --- a/pkg/cibot/label.go +++ b/pkg/cibot/label.go @@ -36,6 +36,54 @@ func GetLabelsMap(comment string) map[string]string { return mapOfLabels } +//GetChangeLabels return the exact list of delete and update labels +func GetChangeLabels(confDelLabels []string, prLabels []gitee.Label) (delLabels, updateLabels []string) { + for _, pl := range prLabels { + isUpdate := true + for _, cdl := range confDelLabels { + //handle lgtm label + if cdl == LabelNameLgtm { + if strings.HasPrefix(pl.Name, fmt.Sprintf(LabelLgtmWithCommenter, "")) || pl.Name == LabelNameLgtm { + delLabels = append(delLabels, pl.Name) + isUpdate = false + } + continue + } + if cdl == "openeuler-cla" { + if strings.HasPrefix(pl.Name,cdl){ + delLabels = append(delLabels, pl.Name) + isUpdate = false + } + continue + } + if cdl == "sig"{ + if strings.HasPrefix(pl.Name,cdl){ + delLabels = append(delLabels, pl.Name) + isUpdate = false + } + continue + } + if cdl == "kind"{ + if strings.HasPrefix(pl.Name,cdl){ + delLabels = append(delLabels, pl.Name) + isUpdate = false + } + continue + } + if cdl == pl.Name { + delLabels = append(delLabels, pl.Name) + isUpdate = false + continue + } + + } + if isUpdate { + updateLabels = append(updateLabels, pl.Name) + } + } + return +} + // GetListOfAddLabels return the exact list of add labels func GetListOfAddLabels(mapOfAddLabels map[string]string, listofRepoLabels []gitee.Label, listofItemLabels []gitee.Label) []string { // init diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index 5a9c73541e12df7717672fb98ad527568409a13e..47c2040e8cc9534a232e10d5fa9f0e1017f5821e 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -19,7 +19,7 @@ const ( ) // HandlePullRequestEvent handles pull request event -func (s *Server) HandlePullRequestEvent(event *gitee.PullRequestEvent) { +func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequestEvent) { if event == nil { return } @@ -98,15 +98,26 @@ func (s *Server) HandlePullRequestEvent(event *gitee.PullRequestEvent) { } listofPrLabels := pr.Labels glog.Infof("List of pr labels: %v", listofPrLabels) - + // remove labels if action_desc is "source_branch_changed" + if len(pr.Labels) == 0 || actionDesc != s.Config.PrUpdateLabelFlag { + return + } + delLabels, updateLabels := GetChangeLabels(s.Config.DelLabels, pr.Labels) + if len(delLabels) == 0 { + return + } + err = s.UpdateLabelsBySourceBranchChange(delLabels, updateLabels, event) + if err != nil { + glog.Info(err) + } // remove lgtm if changes happen - if s.hasLgtmLabel(pr.Labels) { + /*if s.hasLgtmLabel(pr.Labels) { err = s.CheckLgtmByPullRequestUpdate(event) if err != nil { glog.Errorf("check lgtm by pull request update. err: %v", err) return } - } + }*/ case "merge": glog.Info("Received a pull request merge event") @@ -136,6 +147,42 @@ func (s *Server) HandlePullRequestEvent(event *gitee.PullRequestEvent) { } } } + +func (s *Server) UpdateLabelsBySourceBranchChange(delLabels,updateLabels []string,event *gitee.PullRequestEvent) error { + owner := event.Repository.Namespace + repo := event.Repository.Name + prNumber := event.PullRequest.Number + strLabel := strings.Join(updateLabels,",") + strDelLabel := strings.Join(delLabels,",") + body := gitee.PullRequestUpdateParam{} + body.AccessToken = s.Config.GiteeToken + body.Labels = strLabel + glog.Infof("invoke api to remove labels: %v", strLabel) + //update pr + _, response, err := s.GiteeClient.PullRequestsApi.PatchV5ReposOwnerRepoPullsNumber(s.Context, owner, repo, prNumber, body) + if err != nil { + if response != nil && response.StatusCode == 400 { + glog.Infof("remove labels successfully with status code %d: %v", response.StatusCode, strDelLabel) + } else { + glog.Errorf("unable to remove labels: %v err: %v", strDelLabel, err) + return err + } + } else { + glog.Infof("remove labels successfully: %v", strDelLabel) + } + // add comment for update labels + commentContent := `new changes are detected. ***%s*** is removed in this pull request by: ***%s***. :flushed: ` + cBody := gitee.PullRequestCommentPostParam{} + cBody.AccessToken = s.Config.GiteeToken + cBody.Body = fmt.Sprintf(commentContent, strDelLabel,s.Config.BotName) + _, _, err = s.GiteeClient.PullRequestsApi.PostV5ReposOwnerRepoPullsNumberComments(s.Context, owner, repo, prNumber, cBody) + if err != nil { + glog.Errorf("unable to add comment in pull request: %v", err) + return err + } + return nil +} + func (s *Server) SendNote4AutomaticNewFile(event *gitee.PullRequestEvent) { if event == nil { return diff --git a/pkg/cibot/server.go b/pkg/cibot/server.go index 8b4c4b1d16772a6b1437bb56ecbc244aaa99d3f4..de8d006530d9843badb91ee384428e5ad6671dc7 100644 --- a/pkg/cibot/server.go +++ b/pkg/cibot/server.go @@ -2,6 +2,7 @@ package cibot import ( "context" + "encoding/json" "fmt" "net/http" @@ -27,7 +28,8 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, err.Error()) return } - glog.Infof("payload: %v", string(payload)) + pstr := string(payload) + glog.Infof("payload: %v", pstr) // parse into Event messagetype := gitee.WebHookType(r) @@ -58,7 +60,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { go s.HandleIssueEvent(event.(*gitee.IssueEvent)) case *gitee.PullRequestEvent: glog.Info("received a pull request event") - go s.HandlePullRequestEvent(event.(*gitee.PullRequestEvent)) + actionDesc:= struct { + ActionDesc string `json:"action_desc,omitempty"` + }{} + err := json.Unmarshal(payload, &actionDesc) + if err != nil{ + glog.Info(err) + } + go s.HandlePullRequestEvent(actionDesc.ActionDesc,event.(*gitee.PullRequestEvent)) case *gitee.TagPushEvent: glog.Info("received a tag push event") }