|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package doctor |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "code.gitea.io/gitea/models/db" |
| 10 | + "code.gitea.io/gitea/modules/log" |
| 11 | +) |
| 12 | + |
| 13 | +// checkCommitSHAOfPullRequestCodeReviewComment will check if the commit SHA of pull request code review comments |
| 14 | +// For a comment with negative line number, it should be the merge base of the pull request if the comment is on the files page |
| 15 | +// if it's on a special commit page or a range commit page, it should be the previous commit when reviewing that commit/commit range |
| 16 | +// so that this may be broken for those comments submitted in a special commit(non the first one) page or a range commit page |
| 17 | +// NOTICE: the fix can only be done once, so it should be run twice or more |
| 18 | +func checkCommitSHAOfPullRequestCodeReviewComment(ctx context.Context, logger log.Logger, autofix bool) error { |
| 19 | + count, err := db.GetEngine(ctx).SQL("SELECT 1 FROM comment where line < 0 AND commit_sha != (select merge_base from pull_request WHERE issue_id = comment.issue_id)").Count() |
| 20 | + if err != nil { |
| 21 | + logger.Critical("Error: %v whilst counting wrong comment commit sha", err) |
| 22 | + return err |
| 23 | + } |
| 24 | + if count > 0 { |
| 25 | + if autofix { |
| 26 | + total, err := db.GetEngine(ctx).Exec("UPDATE comment SET commit_sha = (select merge_base from pull_request WHERE issue_id = comment.issue_id) WHERE line < 0") |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + logger.Info("%d comments with wrong commit sha fixed\nWARNING: This doctor can only fix this once, so it should NOT be run twice or more", total) |
| 31 | + } else { |
| 32 | + logger.Warn("%d comments with wrong commit sha exist", count) |
| 33 | + } |
| 34 | + } |
| 35 | + return nil |
| 36 | +} |
| 37 | + |
| 38 | +func init() { |
| 39 | + Register(&Check{ |
| 40 | + Title: "Check if comment with negative line number has wrong commit sha", |
| 41 | + Name: "check-commitsha-review-comment", |
| 42 | + IsDefault: true, |
| 43 | + Run: checkCommitSHAOfPullRequestCodeReviewComment, |
| 44 | + Priority: 3, |
| 45 | + }) |
| 46 | +} |
0 commit comments