Skip to content

Commit e873743

Browse files
yp05327denyskon
authored andcommitted
Fix empty project displayed in issue sidebar (go-gitea#25802)
Before: ![image](https://github.com/go-gitea/gitea/assets/18380374/1ab476dc-2f9b-4c85-9e87-105fc73af1ee) After: ![image](https://github.com/go-gitea/gitea/assets/18380374/786f984d-5c27-4eff-b3d9-159f68034ce4) This issue comes from the change in go-gitea#25468. `LoadProject` will always return at least one record, so we use `ProjectID` to check whether an issue is linked to a project in the old code. As other `issue.LoadXXX` functions, we need to check the return value from `xorm.Session.Get`. In recent unit tests, we only test `issueList.LoadAttributes()` but don't test `issue.LoadAttributes()`. So I added a new test for `issue.LoadAttributes()` in this PR. --------- Co-authored-by: Denys Konovalov <[email protected]>
1 parent 353dcc5 commit e873743

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed

models/issues/issue_list_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ func TestIssueList_LoadAttributes(t *testing.T) {
6767
if issue.ID == int64(1) {
6868
assert.Equal(t, int64(400), issue.TotalTrackedTime)
6969
assert.NotNil(t, issue.Project)
70-
} else if issue.ID == int64(2) {
71-
assert.Equal(t, int64(3682), issue.TotalTrackedTime)
72-
assert.Nil(t, issue.Project)
70+
assert.Equal(t, int64(1), issue.Project.ID)
7371
} else {
7472
assert.Nil(t, issue.Project)
7573
}

models/issues/issue_project.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import (
1616
func (issue *Issue) LoadProject(ctx context.Context) (err error) {
1717
if issue.Project == nil {
1818
var p project_model.Project
19-
if _, err = db.GetEngine(ctx).Table("project").
19+
has, err := db.GetEngine(ctx).Table("project").
2020
Join("INNER", "project_issue", "project.id=project_issue.project_id").
21-
Where("project_issue.issue_id = ?", issue.ID).
22-
Get(&p); err != nil {
21+
Where("project_issue.issue_id = ?", issue.ID).Get(&p)
22+
if err != nil {
2323
return err
24+
} else if has {
25+
issue.Project = &p
2426
}
25-
issue.Project = &p
2627
}
2728
return err
2829
}

models/issues/issue_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
repo_model "code.gitea.io/gitea/models/repo"
1818
"code.gitea.io/gitea/models/unittest"
1919
user_model "code.gitea.io/gitea/models/user"
20+
"code.gitea.io/gitea/modules/setting"
2021
"code.gitea.io/gitea/modules/util"
2122

2223
"github.com/stretchr/testify/assert"
@@ -539,3 +540,47 @@ func TestCountIssues(t *testing.T) {
539540
assert.NoError(t, err)
540541
assert.EqualValues(t, 18, count)
541542
}
543+
544+
func TestIssueLoadAttributes(t *testing.T) {
545+
assert.NoError(t, unittest.PrepareTestDatabase())
546+
setting.Service.EnableTimetracking = true
547+
548+
issueList := issues_model.IssueList{
549+
unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}),
550+
unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4}),
551+
}
552+
553+
for _, issue := range issueList {
554+
assert.NoError(t, issue.LoadAttributes(db.DefaultContext))
555+
assert.EqualValues(t, issue.RepoID, issue.Repo.ID)
556+
for _, label := range issue.Labels {
557+
assert.EqualValues(t, issue.RepoID, label.RepoID)
558+
unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
559+
}
560+
if issue.PosterID > 0 {
561+
assert.EqualValues(t, issue.PosterID, issue.Poster.ID)
562+
}
563+
if issue.AssigneeID > 0 {
564+
assert.EqualValues(t, issue.AssigneeID, issue.Assignee.ID)
565+
}
566+
if issue.MilestoneID > 0 {
567+
assert.EqualValues(t, issue.MilestoneID, issue.Milestone.ID)
568+
}
569+
if issue.IsPull {
570+
assert.EqualValues(t, issue.ID, issue.PullRequest.IssueID)
571+
}
572+
for _, attachment := range issue.Attachments {
573+
assert.EqualValues(t, issue.ID, attachment.IssueID)
574+
}
575+
for _, comment := range issue.Comments {
576+
assert.EqualValues(t, issue.ID, comment.IssueID)
577+
}
578+
if issue.ID == int64(1) {
579+
assert.Equal(t, int64(400), issue.TotalTrackedTime)
580+
assert.NotNil(t, issue.Project)
581+
assert.Equal(t, int64(1), issue.Project.ID)
582+
} else {
583+
assert.Nil(t, issue.Project)
584+
}
585+
}
586+
}

routers/web/org/projects.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,7 @@ func UpdateIssueProject(ctx *context.Context) {
437437
projectID := ctx.FormInt64("id")
438438
for _, issue := range issues {
439439
if issue.Project != nil {
440-
oldProjectID := issue.Project.ID
441-
if oldProjectID == projectID {
440+
if issue.Project.ID == projectID {
442441
continue
443442
}
444443
}

routers/web/repo/projects.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ func UpdateIssueProject(ctx *context.Context) {
386386
projectID := ctx.FormInt64("id")
387387
for _, issue := range issues {
388388
if issue.Project != nil {
389-
oldProjectID := issue.Project.ID
390-
if oldProjectID == projectID {
389+
if issue.Project.ID == projectID {
391390
continue
392391
}
393392
}

0 commit comments

Comments
 (0)