13 Star 25 Fork 28

openEuler / ci-bot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
commitcheck.go 2.21 KB
一键复制 编辑 原始数据 按行查看 历史
package cibot
import (
"gitee.com/openeuler/go-gitee/gitee"
"github.com/antihax/optional"
"github.com/golang/glog"
)
func (s *Server) hasSquashCommitLabel(labels []gitee.Label) bool {
for _, l := range labels {
if s.Config.SquashCommitLabel == l.Name {
return true
}
}
return false
}
func (s *Server) ValidateCommits(event *gitee.PullRequestEvent) error {
if s.Config.CommitsThreshold == 0 || len(s.Config.SquashCommitLabel) == 0 {
glog.Info("'commitsthreshold' or 'squashcommitlabel' is not configured, skip validating pr commits")
return nil
}
// Get Pull Request Commits detail
owner := event.Repository.Namespace
repo := event.Repository.Path
prNumber := event.PullRequest.Number
commitPullRequestOpts := &gitee.GetV5ReposOwnerRepoPullsNumberCommitsOpts{}
commitPullRequestOpts.AccessToken = optional.NewString(s.Config.GiteeToken)
commits, _, err := s.GiteeClient.PullRequestsApi.GetV5ReposOwnerRepoPullsNumberCommits(
s.Context, owner, repo, prNumber, commitPullRequestOpts)
if err != nil {
glog.Errorf("failed to get pull request commits detail : %v", err)
return err
}
commitsExceeded := len(commits) > s.Config.CommitsThreshold
// Get Pull Request Label details
prOpts := &gitee.GetV5ReposOwnerRepoPullsNumberOpts{}
prOpts.AccessToken = optional.NewString(s.Config.GiteeToken)
pr, _, err := s.GiteeClient.PullRequestsApi.GetV5ReposOwnerRepoPullsNumber(s.Context, owner, repo, prNumber, prOpts)
if err != nil {
glog.Errorf("unable to get pull request. err: %v", err)
return err
}
labelParam := &gitee.NoteEvent{}
labelParam.PullRequest = event.PullRequest
labelParam.Repository = event.Repository
labelParam.Comment = &gitee.NoteHook{}
if commitsExceeded && !s.hasSquashCommitLabel(pr.Labels) {
//add squash commits label if needed
err = s.AddSpecifyLabelsInPulRequest(labelParam, []string{s.Config.SquashCommitLabel}, true)
if err != nil {
return err
}
} else if !commitsExceeded && s.hasSquashCommitLabel(pr.Labels) {
//remove label if needed
mapOfRemoveLabels := map[string]string{}
mapOfRemoveLabels[s.Config.SquashCommitLabel] = s.Config.SquashCommitLabel
err = s.RemoveSpecifyLabelsInPulRequest(labelParam, mapOfRemoveLabels)
if err != nil {
return err
}
}
return nil
}
Go
1
https://gitee.com/openeuler/ci-bot.git
git@gitee.com:openeuler/ci-bot.git
openeuler
ci-bot
ci-bot
e91f82195b70

搜索帮助