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.5KB

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