fix(indexer): reindex issues when their projects change (#40299)

With the bleve, elasticsearch or meilisearch issue indexer, a keyword
search filtered by project matched issues by the projects they had when
they were last indexed, so newly added issues were missing and removed
ones still showed up.

- Reindex an issue when its projects change, using `IssueChangeProjects`
from https://github.com/go-gitea/gitea/pull/30205.
- Reindex the issues of a deleted project, so the "No project" filter
finds them again.

Entries that are already stale are corrected on the issue's next change
or an index rebuild.

Fixes: https://github.com/go-gitea/gitea/issues/33451

Assisted-by: Claude Code:claude-opus-5-5

---
🤖 Opened with [Superhuman](https://github.com/gaurav0107/superhuman), an
open-source contribution agent.

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Gaurav Dubeyandsilverwind authored and GitHub committed 2026-11-10 19:26:12 +00:00
1 parent a61ad4876a
commit d5ce1cad7f
6 files changed
+52 -19

No files matched your search

+1 -1
View File
@@ -699,7 +699,7 @@ func DeleteProject(ctx *context.APIContext) {
if ctx.Written() {
return
}
if err := project_model.DeleteProjectByID(ctx, project.ID); err != nil {
if err := project_service.DeleteProject(ctx, project.ID); err != nil {
ctx.APIErrorInternal(err)
return
}
+2 -2
View File
@@ -216,8 +216,8 @@ func DeleteProject(ctx *context.Context) {
return
}
if err := project_model.DeleteProjectByID(ctx, p.ID); err != nil {
ctx.Flash.Error("DeleteProjectByID: " + err.Error())
if err := project_service.DeleteProject(ctx, p.ID); err != nil {
ctx.Flash.Error("DeleteProject: " + err.Error())
} else {
audit.Record(ctx, audit_model.ProjectDelete, ctx.ContextUser, "project", p.Title, "project_id", p.ID)
ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success"))
+2 -2
View File
@@ -196,8 +196,8 @@ func DeleteProject(ctx *context.Context) {
return
}
if err := project_model.DeleteProjectByID(ctx, p.ID); err != nil {
ctx.Flash.Error("DeleteProjectByID: " + err.Error())
if err := project_service.DeleteProject(ctx, p.ID); err != nil {
ctx.Flash.Error("DeleteProject: " + err.Error())
} else {
audit.Record(ctx, audit_model.ProjectDelete, ctx.Repo.Repository, "project", p.Title, "project_id", p.ID)
ctx.Flash.Success(ctx.Tr("repo.projects.deletion_success"))
+6
View File
@@ -139,6 +139,12 @@ func (r *indexerNotifier) IssueChangeMilestone(ctx context.Context, doer *user_m
issue_indexer.UpdateIssueIndexer(ctx, issue.ID)
}
func (r *indexerNotifier) IssueChangeProjects(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, addedProjectIDs, removedProjectIDs []int64) {
if len(addedProjectIDs) > 0 || len(removedProjectIDs) > 0 {
issue_indexer.UpdateIssueIndexer(ctx, issue.ID)
}
}
func (r *indexerNotifier) IssueChangeLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue,
addedLabels, removedLabels []*issues_model.Label,
) {
+18
View File
@@ -8,9 +8,27 @@ import (
"gitea.dev/models/db"
project_model "gitea.dev/models/project"
issue_indexer "gitea.dev/modules/indexer/issues"
"gitea.dev/modules/optional"
"xorm.io/builder"
)
// DeleteProject deletes the project and reindexes its issues to drop it from their search entries.
func DeleteProject(ctx context.Context, projectID int64) error {
issueIDs, err := db.FindIDs(ctx, "project_issue", "issue_id", builder.Eq{"project_id": projectID})
if err != nil {
return err
}
if err := project_model.DeleteProjectByID(ctx, projectID); err != nil {
return err
}
for _, issueID := range issueIDs {
issue_indexer.UpdateIssueIndexer(ctx, issueID)
}
return nil
}
// UpdateProjectOptions represents updatable project fields. Fields with no value are left unchanged.
type UpdateProjectOptions struct {
Title optional.Option[string]
+23 -14
View File
@@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"testing"
"time"
"gitea.dev/models/db"
issues_model "gitea.dev/models/issues"
@@ -17,6 +18,7 @@ import (
"gitea.dev/models/unit"
"gitea.dev/models/unittest"
user_model "gitea.dev/models/user"
issue_indexer "gitea.dev/modules/indexer/issues"
project "gitea.dev/services/projects"
"gitea.dev/tests"
@@ -97,20 +99,6 @@ func TestUpdateIssueProject(t *testing.T) {
sess := loginUser(t, "user2")
t.Run("AssignAndRemove", func(t *testing.T) {
req := NewRequestWithValues(t, "POST", "/user2/repo1/issues/projects?issue_ids=2", map[string]string{
"id": "1",
})
sess.MakeRequest(t, req, http.StatusOK)
unittest.AssertExistsAndLoadBean(t, &project_model.ProjectIssue{IssueID: 2, ProjectID: 1})
req = NewRequestWithValues(t, "POST", "/user2/repo1/issues/projects?issue_ids=2", map[string]string{
"id": "",
})
sess.MakeRequest(t, req, http.StatusOK)
unittest.AssertNotExistsBean(t, &project_model.ProjectIssue{IssueID: 2, ProjectID: 1})
})
t.Run("AssignOnCreationNeedsWriter", func(t *testing.T) {
newIssue := func(session *TestSession, title string, status int) {
req := NewRequestWithValues(t, "POST", "/user2/repo1/issues/new", map[string]string{"title": title, "project_ids": "1"})
@@ -122,6 +110,27 @@ func TestUpdateIssueProject(t *testing.T) {
newIssue(loginUser(t, "user4"), "reader issue", http.StatusNotFound)
unittest.AssertNotExistsBean(t, &issues_model.Issue{RepoID: 1, Title: "reader issue"})
})
t.Run("SearchFollowsProjectChanges", func(t *testing.T) {
assertIssue2Found := func(opts issue_indexer.SearchOptions) {
t.Helper()
opts.Keyword, opts.RepoIDs = "second", []int64{1}
assert.EventuallyWithT(t, func(c *assert.CollectT) {
ids, _, err := issue_indexer.SearchIssues(t.Context(), &opts)
assert.NoError(c, err)
assert.Contains(c, ids, int64(2))
}, 10*time.Second, 10*time.Millisecond)
}
sess.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/issues/projects?issue_ids=2", map[string]string{"id": ""}), http.StatusOK)
assertIssue2Found(issue_indexer.SearchOptions{NoProjectOnly: true})
sess.MakeRequest(t, NewRequestWithValues(t, "POST", "/user2/repo1/issues/projects?issue_ids=2", map[string]string{"id": "1"}), http.StatusOK)
assertIssue2Found(issue_indexer.SearchOptions{ProjectIDs: []int64{1}})
sess.MakeRequest(t, NewRequest(t, "POST", "/user2/repo1/projects/1/delete"), http.StatusOK)
assertIssue2Found(issue_indexer.SearchOptions{NoProjectOnly: true})
})
}
func TestUpdateIssueProjectColumn(t *testing.T) {