]> source.dussan.org Git - gitea.git/commitdiff
Fix empty project displayed in issue sidebar (#25802) (#25854)
authorGiteabot <teabot@gitea.io>
Wed, 12 Jul 2023 17:07:03 +0000 (13:07 -0400)
committerGitHub <noreply@github.com>
Wed, 12 Jul 2023 17:07:03 +0000 (19:07 +0200)
Backport #25802 by @yp05327

You can confirm this issue in
https://try.gitea.io/yp05327/testrepo/issues/2

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 #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: yp05327 <576951401@qq.com>
Co-authored-by: Denys Konovalov <privat@denyskon.de>
models/issues/issue_list_test.go
models/issues/issue_project.go
models/issues/issue_test.go
routers/web/org/projects.go
routers/web/repo/projects.go

index 97ce9e43b377734cf04e64756609d04b61d6e277..696c3b765d3d2155262a8eaae0698e5cf6ddd4bf 100644 (file)
@@ -67,9 +67,7 @@ func TestIssueList_LoadAttributes(t *testing.T) {
                if issue.ID == int64(1) {
                        assert.Equal(t, int64(400), issue.TotalTrackedTime)
                        assert.NotNil(t, issue.Project)
-               } else if issue.ID == int64(2) {
-                       assert.Equal(t, int64(3682), issue.TotalTrackedTime)
-                       assert.Nil(t, issue.Project)
+                       assert.Equal(t, int64(1), issue.Project.ID)
                } else {
                        assert.Nil(t, issue.Project)
                }
index b163c683577f0e363f317def6ae235790679fded..782638d997fed91e78e670d046b02d1483fe66e6 100644 (file)
@@ -16,13 +16,14 @@ import (
 func (issue *Issue) LoadProject(ctx context.Context) (err error) {
        if issue.Project == nil {
                var p project_model.Project
-               if _, err = db.GetEngine(ctx).Table("project").
+               has, err := db.GetEngine(ctx).Table("project").
                        Join("INNER", "project_issue", "project.id=project_issue.project_id").
-                       Where("project_issue.issue_id = ?", issue.ID).
-                       Get(&p); err != nil {
+                       Where("project_issue.issue_id = ?", issue.ID).Get(&p)
+               if err != nil {
                        return err
+               } else if has {
+                       issue.Project = &p
                }
-               issue.Project = &p
        }
        return err
 }
index 80699a57b4e86a06ae3b40ec926547a2355b31cb..7f1eab19713787bc011925b9520682f25ccdcad8 100644 (file)
@@ -17,6 +17,7 @@ import (
        repo_model "code.gitea.io/gitea/models/repo"
        "code.gitea.io/gitea/models/unittest"
        user_model "code.gitea.io/gitea/models/user"
+       "code.gitea.io/gitea/modules/setting"
        "code.gitea.io/gitea/modules/util"
 
        "github.com/stretchr/testify/assert"
@@ -539,3 +540,47 @@ func TestCountIssues(t *testing.T) {
        assert.NoError(t, err)
        assert.EqualValues(t, 18, count)
 }
+
+func TestIssueLoadAttributes(t *testing.T) {
+       assert.NoError(t, unittest.PrepareTestDatabase())
+       setting.Service.EnableTimetracking = true
+
+       issueList := issues_model.IssueList{
+               unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 1}),
+               unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 4}),
+       }
+
+       for _, issue := range issueList {
+               assert.NoError(t, issue.LoadAttributes(db.DefaultContext))
+               assert.EqualValues(t, issue.RepoID, issue.Repo.ID)
+               for _, label := range issue.Labels {
+                       assert.EqualValues(t, issue.RepoID, label.RepoID)
+                       unittest.AssertExistsAndLoadBean(t, &issues_model.IssueLabel{IssueID: issue.ID, LabelID: label.ID})
+               }
+               if issue.PosterID > 0 {
+                       assert.EqualValues(t, issue.PosterID, issue.Poster.ID)
+               }
+               if issue.AssigneeID > 0 {
+                       assert.EqualValues(t, issue.AssigneeID, issue.Assignee.ID)
+               }
+               if issue.MilestoneID > 0 {
+                       assert.EqualValues(t, issue.MilestoneID, issue.Milestone.ID)
+               }
+               if issue.IsPull {
+                       assert.EqualValues(t, issue.ID, issue.PullRequest.IssueID)
+               }
+               for _, attachment := range issue.Attachments {
+                       assert.EqualValues(t, issue.ID, attachment.IssueID)
+               }
+               for _, comment := range issue.Comments {
+                       assert.EqualValues(t, issue.ID, comment.IssueID)
+               }
+               if issue.ID == int64(1) {
+                       assert.Equal(t, int64(400), issue.TotalTrackedTime)
+                       assert.NotNil(t, issue.Project)
+                       assert.Equal(t, int64(1), issue.Project.ID)
+               } else {
+                       assert.Nil(t, issue.Project)
+               }
+       }
+}
index be1bb2721d9b4d008b58264249eb66c4227eaec3..6446dee09d6e6c242dcff97c83211c3fd981c156 100644 (file)
@@ -437,8 +437,7 @@ func UpdateIssueProject(ctx *context.Context) {
        projectID := ctx.FormInt64("id")
        for _, issue := range issues {
                if issue.Project != nil {
-                       oldProjectID := issue.Project.ID
-                       if oldProjectID == projectID {
+                       if issue.Project.ID == projectID {
                                continue
                        }
                }
index 6bfb21c134cbddcdf7a638764811b8d1a214e03b..1574c90c02ed00c9d5dab4dc90f4f4e8b6b5dbe7 100644 (file)
@@ -386,8 +386,7 @@ func UpdateIssueProject(ctx *context.Context) {
        projectID := ctx.FormInt64("id")
        for _, issue := range issues {
                if issue.Project != nil {
-                       oldProjectID := issue.Project.ID
-                       if oldProjectID == projectID {
+                       if issue.Project.ID == projectID {
                                continue
                        }
                }