Skip to content

Commit 259e55e

Browse files
committed
fix: return error if issues and prs may be not in sync
1 parent bbbdc38 commit 259e55e

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

models/issues/pull_list.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
user_model "code.gitea.io/gitea/models/user"
1414
"code.gitea.io/gitea/modules/base"
1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/util"
1617

1718
"xorm.io/xorm"
1819
)
@@ -175,7 +176,17 @@ func (prs PullRequestList) loadAttributes(ctx context.Context) error {
175176
}
176177
for _, pr := range prs {
177178
pr.Issue = set[pr.IssueID]
178-
pr.Issue.PullRequest = pr // panic here means issueIDs and prs are not in sync
179+
/*
180+
Old code:
181+
pr.Issue.PullRequest = pr // panic here means issueIDs and prs are not in sync
182+
183+
It's worth panic because it's almost impossible to happen under normal use.
184+
But in integration testing, an asynchronous task could read a database that has been reset.
185+
So returning an error would make more sense, let the caller has a choice to ignore it.
186+
*/
187+
if pr.Issue == nil {
188+
return fmt.Errorf("issues and prs may be not in sync: cannot find issue %v for pr %v: %w", pr.IssueID, pr.ID, util.ErrNotExist)
189+
}
179190
}
180191
return nil
181192
}

tests/integration/pull_update_test.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ func TestAPIPullUpdate(t *testing.T) {
4848
assert.NoError(t, err)
4949
assert.EqualValues(t, 0, diffCount.Behind)
5050
assert.EqualValues(t, 2, diffCount.Ahead)
51-
52-
// Wait for async operations triggered by AddTestPullRequestTask.
53-
// A few seconds is safe enough, but maybe a bit long.
54-
// Considering there are many tests that take tens of seconds, I think that's OK.
55-
time.Sleep(5 * time.Second)
5651
})
5752
}
5853

@@ -81,11 +76,6 @@ func TestAPIPullUpdateByRebase(t *testing.T) {
8176
assert.NoError(t, err)
8277
assert.EqualValues(t, 0, diffCount.Behind)
8378
assert.EqualValues(t, 1, diffCount.Ahead)
84-
85-
// Wait for async operations triggered by AddTestPullRequestTask.
86-
// A few seconds is safe enough, but maybe a bit long.
87-
// Considering there are many tests that take tens of seconds, I think that's OK.
88-
time.Sleep(5 * time.Second)
8979
})
9080
}
9181

0 commit comments

Comments
 (0)