Skip to content

Commit 1ecac1a

Browse files
committed
Don't use migrations but use a doctor to fix old negative line number of code comments
1 parent 45931b3 commit 1ecac1a

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

models/migrations/migrations.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"code.gitea.io/gitea/models/migrations/v1_22"
2525
"code.gitea.io/gitea/models/migrations/v1_23"
2626
"code.gitea.io/gitea/models/migrations/v1_24"
27-
"code.gitea.io/gitea/models/migrations/v1_25"
2827
"code.gitea.io/gitea/models/migrations/v1_6"
2928
"code.gitea.io/gitea/models/migrations/v1_7"
3029
"code.gitea.io/gitea/models/migrations/v1_8"
@@ -383,9 +382,6 @@ func prepareMigrationTasks() []*migration {
383382
newMigration(318, "Add anonymous_access_mode for repo_unit", v1_24.AddRepoUnitAnonymousAccessMode),
384383
newMigration(319, "Add ExclusiveOrder to Label table", v1_24.AddExclusiveOrderColumnToLabelTable),
385384
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
386-
387-
// Gitea 1.24.0 ends at database version 321
388-
newMigration(321, "Migrate commit id of pull requests code review comment", v1_25.MigrateCommitIDOfPullRequestCodeReviewComment),
389385
}
390386
return preparedMigrations
391387
}

models/migrations/v1_25/v321.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

services/doctor/review.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)