123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package service
- import (
- "context"
- "fmt"
- "go-common/app/tool/saga/model"
- "go-common/library/log"
- )
- // MergeRequest ...
- func (s *Service) MergeRequest(c context.Context, event *model.HookMR) (err error) {
- var (
- ok bool
- repo *model.Repo
- url = event.Project.GitSSHURL
- projID = event.Project.ID
- mrIID = int(event.ObjectAttributes.IID)
- sourceBranch = event.ObjectAttributes.SourceBranch
- targetBranch = event.ObjectAttributes.TargetBranch
- wip = event.ObjectAttributes.WorkInProgress
- )
- log.Info("Hook MergeRequest state: %s, projID: %d, mrid: %d", event.ObjectAttributes.State, projID, mrIID)
- if ok, repo = s.findRepo(url); !ok {
- return
- }
- if event.ObjectAttributes.State == model.MRStateOpened {
- if wip {
- log.Info("MergeRequest mr is wip, project ID: [%d], branch: [%s]", event.Project.ID, sourceBranch)
- return
- }
- if !s.validTargetBranch(targetBranch, repo) {
- s.gitlab.CreateMRNote(projID, mrIID, fmt.Sprintf("<pre>警告:目标分支 %s 不在SAGA白名单中,此MR不会触发SAGA行为!</pre>", targetBranch))
- return
- }
- authBranch := s.cmd.GetAuthBranch(targetBranch, repo.Config.AuthBranches)
- if err = s.ShowMRRoleInfo(c, authBranch, repo, event); err != nil {
- return
- }
- } else {
- if event.ObjectAttributes.State == model.MRStateMerged {
- log.Info("MergeRequest.UpdateContributor:%d,%s,%s,%+v", projID, sourceBranch, targetBranch, repo.Config.AuthBranches)
- if err = s.cmd.UpdateContributor(projID, mrIID, sourceBranch, targetBranch, repo.Config.AuthBranches); err != nil {
- return
- }
- }
- if err = s.d.DeleteReportStatus(c, projID, mrIID); err != nil {
- return
- }
- }
- return
- }
- // ShowMRRoleInfo ...
- func (s *Service) ShowMRRoleInfo(c context.Context, authBranch string, repo *model.Repo, event *model.HookMR) (err error) {
- var (
- projID = event.Project.ID
- mrIID = int(event.ObjectAttributes.IID)
- result bool
- )
- if result, err = s.d.ReportStatus(c, projID, mrIID); err != nil {
- return
- }
- log.Info("MergeRequest whether create note auth info: %v", result)
- if !result {
- if len(repo.Config.SuperAuthUsers) > 0 {
- if err = s.ReportSuperRoleInfo(c, repo.Config.SuperAuthUsers, event); err != nil {
- return
- }
- } else {
- if err = s.ReportMRRoleInfo(c, authBranch, event); err != nil {
- return
- }
- }
- if err = s.d.SetReportStatus(c, projID, mrIID, true); err != nil {
- return
- }
- }
- return
- }
- // ReportMRRoleInfo ...
- func (s *Service) ReportMRRoleInfo(c context.Context, authBranch string, event *model.HookMR) (err error) {
- var (
- projID = event.Project.ID
- mrIID = int(event.ObjectAttributes.IID)
- pathOwners []model.RequireReviewFolder
- )
- if pathOwners, err = s.cmd.GetAllPathAuth(projID, mrIID, authBranch); err != nil {
- return
- }
- authInfo := ""
- authInfo = fmt.Sprintf("<pre>SAGA权限信息提示,请review: %s</pre>", event.ObjectAttributes.Title)
- authInfo += "\n"
- for _, os := range pathOwners {
- if len(os.Owners) > 0 {
- //authInfo += "----- PATH: " + os.Folder + ";OWNER: "
- authInfo += fmt.Sprintf("+ PATH: %s;OWNER: ", os.Folder)
- for _, o := range os.Owners {
- if o == "all" {
- authInfo += "所有人" + " 或 "
- } else {
- authInfo += "@" + o + " 或 "
- }
- }
- authInfo = authInfo[:len(authInfo)-len(" 或 ")]
- }
- if len(os.Reviewers) > 0 {
- authInfo += ";REVIEWER: "
- for _, o := range os.Reviewers {
- if o == "all" {
- authInfo += "所有人" + " 与 "
- } else {
- authInfo += "@" + o + " 与 "
- }
- }
- authInfo = authInfo[:len(authInfo)-len(" 与 ")]
- }
- authInfo += "\n\n"
- }
- if _, err = s.gitlab.CreateMRNote(projID, mrIID, authInfo); err != nil {
- return
- }
- return
- }
- // ReportSuperRoleInfo ...
- func (s *Service) ReportSuperRoleInfo(c context.Context, superUsers []string, event *model.HookMR) (err error) {
- var (
- projID = event.Project.ID
- mrIID = int(event.ObjectAttributes.IID)
- )
- authInfo := fmt.Sprintf("<pre>SAGA权限信息提示,已配置超级权限用户,请review: %s</pre>", event.ObjectAttributes.Title)
- authInfo += "\n"
- authInfo += fmt.Sprintf("+ SUPERMAN: ")
- for _, user := range superUsers {
- authInfo += "@" + user + " 或 "
- }
- authInfo = authInfo[:len(authInfo)-len(" 或 ")]
- if _, err = s.gitlab.CreateMRNote(projID, mrIID, authInfo); err != nil {
- return
- }
- return
- }
|