From 1ecac1aaddb04c413c3608bb2208dda636ca9fff Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 18 Jul 2025 18:52:58 -0700 Subject: [PATCH] Don't use migrations but use a doctor to fix old negative line number of code comments --- models/migrations/migrations.go | 4 --- models/migrations/v1_25/v321.go | 14 ---------- services/doctor/review.go | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 18 deletions(-) delete mode 100644 models/migrations/v1_25/v321.go create mode 100644 services/doctor/review.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index e1a6a6f8c79..176372486e8 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -24,7 +24,6 @@ import ( "code.gitea.io/gitea/models/migrations/v1_22" "code.gitea.io/gitea/models/migrations/v1_23" "code.gitea.io/gitea/models/migrations/v1_24" - "code.gitea.io/gitea/models/migrations/v1_25" "code.gitea.io/gitea/models/migrations/v1_6" "code.gitea.io/gitea/models/migrations/v1_7" "code.gitea.io/gitea/models/migrations/v1_8" @@ -383,9 +382,6 @@ func prepareMigrationTasks() []*migration { newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode), newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable), newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor), - - // Gitea 1.24.0 ends at database version 321 - newMigration(321, "Migrate commit id of pull requests code review comment", v1_25.MigrateCommitIDOfPullRequestCodeReviewComment), } return preparedMigrations } diff --git a/models/migrations/v1_25/v321.go b/models/migrations/v1_25/v321.go deleted file mode 100644 index bfed6127db2..00000000000 --- a/models/migrations/v1_25/v321.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2025 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package v1_25 - -import ( - "xorm.io/xorm" -) - -// MigrateCommitIDOfPullRequestCodeReviewComment this will be almost right before comment on the special commit of the pull request -func MigrateCommitIDOfPullRequestCodeReviewComment(x *xorm.Engine) error { - _, err := x.Exec("UPDATE comment SET commit_sha = (select merge_base from pull_request WHERE issue_id = comment.issue_id) WHERE line < 0") - return err -} diff --git a/services/doctor/review.go b/services/doctor/review.go new file mode 100644 index 00000000000..790c0a475e0 --- /dev/null +++ b/services/doctor/review.go @@ -0,0 +1,46 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package doctor + +import ( + "context" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/log" +) + +// checkCommitSHAOfPullRequestCodeReviewComment will check if the commit SHA of pull request code review comments +// 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 +// 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 +// so that this may be broken for those comments submitted in a special commit(non the first one) page or a range commit page +// NOTICE: the fix can only be done once, so it should be run twice or more +func checkCommitSHAOfPullRequestCodeReviewComment(ctx context.Context, logger log.Logger, autofix bool) error { + 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() + if err != nil { + logger.Critical("Error: %v whilst counting wrong comment commit sha", err) + return err + } + if count > 0 { + if autofix { + 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") + if err != nil { + return err + } + 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) + } else { + logger.Warn("%d comments with wrong commit sha exist", count) + } + } + return nil +} + +func init() { + Register(&Check{ + Title: "Check if comment with negative line number has wrong commit sha", + Name: "check-commitsha-review-comment", + IsDefault: true, + Run: checkCommitSHAOfPullRequestCodeReviewComment, + Priority: 3, + }) +}