feat(gitlab): filter issues by milestone (#3278)
Co-authored-by: nova28 <17953305+nova28@users.noreply.github.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Carlos Florencio <carlosmflorencio@gmail.com>
This commit is contained in:
@@ -106,8 +106,10 @@ type Client interface {
|
||||
|
||||
// ListIssues searches for open issues. filter is an optional
|
||||
// additional API filter; customQuery, when non-empty, replaces the
|
||||
// entire generated query.
|
||||
ListIssues(ctx context.Context, filter, customQuery string) ([]*Issue, error)
|
||||
// entire generated query. milestone, when non-empty, restricts results
|
||||
// to that exact milestone title (folded into customQuery when both are
|
||||
// set and customQuery doesn't already name a milestone).
|
||||
ListIssues(ctx context.Context, filter, customQuery, milestone string) ([]*Issue, error)
|
||||
|
||||
// SearchMRs searches for MRs matching the given query.
|
||||
SearchMRs(ctx context.Context, filter, customQuery string) ([]*MR, error)
|
||||
@@ -117,7 +119,7 @@ type Client interface {
|
||||
SearchMRsPaged(ctx context.Context, filter, customQuery string, page, perPage int) (*MRSearchPage, error)
|
||||
|
||||
// ListIssuesPaged is the paginated variant of ListIssues.
|
||||
ListIssuesPaged(ctx context.Context, filter, customQuery string, page, perPage int) (*IssueSearchPage, error)
|
||||
ListIssuesPaged(ctx context.Context, filter, customQuery, milestone string, page, perPage int) (*IssueSearchPage, error)
|
||||
|
||||
// GetIssueState returns the state of a single issue ("opened" or "closed").
|
||||
GetIssueState(ctx context.Context, projectPath string, iid int) (string, error)
|
||||
|
||||
@@ -70,16 +70,17 @@ type rawProject struct {
|
||||
}
|
||||
|
||||
type rawIssue struct {
|
||||
ID int64 `json:"id"`
|
||||
IID int `json:"iid"`
|
||||
ProjectID int64 `json:"project_id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
State string `json:"state"`
|
||||
WebURL string `json:"web_url"`
|
||||
Author rawUser `json:"author"`
|
||||
Labels []string `json:"labels"`
|
||||
Assignees []rawUser `json:"assignees"`
|
||||
ID int64 `json:"id"`
|
||||
IID int `json:"iid"`
|
||||
ProjectID int64 `json:"project_id"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
State string `json:"state"`
|
||||
WebURL string `json:"web_url"`
|
||||
Author rawUser `json:"author"`
|
||||
Labels []string `json:"labels"`
|
||||
Assignees []rawUser `json:"assignees"`
|
||||
Milestone *rawMilestone `json:"milestone"`
|
||||
References struct {
|
||||
Full string `json:"full"`
|
||||
} `json:"references"`
|
||||
@@ -88,6 +89,12 @@ type rawIssue struct {
|
||||
ClosedAt *time.Time `json:"closed_at"`
|
||||
}
|
||||
|
||||
// rawMilestone is GitLab's milestone sub-object on an issue. GitLab returns
|
||||
// milestone: null for an issue with no milestone.
|
||||
type rawMilestone struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
type rawDiscussion struct {
|
||||
ID string `json:"id"`
|
||||
IndividualNote bool `json:"individual_note"`
|
||||
@@ -226,6 +233,10 @@ func convertRawIssue(raw *rawIssue) *Issue {
|
||||
assignees = append(assignees, a.Username)
|
||||
}
|
||||
}
|
||||
milestone := ""
|
||||
if raw.Milestone != nil {
|
||||
milestone = raw.Milestone.Title
|
||||
}
|
||||
return &Issue{
|
||||
ID: raw.ID,
|
||||
IID: raw.IID,
|
||||
@@ -240,6 +251,7 @@ func convertRawIssue(raw *rawIssue) *Issue {
|
||||
ProjectPath: projectPath,
|
||||
Labels: append([]string(nil), raw.Labels...),
|
||||
Assignees: assignees,
|
||||
Milestone: milestone,
|
||||
CreatedAt: raw.CreatedAt,
|
||||
UpdatedAt: raw.UpdatedAt,
|
||||
ClosedAt: raw.ClosedAt,
|
||||
@@ -485,9 +497,12 @@ func buildMRSearchQuery(filter, customQuery string) string {
|
||||
return values.Encode()
|
||||
}
|
||||
|
||||
func buildIssueSearchQuery(filter, customQuery string) string {
|
||||
func buildIssueSearchQuery(filter, customQuery, milestone string) string {
|
||||
if customQuery != "" {
|
||||
return customQuery
|
||||
if milestone == "" {
|
||||
return customQuery
|
||||
}
|
||||
return foldMilestoneIntoQuery(customQuery, milestone)
|
||||
}
|
||||
values := url.Values{}
|
||||
values.Set("state", gitlabStateOpened)
|
||||
@@ -495,9 +510,43 @@ func buildIssueSearchQuery(filter, customQuery string) string {
|
||||
if filter != "" {
|
||||
appendFilter(values, filter)
|
||||
}
|
||||
if milestone != "" {
|
||||
values.Set("milestone", milestone)
|
||||
}
|
||||
return values.Encode()
|
||||
}
|
||||
|
||||
// appendQueryParam adds an escaped key/value pair to a raw query unless that
|
||||
// key already exists. URL fragments are kept at the end, outside the query.
|
||||
// This also preserves malformed-query behavior for callers that do not validate
|
||||
// their input at the HTTP boundary.
|
||||
func appendQueryParam(customQuery, key, value string) string {
|
||||
if parsed, err := url.ParseQuery(customQuery); err == nil && parsed.Has(key) {
|
||||
return customQuery
|
||||
}
|
||||
fragment := ""
|
||||
if index := strings.IndexByte(customQuery, '#'); index >= 0 {
|
||||
fragment = customQuery[index:]
|
||||
customQuery = customQuery[:index]
|
||||
}
|
||||
encoded := url.QueryEscape(value)
|
||||
if customQuery == "" {
|
||||
return key + "=" + encoded + fragment
|
||||
}
|
||||
return customQuery + "&" + key + "=" + encoded + fragment
|
||||
}
|
||||
|
||||
// foldMilestoneIntoQuery merges a milestone into an existing, non-empty
|
||||
// customQuery string, mirroring appendLabelsToQuery's precedent: a custom
|
||||
// query that already names the `milestone` key wins (even if the value is
|
||||
// empty), and an unparseable custom query still gets the milestone appended
|
||||
// rather than silently dropping the user's filter selection. The only caller,
|
||||
// buildIssueSearchQuery, guards both arguments non-empty before calling this,
|
||||
// so there is no empty-customQuery case to handle here.
|
||||
func foldMilestoneIntoQuery(customQuery, milestone string) string {
|
||||
return appendQueryParam(customQuery, "milestone", milestone)
|
||||
}
|
||||
|
||||
// filterTokenReviewRequested is the /gitlab page tab value that maps to
|
||||
// GitLab's reviewer_username param. Defined as a constant because it appears
|
||||
// in multiple spots across the package (translator, MR controller, issues
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildIssueSearchQuery_PresetPathComposesMilestoneAfterFilter(t *testing.T) {
|
||||
got := buildIssueSearchQuery("scope=assigned_to_me", "", "Next")
|
||||
values, err := url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("scope"); got != "assigned_to_me" {
|
||||
t.Errorf("scope = %q, want assigned_to_me (milestone must not clobber the preset)", got)
|
||||
}
|
||||
if got := values.Get("state"); got != "opened" {
|
||||
t.Errorf("state = %q, want opened", got)
|
||||
}
|
||||
if got := values.Get("milestone"); got != "Next" {
|
||||
t.Errorf("milestone = %q, want Next", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_EmptyMilestoneIsByteIdenticalToBeforeFeature(t *testing.T) {
|
||||
got := buildIssueSearchQuery("scope=assigned_to_me", "", "")
|
||||
want := buildIssueSearchQueryWithoutMilestoneForTest("scope=assigned_to_me", "")
|
||||
if got != want {
|
||||
t.Errorf("query = %q, want %q (empty milestone must add nothing)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// buildIssueSearchQueryWithoutMilestoneForTest reproduces the pre-feature
|
||||
// query builder so the byte-identical claim can be checked without a second
|
||||
// production code path.
|
||||
func buildIssueSearchQueryWithoutMilestoneForTest(filter, customQuery string) string {
|
||||
if customQuery != "" {
|
||||
return customQuery
|
||||
}
|
||||
values := url.Values{}
|
||||
values.Set("state", gitlabStateOpened)
|
||||
values.Set("scope", "all")
|
||||
if filter != "" {
|
||||
appendFilter(values, filter)
|
||||
}
|
||||
return values.Encode()
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_CustomQueryWithMilestoneWins(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "state=closed&milestone=Old", "Next")
|
||||
if got != "state=closed&milestone=Old" {
|
||||
t.Errorf("query = %q, want custom query unchanged (custom milestone wins)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario 31, live-path clause: a custom query carrying a repeated
|
||||
// milestone key is forwarded upstream unchanged, both pairs included — the
|
||||
// live path never resolves which one wins, only the mock does (Get,
|
||||
// first-value).
|
||||
func TestBuildIssueSearchQuery_CustomQueryWithRepeatedMilestoneKeyForwardedUnchanged(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "milestone=Old&milestone=Next", "Next")
|
||||
want := "milestone=Old&milestone=Next"
|
||||
if got != want {
|
||||
t.Errorf("query = %q, want %q (both pairs forwarded verbatim)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_CustomQueryWithoutMilestoneIsExtended(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "state=closed", "Next")
|
||||
values, err := url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("state"); got != "closed" {
|
||||
t.Errorf("state = %q, want closed", got)
|
||||
}
|
||||
if got := values.Get("milestone"); got != "Next" {
|
||||
t.Errorf("milestone = %q, want Next", got)
|
||||
}
|
||||
if values.Has("scope") {
|
||||
t.Errorf("scope present in %q, want absent (custom query defaults are not reintroduced)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_CustomQueryFragmentKeepsMilestoneInQuery(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "state=closed#client-fragment", "Next")
|
||||
want := "state=closed&milestone=Next#client-fragment"
|
||||
if got != want {
|
||||
t.Errorf("query = %q, want %q (milestone must precede the URL fragment)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// The HTTP controller rejects malformed custom queries before this helper is
|
||||
// called. Keep this builder-level case to pin its defensive append behavior
|
||||
// for other callers that construct the query directly.
|
||||
func TestBuildIssueSearchQuery_MalformedCustomQueryAppendsMilestone(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "%zz", "Next")
|
||||
want := "%zz&milestone=Next"
|
||||
if got != want {
|
||||
t.Errorf("query = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_EmptyMilestoneKeyInCustomQueryStillWins(t *testing.T) {
|
||||
got := buildIssueSearchQuery("", "state=closed&milestone=", "Next")
|
||||
if got != "state=closed&milestone=" {
|
||||
t.Errorf("query = %q, want custom query unchanged (empty milestone key still wins)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_MalformedFilterCannotTakeMilestoneDownWithIt(t *testing.T) {
|
||||
got := buildIssueSearchQuery("%zz", "", "Next")
|
||||
values, err := url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("milestone"); got != "Next" {
|
||||
t.Errorf("milestone = %q, want Next (unparseable filter must not drop it)", got)
|
||||
}
|
||||
if got := values.Get("state"); got != "opened" {
|
||||
t.Errorf("state = %q, want opened", got)
|
||||
}
|
||||
if got := values.Get("scope"); got != "all" {
|
||||
t.Errorf("scope = %q, want all", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildIssueSearchQuery_MilestoneEscapedExactlyOnce(t *testing.T) {
|
||||
// Preset path: values.Encode() does the single escape.
|
||||
got := buildIssueSearchQuery("", "", "Q3 & Q4=x")
|
||||
values, err := url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("milestone"); got != "Q3 & Q4=x" {
|
||||
t.Errorf("milestone = %q, want %q", got, "Q3 & Q4=x")
|
||||
}
|
||||
if len(values) != 3 {
|
||||
t.Errorf("values = %v, want exactly state/scope/milestone (no injected key)", values)
|
||||
}
|
||||
|
||||
// Escape-hatch path: url.QueryEscape does the single escape.
|
||||
got = buildIssueSearchQuery("", "state=closed", "Q3 & Q4=x")
|
||||
values, err = url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("milestone"); got != "Q3 & Q4=x" {
|
||||
t.Errorf("milestone = %q, want %q", got, "Q3 & Q4=x")
|
||||
}
|
||||
if len(values) != 2 {
|
||||
t.Errorf("values = %v, want exactly state/milestone (no injected key)", values)
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario 22: GitLab's predefined milestone values (None, Any, Upcoming)
|
||||
// are forwarded verbatim on "milestone" — kandev applies no special
|
||||
// handling and never translates any of them onto "milestone_id".
|
||||
func TestBuildIssueSearchQuery_PredefinedMilestoneValuesPassThroughUntranslated(t *testing.T) {
|
||||
for _, value := range []string{"None", "Any", "Upcoming"} {
|
||||
got := buildIssueSearchQuery("", "", value)
|
||||
values, err := url.ParseQuery(got)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseQuery(%q): %v", got, err)
|
||||
}
|
||||
if got := values.Get("milestone"); got != value {
|
||||
t.Errorf("milestone = %q, want %q", got, value)
|
||||
}
|
||||
if values.Has("milestone_id") {
|
||||
t.Errorf("query %q carries milestone_id, want it absent for value %q", got, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertRawIssue_MilestoneTitle(t *testing.T) {
|
||||
raw := &rawIssue{Milestone: &rawMilestone{Title: "Next"}}
|
||||
issue := convertRawIssue(raw)
|
||||
if issue.Milestone != "Next" {
|
||||
t.Errorf("Milestone = %q, want Next", issue.Milestone)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertRawIssue_NullMilestoneDecodesToEmptyString(t *testing.T) {
|
||||
raw := &rawIssue{Milestone: nil}
|
||||
issue := convertRawIssue(raw)
|
||||
if issue.Milestone != "" {
|
||||
t.Errorf("Milestone = %q, want empty for a null milestone", issue.Milestone)
|
||||
}
|
||||
}
|
||||
|
||||
// trimGitLabWhitespace is the Go-side normative trim helper: Unicode
|
||||
// White_Space together with U+FEFF. strings.TrimSpace already covers the
|
||||
// Unicode White_Space set, including U+0085, but it does not cover U+FEFF.
|
||||
// Scenario 27.
|
||||
func TestTrimGitLabWhitespace_MatchesNormativeSet(t *testing.T) {
|
||||
trimmed := []rune{
|
||||
0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x0020, 0x0085, 0x00A0,
|
||||
0x1680, 0x2000, 0x2028, 0x2029, 0x202F, 0x205F, 0x3000, 0xFEFF,
|
||||
}
|
||||
for _, r := range trimmed {
|
||||
got := trimGitLabWhitespace(string(r))
|
||||
if got != "" {
|
||||
t.Errorf("trimGitLabWhitespace(%U) = %q, want empty", r, got)
|
||||
}
|
||||
}
|
||||
|
||||
preserved := []rune{0x200B, 0x180E, 0x2060, 0x00B7}
|
||||
for _, r := range preserved {
|
||||
got := trimGitLabWhitespace(string(r))
|
||||
if got != string(r) {
|
||||
t.Errorf("trimGitLabWhitespace(%U) = %q, want unchanged", r, got)
|
||||
}
|
||||
}
|
||||
|
||||
nelPrefixed := string(rune(0x0085)) + "Next" + string(rune(0xFEFF))
|
||||
if got := trimGitLabWhitespace(nelPrefixed); got != "Next" {
|
||||
t.Errorf("trimGitLabWhitespace(NEL+Next+BOM) = %q, want Next", got)
|
||||
}
|
||||
if got := trimGitLabWhitespace(" Q1 Planning "); got != "Q1 Planning" {
|
||||
t.Errorf("trimGitLabWhitespace(padded) = %q, want interior space preserved", got)
|
||||
}
|
||||
|
||||
// The helper adds the one normative rune that TrimSpace does not trim.
|
||||
bomOnly := string(rune(0xFEFF))
|
||||
if got := trimGitLabWhitespace(bomOnly); got != "" {
|
||||
t.Errorf("trimGitLabWhitespace(BOM alone) = %q, want empty (TrimSpace keeps U+FEFF)", got)
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,10 @@ package gitlab
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -284,6 +287,12 @@ func (c *Controller) httpSearchUserIssues(ctx *gin.Context) {
|
||||
page, perPage := paginationFromQuery(ctx)
|
||||
filter := ctx.Query("filter")
|
||||
customQuery := ctx.Query("custom_query")
|
||||
if customQuery != "" {
|
||||
if _, err := url.ParseQuery(customQuery); err != nil {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{responseErrorKey: "custom_query must be a valid URL query"})
|
||||
return
|
||||
}
|
||||
}
|
||||
if customQuery == "" {
|
||||
if filter == filterTokenReviewRequested {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{responseErrorKey: "review_requested is not supported for issues"})
|
||||
@@ -297,8 +306,9 @@ func (c *Controller) httpSearchUserIssues(ctx *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
milestone := trimGitLabWhitespace(ctx.Query("milestone"))
|
||||
result, err := client.ListIssuesPaged(
|
||||
ctx.Request.Context(), filter, customQuery, page, perPage,
|
||||
ctx.Request.Context(), filter, customQuery, milestone, page, perPage,
|
||||
)
|
||||
if err != nil {
|
||||
writeWorkspaceClientActionError(ctx, err, "issue search")
|
||||
@@ -337,6 +347,16 @@ func (c *Controller) translateMRFilter(ctx *gin.Context, filter string) (string,
|
||||
return translateUserSearchFilter(filter, username), nil
|
||||
}
|
||||
|
||||
// trimGitLabWhitespace trims Unicode White_Space plus U+FEFF (byte order
|
||||
// mark). strings.TrimSpace already covers Unicode White_Space, including
|
||||
// U+0085 (NEL), but it does not trim U+FEFF. The extra rune keeps a pasted BOM
|
||||
// from surviving as a non-empty milestone that cannot match.
|
||||
func trimGitLabWhitespace(s string) string {
|
||||
return strings.TrimFunc(s, func(r rune) bool {
|
||||
return unicode.IsSpace(r) || r == '\uFEFF'
|
||||
})
|
||||
}
|
||||
|
||||
// paginationFromQuery reads ?page=&per_page= with the same clamps SearchMRsPaged
|
||||
// applies internally — surfaced here so 400s on bad input are uniform.
|
||||
func paginationFromQuery(ctx *gin.Context) (int, int) {
|
||||
|
||||
@@ -403,3 +403,65 @@ func TestHttpSearchUserIssues_TranslatesTabFilters(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// httpSearchUserIssues wiring is otherwise only unit-tested one layer down
|
||||
// (buildIssueSearchQuery, trimGitLabWhitespace); nothing previously asserted
|
||||
// end to end that the ?milestone= query key is actually read off the
|
||||
// request and reaches the outbound GitLab call. A typo in the query key
|
||||
// name would have passed every other test in this package.
|
||||
func TestHttpSearchUserIssues_MilestoneReachesUpstreamRequest(t *testing.T) {
|
||||
router, rec, stop := newControllerFixture(t, "alice")
|
||||
defer stop()
|
||||
|
||||
resp := hit(router, "/api/v1/gitlab/user/issues?filter=assigned_to_me&milestone="+url.QueryEscape("Next"))
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", resp.Code, resp.Body.String())
|
||||
}
|
||||
req := rec.findByPath("/api/v4/issues")
|
||||
if req == nil {
|
||||
t.Fatal("/api/v4/issues was never called")
|
||||
}
|
||||
if got := req.Query.Get("milestone"); got != "Next" {
|
||||
t.Errorf("milestone = %q, want Next", got)
|
||||
}
|
||||
if got := req.Query.Get("scope"); got != "assigned_to_me" {
|
||||
t.Errorf("scope = %q, want assigned_to_me (milestone must not clobber the preset)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A whitespace-only milestone (Scenario 3's second boundary) trims to "" and
|
||||
// must produce a request identical to omitting the parameter entirely — no
|
||||
// milestone key on the wire at all.
|
||||
func TestHttpSearchUserIssues_WhitespaceOnlyMilestoneOmitsKey(t *testing.T) {
|
||||
router, rec, stop := newControllerFixture(t, "alice")
|
||||
defer stop()
|
||||
|
||||
resp := hit(router, "/api/v1/gitlab/user/issues?filter=assigned_to_me&milestone="+url.QueryEscape(" "))
|
||||
if resp.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body = %s", resp.Code, resp.Body.String())
|
||||
}
|
||||
req := rec.findByPath("/api/v4/issues")
|
||||
if req == nil {
|
||||
t.Fatal("/api/v4/issues was never called")
|
||||
}
|
||||
if req.Query.Has("milestone") {
|
||||
t.Errorf("query = %v, want no milestone key for a whitespace-only value", req.Query)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHttpSearchUserIssues_MalformedCustomQueryReturns400(t *testing.T) {
|
||||
router, rec, stop := newControllerFixture(t, "alice")
|
||||
defer stop()
|
||||
|
||||
resp := hit(
|
||||
router,
|
||||
"/api/v1/gitlab/user/issues?custom_query="+url.QueryEscape("%zz")+
|
||||
"&milestone="+url.QueryEscape("Next"),
|
||||
)
|
||||
if resp.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want 400 (body: %s)", resp.Code, resp.Body.String())
|
||||
}
|
||||
if req := rec.findByPath("/api/v4/issues"); req != nil {
|
||||
t.Errorf("/api/v4/issues was called with query %v — malformed custom query should be rejected", req.Query)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -559,21 +559,6 @@ func (c *MockClient) GetRepoFileContent(
|
||||
return content, nil
|
||||
}
|
||||
|
||||
func (c *MockClient) ListIssues(context.Context, string, string) ([]*Issue, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
out := []*Issue{}
|
||||
for _, i := range c.issues {
|
||||
out = append(out, i)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *MockClient) ListIssuesPaged(ctx context.Context, filter, customQuery string, page, perPage int) (*IssueSearchPage, error) {
|
||||
issues, _ := c.ListIssues(ctx, filter, customQuery)
|
||||
return &IssueSearchPage{Issues: issues, TotalCount: len(issues), Page: page, PerPage: perPage}, nil
|
||||
}
|
||||
|
||||
func (c *MockClient) SearchMRs(context.Context, string, string) ([]*MR, error) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// resolveEffectiveMilestone derives the milestone MockClient.ListIssues
|
||||
// filters on from the three raw search inputs the controller passes through
|
||||
// unmodified — MockClient never calls buildIssueSearchQuery, so it must
|
||||
// reproduce the fold precedence itself. The first matching rule wins:
|
||||
//
|
||||
// 1. customQuery carries a "milestone" key: its first value, even if empty
|
||||
// (an empty key still wins outright — it must not fall through to the
|
||||
// milestone parameter, matching how an empty fold reaches GitLab as an
|
||||
// empty milestone on the live path).
|
||||
// 2. the milestone parameter, if non-empty.
|
||||
// 3. filter carries a non-empty "milestone" key: its first value.
|
||||
// 4. otherwise, empty.
|
||||
func resolveEffectiveMilestone(filter, customQuery, milestone string) string {
|
||||
if parsed, err := url.ParseQuery(customQuery); err == nil && parsed.Has("milestone") {
|
||||
return parsed.Get("milestone")
|
||||
}
|
||||
if milestone != "" {
|
||||
return milestone
|
||||
}
|
||||
if parsed, err := url.ParseQuery(filter); err == nil {
|
||||
if v := parsed.Get("milestone"); v != "" {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ListIssues returns seeded issues, optionally narrowed to an exact
|
||||
// case-sensitive milestone match. See resolveEffectiveMilestone for how
|
||||
// filter/customQuery/milestone combine. Every other filter key is ignored —
|
||||
// PATClient is the only client that talks to real GitLab filtering.
|
||||
func (c *MockClient) ListIssues(_ context.Context, filter, customQuery, milestone string) ([]*Issue, error) {
|
||||
effective := resolveEffectiveMilestone(filter, customQuery, milestone)
|
||||
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
out := []*Issue{}
|
||||
for _, i := range c.issues {
|
||||
if effective != "" && i.Milestone != effective {
|
||||
continue
|
||||
}
|
||||
out = append(out, i)
|
||||
}
|
||||
sort.Slice(out, func(a, b int) bool {
|
||||
if out[a].ProjectPath != out[b].ProjectPath {
|
||||
return out[a].ProjectPath < out[b].ProjectPath
|
||||
}
|
||||
return out[a].IID < out[b].IID
|
||||
})
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ListIssuesPaged pages over ListIssues' filtered, sorted result. page < 1 is
|
||||
// normalised to 1; perPage < 1 normalises to 0 (no issues, any page). There
|
||||
// is no upper clamp on perPage: the echoed Page/PerPage are the normalised
|
||||
// values actually used, from those two rules only. TotalCount is always the
|
||||
// full filtered count, not the length of the returned slice.
|
||||
func (c *MockClient) ListIssuesPaged(
|
||||
ctx context.Context, filter, customQuery, milestone string, page, perPage int,
|
||||
) (*IssueSearchPage, error) {
|
||||
issues, err := c.ListIssues(ctx, filter, customQuery, milestone)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normPage := page
|
||||
if normPage < 1 {
|
||||
normPage = 1
|
||||
}
|
||||
normPerPage := perPage
|
||||
if normPerPage < 1 {
|
||||
normPerPage = 0
|
||||
}
|
||||
|
||||
// Guard the multiplication itself: normPage is not bounded above, so
|
||||
// (normPage-1)*normPerPage can overflow int before the "start >
|
||||
// len(issues)" clamp below ever runs. Comparing via division first is
|
||||
// safe from overflow and gives the same answer.
|
||||
var start int
|
||||
if normPerPage == 0 || normPage-1 > len(issues)/normPerPage {
|
||||
start = len(issues)
|
||||
} else {
|
||||
start = (normPage - 1) * normPerPage
|
||||
}
|
||||
|
||||
// Guard start+normPerPage the same way: normPerPage is unbounded above
|
||||
// (no clamp), so the addition can itself overflow int. Comparing against
|
||||
// the remaining count first avoids ever computing the sum when it would
|
||||
// exceed len(issues).
|
||||
end := len(issues)
|
||||
if normPerPage < len(issues)-start {
|
||||
end = start + normPerPage
|
||||
}
|
||||
|
||||
return &IssueSearchPage{
|
||||
Issues: issues[start:end],
|
||||
TotalCount: len(issues),
|
||||
Page: normPage,
|
||||
PerPage: normPerPage,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func seedMilestoneIssues(t *testing.T, c *MockClient) {
|
||||
t.Helper()
|
||||
c.SeedIssue("acme/beta", &Issue{IID: 1, ProjectPath: "acme/beta", Title: "beta-1", Milestone: "Next"})
|
||||
c.SeedIssue("acme/alpha", &Issue{IID: 2, ProjectPath: "acme/alpha", Title: "alpha-2", Milestone: "Old"})
|
||||
c.SeedIssue("acme/alpha", &Issue{IID: 1, ProjectPath: "acme/alpha", Title: "alpha-1", Milestone: "Next"})
|
||||
c.SeedIssue("acme/alpha", &Issue{IID: 3, ProjectPath: "acme/alpha", Title: "alpha-3", Milestone: ""})
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_EmptyMilestoneReturnsEverything(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("len(got) = %d, want 4", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_ParameterMilestoneFiltersExact(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "", "", "Next")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 2 {
|
||||
t.Fatalf("len(got) = %d, want 2: %#v", len(got), got)
|
||||
}
|
||||
// Deterministic order: ProjectPath asc, then IID asc.
|
||||
if got[0].ProjectPath != "acme/alpha" || got[0].IID != 1 {
|
||||
t.Errorf("got[0] = %+v, want acme/alpha#1", got[0])
|
||||
}
|
||||
if got[1].ProjectPath != "acme/beta" || got[1].IID != 1 {
|
||||
t.Errorf("got[1] = %+v, want acme/beta#1", got[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_DeterministicOrderAcrossAllIssues(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
want := []struct {
|
||||
Project string
|
||||
IID int
|
||||
}{
|
||||
{"acme/alpha", 1}, {"acme/alpha", 2}, {"acme/alpha", 3}, {"acme/beta", 1},
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("len(got) = %d, want %d", len(got), len(want))
|
||||
}
|
||||
for i, w := range want {
|
||||
if got[i].ProjectPath != w.Project || got[i].IID != w.IID {
|
||||
t.Errorf("got[%d] = %s#%d, want %s#%d", i, got[i].ProjectPath, got[i].IID, w.Project, w.IID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_CustomQueryMilestoneWinsOverParameter(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "", "milestone=Old", "Next")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Milestone != "Old" {
|
||||
t.Fatalf("got = %#v, want exactly one Old-milestone issue", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_CustomQueryEmptyMilestoneKeySuppressesParameterFallback(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
// Rule 1: an empty milestone= key in customQuery wins outright (does not
|
||||
// fall through to the milestone parameter), matching the live-path fold.
|
||||
got, err := c.ListIssues(t.Context(), "", "milestone=", "Next")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("len(got) = %d, want 4 (empty milestone key means unfiltered)", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_HandwrittenCustomQueryUsesFirstMilestoneValue(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "", "milestone=Old&milestone=Next", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Milestone != "Old" {
|
||||
t.Fatalf("got = %#v, want exactly one Old-milestone issue (first value wins)", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_FilterMilestoneFallbackWhenNonEmpty(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "milestone=Old", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 1 || got[0].Milestone != "Old" {
|
||||
t.Fatalf("got = %#v, want exactly one Old-milestone issue", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssues_FilterMilestoneEmptyValueIsIgnored(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
got, err := c.ListIssues(t.Context(), "milestone=", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
if len(got) != 4 {
|
||||
t.Fatalf("len(got) = %d, want 4 (empty filter milestone value is not adopted)", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssuesPaged_PagesOverFilteredSortedResult(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "", 1, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4", page.TotalCount)
|
||||
}
|
||||
if len(page.Issues) != 2 || page.Issues[0].IID != 1 || page.Issues[1].IID != 2 {
|
||||
t.Fatalf("page 1 issues = %#v, want alpha#1, alpha#2", page.Issues)
|
||||
}
|
||||
|
||||
page2, err := c.ListIssuesPaged(t.Context(), "", "", "", 2, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page2.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4", page2.TotalCount)
|
||||
}
|
||||
if len(page2.Issues) != 2 || page2.Issues[0].IID != 3 || page2.Issues[1].ProjectPath != "acme/beta" {
|
||||
t.Fatalf("page 2 issues = %#v, want alpha#3, beta#1", page2.Issues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssuesPaged_PageBeyondEndYieldsEmptyWithSameTotal(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "", 5, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4", page.TotalCount)
|
||||
}
|
||||
if len(page.Issues) != 0 {
|
||||
t.Errorf("len(Issues) = %d, want 0", len(page.Issues))
|
||||
}
|
||||
if page.Page != 5 || page.PerPage != 2 {
|
||||
t.Errorf("Page/PerPage = %d/%d, want 5/2", page.Page, page.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
// Scenario 16: 30 seeded issues, 26 in milestone "Next", per_page=25 pages
|
||||
// the filtered set (not the unfiltered 30), with TotalCount staying at the
|
||||
// filtered count on every page including the beyond-end one.
|
||||
func TestMockClient_ListIssuesPaged_PagesOverFilteredMilestoneSetAcrossMultiplePages(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
for i := 1; i <= 26; i++ {
|
||||
c.SeedIssue("acme/repo", &Issue{IID: i, ProjectPath: "acme/repo", Milestone: "Next"})
|
||||
}
|
||||
for i := 27; i <= 30; i++ {
|
||||
c.SeedIssue("acme/repo", &Issue{IID: i, ProjectPath: "acme/repo", Milestone: "Old"})
|
||||
}
|
||||
|
||||
page1, err := c.ListIssuesPaged(t.Context(), "", "", "Next", 1, 25)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page1.TotalCount != 26 {
|
||||
t.Errorf("page1 TotalCount = %d, want 26", page1.TotalCount)
|
||||
}
|
||||
if len(page1.Issues) != 25 {
|
||||
t.Errorf("page1 len(Issues) = %d, want 25", len(page1.Issues))
|
||||
}
|
||||
|
||||
page2, err := c.ListIssuesPaged(t.Context(), "", "", "Next", 2, 25)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page2.TotalCount != 26 {
|
||||
t.Errorf("page2 TotalCount = %d, want 26", page2.TotalCount)
|
||||
}
|
||||
if len(page2.Issues) != 1 {
|
||||
t.Errorf("page2 len(Issues) = %d, want 1", len(page2.Issues))
|
||||
}
|
||||
|
||||
page3, err := c.ListIssuesPaged(t.Context(), "", "", "Next", 3, 25)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page3.TotalCount != 26 {
|
||||
t.Errorf("page3 TotalCount = %d, want 26", page3.TotalCount)
|
||||
}
|
||||
if len(page3.Issues) != 0 {
|
||||
t.Errorf("page3 len(Issues) = %d, want 0", len(page3.Issues))
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssuesPaged_NegativePageNormalisedToOne(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "", -3, 2)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page.Page != 1 {
|
||||
t.Errorf("Page = %d, want normalised to 1", page.Page)
|
||||
}
|
||||
if len(page.Issues) != 2 || page.Issues[0].IID != 1 {
|
||||
t.Fatalf("issues = %#v, want the first page's worth", page.Issues)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssuesPaged_NonPositivePerPageReturnsNoIssues(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if len(page.Issues) != 0 {
|
||||
t.Errorf("len(Issues) = %d, want 0", len(page.Issues))
|
||||
}
|
||||
if page.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4 (full filtered count, even with no results)", page.TotalCount)
|
||||
}
|
||||
if page.PerPage != 0 {
|
||||
t.Errorf("PerPage = %d, want normalised to 0", page.PerPage)
|
||||
}
|
||||
|
||||
negative, err := c.ListIssuesPaged(t.Context(), "", "", "", 1, -5)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if len(negative.Issues) != 0 {
|
||||
t.Errorf("len(Issues) = %d, want 0", len(negative.Issues))
|
||||
}
|
||||
if negative.PerPage != 0 {
|
||||
t.Errorf("PerPage = %d, want normalised to 0", negative.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
// The HTTP layer's paginationFromQuery only floors page/perPage, it never
|
||||
// caps the upper bound (unlike PATClient's clampSearchPage), so an
|
||||
// out-of-range per_page can reach the mock directly. ListIssuesPaged must
|
||||
// not overflow its start/end arithmetic and panic on the resulting slice.
|
||||
// Spec (spec.md:736-747) authorizes only two normalisations — page < 1 and
|
||||
// perPage < 1 — so an extreme-but-positive perPage is echoed back as given,
|
||||
// not clamped to a page size the spec never mentions.
|
||||
func TestMockClient_ListIssuesPaged_ExtremePerPageDoesNotOverflowOrPanic(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
first, err := c.ListIssuesPaged(t.Context(), "", "", "", 1, math.MaxInt)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if len(first.Issues) != 4 {
|
||||
t.Errorf("len(Issues) = %d, want all 4 on page 1", len(first.Issues))
|
||||
}
|
||||
if first.PerPage != math.MaxInt {
|
||||
t.Errorf("PerPage = %d, want echoed back as math.MaxInt, not clamped", first.PerPage)
|
||||
}
|
||||
if first.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4", first.TotalCount)
|
||||
}
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "", 2, math.MaxInt)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page.TotalCount != 4 {
|
||||
t.Errorf("TotalCount = %d, want 4", page.TotalCount)
|
||||
}
|
||||
if len(page.Issues) != 0 {
|
||||
t.Errorf("len(Issues) = %d, want 0 (page 2 is beyond the end of the unfiltered set)", len(page.Issues))
|
||||
}
|
||||
if page.PerPage != math.MaxInt {
|
||||
t.Errorf("PerPage = %d, want echoed back as math.MaxInt, not clamped", page.PerPage)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMockClient_ListIssuesPaged_ForwardsMilestoneWithoutResolvingTwice(t *testing.T) {
|
||||
c := NewMockClient(DefaultHost)
|
||||
seedMilestoneIssues(t, c)
|
||||
|
||||
page, err := c.ListIssuesPaged(t.Context(), "", "", "Next", 1, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssuesPaged() error = %v", err)
|
||||
}
|
||||
if page.TotalCount != 2 {
|
||||
t.Errorf("TotalCount = %d, want 2", page.TotalCount)
|
||||
}
|
||||
for _, issue := range page.Issues {
|
||||
if issue.Milestone != "Next" {
|
||||
t.Errorf("issue %+v has milestone %q, want Next", issue, issue.Milestone)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -349,6 +349,7 @@ type Issue struct {
|
||||
ProjectPath string `json:"project_path"`
|
||||
Labels []string `json:"labels"`
|
||||
Assignees []string `json:"assignees"`
|
||||
Milestone string `json:"milestone,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ClosedAt *time.Time `json:"closed_at,omitempty"`
|
||||
|
||||
@@ -123,11 +123,11 @@ func (c *NoopClient) GetRepoFileContent(context.Context, string, string, string)
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
func (c *NoopClient) ListIssues(context.Context, string, string) ([]*Issue, error) {
|
||||
func (c *NoopClient) ListIssues(context.Context, string, string, string) ([]*Issue, error) {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
func (c *NoopClient) ListIssuesPaged(context.Context, string, string, int, int) (*IssueSearchPage, error) {
|
||||
func (c *NoopClient) ListIssuesPaged(context.Context, string, string, string, int, int) (*IssueSearchPage, error) {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
|
||||
|
||||
@@ -262,17 +262,17 @@ func (c *PATClient) SearchMRsPaged(ctx context.Context, filter, customQuery stri
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *PATClient) ListIssues(ctx context.Context, filter, customQuery string) ([]*Issue, error) {
|
||||
page, err := c.ListIssuesPaged(ctx, filter, customQuery, defaultPageStart, defaultPageSize)
|
||||
func (c *PATClient) ListIssues(ctx context.Context, filter, customQuery, milestone string) ([]*Issue, error) {
|
||||
page, err := c.ListIssuesPaged(ctx, filter, customQuery, milestone, defaultPageStart, defaultPageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return page.Issues, nil
|
||||
}
|
||||
|
||||
func (c *PATClient) ListIssuesPaged(ctx context.Context, filter, customQuery string, page, perPage int) (*IssueSearchPage, error) {
|
||||
func (c *PATClient) ListIssuesPaged(ctx context.Context, filter, customQuery, milestone string, page, perPage int) (*IssueSearchPage, error) {
|
||||
page, perPage = clampSearchPage(page, perPage)
|
||||
query := buildIssueSearchQuery(filter, customQuery)
|
||||
query := buildIssueSearchQuery(filter, customQuery, milestone)
|
||||
endpoint := fmt.Sprintf("/issues?%s&page=%d&per_page=%d", query, page, perPage)
|
||||
var raw []rawIssue
|
||||
total, err := c.getWithTotal(ctx, endpoint, &raw)
|
||||
|
||||
@@ -54,7 +54,7 @@ func TestPATClientListIssuesUsesDefaultPagination(t *testing.T) {
|
||||
}))
|
||||
t.Cleanup(stop)
|
||||
|
||||
issues, err := NewPATClient(host, "tok").ListIssues(t.Context(), "open", "assignee_username=alice")
|
||||
issues, err := NewPATClient(host, "tok").ListIssues(t.Context(), "open", "assignee_username=alice", "")
|
||||
if err != nil {
|
||||
t.Fatalf("ListIssues() error = %v", err)
|
||||
}
|
||||
|
||||
@@ -430,7 +430,7 @@ func TestPATClient_ListIssuesPaged_ScopeAssignedToMe(t *testing.T) {
|
||||
defer stop()
|
||||
|
||||
c := NewPATClient(host, "tok")
|
||||
if _, err := c.ListIssuesPaged(context.Background(), "scope=assigned_to_me", "", 1, 25); err != nil {
|
||||
if _, err := c.ListIssuesPaged(context.Background(), "scope=assigned_to_me", "", "Next", 1, 25); err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
if receivedPath != "/issues" {
|
||||
@@ -442,6 +442,9 @@ func TestPATClient_ListIssuesPaged_ScopeAssignedToMe(t *testing.T) {
|
||||
if got := receivedQuery.Get("state"); got != "opened" {
|
||||
t.Errorf("state = %q, want opened", got)
|
||||
}
|
||||
if got := receivedQuery.Get("milestone"); got != "Next" {
|
||||
t.Errorf("milestone = %q, want Next — must reach the server", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPATClient_ListIssuesPaged_HonoursTotalHeader(t *testing.T) {
|
||||
@@ -467,7 +470,7 @@ func TestPATClient_ListIssuesPaged_HonoursTotalHeader(t *testing.T) {
|
||||
defer stop()
|
||||
|
||||
c := NewPATClient(host, "tok")
|
||||
page, err := c.ListIssuesPaged(context.Background(), "", "", 2, 20)
|
||||
page, err := c.ListIssuesPaged(context.Background(), "", "", "", 2, 20)
|
||||
if err != nil {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ type captureIssueQueryClient struct {
|
||||
listCalls int
|
||||
}
|
||||
|
||||
func (c *captureIssueQueryClient) ListIssues(_ context.Context, filter, customQuery string) ([]*Issue, error) {
|
||||
func (c *captureIssueQueryClient) ListIssues(_ context.Context, filter, customQuery, _ string) ([]*Issue, error) {
|
||||
c.filter = filter
|
||||
c.customQuery = customQuery
|
||||
c.listCalls++
|
||||
|
||||
@@ -287,7 +287,7 @@ func (s *Service) fetchIssues(ctx context.Context, watch *IssueWatch) ([]*Issue,
|
||||
filter += "&labels=" + url.QueryEscape(strings.Join(watch.Labels, ","))
|
||||
}
|
||||
}
|
||||
issues, err := client.ListIssues(ctx, filter, customQuery)
|
||||
issues, err := client.ListIssues(ctx, filter, customQuery, "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list issues: %w", err)
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func (s *Service) SearchMentionIssuesForWorkspace(
|
||||
return collectMentionSearchPages(ctx, scope, limit, MentionIssueURL, func(
|
||||
ctx context.Context, pageNumber, pageSize int,
|
||||
) (mentionSearchPage, error) {
|
||||
page, err := client.ListIssuesPaged(ctx, "", titleQuery, pageNumber, pageSize)
|
||||
page, err := client.ListIssuesPaged(ctx, "", titleQuery, "", pageNumber, pageSize)
|
||||
if err != nil {
|
||||
return mentionSearchPage{}, fmt.Errorf("search GitLab issue mentions: %w", err)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ type mentionRecordingClient struct {
|
||||
|
||||
func (c *mentionRecordingClient) ListIssuesPaged(
|
||||
ctx context.Context,
|
||||
filter, customQuery string,
|
||||
filter, customQuery, _ string,
|
||||
page, perPage int,
|
||||
) (*IssueSearchPage, error) {
|
||||
return c.issues(ctx, filter, customQuery, page, perPage)
|
||||
|
||||
@@ -67,8 +67,8 @@ func TestServiceClientActionsReturnErrNoClient(t *testing.T) {
|
||||
{name: "branches", call: func() error { _, err := service.ListProjectBranches(t.Context(), "acme/widget"); return err }},
|
||||
{name: "search MRs", call: func() error { _, err := service.SearchUserMRs(t.Context(), "", ""); return err }},
|
||||
{name: "search MRs paged", call: func() error { _, err := service.SearchUserMRsPaged(t.Context(), "", "", 1, 10); return err }},
|
||||
{name: "search issues", call: func() error { _, err := service.SearchUserIssues(t.Context(), "", ""); return err }},
|
||||
{name: "search issues paged", call: func() error { _, err := service.SearchUserIssuesPaged(t.Context(), "", "", 1, 10); return err }},
|
||||
{name: "search issues", call: func() error { _, err := service.SearchUserIssues(t.Context(), "", "", ""); return err }},
|
||||
{name: "search issues paged", call: func() error { _, err := service.SearchUserIssuesPaged(t.Context(), "", "", "", 1, 10); return err }},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
||||
@@ -25,21 +25,21 @@ func (s *Service) SearchUserMRsPaged(ctx context.Context, filter, customQuery st
|
||||
}
|
||||
|
||||
// SearchUserIssues returns issues matching a filter for the configured user.
|
||||
func (s *Service) SearchUserIssues(ctx context.Context, filter, customQuery string) ([]*Issue, error) {
|
||||
func (s *Service) SearchUserIssues(ctx context.Context, filter, customQuery, milestone string) ([]*Issue, error) {
|
||||
client := s.Client()
|
||||
if client == nil {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
return client.ListIssues(ctx, filter, customQuery)
|
||||
return client.ListIssues(ctx, filter, customQuery, milestone)
|
||||
}
|
||||
|
||||
// SearchUserIssuesPaged returns paginated issues.
|
||||
func (s *Service) SearchUserIssuesPaged(ctx context.Context, filter, customQuery string, page, perPage int) (*IssueSearchPage, error) {
|
||||
func (s *Service) SearchUserIssuesPaged(ctx context.Context, filter, customQuery, milestone string, page, perPage int) (*IssueSearchPage, error) {
|
||||
client := s.Client()
|
||||
if client == nil {
|
||||
return nil, ErrNoClient
|
||||
}
|
||||
return client.ListIssuesPaged(ctx, filter, customQuery, page, perPage)
|
||||
return client.ListIssuesPaged(ctx, filter, customQuery, milestone, page, perPage)
|
||||
}
|
||||
|
||||
// GetStats aggregates open MR / awaiting-review / open-issue counts.
|
||||
@@ -85,7 +85,7 @@ func (s *Service) getStatsWithClient(ctx context.Context, client Client) (*Stats
|
||||
} else if awaiting != nil {
|
||||
awaitingMRs = awaiting.TotalCount
|
||||
}
|
||||
issues, err := client.ListIssuesPaged(ctx, "scope=assigned_to_me&state=opened", "", 1, 1)
|
||||
issues, err := client.ListIssuesPaged(ctx, "scope=assigned_to_me&state=opened", "", "", 1, 1)
|
||||
openIssues := 0
|
||||
if err != nil {
|
||||
s.logger.Warn("gitlab stats: open issues", zap.Error(err))
|
||||
|
||||
@@ -836,17 +836,11 @@ func (s *Service) requireStore() *Store {
|
||||
// appendLabelsToQuery merges a label list into an existing customQuery string.
|
||||
// If the query already has a `labels` key the caller's value is kept (we
|
||||
// don't want to silently double-up). url.ParseQuery is used for an exact
|
||||
// key match — strings.Contains("labels=") would false-positive on keys
|
||||
// like `mylabels=` and silently drop the watch's labels.
|
||||
// key match by the shared appendQueryParam helper — strings.Contains("labels=")
|
||||
// would false-positive on keys like `mylabels=` and silently drop the watch's
|
||||
// labels.
|
||||
func appendLabelsToQuery(customQuery string, labels []string) string {
|
||||
if parsed, err := url.ParseQuery(customQuery); err == nil && parsed.Has("labels") {
|
||||
return customQuery
|
||||
}
|
||||
encoded := url.QueryEscape(strings.Join(labels, ","))
|
||||
if customQuery == "" {
|
||||
return "labels=" + encoded
|
||||
}
|
||||
return customQuery + "&labels=" + encoded
|
||||
return appendQueryParam(customQuery, "labels", strings.Join(labels, ","))
|
||||
}
|
||||
|
||||
func normalizeProjectFilters(in []ProjectFilter) []ProjectFilter {
|
||||
|
||||
@@ -38,6 +38,14 @@ func TestAppendLabelsToQuery_DoesNotMatchSimilarKeys(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendLabelsToQuery_InsertsBeforeFragment(t *testing.T) {
|
||||
got := appendLabelsToQuery("state=opened#client-fragment", []string{"bug"})
|
||||
want := "state=opened&labels=bug#client-fragment"
|
||||
if got != want {
|
||||
t.Fatalf("got %q, want %q (labels must precede the URL fragment)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsValidCleanupPolicy(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"": true,
|
||||
|
||||
Reference in New Issue
Block a user