From d5c6036c533ac080ce552a6f5514d61cc003178c Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 21 Oct 2024 00:42:15 +0200 Subject: [PATCH] fix: correct SQL query for active issues - The `OR` should be inside the `AND` condition to not 'bypass' the other conditions. - Added minimal unit test. - Regression from 2675a24649af2fff34f5c7e416d6ff78591d8d9c (cherry picked from commit 0055fdbdc46fc9b23c6a82de4787c408f14e9817) --- models/activities/repo_activity.go | 3 +-- models/activities/repo_activity_test.go | 30 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 models/activities/repo_activity_test.go diff --git a/models/activities/repo_activity.go b/models/activities/repo_activity.go index 3ffad035b7..ffa709ad19 100644 --- a/models/activities/repo_activity.go +++ b/models/activities/repo_activity.go @@ -337,8 +337,7 @@ func newlyCreatedIssues(ctx context.Context, repoID int64, fromTime time.Time) * func activeIssues(ctx context.Context, repoID int64, fromTime time.Time) *xorm.Session { sess := db.GetEngine(ctx).Where("issue.repo_id = ?", repoID). And("issue.is_pull = ?", false). - And("issue.created_unix >= ?", fromTime.Unix()). - Or("issue.closed_unix >= ?", fromTime.Unix()) + And("issue.created_unix >= ? OR issue.closed_unix >= ?", fromTime.Unix(), fromTime.Unix()) return sess } diff --git a/models/activities/repo_activity_test.go b/models/activities/repo_activity_test.go new file mode 100644 index 0000000000..06cd0e1e8a --- /dev/null +++ b/models/activities/repo_activity_test.go @@ -0,0 +1,30 @@ +// Copyright 2024 The Forgejo Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package activities + +import ( + "testing" + "time" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetActivityStats(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) + + stats, err := GetActivityStats(db.DefaultContext, repo, time.Unix(0, 0), true, true, true, true) + require.NoError(t, err) + + assert.EqualValues(t, 2, stats.ActiveIssueCount()) + assert.EqualValues(t, 2, stats.OpenedIssueCount()) + assert.EqualValues(t, 0, stats.ClosedIssueCount()) + assert.EqualValues(t, 3, stats.ActivePRCount()) +}