You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

comment.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package pull
  4. import (
  5. "context"
  6. issues_model "code.gitea.io/gitea/models/issues"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/json"
  11. issue_service "code.gitea.io/gitea/services/issue"
  12. )
  13. // getCommitIDsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
  14. // isForcePush will be true if oldCommit isn't on the branch
  15. // Commit on baseBranch will skip
  16. func getCommitIDsFromRepo(ctx context.Context, repo *repo_model.Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, isForcePush bool, err error) {
  17. repoPath := repo.RepoPath()
  18. gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repoPath)
  19. if err != nil {
  20. return nil, false, err
  21. }
  22. defer closer.Close()
  23. oldCommit, err := gitRepo.GetCommit(oldCommitID)
  24. if err != nil {
  25. return nil, false, err
  26. }
  27. newCommit, err := gitRepo.GetCommit(newCommitID)
  28. if err != nil {
  29. return nil, false, err
  30. }
  31. isForcePush, err = newCommit.IsForcePush(oldCommitID)
  32. if err != nil {
  33. return nil, false, err
  34. }
  35. if isForcePush {
  36. commitIDs = make([]string, 2)
  37. commitIDs[0] = oldCommitID
  38. commitIDs[1] = newCommitID
  39. return commitIDs, isForcePush, err
  40. }
  41. // Find commits between new and old commit exclusing base branch commits
  42. commits, err := gitRepo.CommitsBetweenNotBase(newCommit, oldCommit, baseBranch)
  43. if err != nil {
  44. return nil, false, err
  45. }
  46. commitIDs = make([]string, 0, len(commits))
  47. for i := len(commits) - 1; i >= 0; i-- {
  48. commitIDs = append(commitIDs, commits[i].ID.String())
  49. }
  50. return commitIDs, isForcePush, err
  51. }
  52. // CreatePushPullComment create push code to pull base comment
  53. func CreatePushPullComment(ctx context.Context, pusher *user_model.User, pr *issues_model.PullRequest, oldCommitID, newCommitID string) (comment *issues_model.Comment, err error) {
  54. if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
  55. return nil, nil
  56. }
  57. ops := &issues_model.CreateCommentOptions{
  58. Type: issues_model.CommentTypePullRequestPush,
  59. Doer: pusher,
  60. Repo: pr.BaseRepo,
  61. }
  62. var data issues_model.PushActionContent
  63. data.CommitIDs, data.IsForcePush, err = getCommitIDsFromRepo(ctx, pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
  64. if err != nil {
  65. return nil, err
  66. }
  67. ops.Issue = pr.Issue
  68. dataJSON, err := json.Marshal(data)
  69. if err != nil {
  70. return nil, err
  71. }
  72. ops.Content = string(dataJSON)
  73. comment, err = issue_service.CreateComment(ctx, ops)
  74. return comment, err
  75. }