code.gitea.io/gitea@v1.21.7/services/pull/comment.go (about)

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