diff --git a/pkg/cibot/issue.go b/pkg/cibot/issue.go index 04e7d9e3792dc6a76c24ca82de43acbba1e99103..97d9d02d3596f4ed5a2b66bdb15b1b2c8981f2db 100644 --- a/pkg/cibot/issue.go +++ b/pkg/cibot/issue.go @@ -2,7 +2,8 @@ package cibot import ( "fmt" - + "strings" + "gitee.com/openeuler/ci-bot/pkg/cibot/database" "gitee.com/openeuler/go-gitee/gitee" "github.com/golang/glog" ) @@ -18,32 +19,55 @@ func (s *Server) HandleIssueEvent(event *gitee.IssueEvent) { case "open": glog.Info("received a issue open event") + owner := event.Repository.Namespace + repo := event.Repository.Path + number := event.Issue.Number + // get sig name add a tag to describe the sig name of the repo. + sigName := s.getSigNameFromRepo(event.Repository.FullName) + if len(sigName) > 0 { + label := []string{fmt.Sprintf("sig/%s", sigName)} + labelops := gitee.PullRequestLabelPostParam{s.Config.GiteeToken, label} + _, _, error := s.GiteeClient.LabelsApi.PostV5ReposOwnerRepoIssuesNumberLabels(s.Context, owner, repo, number, labelops) + if error != nil { + glog.Errorf("unable to add label in issue: %v", error) + } + } + + //get committor list: + var ps []database.Privileges + err := database.DBConnection.Model(&database.Privileges{}). + Where("owner = ? and repo = ? and type = ?", owner, repo, PrivilegeDeveloper).Find(&ps).Error + if err != nil { + glog.Errorf("unable to get members: %v", err) + } + var committors []string + if len(ps) > 0 { + for _, p := range ps { + if len(committors) < 10 { + committor := fmt.Sprintf("***@%s***", p.User) + committors = append(committors, committor) + } + } + } + committor_list := strings.Join(committors, ", ") + if len(committor_list) > 0 { + sigPath := fmt.Sprintf(SigPath, sigName) + proInfo := fmt.Sprintf(DisplayCommittors, sigName, sigPath) + committor_list = proInfo + committor_list + "." + } + // add comment body := gitee.IssueCommentPostParam{} body.AccessToken = s.Config.GiteeToken - body.Body = fmt.Sprintf(tipBotMessage, event.Sender.Login, s.Config.CommunityName, s.Config.CommunityName, - s.Config.BotName, s.Config.CommandLink) + body.Body = fmt.Sprintf(tipBotMessage, event.Sender.Login, s.Config.CommunityName, s.Config.CommandLink, committor_list) //Issue could exists without belonging to any repo. if event.Repository == nil { glog.Warningf("Issue is not created on repo, skip posting issue comment.") return } - owner := event.Repository.Namespace - repo := event.Repository.Path - number := event.Issue.Number - _, _, err := s.GiteeClient.IssuesApi.PostV5ReposOwnerRepoIssuesNumberComments(s.Context, owner, repo, number, body) + _, _, err = s.GiteeClient.IssuesApi.PostV5ReposOwnerRepoIssuesNumberComments(s.Context, owner, repo, number, body) if err != nil { glog.Errorf("unable to add comment in issue: %v", err) } - - sigName := s.getSigNameFromRepo(event.Repository.FullName) - if len(sigName) > 0 { - label := []string{fmt.Sprintf("sig/%s", sigName)} - labelops := gitee.PullRequestLabelPostParam{s.Config.GiteeToken, label} - _, _, err = s.GiteeClient.LabelsApi.PostV5ReposOwnerRepoIssuesNumberLabels(s.Context, owner, repo, number, labelops) - if err != nil { - glog.Errorf("unable to add label in issue: %v", err) - } - } } } diff --git a/pkg/cibot/label.go b/pkg/cibot/label.go index 6ba1a5525081d3a79e151074806ab04f990a1e9c..c478a303b8172b1cf301f83e75fdde8b00b2231b 100644 --- a/pkg/cibot/label.go +++ b/pkg/cibot/label.go @@ -521,18 +521,13 @@ func (s *Server) patchPRLabels(labels []string, owner, repo string, number int32 // invoke gitee api to add labels if len(newLabels) > 0 { // build label string - var labelSlice []string - for _, l := range pr.Labels { - labelSlice = append(labelSlice, l.Name) - } - labelSlice = append(labelSlice, newLabels...) - body := gitee.PullRequestUpdateParam{} + body := gitee.PullRequestLabelPostParam{} body.AccessToken = s.Config.GiteeToken - body.Labels = strings.Join(labelSlice, ",") - glog.Infof("invoke api to add labels: %v", labelSlice) + body.Body = newLabels + glog.Infof("invoke api to add labels: %v", newLabels) // patch labels - _, response, err := s.GiteeClient.PullRequestsApi.PatchV5ReposOwnerRepoPullsNumber(s.Context, owner, repo, number, body) + _, response, err := s.GiteeClient.PullRequestsApi.PostV5ReposOwnerRepoPullsNumberLabels(s.Context, owner, repo, number, body) if err != nil { if response.StatusCode == 400 { glog.Infof("add labels successfully with status code %d: %v", response.StatusCode, newLabels) diff --git a/pkg/cibot/lgtm.go b/pkg/cibot/lgtm.go index b9f10d48cad80bb9b623272f7a90bab9550d9bff..308a53817342731d3c3377bb142ed70a039172fd 100644 --- a/pkg/cibot/lgtm.go +++ b/pkg/cibot/lgtm.go @@ -11,9 +11,9 @@ import ( ) const ( - lgtmSelfOwnMessage = `***lgtm*** can not be added in your self-own pull request. :astonished: ` - lgtmAddedMessage = `***lgtm*** is added in this pull request by: ***%s***. :wave: -**NOTE:**: If you find this pull request unmerged while all conditions meets, you are encouraged use command: "/check-pr" to try it again. :smile: ` + lgtmSelfOwnMessage = `Sorry, you cannot add ***lgtm*** to the pull request you created. :astonished:` + lgtmAddedMessage = `***lgtm*** was added to this pull request by: ***%s***. :wave: +**NOTE:**: If this pull request is not merged while all conditions are met, comment "/check-pr" to try again. :smile: ` lgtmRemovedMessage = `***lgtm*** is removed in this pull request by: ***%s***. :flushed: ` lgtmAddNoPermissionMessage = `Thanks for your review, ***%s***, your opinion is very important to us.:wave: The maintainers will consider your advice carefully.` diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index 603be48b72026c2a385f51986707bb998ec12a83..cfa30fd97bb29c078a0d24ecd820afa250a5e6c2 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -220,24 +220,25 @@ func (s *Server) UpdateLabelsBySourceBranchChange(delLabels, updateLabels []stri prNumber := event.PullRequest.Number strLabel := strings.Join(updateLabels, ",") strDelLabel := strings.Join(delLabels, ",") - body := gitee.PullRequestUpdateParam{} - body.AccessToken = s.Config.GiteeToken - body.Labels = strLabel + body := gitee.DeleteV5ReposOwnerRepoPullsLabelOpts{} + body.AccessToken = optional.NewString(s.Config.GiteeToken) 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) + for _, dellalbe := range strDelLabel { + response, err := s.GiteeClient.PullRequestsApi.DeleteV5ReposOwnerRepoPullsLabel(s.Context, owner, repo, prNumber, dellalbe, &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.Errorf("unable to remove labels: %v err: %v", strDelLabel, err) - return err + glog.Infof("remove labels successfully: %v", strDelLabel) } - } 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: ` + commentContent := `Changes detected. ***%s*** was removed from this pull request by: ***%s***. :flushed: ` cBody := gitee.PullRequestCommentPostParam{} cBody.AccessToken = s.Config.GiteeToken cBody.Body = fmt.Sprintf(commentContent, strDelLabel, s.Config.BotName) diff --git a/pkg/cibot/utils.go b/pkg/cibot/utils.go index a20a60c05847cb34b89c5e5ee1fdd983a17e9948..540238d424cd08dd41a680b7ceba6aaffcf11ae8 100644 --- a/pkg/cibot/utils.go +++ b/pkg/cibot/utils.go @@ -21,10 +21,11 @@ const ( LabelLgtmWithCommenter = "lgtm-%s" LabelNameApproved = "approved" LabelHiddenValue = "" - tipBotMessage = `Hey ***%s***, Welcome to %s Community. -You can follow the instructions at <%s> to interact with the Bot. + tipBotMessage = `Hi ***%s***, welcome to the %s Community. +I'm the Bot here serving you. You can find the instructions on how to interact with me at +<%s>. %s` - DisplayCommittors = `If you have any questions, you could contact SIG: [%s](%s), and maintainers: ` + DisplayCommittors = `If you have any questions, please contact the SIG: [%s](%s), and any of the maintainers: ` SigPath = `https://gitee.com/openeuler/community/tree/master/sig/%s` AutoAddPrjMsg = `Since you have added a item to the src-openeuler.yaml file, we will automatically generate a default package in project openEuler:Factory on OBS cluster for you. If you need a more customized configuration, you can configure it according to the following instructions: `