From 51d59e478cdfccdb0477b55ea6eddf689a988c84 Mon Sep 17 00:00:00 2001 From: georgecao Date: Mon, 23 Aug 2021 12:00:02 +0800 Subject: [PATCH] Add committor info to PR's first comment. --- pkg/cibot/pullrequest.go | 55 ++++++++++++++++++++++++++++------------ pkg/cibot/utils.go | 7 ++--- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index c9d8c26..c437881 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "strings" + "gitee.com/openeuler/ci-bot/pkg/cibot/database" "gitee.com/openeuler/go-gitee/gitee" "github.com/antihax/optional" "github.com/golang/glog" @@ -36,15 +37,50 @@ func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequ case "open": glog.Info("received a pull request open event") + // add a tag to describe the sig name of the repo. + sigName := s.getSigNameFromRepo(event.Repository.FullName) + if len(sigName) > 0 { + addlabel := &gitee.NoteEvent{} + addlabel.PullRequest = event.PullRequest + addlabel.Repository = event.Repository + addlabel.Comment = &gitee.NoteHook{} + errors := s.AddSpecifyLabelsInPulRequest(addlabel, []string{fmt.Sprintf("sig/%s", sigName)}, true) + if errors != nil { + glog.Errorf("Add special label sig info failed: %v", errors) + } + } + + + //get committor list: + var ps []database.Privileges + err := database.DBConnection.Model(&database.Privileges{}). + Where("owner = ? and repo = ? and type = ?", event.Repository.Namespace, event.Repository.Path, 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 { + committors = append(committors, fmt.Sprintf("***@%s***", p.User)) + } + } + } + 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.PullRequestCommentPostParam{} 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) owner := event.Repository.Namespace repo := event.Repository.Path number := event.PullRequest.Number - _, _, err := s.GiteeClient.PullRequestsApi.PostV5ReposOwnerRepoPullsNumberComments(s.Context, owner, repo, number, body) + _, _, err = s.GiteeClient.PullRequestsApi.PostV5ReposOwnerRepoPullsNumberComments(s.Context, owner, repo, number, body) if err != nil { glog.Errorf("unable to add comment in pull request: %v", err) } @@ -58,19 +94,6 @@ func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequ } } } - // add a tag to describe the sig name of the repo. - sigName := s.getSigNameFromRepo(event.Repository.FullName) - if len(sigName) > 0{ - addlabel := &gitee.NoteEvent{} - addlabel.PullRequest = event.PullRequest - addlabel.Repository = event.Repository - addlabel.Comment = &gitee.NoteHook{} - err = s.AddSpecifyLabelsInPulRequest(addlabel, []string{fmt.Sprintf("sig/%s", sigName)}, true) - if err != nil { - glog.Errorf("Add special label sig info failed: %v", err) - return - } - } if s.Config.AutoDetectCla { err = s.CheckCLAByPullRequestEvent(event) diff --git a/pkg/cibot/utils.go b/pkg/cibot/utils.go index 0a99e3f..a20a60c 100644 --- a/pkg/cibot/utils.go +++ b/pkg/cibot/utils.go @@ -22,9 +22,10 @@ const ( LabelNameApproved = "approved" LabelHiddenValue = "" tipBotMessage = `Hey ***%s***, Welcome to %s Community. -All of the projects in %s Community are maintained by ***@%s***. -That means the developers can comment below every pull request or issue to trigger Bot Commands. -Please follow instructions at <%s> to find the details.` +You can follow the instructions at <%s> to interact with the Bot. +%s` + DisplayCommittors = `If you have any questions, you could contact SIG: [%s](%s), and 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: ` ) -- Gitee