From ccce338f00341ade5e8d2243632f6a5ccb98a067 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jul 2023 05:15:42 +0000 Subject: [PATCH 1/5] fix --- models/issues/issue_project.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/models/issues/issue_project.go b/models/issues/issue_project.go index b163c683577f0..782638d997fed 100644 --- a/models/issues/issue_project.go +++ b/models/issues/issue_project.go @@ -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 } From 50b9574c655bc267f86b6ffd8f0ce02eaf594b0b Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jul 2023 06:28:33 +0000 Subject: [PATCH 2/5] add nil check --- routers/web/org/projects.go | 8 +++++--- routers/web/repo/projects.go | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/routers/web/org/projects.go b/routers/web/org/projects.go index e525f2c43f3ac..c568b1c07164c 100644 --- a/routers/web/org/projects.go +++ b/routers/web/org/projects.go @@ -436,9 +436,11 @@ func UpdateIssueProject(ctx *context.Context) { projectID := ctx.FormInt64("id") for _, issue := range issues { - oldProjectID := issue.Project.ID - if oldProjectID == projectID { - continue + if issue.Project != nil { + oldProjectID := issue.Project.ID + if oldProjectID == projectID { + continue + } } if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil { diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index 6da9edfd0b802..b1033fe0030e9 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -385,9 +385,11 @@ func UpdateIssueProject(ctx *context.Context) { projectID := ctx.FormInt64("id") for _, issue := range issues { - oldProjectID := issue.Project.ID - if oldProjectID == projectID { - continue + if issue.Project != nil { + oldProjectID := issue.Project.ID + if oldProjectID == projectID { + continue + } } if err := issues_model.ChangeProjectAssign(issue, ctx.Doer, projectID); err != nil { From e76a7178ffb5b8a43b3af3db0e2d1bfd04f77537 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jul 2023 06:28:52 +0000 Subject: [PATCH 3/5] improve unit test --- models/issues/issue_list_test.go | 4 +-- models/issues/issue_test.go | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/models/issues/issue_list_test.go b/models/issues/issue_list_test.go index 97ce9e43b3777..696c3b765d3d2 100644 --- a/models/issues/issue_list_test.go +++ b/models/issues/issue_list_test.go @@ -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) } diff --git a/models/issues/issue_test.go b/models/issues/issue_test.go index 80699a57b4e86..7f1eab1971378 100644 --- a/models/issues/issue_test.go +++ b/models/issues/issue_test.go @@ -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) + } + } +} From 6c4eef4882577b0e124af35849175df98c6e487e Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 14:38:10 +0900 Subject: [PATCH 4/5] Update routers/web/org/projects.go Co-authored-by: Denys Konovalov --- routers/web/org/projects.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/routers/web/org/projects.go b/routers/web/org/projects.go index c568b1c07164c..a15e52a48b09f 100644 --- a/routers/web/org/projects.go +++ b/routers/web/org/projects.go @@ -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 } } From 901d3740a6fb675d472c29d71a2dcdd70af80987 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 14:38:27 +0900 Subject: [PATCH 5/5] Update routers/web/repo/projects.go Co-authored-by: Denys Konovalov --- routers/web/repo/projects.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index b1033fe0030e9..0ae658dcbba58 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -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 } }