comment.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "go-common/app/tool/saga/model"
  7. "go-common/library/log"
  8. )
  9. // HandleGitlabComment handle comment webhook
  10. func (s *Service) HandleGitlabComment(c context.Context, event *model.HookComment) (err error) {
  11. var (
  12. comment = strings.TrimSpace(event.ObjectAttributes.Note)
  13. projName = event.MergeRequest.Source.Name
  14. targetBranch = event.MergeRequest.TargetBranch
  15. gitRepo *model.Repo
  16. ok bool
  17. )
  18. if event.ObjectAttributes.NoteableType != model.HookCommentTypeMR {
  19. log.Info("Comment hook noteableType [%s] ignore", event.ObjectAttributes.NoteableType)
  20. return
  21. }
  22. if event.MergeRequest.State == model.MRStateMerged {
  23. log.Info("Gitlab MR [%d] has been merged", event.MergeRequest.ID)
  24. return
  25. }
  26. if gitRepo, ok = s.gitRepoMap[projName]; !ok {
  27. log.Info("Gitlab MR (%d) unknown projName (%s)", event.MergeRequest.ID, projName)
  28. return
  29. }
  30. if !s.validTargetBranch(targetBranch, gitRepo) {
  31. log.Info("Target branch (%s) is not in white list, won't serve comment!", targetBranch)
  32. return
  33. }
  34. fmt.Println("HandleGitlabComment:", event)
  35. if err = s.cmd.Exec(c, comment, event, gitRepo); err != nil {
  36. log.Error("Command Exec cmd: %s err: (%+v)", comment, err)
  37. return
  38. }
  39. return
  40. }