Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion gitlab/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func convertGitLabIssue(i *gitlab.Issue) forge.Issue {
HTMLURL: i.WebURL,
}

// Normalize "opened" to "open"
if result.State == stateOpened {
result.State = stateOpen
}

if i.Author != nil {
result.Author = forge.User{
Login: i.Author.Username,
Expand Down Expand Up @@ -131,7 +136,12 @@ func (s *gitLabIssueService) List(ctx context.Context, owner, repo string, opts
}

if opts.State != "" && opts.State != stateAll {
glOpts.State = gitlab.Ptr(opts.State)
// GitLab uses "opened" not "open"
state := opts.State
if state == stateOpen {
state = stateOpened
}
glOpts.State = gitlab.Ptr(state)
}
if opts.Assignee != "" {
glOpts.AssigneeUsername = gitlab.Ptr(opts.Assignee)
Expand Down
39 changes: 39 additions & 0 deletions gitlab/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gitlab

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
Expand All @@ -27,3 +29,40 @@ func TestGitLabCreateIssueRejectsAssignees(t *testing.T) {
t.Fatalf("expected error to mention 'assignee IDs', got: %v", err)
}
}

func TestGitLabListIssuesWithOpenState(t *testing.T) {
var gotState string
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v4/projects/mygroup%2Fmyrepo/issues", func(w http.ResponseWriter, r *http.Request) {
gotState = r.URL.Query().Get("state")
_ = json.NewEncoder(w).Encode([]map[string]any{
{
"id": 1,
"iid": 1,
"title": "Test issue",
"description": "Body",
"state": "opened",
"web_url": "https://gitlab.com/mygroup/myrepo/-/issues/1",
"updated_at": "2024-01-01T00:00:00Z",
"user_notes_count": 0,
},
})
})

srv := httptest.NewServer(mux)
defer srv.Close()

f := New(srv.URL, "test-token", nil)

issues, err := f.Issues().List(context.Background(), "mygroup", "myrepo", forge.ListIssueOpts{
State: "open",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(issues) != 1 {
t.Fatalf("expected 1 issue, got %d", len(issues))
}
assertEqual(t, "state query param", "opened", gotState)
assertEqual(t, "issue state", "open", issues[0].State)
}
15 changes: 13 additions & 2 deletions gitlab/milestones.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

const (
stateOpen = "open"
stateOpen = "open"
stateActive = "active"
)

type gitLabMilestoneService struct {
Expand All @@ -28,6 +29,11 @@ func convertGitLabMilestone(m *gitlab.Milestone) forge.Milestone {
Description: m.Description,
State: m.State,
}

// Normalize "active" to "open"
if result.State == stateActive {
result.State = stateOpen
}
if m.DueDate != nil {
t := time.Time(*m.DueDate)
result.DueDate = &t
Expand All @@ -51,7 +57,12 @@ func (s *gitLabMilestoneService) List(ctx context.Context, owner, repo string, o
}

if opts.State != "" && opts.State != stateAll {
glOpts.State = gitlab.Ptr(opts.State)
// GitLab uses "active" not "open"
state := opts.State
if state == stateOpen {
state = stateActive
}
glOpts.State = gitlab.Ptr(state)
}

var all []forge.Milestone
Expand Down
43 changes: 43 additions & 0 deletions gitlab/milestones_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package gitlab

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"

forge "github.com/git-pkgs/forge"
)

func TestGitLabListMilestonesWithOpenState(t *testing.T) {
var gotState string
mux := http.NewServeMux()
mux.HandleFunc("GET /api/v4/projects/mygroup%2Fmyrepo/milestones", func(w http.ResponseWriter, r *http.Request) {
gotState = r.URL.Query().Get("state")
_ = json.NewEncoder(w).Encode([]map[string]any{
{
"id": 1,
"title": "v1.0",
"state": "active",
},
})
})

srv := httptest.NewServer(mux)
defer srv.Close()

f := New(srv.URL, "test-token", nil)

milestones, err := f.Milestones().List(context.Background(), "mygroup", "myrepo", forge.ListMilestoneOpts{
State: "open",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(milestones) != 1 {
t.Fatalf("expected 1 milestone, got %d", len(milestones))
}
assertEqual(t, "state query param", "active", gotState)
assertEqual(t, "milestone state", "open", milestones[0].State)
}
Loading