Skip to content

Commit

Permalink
Merge pull request jenkins-x#107 from abayer/more-pr-list-options
Browse files Browse the repository at this point in the history
feat: Add labels and update/create times to GitLab PR listing
  • Loading branch information
jenkins-x-bot authored Apr 24, 2020
2 parents 90e721d + 4ba5008 commit 2a89b51
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions scm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,7 @@ func (d Driver) String() (s string) {
return "unknown"
}
}

// SearchTimeFormat is a time.Time format string for ISO8601 which is the
// format that GitHub requires for times specified as part of a search query.
const SearchTimeFormat = "2006-01-02T15:04:05Z"
7 changes: 5 additions & 2 deletions scm/driver/gitlab/integration/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package integration
import (
"context"
"testing"
"time"

"github.com/jenkins-x/go-scm/scm"
)
Expand All @@ -28,9 +29,11 @@ func testPullRequests(client *scm.Client) func(t *testing.T) {
func testPullRequestList(client *scm.Client) func(t *testing.T) {
return func(t *testing.T) {
t.Parallel()
updatedAfter, _ := time.Parse(scm.SearchTimeFormat, "2015-12-18T17:30:22.522Z")
opts := scm.PullRequestListOptions{
Open: true,
Closed: true,
Open: true,
Closed: true,
UpdatedAfter: &updatedAfter,
}
result, _, err := client.PullRequests.List(context.Background(), "gitlab-org/testme", opts)
if err != nil {
Expand Down
13 changes: 12 additions & 1 deletion scm/driver/gitlab/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"io/ioutil"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/h2non/gock"
Expand Down Expand Up @@ -53,19 +54,29 @@ func TestPullFind(t *testing.T) {
func TestPullList(t *testing.T) {
defer gock.Off()

updatedAfter, _ := time.Parse(scm.SearchTimeFormat, "2015-12-18T17:30:22.522Z")
gock.New("https://gitlab.com").
Get("/api/v4/projects/diaspora/diaspora/merge_requests").
MatchParam("labels", "Community contribution,Manage").
MatchParam("page", "1").
MatchParam("per_page", "30").
MatchParam("state", "all").
MatchParam("updated_after", "2015-12-18T17:30:22Z").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
SetHeaders(mockPageHeaders).
File("testdata/merges.json")

client := NewDefault()
got, res, err := client.PullRequests.List(context.Background(), "diaspora/diaspora", scm.PullRequestListOptions{Page: 1, Size: 30, Open: true, Closed: true})
got, res, err := client.PullRequests.List(context.Background(), "diaspora/diaspora", scm.PullRequestListOptions{
Page: 1,
Size: 30,
Open: true,
Closed: true,
Labels: []string{"Community contribution", "Manage"},
UpdatedAfter: &updatedAfter,
})
if err != nil {
t.Error(err)
return
Expand Down
5 changes: 4 additions & 1 deletion scm/driver/gitlab/testdata/merges.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"assignee": null,
"source_project_id": 32732,
"target_project_id": 32732,
"labels": [],
"labels": [
"Community contribution",
"Manage"
],
"work_in_progress": false,
"milestone": null,
"merge_when_pipeline_succeeds": false,
Expand Down
1 change: 1 addition & 0 deletions scm/driver/gitlab/testdata/merges.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"Ref": "refs/merge-requests/1/head",
"Source": "fix",
"Target": "master",
"Labels": [{"Name":"Community contribution"},{"Name":"Manage"}],
"Link": "https://gitlab.com/gitlab-org/testme/merge_requests/1",
"Closed": true,
"Merged": false,
Expand Down
15 changes: 15 additions & 0 deletions scm/driver/gitlab/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,20 @@ func encodePullRequestListOptions(opts scm.PullRequestListOptions) string {
} else if opts.Open {
params.Set("state", "opened")
}
if len(opts.Labels) > 0 {
params.Set("labels", strings.Join(opts.Labels, ","))
}
if opts.CreatedAfter != nil {
params.Set("created_after", opts.CreatedAfter.Format(scm.SearchTimeFormat))
}
if opts.CreatedBefore != nil {
params.Set("created_before", opts.CreatedBefore.Format(scm.SearchTimeFormat))
}
if opts.UpdatedAfter != nil {
params.Set("updated_after", opts.UpdatedAfter.Format(scm.SearchTimeFormat))
}
if opts.UpdatedBefore != nil {
params.Set("updated_before", opts.UpdatedBefore.Format(scm.SearchTimeFormat))
}
return params.Encode()
}
13 changes: 9 additions & 4 deletions scm/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ type (
// PullRequestListOptions provides options for querying
// a list of repository merge requests.
PullRequestListOptions struct {
Page int
Size int
Open bool
Closed bool
Page int
Size int
Open bool
Closed bool
Labels []string
UpdatedAfter *time.Time
UpdatedBefore *time.Time
CreatedAfter *time.Time
CreatedBefore *time.Time
}

// PullRequestBranch contains information about a particular branch in a PR.
Expand Down

0 comments on commit 2a89b51

Please sign in to comment.