From dbeef6bb0268460f22ed6158704aa88002bf5e0c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sun, 12 Jan 2020 11:20:49 +0100 Subject: [PATCH] =?UTF-8?q?[BugFix]=20[API]=20=E2=80=8B/repos=E2=80=8B/iss?= =?UTF-8?q?ues=E2=80=8B/search=20(#9698)=20(#9724)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix * fix options * add TEST Co-authored-by: Antoine GIRARD Co-authored-by: Antoine GIRARD --- integrations/api_issue_test.go | 45 ++++++++++++++++++++++++++++++++++ routers/api/v1/repo/issue.go | 28 ++++++++++++--------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/integrations/api_issue_test.go b/integrations/api_issue_test.go index 382fe606b..ce1c4b7d3 100644 --- a/integrations/api_issue_test.go +++ b/integrations/api_issue_test.go @@ -7,6 +7,7 @@ package integrations import ( "fmt" "net/http" + "net/url" "testing" "code.gitea.io/gitea/models" @@ -120,3 +121,47 @@ func TestAPIEditIssue(t *testing.T) { assert.Equal(t, body, issueAfter.Content) assert.Equal(t, title, issueAfter.Title) } + +func TestAPISearchIssue(t *testing.T) { + defer prepareTestEnv(t)() + + session := loginUser(t, "user2") + token := getTokenForLoggedInUser(t, session) + + link, _ := url.Parse("/api/v1/repos/issues/search") + req := NewRequest(t, "GET", link.String()) + resp := session.MakeRequest(t, req, http.StatusOK) + var apiIssues []*api.Issue + DecodeJSON(t, resp, &apiIssues) + + assert.Len(t, apiIssues, 8) + + query := url.Values{} + query.Add("token", token) + link.RawQuery = query.Encode() + req = NewRequest(t, "GET", link.String()) + resp = session.MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &apiIssues) + assert.Len(t, apiIssues, 8) + + query.Add("state", "closed") + link.RawQuery = query.Encode() + req = NewRequest(t, "GET", link.String()) + resp = session.MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &apiIssues) + assert.Len(t, apiIssues, 2) + + query.Set("state", "all") + link.RawQuery = query.Encode() + req = NewRequest(t, "GET", link.String()) + resp = session.MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &apiIssues) + assert.Len(t, apiIssues, 10) //there are more but 10 is page item limit + + query.Add("page", "2") + link.RawQuery = query.Encode() + req = NewRequest(t, "GET", link.String()) + resp = session.MakeRequest(t, req, http.StatusOK) + DecodeJSON(t, resp, &apiIssues) + assert.Len(t, apiIssues, 0) +} diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index ad82d53e7..69b8a3699 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -67,20 +67,24 @@ func SearchIssues(ctx *context.APIContext) { // find repos user can access (for issue search) repoIDs := make([]int64, 0) + opts := &models.SearchRepoOptions{ + PageSize: 15, + Private: false, + AllPublic: true, + TopicOnly: false, + Collaborate: util.OptionalBoolNone, + UserIsAdmin: ctx.IsUserSiteAdmin(), + OrderBy: models.SearchOrderByRecentUpdated, + } + if ctx.IsSigned { + opts.Private = true + opts.AllLimited = true + opts.UserID = ctx.User.ID + } issueCount := 0 for page := 1; ; page++ { - repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{ - Page: page, - PageSize: 15, - Private: true, - Keyword: "", - OwnerID: ctx.User.ID, - TopicOnly: false, - Collaborate: util.OptionalBoolNone, - UserIsAdmin: ctx.IsUserSiteAdmin(), - UserID: ctx.User.ID, - OrderBy: models.SearchOrderByRecentUpdated, - }) + opts.Page = page + repos, count, err := models.SearchRepositoryByName(opts) if err != nil { ctx.Error(http.StatusInternalServerError, "SearchRepositoryByName", err) return