Skip to content

Commit ae6692d

Browse files
Copilotsilverwind
andcommitted
Refactor self-reference check into separate function
Extract isSelfReference function to improve code organization and reusability Co-authored-by: silverwind <115237+silverwind@users.noreply.github.com>
1 parent be7bed9 commit ae6692d

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

services/issue/commit.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ func issueAddTime(ctx context.Context, issue *issues_model.Issue, doer *user_mod
8989
return err
9090
}
9191

92+
// isSelfReference checks if a commit is the merge commit of the PR it references.
93+
// This prevents creating self-referencing timeline entries when a PR merge commit
94+
// contains a reference to its own PR number in the commit message.
95+
func isSelfReference(ctx context.Context, issue *issues_model.Issue, commitSHA string) bool {
96+
if !issue.IsPull {
97+
return false
98+
}
99+
100+
if err := issue.LoadPullRequest(ctx); err != nil {
101+
if !issues_model.IsErrPullRequestNotExist(err) {
102+
log.Error("LoadPullRequest: %v", err)
103+
}
104+
return false
105+
}
106+
107+
return issue.PullRequest.MergedCommitID == commitSHA
108+
}
109+
92110
// getIssueFromRef returns the issue referenced by a ref. Returns a nil *Issue
93111
// if the provided ref references a non-existent issue.
94112
func getIssueFromRef(ctx context.Context, repo *repo_model.Repository, index int64) (*issues_model.Issue, error) {
@@ -159,15 +177,8 @@ func UpdateIssuesCommit(ctx context.Context, doer *user_model.User, repo *repo_m
159177
}
160178

161179
// Skip self-references: if this commit is the merge commit of the PR it references
162-
if refIssue.IsPull {
163-
if err := refIssue.LoadPullRequest(ctx); err != nil {
164-
if !issues_model.IsErrPullRequestNotExist(err) {
165-
log.Error("LoadPullRequest: %v", err)
166-
}
167-
} else if refIssue.PullRequest.MergedCommitID == c.Sha1 {
168-
// This is a self-reference (PR merge commit referencing its own PR), skip it
169-
continue
170-
}
180+
if isSelfReference(ctx, refIssue, c.Sha1) {
181+
continue
171182
}
172183

173184
message := fmt.Sprintf(`<a href="%s/commit/%s">%s</a>`, html.EscapeString(repo.Link()), html.EscapeString(url.PathEscape(c.Sha1)), html.EscapeString(strings.SplitN(c.Message, "\n", 2)[0]))

0 commit comments

Comments
 (0)