Don't use migrations but use a doctor to fix old negative line number of code comments

This commit is contained in:
Lunny Xiao 2025-07-18 18:52:58 -07:00
parent 45931b3df3
commit 1ecac1aadd
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 46 additions and 18 deletions

View File

@ -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
}

View File

@ -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
}

46
services/doctor/review.go Normal file
View File

@ -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,
})
}