]> source.dussan.org Git - gitea.git/commitdiff
Update issue indexer after merging a PR (#30715) (#30903)
authorGiteabot <teabot@gitea.io>
Thu, 9 May 2024 05:51:57 +0000 (13:51 +0800)
committerGitHub <noreply@github.com>
Thu, 9 May 2024 05:51:57 +0000 (13:51 +0800)
Backport #30715 by @Zettat123

Fix #30684

Co-authored-by: Zettat123 <zettat123@gmail.com>
services/indexer/notify.go
tests/integration/pull_merge_test.go

index f1e21a2d40ed1587ceafc987264bb85e7bf0dd2f..e2cfe477d3d29decee30ff3f8c4ef214d81dadc3 100644 (file)
@@ -152,3 +152,19 @@ func (r *indexerNotifier) IssueChangeLabels(ctx context.Context, doer *user_mode
 func (r *indexerNotifier) IssueClearLabels(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) {
        issue_indexer.UpdateIssueIndexer(ctx, issue.ID)
 }
+
+func (r *indexerNotifier) MergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
+       if err := pr.LoadIssue(ctx); err != nil {
+               log.Error("LoadIssue: %v", err)
+               return
+       }
+       issue_indexer.UpdateIssueIndexer(ctx, pr.Issue.ID)
+}
+
+func (r *indexerNotifier) AutoMergePullRequest(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
+       if err := pr.LoadIssue(ctx); err != nil {
+               log.Error("LoadIssue: %v", err)
+               return
+       }
+       issue_indexer.UpdateIssueIndexer(ctx, pr.Issue.ID)
+}
index daf411f452a44d73866f0b363158232d2f114dee..826568caf2b4f1bfbc8e2c8fe490e0a8e8454702 100644 (file)
@@ -26,6 +26,7 @@ import (
        "code.gitea.io/gitea/models/webhook"
        "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/gitrepo"
+       "code.gitea.io/gitea/modules/queue"
        api "code.gitea.io/gitea/modules/structs"
        "code.gitea.io/gitea/modules/test"
        "code.gitea.io/gitea/modules/translation"
@@ -587,3 +588,63 @@ func TestPullDontRetargetChildOnWrongRepo(t *testing.T) {
                assert.EqualValues(t, "Closed", prStatus)
        })
 }
+
+func TestPullMergeIndexerNotifier(t *testing.T) {
+       onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
+               // create a pull request
+               session := loginUser(t, "user1")
+               testRepoFork(t, session, "user2", "repo1", "user1", "repo1")
+               testEditFile(t, session, "user1", "repo1", "master", "README.md", "Hello, World (Edited)\n")
+               createPullResp := testPullCreate(t, session, "user1", "repo1", false, "master", "master", "Indexer notifier test pull")
+
+               assert.NoError(t, queue.GetManager().FlushAll(context.Background(), 0))
+               time.Sleep(time.Second)
+
+               repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{
+                       OwnerName: "user2",
+                       Name:      "repo1",
+               })
+               issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{
+                       RepoID:   repo1.ID,
+                       Title:    "Indexer notifier test pull",
+                       IsPull:   true,
+                       IsClosed: false,
+               })
+
+               // build the request for searching issues
+               link, _ := url.Parse("/api/v1/repos/issues/search")
+               query := url.Values{}
+               query.Add("state", "closed")
+               query.Add("type", "pulls")
+               query.Add("q", "notifier")
+               link.RawQuery = query.Encode()
+
+               // search issues
+               searchIssuesResp := session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
+               var apiIssuesBefore []*api.Issue
+               DecodeJSON(t, searchIssuesResp, &apiIssuesBefore)
+               assert.Len(t, apiIssuesBefore, 0)
+
+               // merge the pull request
+               elem := strings.Split(test.RedirectURL(createPullResp), "/")
+               assert.EqualValues(t, "pulls", elem[3])
+               testPullMerge(t, session, elem[1], elem[2], elem[4], repo_model.MergeStyleMerge, false)
+
+               // check if the issue is closed
+               issue = unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{
+                       ID: issue.ID,
+               })
+               assert.True(t, issue.IsClosed)
+
+               assert.NoError(t, queue.GetManager().FlushAll(context.Background(), 0))
+               time.Sleep(time.Second)
+
+               // search issues again
+               searchIssuesResp = session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK)
+               var apiIssuesAfter []*api.Issue
+               DecodeJSON(t, searchIssuesResp, &apiIssuesAfter)
+               if assert.Len(t, apiIssuesAfter, 1) {
+                       assert.Equal(t, issue.ID, apiIssuesAfter[0].ID)
+               }
+       })
+}