From 233653bf89181d3ae91d3c56023365341c1f8a43 Mon Sep 17 00:00:00 2001 From: Ori Hoch Date: Tue, 3 Feb 2026 04:11:30 +0100 Subject: [PATCH] fix searching issues by org labels via api (#11109) fixes #9028 - when searching issues via repo api, org labels are ignored added integration test that reproduces the bug Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/11109 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Gusted Co-authored-by: Ori Hoch Co-committed-by: Ori Hoch --- routers/api/v1/repo/issue.go | 8 ++++++++ tests/integration/api_issue_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index 3c6c1044c6..dcafc54c0e 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -447,6 +447,14 @@ func ListIssues(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err) return } + if ctx.Repo.Owner.IsOrganization() { + orgLabelIDs, err := issues_model.GetLabelIDsInOrgByNames(ctx, ctx.Repo.Owner.ID, split) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetLabelIDsInOrgByNames", err) + return + } + labelIDs = append(labelIDs, orgLabelIDs...) + } } var mileIDs []int64 diff --git a/tests/integration/api_issue_test.go b/tests/integration/api_issue_test.go index 581282223d..44e07b95c8 100644 --- a/tests/integration/api_issue_test.go +++ b/tests/integration/api_issue_test.go @@ -125,6 +125,34 @@ func TestAPIListIssues(t *testing.T) { }) } +func TestAPIListIssuesWithLabels(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3}) + owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID}) + issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 6, RepoID: repo.ID}) + orgLabel := unittest.AssertExistsAndLoadBean(t, &issues_model.Label{ID: 4, OrgID: owner.ID}) + + session := loginUser(t, "user1") + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadIssue, auth_model.AccessTokenScopeWriteIssue) + + addLabelsURL := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/labels", owner.Name, repo.Name, issue.Index) + req := NewRequestWithJSON(t, "POST", addLabelsURL, &api.IssueLabelsOption{Labels: []any{orgLabel.Name}}).AddTokenAuth(token) + MakeRequest(t, req, http.StatusOK) + + link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/issues", owner.Name, repo.Name)) + link.RawQuery = url.Values{"state": {"all"}, "labels": {orgLabel.Name}}.Encode() + + req = NewRequest(t, "GET", link.String()).AddTokenAuth(token) + resp := MakeRequest(t, req, http.StatusOK) + + var apiIssues []*api.Issue + DecodeJSON(t, resp, &apiIssues) + if assert.Len(t, apiIssues, 1) { + assert.Equal(t, issue.ID, apiIssues[0].ID) + } +} + func TestAPIListIssuesPublicOnly(t *testing.T) { defer tests.PrepareTestEnv(t)()