From 08eeda13463c661b1d8f20bdfc43c74bed580444 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Tue, 3 Nov 2020 17:11:39 +0800 Subject: [PATCH 01/16] fix issue:I1YW0H fix issue:I1YW0H delete pullrequest_test.go because extraLgtmCountRequired: Compatible with designated organization warehouses and optimized configuration items fix issue I1WY0H and add doc --- config.yaml | 10 +++++ pkg/cibot/config/config.go | 79 +++++++++++++++++++---------------- pkg/cibot/lgtm.go | 47 +++++++++++++++++---- pkg/cibot/pullrequest.go | 9 ++-- pkg/cibot/pullrequest_test.go | 37 ---------------- 5 files changed, 98 insertions(+), 84 deletions(-) delete mode 100644 pkg/cibot/pullrequest_test.go diff --git a/config.yaml b/config.yaml index 39d51ee..7fb1f85 100644 --- a/config.yaml +++ b/config.yaml @@ -17,6 +17,16 @@ delLabels: - sig - kind lgtmCountsRequired: 1 +#Add additional lgtm label quantity limit settings to the organization or repositories +#the member variables lcrType indicates whether the setting is for an organization or a repositories, +#optional configuration items: org or repo . +extraLgtmCountRequired: + - lcrType: repo + lcrName: xwzQmxx/test + lcrCount: 2 + - lcrType: org + lcrName: cve-test + lcrCount: 4 requiringLabels: - openeuler-cla/yes missingLabels: diff --git a/pkg/cibot/config/config.go b/pkg/cibot/config/config.go index 5c33867..b82395a 100644 --- a/pkg/cibot/config/config.go +++ b/pkg/cibot/config/config.go @@ -1,42 +1,43 @@ package config type Config struct { - GiteeToken string `yaml:"giteeToken" envVariable:"GITEE_TOKEN"` - WebhookSecret string `yaml:"webhookSecret" envVariable:"WEBHOOK_SECRET"` - DataBaseType string `yaml:"databaseType"` - DataBaseHost string `yaml:"databaseHost" envVariable:"DATABASE_HOST"` - DataBasePort int `yaml:"databasePort" envVariable:"DATABASE_PORT"` - 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"` - WatchSigFileDuration int `yaml:"watchSigFileDuration"` - WatchOwnerFiles []WatchOwnerFile `yaml:"watchOwnerFiles"` - WatchOwnerFileDuration int `yaml:"watchOwnerFileDuration"` - WatchFrozenFile []WatchFrozenFile `yaml:"watchFrozenFile"` - WatchFrozenDuration int `yaml:"watchFrozenDuration"` - BotName string `yaml:"botName"` - CommunityName string `yaml:"communityName"` - ClaLink string `yaml:"claLink"` - CommandLink string `yaml:"commandLink"` - ContactEmail string `yaml:"contactEmail"` - LgtmCountsRequired int `yaml:"lgtmCountsRequired"` - AccordingFile string `yaml:"accordingfile"` - NewFileOwner string `yaml:"newfileowner"` - NewFileRepo string `yaml:"newfilerepo"` - NewFileBranch string `yaml:"newfilebranch"` - ServiceFile string `yaml:"tmpservicefile"` - ServicePath string `yaml:"tmpservicepath"` - GuideURL string `yaml:"guideurl"` - CommitsThreshold int `yaml:"commitsThreshold"` - SquashCommitLabel string `yaml:"squashCommitLabel"` - RequiringLabels []string `yaml:"requiringLabels"` - MissingLabels []string `yaml:"missingLabels"` - AutoDetectCla bool `yaml:"autoDetectCla"` + GiteeToken string `yaml:"giteeToken" envVariable:"GITEE_TOKEN"` + WebhookSecret string `yaml:"webhookSecret" envVariable:"WEBHOOK_SECRET"` + DataBaseType string `yaml:"databaseType"` + DataBaseHost string `yaml:"databaseHost" envVariable:"DATABASE_HOST"` + DataBasePort int `yaml:"databasePort" envVariable:"DATABASE_PORT"` + 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"` + WatchSigFileDuration int `yaml:"watchSigFileDuration"` + WatchOwnerFiles []WatchOwnerFile `yaml:"watchOwnerFiles"` + WatchOwnerFileDuration int `yaml:"watchOwnerFileDuration"` + WatchFrozenFile []WatchFrozenFile `yaml:"watchFrozenFile"` + WatchFrozenDuration int `yaml:"watchFrozenDuration"` + BotName string `yaml:"botName"` + CommunityName string `yaml:"communityName"` + ClaLink string `yaml:"claLink"` + CommandLink string `yaml:"commandLink"` + ContactEmail string `yaml:"contactEmail"` + LgtmCountsRequired int `yaml:"lgtmCountsRequired"` + ExtraLgtmCountRequired []ExtraLgtmCountRequire `yaml:"extraLgtmCountRequired"` + AccordingFile string `yaml:"accordingfile"` + NewFileOwner string `yaml:"newfileowner"` + NewFileRepo string `yaml:"newfilerepo"` + NewFileBranch string `yaml:"newfilebranch"` + ServiceFile string `yaml:"tmpservicefile"` + ServicePath string `yaml:"tmpservicepath"` + GuideURL string `yaml:"guideurl"` + CommitsThreshold int `yaml:"commitsThreshold"` + SquashCommitLabel string `yaml:"squashCommitLabel"` + RequiringLabels []string `yaml:"requiringLabels"` + MissingLabels []string `yaml:"missingLabels"` + AutoDetectCla bool `yaml:"autoDetectCla"` } type WatchProjectFile struct { @@ -66,3 +67,9 @@ type WatchFrozenFile struct { FrozenFilePath string `yaml:"frozenFilePath"` FrozenFileRef string `yaml:"frozenFileRef"` } + +type ExtraLgtmCountRequire struct { + LcrType string `yaml:"lcrType"` + LcrName string `yaml:"lcrName"` + LcrCount int `yaml:"lcrCount"` +} diff --git a/pkg/cibot/lgtm.go b/pkg/cibot/lgtm.go index 48945a0..567b7be 100644 --- a/pkg/cibot/lgtm.go +++ b/pkg/cibot/lgtm.go @@ -11,8 +11,8 @@ 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: + 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: ` 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: @@ -20,6 +20,8 @@ The maintainers will consider your advice carefully.` lgtmRemoveNoPermissionMessage = `***%s*** has no permission to remove ***lgtm*** in this pull request. :astonished: please contact to the collaborators in this repository.` lgtmRemovePullRequestChangeMessage = `new changes are detected. ***lgtm*** is removed in this pull request by: ***%s***. :flushed: ` + lgtmRepo = "repo" + lgtmOrg = "org" ) // AddLgtm adds lgtm label @@ -84,7 +86,7 @@ func (s *Server) AddLgtm(event *gitee.NoteEvent) error { addlabel.PullRequest = event.PullRequest addlabel.Repository = event.Repository addlabel.Comment = &gitee.NoteHook{} - err = s.AddSpecifyLabelsInPulRequest(addlabel, []string{s.getLgtmLable(commentAuthor)}, true) + err = s.AddSpecifyLabelsInPulRequest(addlabel, []string{s.getLgtmLable(commentAuthor, owner, repo)}, true) if err != nil { return err } @@ -124,13 +126,44 @@ func (s *Server) AddLgtm(event *gitee.NoteEvent) error { return nil } -func (s *Server) getLgtmLable(commenter string) string { - if s.Config.LgtmCountsRequired > 1 { +func (s *Server) getLgtmLable(commenter, owner, repo string) string { + if s.calculateLgtmLabel(owner, repo) > 1 { return fmt.Sprintf(LabelLgtmWithCommenter, strings.ToLower(commenter)) } return LabelNameLgtm } +func (s *Server) calculateLgtmLabel(owner, repo string) int { + if len(s.Config.ExtraLgtmCountRequired) > 0 { + repoNum := 0 + orgNum := 0 + for _, v := range s.Config.ExtraLgtmCountRequired { + if v.LcrType == lgtmRepo { + if strings.Contains(v.LcrName,"/"){ + or := strings.Split(v.LcrName, "/") + if len(or)==2 && strings.ToLower(or[0])==owner && strings.ToLower(or[1]) == repo { + repoNum = v.LcrCount + } + }else if strings.ToLower(v.LcrName) == repo { + repoNum = v.LcrCount + } + } + if v.LcrType == lgtmOrg && v.LcrName == owner { + orgNum = v.LcrCount + } + } + if repoNum > 0 { + return repoNum + } + if orgNum > 0 { + return orgNum + } + return s.Config.LgtmCountsRequired + } else { + return s.Config.LgtmCountsRequired + } +} + // RemoveLgtm removes lgtm label func (s *Server) RemoveLgtm(event *gitee.NoteEvent) error { // handle PullRequest @@ -195,7 +228,7 @@ func (s *Server) RemoveLgtm(event *gitee.NoteEvent) error { removelabel.Repository = event.Repository removelabel.Comment = &gitee.NoteHook{} mapOfRemoveLabels := map[string]string{} - lgtmLable := s.getLgtmLable(commentAuthor) + lgtmLable := s.getLgtmLable(commentAuthor, owner, repo) mapOfRemoveLabels[lgtmLable] = lgtmLable err := s.RemoveSpecifyLabelsInPulRequest(removelabel, mapOfRemoveLabels) if err != nil { @@ -219,7 +252,7 @@ func (s *Server) RemoveLgtm(event *gitee.NoteEvent) error { func (s *Server) collectExistingLgtmLabel(owner, repo string, number int32) (map[string]string, error) { labels := make(map[string]string) - if s.Config.LgtmCountsRequired > 1 { + if s.calculateLgtmLabel(owner, repo) > 1 { lvos := &gitee.GetV5ReposOwnerRepoPullsNumberOpts{ AccessToken: optional.NewString(s.Config.GiteeToken), } diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index 61a7195..b6703bf 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -403,13 +403,14 @@ func (s *Server) hasLgtmLabel(labels []gitee.Label) bool { return false } -func (s *Server) readyForMerge(labels []gitee.Label) bool { +func (s *Server) readyForMerge(labels []gitee.Label,owner,repo string) bool { aproveLabel := 0 lgtmLabel := 0 lgtmPrefix := "" leastLgtm := 0 - if s.Config.LgtmCountsRequired > 1 { - leastLgtm = s.Config.LgtmCountsRequired + count := s.calculateLgtmLabel(owner,repo) + if count > 1 { + leastLgtm = count lgtmPrefix = fmt.Sprintf(LabelLgtmWithCommenter, "") } else { leastLgtm = 1 @@ -452,7 +453,7 @@ func (s *Server) MergePullRequest(event *gitee.NoteEvent) error { listofPrLabels := pr.Labels glog.Infof("List of pr labels: %v", listofPrLabels) // ready to merge - if s.readyForMerge(listofPrLabels) { + if s.readyForMerge(listofPrLabels,owner,repo) { nonRequiringLabels, nonMissingLabels := s.legalLabelsForMerge(listofPrLabels) if len(nonRequiringLabels) == 0 && len(nonMissingLabels) == 0 { // current pr can be merged diff --git a/pkg/cibot/pullrequest_test.go b/pkg/cibot/pullrequest_test.go deleted file mode 100644 index cf3d7e8..0000000 --- a/pkg/cibot/pullrequest_test.go +++ /dev/null @@ -1,37 +0,0 @@ -package cibot - -import "testing" - -import ( - "testing" -) - -func Test_formatDescription(t *testing.T) { - type args struct { - user string - reviewers []string - signers []string - } - tests := []struct { - name string - args args - want string - }{ - { - name: "test description format", - args: args{ - user: "fakeuser", - reviewers: []string{"@aaa", "@bbb"}, - signers: []string{"@ccc", "@ddd"}, - }, - want: "From: @fakeuser\nReviewed-by: @aaa,@bbb\nSigned-off-by: @ccc,@ddd\n", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := formatDescription(tt.args.user, tt.args.reviewers, tt.args.signers); got != tt.want { - t.Errorf("formatDescription() = %v, want %v", got, tt.want) - } - }) - } -} -- Gitee From 7377f8559e7cd43ef7409326ead1f29776e8c140 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Tue, 3 Nov 2020 19:56:13 +0800 Subject: [PATCH 02/16] Add document description --- README.md | 8 ++++++++ pkg/cibot/lgtm.go | 12 ++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fb811e4..23b0759 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,15 @@ If you want to clear some tags when the pull request source branch changes, * 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. +### extraLgtmCountRequired config + If you want to set the number of lgtm tags for a separate repository or organization, + you can configure this configuration item.The configuration item is a list, and the + list element contains the following configuration items: + * lcrType Indicates whether the configuration is for the repository or the organization + * lcrName Configure the spatial address of the repository or organization + * lcrCount Number of lgtm tags +**If your configuration is for a specific repository, you should configure the full path instead of just the repository space address. For example: lcrName:openEuler/ci-bot** ## Getting Started diff --git a/pkg/cibot/lgtm.go b/pkg/cibot/lgtm.go index 567b7be..9ba36f5 100644 --- a/pkg/cibot/lgtm.go +++ b/pkg/cibot/lgtm.go @@ -138,13 +138,13 @@ func (s *Server) calculateLgtmLabel(owner, repo string) int { repoNum := 0 orgNum := 0 for _, v := range s.Config.ExtraLgtmCountRequired { - if v.LcrType == lgtmRepo { - if strings.Contains(v.LcrName,"/"){ + if v.LcrType == lgtmRepo { + if strings.Contains(v.LcrName, "/") { or := strings.Split(v.LcrName, "/") - if len(or)==2 && strings.ToLower(or[0])==owner && strings.ToLower(or[1]) == repo { + if len(or) == 2 && strings.ToLower(or[0]) == owner && strings.ToLower(or[1]) == repo { repoNum = v.LcrCount } - }else if strings.ToLower(v.LcrName) == repo { + } else if strings.ToLower(v.LcrName) == repo { repoNum = v.LcrCount } } @@ -159,9 +159,9 @@ func (s *Server) calculateLgtmLabel(owner, repo string) int { return orgNum } return s.Config.LgtmCountsRequired - } else { - return s.Config.LgtmCountsRequired } + return s.Config.LgtmCountsRequired + } // RemoveLgtm removes lgtm label -- Gitee From e1d5ee9f561b38f17c25d9457f0e522a0dba9e97 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Tue, 3 Nov 2020 20:14:12 +0800 Subject: [PATCH 03/16] add pullrequest_test.go --- pkg/cibot/pullrequest_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 pkg/cibot/pullrequest_test.go diff --git a/pkg/cibot/pullrequest_test.go b/pkg/cibot/pullrequest_test.go new file mode 100644 index 0000000..9a2a8ac --- /dev/null +++ b/pkg/cibot/pullrequest_test.go @@ -0,0 +1,34 @@ +package cibot + +import "testing" + + +func Test_formatDescription(t *testing.T) { + type args struct { + user string + reviewers []string + signers []string + } + tests := []struct { + name string + args args + want string + }{ + { + name: "test description format", + args: args{ + user: "fakeuser", + reviewers: []string{"@aaa", "@bbb"}, + signers: []string{"@ccc", "@ddd"}, + }, + want: "From: @fakeuser\nReviewed-by: @aaa,@bbb\nSigned-off-by: @ccc,@ddd\n", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := formatDescription(tt.args.user, tt.args.reviewers, tt.args.signers); got != tt.want { + t.Errorf("formatDescription() = %v, want %v", got, tt.want) + } + }) + } +} \ No newline at end of file -- Gitee From 07e9dc1d8c74f5d4fff14eb6201077b5fd935850 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Wed, 4 Nov 2020 16:08:15 +0800 Subject: [PATCH 04/16] fix issue I23WCN --- config.yaml | 5 ++++- pkg/cibot/config/config.go | 2 ++ pkg/cibot/pullrequest.go | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/config.yaml b/config.yaml index 7fb1f85..efd5b49 100644 --- a/config.yaml +++ b/config.yaml @@ -71,4 +71,7 @@ newfilebranch: "master" tmpservicefile: "_service" tmpservicepath: "master/openEuler:Factory/#projectname#/_service" guideurl: "https://gitee.com/openeuler/community/tree/master/zh/contributors" -autoDetectCla: false \ No newline at end of file +autoDetectCla: false +checkPrReviewer: true +#Tips for setting reviewers +setReviewerTip: "Thank you for submitting a PullRequest, but it is detected that you have not set a reviewer, please set a reviewer. " \ No newline at end of file diff --git a/pkg/cibot/config/config.go b/pkg/cibot/config/config.go index b82395a..c0d43a5 100644 --- a/pkg/cibot/config/config.go +++ b/pkg/cibot/config/config.go @@ -38,6 +38,8 @@ type Config struct { RequiringLabels []string `yaml:"requiringLabels"` MissingLabels []string `yaml:"missingLabels"` AutoDetectCla bool `yaml:"autoDetectCla"` + CheckPrReviewer bool `yaml:"checkPrReviewer"` + SetReviewerTip string `yaml:"setReviewerTip"` } type WatchProjectFile struct { diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index b6703bf..bd05c46 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -49,6 +49,16 @@ func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequ glog.Errorf("unable to add comment in pull request: %v", err) } + if s.Config.CheckPrReviewer { + if !checkPrHasSetReviewer(event.PullRequest) { + body.Body = fmt.Sprintf(" ***@%s*** %s",event.Sender.Login,s.Config.SetReviewerTip) + _, _, 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) + } + } + } + if s.Config.AutoDetectCla { err = s.CheckCLAByPullRequestEvent(event) if err != nil { @@ -619,3 +629,10 @@ func checkFrozenCanMerge(commenter, branch string) ([]string,bool) { return frozen,true } } + +func checkPrHasSetReviewer(pr *gitee.PullRequestHook) bool { + if pr != nil && len(pr.Assignees)>0 { + return true + } + return false +} \ No newline at end of file -- Gitee From fa739b1d51e7d6e6ad0ee747dd0be2705f4d5c7f Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Wed, 4 Nov 2020 16:22:10 +0800 Subject: [PATCH 05/16] Repair defects --- pkg/cibot/pullrequest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index bd05c46..041ea89 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -52,7 +52,7 @@ func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequ if s.Config.CheckPrReviewer { if !checkPrHasSetReviewer(event.PullRequest) { body.Body = fmt.Sprintf(" ***@%s*** %s",event.Sender.Login,s.Config.SetReviewerTip) - _, _, 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) } -- Gitee From 3a13405ad154f9baaf634b5cff3dac9c999d4d96 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Thu, 5 Nov 2020 09:29:48 +0800 Subject: [PATCH 06/16] Fix the bug that the number of pr reviewers is inaccurate --- pkg/cibot/pullrequest.go | 45 ++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index 041ea89..401b049 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -50,8 +50,8 @@ func (s *Server) HandlePullRequestEvent(actionDesc string, event *gitee.PullRequ } if s.Config.CheckPrReviewer { - if !checkPrHasSetReviewer(event.PullRequest) { - body.Body = fmt.Sprintf(" ***@%s*** %s",event.Sender.Login,s.Config.SetReviewerTip) + if !s.checkPrHasSetReviewer(event) { + body.Body = fmt.Sprintf(" ***@%s*** %s", event.Sender.Login, s.Config.SetReviewerTip) _, _, 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) @@ -413,12 +413,12 @@ func (s *Server) hasLgtmLabel(labels []gitee.Label) bool { return false } -func (s *Server) readyForMerge(labels []gitee.Label,owner,repo string) bool { +func (s *Server) readyForMerge(labels []gitee.Label, owner, repo string) bool { aproveLabel := 0 lgtmLabel := 0 lgtmPrefix := "" leastLgtm := 0 - count := s.calculateLgtmLabel(owner,repo) + count := s.calculateLgtmLabel(owner, repo) if count > 1 { leastLgtm = count lgtmPrefix = fmt.Sprintf(LabelLgtmWithCommenter, "") @@ -463,18 +463,18 @@ func (s *Server) MergePullRequest(event *gitee.NoteEvent) error { listofPrLabels := pr.Labels glog.Infof("List of pr labels: %v", listofPrLabels) // ready to merge - if s.readyForMerge(listofPrLabels,owner,repo) { + if s.readyForMerge(listofPrLabels, owner, repo) { nonRequiringLabels, nonMissingLabels := s.legalLabelsForMerge(listofPrLabels) if len(nonRequiringLabels) == 0 && len(nonMissingLabels) == 0 { // current pr can be merged - if c,b :=checkFrozenCanMerge(event.Author.Login, pr.Base.Ref) ;!b{ + if c, b := checkFrozenCanMerge(event.Author.Login, pr.Base.Ref); !b { //send comment to pr body := gitee.PullRequestCommentPostParam{} body.AccessToken = s.Config.GiteeToken - if len(c) >0{ + if len(c) > 0 { body.Body = fmt.Sprintf("**Merge failed** The current pull request merge target has been frozen, and only the branch owner( @%s ) can merge.", - strings.Join(c," , @")) - }else { + strings.Join(c, " , @")) + } else { body.Body = "**Merge failed** The current pull request merge target has been frozen, and only the branch owner can merge." } @@ -614,7 +614,7 @@ func getSignersAndReviewers(user string, comments []gitee.PullRequestComments) ( return signers, reviewers, nil } -func checkFrozenCanMerge(commenter, branch string) ([]string,bool) { +func checkFrozenCanMerge(commenter, branch string) ([]string, bool) { frozen, isFrozen := IsBranchFrozen(branch) if isFrozen { canMerge := false @@ -624,15 +624,30 @@ func checkFrozenCanMerge(commenter, branch string) ([]string,bool) { break } } - return frozen,canMerge + return frozen, canMerge } else { - return frozen,true + return frozen, true } } -func checkPrHasSetReviewer(pr *gitee.PullRequestHook) bool { - if pr != nil && len(pr.Assignees)>0 { +func (s *Server) checkPrHasSetReviewer(pre *gitee.PullRequestEvent) bool { + if pre.PullRequest != nil && len(pre.PullRequest.Assignees) > 0 { return true + } else { + //get pr info + owner := pre.Repository.Namespace + repo := pre.Repository.Name + number := pre.PullRequest.Number + lvos := &gitee.GetV5ReposOwnerRepoPullsNumberOpts{} + lvos.AccessToken = optional.NewString(s.Config.GiteeToken) + pr, _, err := s.GiteeClient.PullRequestsApi.GetV5ReposOwnerRepoPullsNumber(s.Context, owner, repo, number, lvos) + if err != nil { + glog.Errorf("unable to get pull request. err: %v", err) + return false + } + if len(pr.Assignees) > 0 { + return true + } } return false -} \ No newline at end of file +} -- Gitee From 128d470d25684c5a17ae2601ab71fa6793494031 Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Fri, 6 Nov 2020 15:55:23 +0800 Subject: [PATCH 07/16] Add branch depending on yaml file. --- pkg/cibot/repohandler.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index 22eb8d2..d1b4990 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -185,9 +185,9 @@ func (handler *RepoHandler) watch() { result := true for i := 0; i < len(ps.Repositories); i++ { // get repositories length - lenRepositories, err := handler.getRepositoriesLength(ps.Community, *ps.Repositories[i].Name) - if err != nil { - glog.Errorf("failed to get repositories length: %v", err) + lenRepositories, errex := handler.getRepositoriesLength(ps.Community, *ps.Repositories[i].Name) + if errex != nil { + glog.Errorf("failed to get repositories length: %v", errex) result = false continue } @@ -382,6 +382,19 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository return err } glog.Infof("end to create repository: %s", *repo.Name) + + // create branch + getOpts := &gitee.PostV5ReposOwnerRepoBranchesOpts{} + getOpts.AccessToken = optional.NewString(handler.Config.GiteeToken) + for _, br := range repo.ProtectedBranches { + _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, + owner, *repo.Name, "master", br, getOpts) + if err != nil { + glog.Errorf("fail to add branch (%s) for repository (%s): %v", br, *repo.Name, err) + return err + } + glog.Infof("Add branch (%s) for repository (%s)", br, *repo.Name) + } return nil } -- Gitee From 3d9954576e8cf4766fa1cb25f156a87bcbc89fd4 Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Fri, 6 Nov 2020 16:46:50 +0800 Subject: [PATCH 08/16] modify add branch. --- pkg/cibot/repohandler.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index d1b4990..e8f4422 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -389,6 +389,9 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository for _, br := range repo.ProtectedBranches { _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, owner, *repo.Name, "master", br, getOpts) + if br == "master" { + continue + } if err != nil { glog.Errorf("fail to add branch (%s) for repository (%s): %v", br, *repo.Name, err) return err -- Gitee From fd0a09ff32641324259ccf5da19014d95042b40c Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Mon, 9 Nov 2020 09:49:58 +0800 Subject: [PATCH 09/16] fix issue I224AJJ Check log output --- pkg/cibot/clahandler.go | 4 ++-- pkg/cibot/database/database.go | 2 -- pkg/cibot/oauth.go | 4 ++-- pkg/cibot/server.go | 3 --- 4 files changed, 4 insertions(+), 9 deletions(-) diff --git a/pkg/cibot/clahandler.go b/pkg/cibot/clahandler.go index 9dfaf6e..57fa3b1 100644 --- a/pkg/cibot/clahandler.go +++ b/pkg/cibot/clahandler.go @@ -292,7 +292,7 @@ func (s *CLAHandler) HandleRequest(w http.ResponseWriter, request CLARequest, ac } // tostring - data, err := cds.ToString() + _, err = cds.ToString() if err != nil { s.HandleResult(w, CLAResult{ IsSuccess: false, @@ -301,7 +301,7 @@ func (s *CLAHandler) HandleRequest(w http.ResponseWriter, request CLARequest, ac }) return } - glog.Infof("add cla details data: %s", data) + // Check email in database var lenEmail int diff --git a/pkg/cibot/database/database.go b/pkg/cibot/database/database.go index c7b2b04..1558854 100644 --- a/pkg/cibot/database/database.go +++ b/pkg/cibot/database/database.go @@ -47,8 +47,6 @@ func ConnectDataBase(config config.Config) (*gorm.DB, error) { config.DataBaseHost, config.DataBasePort, config.DataBaseName) - glog.Infof("connecting str: %v", connStr) - return gorm.Open(config.DataBaseType, connStr) } diff --git a/pkg/cibot/oauth.go b/pkg/cibot/oauth.go index d6e9983..9469da9 100644 --- a/pkg/cibot/oauth.go +++ b/pkg/cibot/oauth.go @@ -3,10 +3,10 @@ package cibot import ( "context" "fmt" + "github.com/golang/glog" "os" "strings" - "github.com/golang/glog" "golang.org/x/oauth2" ) @@ -30,7 +30,7 @@ func GetToken(code string) (*oauth2.Token, error) { ctx := context.Background() config := Setup(client, redirect, secret) - glog.Infof("Token request config : %v, code: %s", config, code) + glog.Infof("Token request config, code: %s", code) return config.Exchange(ctx, code) diff --git a/pkg/cibot/server.go b/pkg/cibot/server.go index 3ec09ff..d45e633 100644 --- a/pkg/cibot/server.go +++ b/pkg/cibot/server.go @@ -28,9 +28,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, err.Error()) return } - // pstr := string(payload) - // glog.Infof("payload: %v", pstr) - // parse into Event messagetype := gitee.WebHookType(r) glog.Infof("message type: %v", messagetype) -- Gitee From d873c8dca6436f674a765a918e0ef9c610b401ff Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Mon, 9 Nov 2020 19:11:29 +0800 Subject: [PATCH 10/16] update add branch function. --- go.mod | 2 +- go.sum | 2 ++ pkg/cibot/repohandler.go | 9 +++++---- .../go-gitee/gitee/api_pull_requests.go | 15 +++------------ .../go-gitee/gitee/api_repositories.go | 18 +++--------------- .../go-gitee/gitee/hook_event_models.go | 5 +++-- .../go-gitee/gitee/hook_event_types.go | 1 + .../go-gitee/gitee/model_commit_tree.go | 15 +++++++++++++++ .../gitee/model_create_branch_param.go | 19 +++++++++++++++++++ .../model_pull_request_comment_patch_param.go | 18 ++++++++++++++++++ .../go-gitee/gitee/model_repo_commit.go | 18 +++++++++--------- .../go-gitee/gitee/model_single_commit.go | 14 ++++++++++++++ vendor/modules.txt | 2 +- 13 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 vendor/gitee.com/openeuler/go-gitee/gitee/model_commit_tree.go create mode 100644 vendor/gitee.com/openeuler/go-gitee/gitee/model_create_branch_param.go create mode 100644 vendor/gitee.com/openeuler/go-gitee/gitee/model_pull_request_comment_patch_param.go create mode 100644 vendor/gitee.com/openeuler/go-gitee/gitee/model_single_commit.go diff --git a/go.mod b/go.mod index 92a62c7..b9a9bd8 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitee.com/openeuler/ci-bot go 1.13 require ( - gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179 + gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be github.com/antihax/optional v1.0.0 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/protobuf v1.3.2 // indirect diff --git a/go.sum b/go.sum index 38d5a8c..3c0c714 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ cloud.google.com/go v0.37.4 h1:glPeL3BQJsbF6aIIYfZizMwc5LTYz250bDMjttbBGAU= cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw= gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179 h1:7bHzennVUJlv27Gljny95CyXknEBrfmHL6KdHCuHumM= gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= +gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be h1:eh0eMiF5PNuD196nW8DcVNsGNchbeyKk+nlVgKtnBzg= +gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index e8f4422..fbacea9 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -384,11 +384,12 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository glog.Infof("end to create repository: %s", *repo.Name) // create branch - getOpts := &gitee.PostV5ReposOwnerRepoBranchesOpts{} - getOpts.AccessToken = optional.NewString(handler.Config.GiteeToken) + repobranchbody := gitee.CreateBranchParam{} + repobranchbody.AccessToken = handler.Config.GiteeToken + repobranchbody.Refs = "master" for _, br := range repo.ProtectedBranches { - _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, - owner, *repo.Name, "master", br, getOpts) + repobranchbody.BranchName = br + _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, owner, *repo.Name, repobranchbody) if br == "master" { continue } diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/api_pull_requests.go b/vendor/gitee.com/openeuler/go-gitee/gitee/api_pull_requests.go index 01845a8..30fedb8 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/api_pull_requests.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/api_pull_requests.go @@ -1513,17 +1513,10 @@ PullRequestsApiService 编辑评论 * @param repo 仓库路径(path) * @param id 评论的ID * @param body 必填。评论内容 - * @param optional nil or *PatchV5ReposOwnerRepoPullsCommentsIdOpts - Optional Parameters: - * @param "AccessToken" (optional.String) - 用户授权码 @return PullRequestComments */ - -type PatchV5ReposOwnerRepoPullsCommentsIdOpts struct { - AccessToken optional.String -} - -func (a *PullRequestsApiService) PatchV5ReposOwnerRepoPullsCommentsId(ctx context.Context, owner string, repo string, id int32, body string, localVarOptionals *PatchV5ReposOwnerRepoPullsCommentsIdOpts) (PullRequestComments, *http.Response, error) { +func (a *PullRequestsApiService) PatchV5ReposOwnerRepoPullsCommentsId(ctx context.Context, owner string, repo string, id int32, body PullRequestCommentPatchParam) (PullRequestComments, *http.Response, error) { var ( localVarHttpMethod = strings.ToUpper("Patch") localVarPostBody interface{} @@ -1559,10 +1552,8 @@ func (a *PullRequestsApiService) PatchV5ReposOwnerRepoPullsCommentsId(ctx contex if localVarHttpHeaderAccept != "" { localVarHeaderParams["Accept"] = localVarHttpHeaderAccept } - if localVarOptionals != nil && localVarOptionals.AccessToken.IsSet() { - localVarFormParams.Add("access_token", parameterToString(localVarOptionals.AccessToken.Value(), "")) - } - localVarFormParams.Add("body", parameterToString(body, "")) + // body params + localVarPostBody = &body r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { return localVarReturnValue, nil, err diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go b/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go index ccdffbb..5cffe97 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go @@ -4417,19 +4417,11 @@ RepositoriesApiService 创建分支 * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). * @param owner 仓库所属空间地址(企业、组织或个人的地址path) * @param repo 仓库路径(path) - * @param refs 起点名称, 默认:master - * @param branchName 新创建的分支名称 - * @param optional nil or *PostV5ReposOwnerRepoBranchesOpts - Optional Parameters: - * @param "AccessToken" (optional.String) - 用户授权码 + * @param body 新建分支内容 @return CompleteBranch */ - -type PostV5ReposOwnerRepoBranchesOpts struct { - AccessToken optional.String -} - -func (a *RepositoriesApiService) PostV5ReposOwnerRepoBranches(ctx context.Context, owner string, repo string, refs string, branchName string, localVarOptionals *PostV5ReposOwnerRepoBranchesOpts) (CompleteBranch, *http.Response, error) { +func (a *RepositoriesApiService) PostV5ReposOwnerRepoBranches(ctx context.Context, owner string, repo string, body CreateBranchParam) (CompleteBranch, *http.Response, error) { var ( localVarHttpMethod = strings.ToUpper("Post") localVarPostBody interface{} @@ -4464,11 +4456,7 @@ func (a *RepositoriesApiService) PostV5ReposOwnerRepoBranches(ctx context.Contex if localVarHttpHeaderAccept != "" { localVarHeaderParams["Accept"] = localVarHttpHeaderAccept } - if localVarOptionals != nil && localVarOptionals.AccessToken.IsSet() { - localVarFormParams.Add("access_token", parameterToString(localVarOptionals.AccessToken.Value(), "")) - } - localVarFormParams.Add("refs", parameterToString(refs, "")) - localVarFormParams.Add("branch_name", parameterToString(branchName, "")) + localVarPostBody = &body r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { return localVarReturnValue, nil, err diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_models.go b/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_models.go index fbdba51..137586f 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_models.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_models.go @@ -158,6 +158,7 @@ type PullRequestHook struct { PatchUrl string `json:"patch_url,omitempty"` Title string `json:"title,omitempty"` Body string `json:"body,omitempty"` + Labels []LabelHook `json:"labels,omitempty"` CreatedAt string `json:"created_at,omitempty"` UpdatedAt string `json:"updated_at,omitempty"` ClosedAt string `json:"closed_at,omitempty"` @@ -170,13 +171,13 @@ type PullRequestHook struct { Tester []UserHook `json:"tester,omitempty"` Testers []UserHook `json:"testers,omitempty"` NeedTest bool `json:"need_test,omitempty"` - need_review bool `json:"need_review,omitempty"` + NeedReview bool `json:"need_review,omitempty"` Milestone *MilestoneHook `json:"milestone,omitempty"` Head *BranchHook `json:"head,omitempty"` Base *BranchHook `json:"base,omitempty"` Merged bool `json:"merged,omitempty"` Mergeable bool `json:"mergeable,omitempty"` - merge_status string `json:"merge_status,omitempty"` + MergeStatus string `json:"merge_status,omitempty"` UpdatedBy *UserHook `json:"updated_by,omitempty"` Comments int32 `json:"comments,omitempty"` Commits int32 `json:"commits,omitempty"` diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_types.go b/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_types.go index 2a12e7f..f6820cb 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_types.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/hook_event_types.go @@ -72,6 +72,7 @@ type RepoInfo struct { type PullRequestEvent struct { Action *string `json:"action,omitempty"` + ActionDesc *string `json:"action_desc,omitempty"` PullRequest *PullRequestHook `json:"pull_request,omitempty"` Number int64 `json:"number,omitempty"` IID int64 `json:"iid,omitempty"` diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_commit_tree.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_commit_tree.go new file mode 100644 index 0000000..70c0f39 --- /dev/null +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_commit_tree.go @@ -0,0 +1,15 @@ +/* + * 码云 Open API + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 5.3.2 + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package gitee + +type CommitTree struct { + Sha string `json:"sha,omitempty"` + Url string `json:"url,omitempty"` +} diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_create_branch_param.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_create_branch_param.go new file mode 100644 index 0000000..988192c --- /dev/null +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_create_branch_param.go @@ -0,0 +1,19 @@ +/* + * 码云 Open API + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 5.3.2 + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package gitee + +type CreateBranchParam struct { + // 用户授权码 + AccessToken string `json:"access_token,omitempty"` + // 分支起点名称,默认master + Refs string `json:"refs,omitempty"` + // 新创建的分支名称 + BranchName string `json:"branch_name,omitempty"` +} diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_pull_request_comment_patch_param.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_pull_request_comment_patch_param.go new file mode 100644 index 0000000..2695cb3 --- /dev/null +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_pull_request_comment_patch_param.go @@ -0,0 +1,18 @@ +/* + * 码云 Open API + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 5.3.2 + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package gitee + +// edit pull request comment +type PullRequestCommentPatchParam struct { + // 用户授权码 + AccessToken string `json:"access_token,omitempty"` + // 必填。评论内容 + Body string `json:"body,omitempty"` +} diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_repo_commit.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_repo_commit.go index c0969ea..3913a6f 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/model_repo_commit.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_repo_commit.go @@ -11,13 +11,13 @@ package gitee // 仓库的某个提交 type RepoCommit struct { - Url string `json:"url,omitempty"` - Sha string `json:"sha,omitempty"` - HtmlUrl string `json:"html_url,omitempty"` - CommentsUrl string `json:"comments_url,omitempty"` - Commit string `json:"commit,omitempty"` - Author string `json:"author,omitempty"` - Committer string `json:"committer,omitempty"` - Parents string `json:"parents,omitempty"` - Stats string `json:"stats,omitempty"` + Url string `json:"url,omitempty"` + Sha string `json:"sha,omitempty"` + HtmlUrl string `json:"html_url,omitempty"` + CommentsUrl string `json:"comments_url,omitempty"` + Commit *SingleCommit `json:"commit,omitempty"` + Author string `json:"author,omitempty"` + Committer string `json:"committer,omitempty"` + Parents string `json:"parents,omitempty"` + Stats string `json:"stats,omitempty"` } diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_single_commit.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_single_commit.go new file mode 100644 index 0000000..edc8d6d --- /dev/null +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_single_commit.go @@ -0,0 +1,14 @@ +/* + * 码云 Open API + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 5.3.2 + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package gitee + +type SingleCommit struct { + Tree *CommitTree `json:"tree,omitempty"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 99412ab..7881c08 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179 +# gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be gitee.com/openeuler/go-gitee/gitee # github.com/antihax/optional v1.0.0 github.com/antihax/optional -- Gitee From c6b9bc7398b6218bf6999217677e0115e1540a5d Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Tue, 10 Nov 2020 14:38:45 +0800 Subject: [PATCH 11/16] Update reviewer info while add a repository. --- go.mod | 2 +- go.sum | 2 + pkg/cibot/repohandler.go | 14 ++++ .../go-gitee/gitee/api_repositories.go | 78 +++++++++++++++++++ .../go-gitee/gitee/model_set_repo_reviewer.go | 23 ++++++ vendor/modules.txt | 2 +- 6 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go diff --git a/go.mod b/go.mod index b9a9bd8..1091c10 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitee.com/openeuler/ci-bot go 1.13 require ( - gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be + gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 github.com/antihax/optional v1.0.0 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/protobuf v1.3.2 // indirect diff --git a/go.sum b/go.sum index 3c0c714..5af9418 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179 h1:7bHzennVUJlv2 gitee.com/openeuler/go-gitee v0.0.0-20200424023021-ced5a4d67179/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be h1:eh0eMiF5PNuD196nW8DcVNsGNchbeyKk+nlVgKtnBzg= gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= +gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 h1:rgQdF72XcPKOVyM3nmf0Lyp7Mh3mDqxD/uMvY7sixOE= +gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index fbacea9..8584bfc 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -548,6 +548,7 @@ func (handler *RepoHandler) addBranchProtections(community string, r Repository, // handleRepositorySetting handles that the repo settings, including: // 1. type is private or public // 2. commentable is true or false +// 3. set none reviewer but not ci-bot(default) func (handler *RepoHandler) handleRepositorySetting(community string, r Repository) error { // get repos from DB var rs database.Repositories @@ -634,5 +635,18 @@ func (handler *RepoHandler) handleRepositorySetting(community string, r Reposito } } + // set none reviewer but not ci-bot(default) + reviewerBody := gitee.SetRepoReviewer{} + reviewerBody.AccessToken = handler.Config.GiteeToken + reviewerBody.Assigneers = " " + reviewerBody.Testers = " " + reviewerBody.AssigneersNumber = 0 + reviewerBody.TestersNumber = 0 + _, err = handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) + if err != nil { + glog.Errorf("Set repository reviewer info failed: %v", err) + return err + } + return nil } diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go b/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go index 5cffe97..aaad8fc 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/api_repositories.go @@ -4456,6 +4456,7 @@ func (a *RepositoriesApiService) PostV5ReposOwnerRepoBranches(ctx context.Contex if localVarHttpHeaderAccept != "" { localVarHeaderParams["Accept"] = localVarHttpHeaderAccept } + // body params localVarPostBody = &body r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) if err != nil { @@ -5754,3 +5755,80 @@ func (a *RepositoriesApiService) PutV5ReposOwnerRepoKeysEnableId(ctx context.Con return localVarHttpResponse, nil } + +/* +RepositoriesApiService 修改代码审查设置 +修改代码审查设置 + * @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background(). + * @param owner 仓库所属空间地址(企业、组织或个人的地址path) + * @param repo 仓库路径(path) + * @param body 修改代码审查的信息 + + +*/ +func (a *RepositoriesApiService) PutV5ReposOwnerRepoReviewer(ctx context.Context, owner string, repo string, body SetRepoReviewer) (*http.Response, error) { + var ( + localVarHttpMethod = strings.ToUpper("Put") + localVarPostBody interface{} + localVarFileName string + localVarFileBytes []byte + + ) + + // create path and map variables + localVarPath := a.client.cfg.BasePath + "/v5/repos/{owner}/{repo}/reviewer" + localVarPath = strings.Replace(localVarPath, "{"+"owner"+"}", fmt.Sprintf("%v", owner), -1) + localVarPath = strings.Replace(localVarPath, "{"+"repo"+"}", fmt.Sprintf("%v", repo), -1) + + localVarHeaderParams := make(map[string]string) + localVarQueryParams := url.Values{} + localVarFormParams := url.Values{} + + // to determine the Content-Type header + localVarHttpContentTypes := []string{"application/json", "multipart/form-data"} + + // set Content-Type header + localVarHttpContentType := selectHeaderContentType(localVarHttpContentTypes) + if localVarHttpContentType != "" { + localVarHeaderParams["Content-Type"] = localVarHttpContentType + } + + // to determine the Accept header + localVarHttpHeaderAccepts := []string{"application/json"} + + // set Accept header + localVarHttpHeaderAccept := selectHeaderAccept(localVarHttpHeaderAccepts) + if localVarHttpHeaderAccept != "" { + localVarHeaderParams["Accept"] = localVarHttpHeaderAccept + } + // body params + localVarPostBody = &body + r, err := a.client.prepareRequest(ctx, localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes) + if err != nil { + return nil, err + } + + localVarHttpResponse, err := a.client.callAPI(r) + if err != nil || localVarHttpResponse == nil { + return localVarHttpResponse, err + } + + localVarBody, err := ioutil.ReadAll(localVarHttpResponse.Body) + localVarHttpResponse.Body.Close() + if err != nil { + return localVarHttpResponse, err + } + + + if localVarHttpResponse.StatusCode >= 300 { + newErr := GenericSwaggerError{ + body: localVarBody, + error: localVarHttpResponse.Status, + } + + return localVarHttpResponse, newErr + } + + return localVarHttpResponse, nil +} + diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go new file mode 100644 index 0000000..d23535b --- /dev/null +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go @@ -0,0 +1,23 @@ +/* + * 码云 Open API + * + * No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen) + * + * API version: 5.3.2 + * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git) + */ + +package gitee + +type SetRepoReviewer struct { + // 用户授权码 + AccessToken string `json:"access_token,omitempty"` + // 审查人员 + Assigneers string `json:"assigneers,omitempty"` + // 测试人员 + Testers string `json:"testers,omitempty"` + // 最少审查人数 + AssigneersNumber int32 `json:"assigneers_number,omitempty"` + // 最少测试人员 + TestersNumber int32 `json:"testers_number,omitempty"` +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 7881c08..1977509 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be +# gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 gitee.com/openeuler/go-gitee/gitee # github.com/antihax/optional v1.0.0 github.com/antihax/optional -- Gitee From 54164478dedae9ea9227b60e4a08b51a20f6bec4 Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Tue, 10 Nov 2020 15:30:19 +0800 Subject: [PATCH 12/16] To fix the bug of parameter spelling error. --- pkg/cibot/repohandler.go | 4 ++-- .../openeuler/go-gitee/gitee/model_set_repo_reviewer.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index 8584bfc..ea52a22 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -638,9 +638,9 @@ func (handler *RepoHandler) handleRepositorySetting(community string, r Reposito // set none reviewer but not ci-bot(default) reviewerBody := gitee.SetRepoReviewer{} reviewerBody.AccessToken = handler.Config.GiteeToken - reviewerBody.Assigneers = " " + reviewerBody.Assignees = " " reviewerBody.Testers = " " - reviewerBody.AssigneersNumber = 0 + reviewerBody.AssigneesNumber = 0 reviewerBody.TestersNumber = 0 _, err = handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) if err != nil { diff --git a/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go b/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go index d23535b..a7c052b 100644 --- a/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go +++ b/vendor/gitee.com/openeuler/go-gitee/gitee/model_set_repo_reviewer.go @@ -13,11 +13,11 @@ type SetRepoReviewer struct { // 用户授权码 AccessToken string `json:"access_token,omitempty"` // 审查人员 - Assigneers string `json:"assigneers,omitempty"` + Assignees string `json:"assignees,omitempty"` // 测试人员 Testers string `json:"testers,omitempty"` // 最少审查人数 - AssigneersNumber int32 `json:"assigneers_number,omitempty"` + AssigneesNumber int32 `json:"assignees_number,omitempty"` // 最少测试人员 TestersNumber int32 `json:"testers_number,omitempty"` } -- Gitee From 3ca8a992ae9b34e72c624469c3bda514befc1911 Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Wed, 11 Nov 2020 16:38:49 +0800 Subject: [PATCH 13/16] Add debug info --- go.mod | 2 +- go.sum | 2 ++ pkg/cibot/repohandler.go | 8 ++++---- vendor/modules.txt | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 1091c10..d610ceb 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitee.com/openeuler/ci-bot go 1.13 require ( - gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 + gitee.com/openeuler/go-gitee v0.0.0-20201111035308-c4384bf8d333 github.com/antihax/optional v1.0.0 github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/protobuf v1.3.2 // indirect diff --git a/go.sum b/go.sum index 5af9418..dd5af71 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be h1:eh0eMiF5PNuD1 gitee.com/openeuler/go-gitee v0.0.0-20201109090511-81a247e275be/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 h1:rgQdF72XcPKOVyM3nmf0Lyp7Mh3mDqxD/uMvY7sixOE= gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= +gitee.com/openeuler/go-gitee v0.0.0-20201111035308-c4384bf8d333 h1:Uk05BpyrV24VNVFPd47lEOg0OmUMgnIgd50gnyWqC8s= +gitee.com/openeuler/go-gitee v0.0.0-20201111035308-c4384bf8d333/go.mod h1:TQrS/LP/DFXLqM+lVrZd4nL2pbTrqiXABGT9PJepVTA= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index ea52a22..308e868 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -642,10 +642,10 @@ func (handler *RepoHandler) handleRepositorySetting(community string, r Reposito reviewerBody.Testers = " " reviewerBody.AssigneesNumber = 0 reviewerBody.TestersNumber = 0 - _, err = handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) - if err != nil { - glog.Errorf("Set repository reviewer info failed: %v", err) - return err + response, errex := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) + if errex != nil { + glog.Errorf("Set repository reviewer info failed: %v, %s", errex, response.Status) + return errex } return nil diff --git a/vendor/modules.txt b/vendor/modules.txt index 1977509..62bfd29 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# gitee.com/openeuler/go-gitee v0.0.0-20201110031012-c7f697cc7b12 +# gitee.com/openeuler/go-gitee v0.0.0-20201111035308-c4384bf8d333 gitee.com/openeuler/go-gitee/gitee # github.com/antihax/optional v1.0.0 github.com/antihax/optional -- Gitee From c5b8313904b5ef0bac039de7c93f7831e15fb9ad Mon Sep 17 00:00:00 2001 From: GeorgeCao-hw Date: Mon, 23 Nov 2020 15:47:47 +0800 Subject: [PATCH 14/16] Add branch depend on repository config yaml --- pkg/cibot/inithandler.go | 201 ++++++++++++++------------------ pkg/cibot/repohandler.go | 243 +++++++++++++++++++-------------------- 2 files changed, 204 insertions(+), 240 deletions(-) diff --git a/pkg/cibot/inithandler.go b/pkg/cibot/inithandler.go index a1e27fe..c2d1da7 100644 --- a/pkg/cibot/inithandler.go +++ b/pkg/cibot/inithandler.go @@ -25,6 +25,7 @@ const ( PermissionPull = "pull" BranchProtected = "protected" + BranchNormal = "normal" // not supported yet BranchReadonly = "readonly" ) @@ -60,6 +61,7 @@ type Repository struct { Developers []string `yaml:"developers"` Viewers []string `yaml:"viewers"` Reporters []string `yaml:"reporters"` + Branches []Branch `yaml:"branches"` } // IsCommentable returns if contributors are able to comment @@ -172,7 +174,7 @@ func (handler *InitHandler) watch() { watchPath := wf.WatchprojectFilePath watchRef := wf.WatchProjectFileRef - glog.Infof("begin to serve. watchOwner: %s watchRepo: %s watchPath: %s watchRef: %s watchDuration: %d", + glog.Infof("Init handler begin to serve. watchOwner: %s watchRepo: %s watchPath: %s watchRef: %s watchDuration: %d", watchOwner, watchRepo, watchPath, watchRef, watchDuration) // get project file @@ -858,145 +860,112 @@ func (handler *InitHandler) removeReporters(c Community, r Repository, mapReport func (handler *InitHandler) handleBranches(c Community, r Repository) error { // if the branches are defined in the repositories, it means that // all the branches defined in the community will not inherited by repositories - mapBranches := make(map[string]string) - if len(r.ProtectedBranches) > 0 { + if len(r.Branches) > 0 { // using repository branches - glog.Infof("using repository branches: %s", *r.Name) - for _, b := range r.ProtectedBranches { - mapBranches[b] = b - } - } else { - // using community branches - glog.Infof("using community branches: %s", *r.Name) - for _, b := range c.ProtectedBranches { - mapBranches[b] = b + glog.Infof("Setting repository branches: %s", r.Name) + for _, b := range r.Branches { + //check yaml branches is in db + var bs []database.Branches + err := database.DBConnection.Model(&database.Branches{}). + Where("owner = ? and repo = ? and name = ?", c.Name, r.Name, b.Name).Find(&bs).Error + if err != nil { + glog.Infof("Do not have branches (%s), need to create. %v, ", b.Name, err) + err = handler.addBranchInGiteeAndDb(c, r, b) + if err != nil { + glog.Errorf("Add branch(%s) of repor(%s) failed: %v", b.Name, r.Name, err) + } + continue + } + //branch exist already, need to change some feature only. + err = handler.changeBranchInGiteeAndDb(c, r, b, bs[0]) + if err != nil { + glog.Errorf("Change branch(%s) of repor(%s) failed: %v", b.Name, r.Name, err) + } + continue } } - - // get branches from DB - var bs []database.Branches - err := database.DBConnection.Model(&database.Branches{}). - Where("owner = ? and repo = ?", c.Name, r.Name).Find(&bs).Error - if err != nil { - glog.Errorf("unable to get branches: %v", err) - return err - } - mapBranchesInDB := make(map[string]string) - for _, b := range bs { - mapBranchesInDB[b.Name] = strconv.Itoa(int(b.ID)) - } - - // un-protected branches - err = handler.removeBranchProtections(c, r, mapBranches, mapBranchesInDB) - if err != nil { - glog.Errorf("unable to un-protected branches: %v", err) - } - - // protected branches - err = handler.addBranchProtections(c, r, mapBranches, mapBranchesInDB) - if err != nil { - glog.Errorf("unable to protected branches: %v", err) - } - return nil } // unprotectedBranches unprotect branches -func (handler *InitHandler) removeBranchProtections(c Community, r Repository, mapBranches, mapBranchesInDB map[string]string) error { - // remove branch protections - listOfUnprotectedBranches := make([]string, 0) - - for k := range mapBranchesInDB { - if _, exists := mapBranches[k]; !exists { - listOfUnprotectedBranches = append(listOfUnprotectedBranches, k) - } +func (handler *InitHandler) changeBranchInGiteeAndDb(c Community, r Repository, br Branch, bs database.Branches) error { + // if branch features are same as ones in db, do nothing. + if *r.Name == bs.Repo && *br.Name == bs.Name && *br.Type == bs.Type { + return nil } - glog.Infof("list of un-protected branches: %v", listOfUnprotectedBranches) - if len(listOfUnprotectedBranches) > 0 { + // change branch protected freature in Gitee + var brType string + brType = BranchNormal + if *br.Type == BranchProtected { + protectBody := gitee.BranchProtectionPutParam{} + protectBody.AccessToken = handler.Config.GiteeToken + _, _, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, *c.Name, *r.Name, *br.Name, protectBody) + if err != nil { + glog.Errorf("failed to add branch protection: %v", err) + } + brType = BranchProtected + } else { opts := &gitee.DeleteV5ReposOwnerRepoBranchesBranchProtectionOpts{} opts.AccessToken = optional.NewString(handler.Config.GiteeToken) - - glog.Infof("begin to remove branch protections for %s", *r.Name) - for _, v := range listOfUnprotectedBranches { - // remove branch protection from gitee - _, err := handler.GiteeClient.RepositoriesApi.DeleteV5ReposOwnerRepoBranchesBranchProtection( - handler.Context, *c.Name, *r.Name, v, opts) - if err != nil { - glog.Errorf("failed to remove branch protection: %v", err) - continue - } - // remove branch protection from DB - id, _ := strconv.Atoi(mapBranchesInDB[v]) - bs := database.Branches{} - bs.ID = uint(id) - err = database.DBConnection.Delete(&bs).Error - if err != nil { - glog.Errorf("failed to remove branch protection in database: %v", err) - } + _, err := handler.GiteeClient.RepositoriesApi.DeleteV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, *c.Name, *r.Name, *br.Name, opts) + if err != nil { + glog.Errorf("failed to remove branch protection: %v", err) } - glog.Infof("end to remove branch protections for %s", *r.Name) + } + + // change branch protected freature in DB + updatebranch := &database.Branches{} + updatebranch.ID = bs.ID + err := database.DBConnection.Model(updatebranch).Update("Type", brType).Error + if err != nil { + glog.Errorf("unable to update type: %v", err) } return nil } // addBranchProtections protects branches -func (handler *InitHandler) addBranchProtections(c Community, r Repository, mapBranches, mapBranchesInDB map[string]string) error { - // add branch protections - listOfProtectedBranches := make([]string, 0) - - for k := range mapBranches { - if _, exits := mapBranchesInDB[k]; !exits { - listOfProtectedBranches = append(listOfProtectedBranches, k) - } +func (handler *InitHandler) addBranchInGiteeAndDb(c Community, r Repository, br Branch) error { + // create branch in gitee + repobranchbody := gitee.CreateBranchParam{} + repobranchbody.AccessToken = handler.Config.GiteeToken + repobranchbody.BranchName = *br.Name + repobranchbody.Refs = *br.CreateFrom + _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, *c.Name, *r.Name, repobranchbody) + if err != nil { + glog.Errorf("fail to add branch (%s) for repository (%s): %v", *br.Name, *r.Name, err) + return nil } - glog.Infof("list of protected branches: %v", listOfProtectedBranches) - - if len(listOfProtectedBranches) > 0 { - getOpts := &gitee.GetV5ReposOwnerRepoBranchesBranchOpts{} - getOpts.AccessToken = optional.NewString(handler.Config.GiteeToken) + glog.Infof("Add branch (%s) for repository (%s) in gitee.", *br.Name, *r.Name) + // add branch protection to gitee + var brType string + brType = BranchNormal + if *br.Type == BranchProtected { protectBody := gitee.BranchProtectionPutParam{} protectBody.AccessToken = handler.Config.GiteeToken - - glog.Errorf("begin to add branch protections for %s", *r.Name) - for _, v := range listOfProtectedBranches { - // check if protected branch exists - branchObj, response, _ := handler.GiteeClient.RepositoriesApi.GetV5ReposOwnerRepoBranchesBranch( - handler.Context, *c.Name, *r.Name, v, getOpts) - if response.StatusCode == 404 { - glog.Errorf("branch %s not exists, no need for protection", v) - continue - } - - // If branch has alreay been protected, no need for protection - if branchObj.Protected == true { - glog.Errorf("branch %s has been protected already, no need for protection", v) - continue - } - - // add branch protection to gitee - _, response, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( - handler.Context, *c.Name, *r.Name, v, protectBody) - if err != nil { - glog.Errorf("failed to add branch protection: %v", err) - continue - } - // add branch protection to database - bs := database.Branches{ - Owner: *c.Name, - Repo: *r.Name, - Name: v, - Type: BranchProtected, - } - err = database.DBConnection.Create(&bs).Error - if err != nil { - glog.Errorf("failed to add branch protection in database: %v", err) - } + _, _, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, *c.Name, *r.Name, *br.Name, protectBody) + if err != nil { + glog.Errorf("failed to add branch protection: %v", err) } - glog.Infof("end to add branch protections for %s", *r.Name) + brType = BranchProtected + } + // add branch to database + bs := database.Branches{ + Owner: *c.Name, + Repo: *r.Name, + Name: *br.Name, + Type: brType, + AdditionalInfo: *br.CreateFrom, + } + err = database.DBConnection.Create(&bs).Error + if err != nil { + glog.Errorf("failed to add branch protection in database: %v", err) } return nil diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index 308e868..0e5fb42 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -22,10 +22,17 @@ type RepoHandler struct { } type Repos struct { + Version string `yaml:"version"` Community string `yaml:"community"` Repositories []Repository `yaml:"repositories"` } +type Branch struct { + Name *string `yaml:"name"` + Type *string `yaml:"type"` + CreateFrom *string `yaml:"create_from"` +} + // Serve func (handler *RepoHandler) Serve() { // init sha @@ -386,13 +393,16 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository // create branch repobranchbody := gitee.CreateBranchParam{} repobranchbody.AccessToken = handler.Config.GiteeToken - repobranchbody.Refs = "master" - for _, br := range repo.ProtectedBranches { - repobranchbody.BranchName = br - _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, owner, *repo.Name, repobranchbody) - if br == "master" { + for _, br := range repo.Branches { + if *br.Name == "master" { continue } + repobranchbody.BranchName = *br.Name + if *br.CreateFrom == "" { + *br.CreateFrom = "master" + } + repobranchbody.Refs = *br.CreateFrom + _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, owner, *repo.Name, repobranchbody) if err != nil { glog.Errorf("fail to add branch (%s) for repository (%s): %v", br, *repo.Name, err) return err @@ -407,139 +417,122 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository func (handler *RepoHandler) handleBranches(community string, r Repository) error { // if the branches are defined in the repositories, it means that // all the branches defined in the community will not inherited by repositories - mapBranches := make(map[string]string) - if len(r.ProtectedBranches) > 0 { + if len(r.Branches) > 0 { // using repository branches - glog.Infof("using repository branches: %s", *r.Name) - for _, b := range r.ProtectedBranches { - mapBranches[b] = b - } - } - - // get branches from DB - var bs []database.Branches - err := database.DBConnection.Model(&database.Branches{}). - Where("owner = ? and repo = ?", community, r.Name).Find(&bs).Error - if err != nil { - glog.Errorf("unable to get branches: %v", err) - return err - } - mapBranchesInDB := make(map[string]string) - for _, b := range bs { - mapBranchesInDB[b.Name] = strconv.Itoa(int(b.ID)) - } - - // un-protected branches - err = handler.removeBranchProtections(community, r, mapBranches, mapBranchesInDB) - if err != nil { - glog.Errorf("unable to un-protected branches: %v", err) - } + glog.Infof("Setting repository branches: %s", *r.Name) + for _, b := range r.Branches { + //check yaml branches is in db + var bs []database.Branches + err := database.DBConnection.Model(&database.Branches{}). + Where("owner = ? and repo = ? and name = ?", community, r.Name, b.Name).Find(&bs).Error + if err != nil || len(bs) == 0 { + glog.Infof("Do not have branches (%s), need to create. %v, ", *b.Name, err) + err = handler.addBranchGiteeAndDb(community, r, b) + if err != nil { + glog.Errorf("Add branch(%s) of repor(%s) failed: %v", *b.Name, *r.Name, err) + } + continue + } - // protected branches - err = handler.addBranchProtections(community, r, mapBranches, mapBranchesInDB) - if err != nil { - glog.Errorf("unable to protected branches: %v", err) + glog.Infof("Branch exist, branch(%s) of repo(%s).", *b.Name, *r.Name) + err = handler.changeBranchGiteeAndDb(community, r, b, bs[0]) + if err != nil { + glog.Errorf("Change branch(%s) of repor(%s) failed: %v", *b.Name, *r.Name, err) + } + continue + } } - return nil } // unprotectedBranches unprotect branches -func (handler *RepoHandler) removeBranchProtections(community string, r Repository, mapBranches, mapBranchesInDB map[string]string) error { - // remove branch protections - listOfUnprotectedBranches := make([]string, 0) - - for k := range mapBranchesInDB { - if _, exists := mapBranches[k]; !exists { - listOfUnprotectedBranches = append(listOfUnprotectedBranches, k) - } +func (handler *RepoHandler) changeBranchGiteeAndDb(community string, r Repository, br Branch, bs database.Branches) error { + // if branch features are same as ones in db, do nothing. + if *r.Name == bs.Repo && *br.Name == bs.Name && *br.Type == bs.Type { + return nil } - glog.Infof("list of un-protected branches: %v", listOfUnprotectedBranches) - if len(listOfUnprotectedBranches) > 0 { + // change branch protected freature in Gitee + var brType string + brType = BranchNormal + if *br.Type == BranchProtected { + protectBody := gitee.BranchProtectionPutParam{} + protectBody.AccessToken = handler.Config.GiteeToken + _, _, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, community, *r.Name, *br.Name, protectBody) + if err != nil { + glog.Errorf("failed to add branch protection: %v", err) + } + brType = BranchProtected + } else { opts := &gitee.DeleteV5ReposOwnerRepoBranchesBranchProtectionOpts{} opts.AccessToken = optional.NewString(handler.Config.GiteeToken) - - glog.Infof("begin to remove branch protections for %s", *r.Name) - for _, v := range listOfUnprotectedBranches { - // remove branch protection from gitee - _, err := handler.GiteeClient.RepositoriesApi.DeleteV5ReposOwnerRepoBranchesBranchProtection( - handler.Context, community, *r.Name, v, opts) - if err != nil { - glog.Errorf("failed to remove branch protection: %v", err) - continue - } - // remove branch protection from DB - id, _ := strconv.Atoi(mapBranchesInDB[v]) - bs := database.Branches{} - bs.ID = uint(id) - err = database.DBConnection.Delete(&bs).Error - if err != nil { - glog.Errorf("failed to remove branch protection in database: %v", err) - } + _, err := handler.GiteeClient.RepositoriesApi.DeleteV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, community, *r.Name, *br.Name, opts) + if err != nil { + glog.Errorf("failed to remove branch protection: %v", err) } - glog.Infof("end to remove branch protections for %s", *r.Name) + } + + // change branch protected freature in DB + updatebranch := &database.Branches{} + updatebranch.ID = bs.ID + err := database.DBConnection.Model(updatebranch).Update("Type", brType).Error + if err != nil { + glog.Errorf("unable to update type: %v", err) } return nil } // addBranchProtections protects branches -func (handler *RepoHandler) addBranchProtections(community string, r Repository, mapBranches, mapBranchesInDB map[string]string) error { - // add branch protections - listOfProtectedBranches := make([]string, 0) - - for k := range mapBranches { - if _, exits := mapBranchesInDB[k]; !exits { - listOfProtectedBranches = append(listOfProtectedBranches, k) - } +func (handler *RepoHandler) addBranchGiteeAndDb(community string, r Repository, br Branch) error { + // create branch in gitee + repobranchbody := gitee.CreateBranchParam{} + repobranchbody.AccessToken = handler.Config.GiteeToken + if br.Name != nil { + repobranchbody.BranchName = *br.Name + } else { + return nil + } + if br.CreateFrom != nil { + repobranchbody.Refs = *br.CreateFrom + } else { + repobranchbody.Refs = "" } - glog.Infof("list of protected branches: %v", listOfProtectedBranches) - if len(listOfProtectedBranches) > 0 { - getOpts := &gitee.GetV5ReposOwnerRepoBranchesBranchOpts{} - getOpts.AccessToken = optional.NewString(handler.Config.GiteeToken) + _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, community, *r.Name, repobranchbody) + if err != nil { + glog.Errorf("fail to add branch (%s) for repository (%s): %v", *br.Name, *r.Name, err) + return nil + } + glog.Infof("Add branch (%s) for repository (%s) in gitee.", *br.Name, *r.Name) + // add branch protection to gitee + var brType string + brType = BranchNormal + if *br.Type == BranchProtected { protectBody := gitee.BranchProtectionPutParam{} protectBody.AccessToken = handler.Config.GiteeToken - - glog.Infof("begin to add branch protections for %s", *r.Name) - for _, v := range listOfProtectedBranches { - // check if protected branch exists - branchObj, response, _ := handler.GiteeClient.RepositoriesApi.GetV5ReposOwnerRepoBranchesBranch( - handler.Context, community, *r.Name, v, getOpts) - if response.StatusCode == 404 { - glog.Errorf("branch %s does not exist, no need for protection", v) - continue - } - - // If branch has alreay been protected, no need for protection - if branchObj.Protected == true { - glog.Errorf("branch %s has been protected already, no need for protection", v) - continue - } - - // add branch protection to gitee - _, response, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( - handler.Context, community, *r.Name, v, protectBody) - if err != nil { - glog.Errorf("failed to add branch protection: %v", err) - continue - } - // add branch protection to database - bs := database.Branches{ - Owner: community, - Repo: *r.Name, - Name: v, - Type: BranchProtected, - } - err = database.DBConnection.Create(&bs).Error - if err != nil { - glog.Errorf("failed to add branch protection in database: %v", err) - } + _, _, err := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoBranchesBranchProtection( + handler.Context, community, *r.Name, *br.Name, protectBody) + if err != nil { + glog.Errorf("failed to add branch protection: %v", err) } - glog.Infof("end to add branch protections for %s", *r.Name) + brType = BranchProtected + } + // add branch to database + bs := database.Branches{ + Owner: community, + Repo: *r.Name, + Name: *br.Name, + Type: brType, + AdditionalInfo: *br.CreateFrom, + } + err = database.DBConnection.Create(&bs).Error + if err != nil { + glog.Errorf("failed to add branch protection in database: %v", err) } return nil @@ -636,17 +629,19 @@ func (handler *RepoHandler) handleRepositorySetting(community string, r Reposito } // set none reviewer but not ci-bot(default) - reviewerBody := gitee.SetRepoReviewer{} - reviewerBody.AccessToken = handler.Config.GiteeToken - reviewerBody.Assignees = " " - reviewerBody.Testers = " " - reviewerBody.AssigneesNumber = 0 - reviewerBody.TestersNumber = 0 - response, errex := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) - if errex != nil { - glog.Errorf("Set repository reviewer info failed: %v, %s", errex, response.Status) - return errex - } + + // reviewerBody := gitee.SetRepoReviewer{} + // reviewerBody.AccessToken = handler.Config.GiteeToken + // reviewerBody.Assignees = " " + // reviewerBody.Testers = " " + // reviewerBody.AssigneesNumber = 0 + // reviewerBody.TestersNumber = 0 + // response, errex := handler.GiteeClient.RepositoriesApi.PutV5ReposOwnerRepoReviewer(handler.Context, community, *r.Name, reviewerBody) + // if errex != nil { + // glog.Errorf("Set repository reviewer info failed: %v, %s", errex, response.Status) + // glog.Errorf("requestURL:%s, %s", response.Request.RequestURI, response.Request.Host) + // return errex + // } return nil } -- Gitee From 9f93cd890a536b717ffe6f395e5d622aebe1c3d9 Mon Sep 17 00:00:00 2001 From: xwzQmxx <1499273991@qq.com> Date: Fri, 27 Nov 2020 15:30:39 +0800 Subject: [PATCH 15/16] fix issue I275ZO --- pkg/cibot/checkpr.go | 37 ++++++----- pkg/cibot/pullrequest.go | 140 +++++++++++++++++++-------------------- 2 files changed, 91 insertions(+), 86 deletions(-) diff --git a/pkg/cibot/checkpr.go b/pkg/cibot/checkpr.go index 0a0e801..2866dcd 100644 --- a/pkg/cibot/checkpr.go +++ b/pkg/cibot/checkpr.go @@ -4,26 +4,33 @@ import ( "gitee.com/openeuler/go-gitee/gitee" "github.com/golang/glog" ) + var checkPrComment = "Cannot use \"/check-pr\", because this command is only used to detect open pull requests" + //CheckPr Check whether the pull request can be merged -func (s *Server)CheckPr(event *gitee.NoteEvent) (err error) { - if *event.NoteableType == "PullRequest" && event.PullRequest.State == "open"{ +func (s *Server) CheckPr(event *gitee.NoteEvent) (err error) { + if *event.NoteableType == "PullRequest" && event.PullRequest.State == "open" { err := s.MergePullRequest(event) if err != nil { - return err - } - }else { - body := gitee.PullRequestCommentPostParam{} - body.AccessToken = s.Config.GiteeToken - body.Body = checkPrComment - owner := event.Repository.Namespace - repo := event.Repository.Name - number := event.PullRequest.Number - _, _, 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) - return err + comment := err.Error() + return s.addCommentToPullRequest(event.Repository.Namespace, event.Repository.Name, + comment, event.PullRequest.Number) } + } else { + return s.addCommentToPullRequest(event.Repository.Namespace, event.Repository.Name, + checkPrComment, event.PullRequest.Number) + } + return nil +} + +func (s *Server) addCommentToPullRequest(owner, repo, comment string, number int32) error { + body := gitee.PullRequestCommentPostParam{} + body.AccessToken = s.Config.GiteeToken + body.Body = comment + _, _, 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) + return err } return nil } diff --git a/pkg/cibot/pullrequest.go b/pkg/cibot/pullrequest.go index 401b049..ffd49b1 100644 --- a/pkg/cibot/pullrequest.go +++ b/pkg/cibot/pullrequest.go @@ -413,7 +413,7 @@ func (s *Server) hasLgtmLabel(labels []gitee.Label) bool { return false } -func (s *Server) readyForMerge(labels []gitee.Label, owner, repo string) bool { +func (s *Server) readyForMerge(labels []gitee.Label, owner, repo string) error { aproveLabel := 0 lgtmLabel := 0 lgtmPrefix := "" @@ -434,7 +434,12 @@ func (s *Server) readyForMerge(labels []gitee.Label, owner, repo string) bool { } } glog.Infof("Pr labels have approved: %d lgtm: %d, required (%d)", aproveLabel, lgtmLabel, leastLgtm) - return aproveLabel == 1 && lgtmLabel >= leastLgtm + if aproveLabel == 1 && lgtmLabel >= leastLgtm { + return nil + } else { + return fmt.Errorf("This pull request can not be merged, please check that the number of **lgtm** labels >= %d "+ + "and there are an **approve** labels. ", leastLgtm) + } } // check with the labels constraints requiring/missing to determine if mergable @@ -463,83 +468,76 @@ func (s *Server) MergePullRequest(event *gitee.NoteEvent) error { listofPrLabels := pr.Labels glog.Infof("List of pr labels: %v", listofPrLabels) // ready to merge - if s.readyForMerge(listofPrLabels, owner, repo) { - nonRequiringLabels, nonMissingLabels := s.legalLabelsForMerge(listofPrLabels) - if len(nonRequiringLabels) == 0 && len(nonMissingLabels) == 0 { - // current pr can be merged - if c, b := checkFrozenCanMerge(event.Author.Login, pr.Base.Ref); !b { - //send comment to pr - body := gitee.PullRequestCommentPostParam{} - body.AccessToken = s.Config.GiteeToken - if len(c) > 0 { - body.Body = fmt.Sprintf("**Merge failed** The current pull request merge target has been frozen, and only the branch owner( @%s ) can merge.", - strings.Join(c, " , @")) - } else { - body.Body = "**Merge failed** The current pull request merge target has been frozen, and only the branch owner can merge." + err = s.readyForMerge(listofPrLabels, owner, repo) + if err != nil { + return err + } + nonRequiringLabels, nonMissingLabels := s.legalLabelsForMerge(listofPrLabels) + if len(nonRequiringLabels) == 0 && len(nonMissingLabels) == 0 { + // current pr can be merged + if c, b := checkFrozenCanMerge(event.Author.Login, pr.Base.Ref); !b { + //send comment to pr + comment := "" + if len(c) > 0 { + comment = fmt.Sprintf("**Merge failed** The current pull request merge target has been frozen, and only the branch owner( @%s ) can merge.", + strings.Join(c, " , @")) + } else { + comment = "**Merge failed** The current pull request merge target has been frozen, and only the branch owner can merge." + } + err = s.addCommentToPullRequest(owner, repo, comment, prNumber) + if err != nil { + glog.Errorf("Cannot add comments to pull request: %v", err) + } + } else { + if event.PullRequest.Mergeable { + // remove assignees + err = s.RemoveAssigneesInPullRequest(event) + if err != nil { + glog.Errorf("unable to remove assignees. err: %v", err) } - - _, _, err = s.GiteeClient.PullRequestsApi.PostV5ReposOwnerRepoPullsNumberComments(s.Context, owner, repo, prNumber, body) + // remove testers + err = s.RemoveTestersInPullRequest(event) if err != nil { - glog.Errorf("Cannot add comments to pull request: %v", err) + glog.Errorf("unable to remove testers. err: %v", err) } - } else { - if event.PullRequest.Mergeable { - // remove assignees - err = s.RemoveAssigneesInPullRequest(event) - if err != nil { - glog.Errorf("unable to remove assignees. err: %v", err) - return err - } - // remove testers - err = s.RemoveTestersInPullRequest(event) - if err != nil { - glog.Errorf("unable to remove testers. err: %v", err) - return err - } - // merge pr - body := gitee.PullRequestMergePutParam{} - body.AccessToken = s.Config.GiteeToken - // generate merge body - description, err := s.generateMergeDescription(event) - if err != nil { - glog.Errorf("unable to get merge description.err: %v", err) - return err - } - body.Description = description - - _, err = s.GiteeClient.PullRequestsApi.PutV5ReposOwnerRepoPullsNumberMerge(s.Context, owner, repo, prNumber, body) - if err != nil { - glog.Errorf("unable to merge pull request. err: %v", err) - return err - } + // merge pr + body := gitee.PullRequestMergePutParam{} + body.AccessToken = s.Config.GiteeToken + // generate merge body + description, err := s.generateMergeDescription(event) + if err != nil { + glog.Errorf("unable to get merge description.err: %v", err) + return fmt.Errorf(`The pull request merge failed, please use command "/check-pr" to try again. `) } - } - } else { - // add comment to pr to show the labels reason of not mergable - nonRequiringMsg := "" - if len(nonRequiringLabels) > 0 { - nonRequiringMsg = fmt.Sprintf(nonRequiringLabelsMessage, strings.Join(nonRequiringLabels, ",")) - } - nonMissingMsg := "" - if len(nonMissingLabels) > 0 { - nonMissingMsg = fmt.Sprintf(nonMissingLabelsMessage, strings.Join(nonMissingLabels, ",")) - } + body.Description = description - // add comment back to pr - body := gitee.PullRequestCommentPostParam{} - body.AccessToken = s.Config.GiteeToken - body.Body = fmt.Sprintf(cannotMergeMessage, fmt.Sprintf("%s%s", nonRequiringMsg, nonMissingMsg)) - owner := event.Repository.Namespace - repo := event.Repository.Name - number := event.PullRequest.Number - _, _, 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) - return err + _, err = s.GiteeClient.PullRequestsApi.PutV5ReposOwnerRepoPullsNumberMerge(s.Context, owner, repo, prNumber, body) + if err != nil { + glog.Errorf("unable to merge pull request. err: %v", err) + return fmt.Errorf(`The pull request merge failed, please use command "/check-pr" to try again. `) + } } } + } else { + // add comment to pr to show the labels reason of not mergable + nonRequiringMsg := "" + if len(nonRequiringLabels) > 0 { + nonRequiringMsg = fmt.Sprintf(nonRequiringLabelsMessage, strings.Join(nonRequiringLabels, ",")) + } + nonMissingMsg := "" + if len(nonMissingLabels) > 0 { + nonMissingMsg = fmt.Sprintf(nonMissingLabelsMessage, strings.Join(nonMissingLabels, ",")) + } + // add comment back to pr + comment := fmt.Sprintf(cannotMergeMessage, fmt.Sprintf("%s%s", nonRequiringMsg, nonMissingMsg)) + owner := event.Repository.Namespace + repo := event.Repository.Name + number := event.PullRequest.Number + err = s.addCommentToPullRequest(owner, repo, comment, number) + if err != nil { + glog.Errorf("unable to add comment in pull request: %v", err) + } } - return nil } -- Gitee From c230aa529986a16df97e7384304fc1eb6f1c5ca6 Mon Sep 17 00:00:00 2001 From: georgecao Date: Wed, 30 Dec 2020 12:47:15 +0800 Subject: [PATCH 16/16] fix a bug about branch creating. --- pkg/cibot/repohandler.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/cibot/repohandler.go b/pkg/cibot/repohandler.go index 0e5fb42..1db37fa 100644 --- a/pkg/cibot/repohandler.go +++ b/pkg/cibot/repohandler.go @@ -404,10 +404,10 @@ func (handler *RepoHandler) addRepositoriesinGitee(owner string, repo Repository repobranchbody.Refs = *br.CreateFrom _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, owner, *repo.Name, repobranchbody) if err != nil { - glog.Errorf("fail to add branch (%s) for repository (%s): %v", br, *repo.Name, err) + glog.Errorf("fail to add branch (%s) for repository (%s): %v", *br.Name, *repo.Name, err) return err } - glog.Infof("Add branch (%s) for repository (%s)", br, *repo.Name) + glog.Infof("Add branch (%s) for repository (%s)", *br.Name, *repo.Name) } return nil } @@ -505,9 +505,9 @@ func (handler *RepoHandler) addBranchGiteeAndDb(community string, r Repository, _, _, err := handler.GiteeClient.RepositoriesApi.PostV5ReposOwnerRepoBranches(handler.Context, community, *r.Name, repobranchbody) if err != nil { glog.Errorf("fail to add branch (%s) for repository (%s): %v", *br.Name, *r.Name, err) - return nil + } else { + glog.Infof("Add branch (%s) for repository (%s) in gitee.", *br.Name, *r.Name) } - glog.Infof("Add branch (%s) for repository (%s) in gitee.", *br.Name, *r.Name) // add branch protection to gitee var brType string @@ -528,7 +528,7 @@ func (handler *RepoHandler) addBranchGiteeAndDb(community string, r Repository, Repo: *r.Name, Name: *br.Name, Type: brType, - AdditionalInfo: *br.CreateFrom, + AdditionalInfo: repobranchbody.Refs, } err = database.DBConnection.Create(&bs).Error if err != nil { -- Gitee