Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the ability to filter MRs by 'state' to Gitlab PR Generator #9540

Merged
merged 2 commits into from
Jun 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion applicationset/generators/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (g *PullRequestGenerator) selectServiceProvider(ctx context.Context, genera
if err != nil {
return nil, fmt.Errorf("error fetching Secret token: %v", err)
}
return pullrequest.NewGitLabService(ctx, token, providerConfig.API, providerConfig.Project, providerConfig.Labels)
return pullrequest.NewGitLabService(ctx, token, providerConfig.API, providerConfig.Project, providerConfig.Labels, providerConfig.PullRequestState)
}
if generatorConfig.Gitea != nil {
providerConfig := generatorConfig.Gitea
Expand Down
20 changes: 13 additions & 7 deletions applicationset/services/pull_request/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
)

type GitLabService struct {
client *gitlab.Client
project string
labels []string
client *gitlab.Client
project string
labels []string
pullRequestState string
}

var _ PullRequestService = (*GitLabService)(nil)

func NewGitLabService(ctx context.Context, token, url, project string, labels []string) (PullRequestService, error) {
func NewGitLabService(ctx context.Context, token, url, project string, labels []string, pullRequestState string) (PullRequestService, error) {
var clientOptionFns []gitlab.ClientOptionFunc

// Set a custom Gitlab base URL if one is provided
Expand All @@ -34,9 +35,10 @@ func NewGitLabService(ctx context.Context, token, url, project string, labels []
}

return &GitLabService{
client: client,
project: project,
labels: labels,
client: client,
project: project,
labels: labels,
pullRequestState: pullRequestState,
}, nil
}

Expand All @@ -55,6 +57,10 @@ func (g *GitLabService) List(ctx context.Context) ([]*PullRequest, error) {
Labels: labels,
}

if g.pullRequestState != "" {
opts.State = &g.pullRequestState
}

pullRequests := []*PullRequest{}
for {
mrs, resp, err := g.client.MergeRequests.ListProjectMergeRequests(g.project, opts)
Expand Down
27 changes: 23 additions & 4 deletions applicationset/services/pull_request/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestGitLabServiceCustomBaseURL(t *testing.T) {
writeMRListResponse(t, w)
})

svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", nil)
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", nil, "")
assert.NoError(t, err)

_, err = svc.List(context.Background())
Expand All @@ -53,7 +53,7 @@ func TestGitLabServiceToken(t *testing.T) {
writeMRListResponse(t, w)
})

svc, err := NewGitLabService(context.Background(), "token-123", server.URL, "278964", nil)
svc, err := NewGitLabService(context.Background(), "token-123", server.URL, "278964", nil, "")
assert.NoError(t, err)

_, err = svc.List(context.Background())
Expand All @@ -72,7 +72,7 @@ func TestList(t *testing.T) {
writeMRListResponse(t, w)
})

svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{})
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{}, "")
assert.NoError(t, err)

prs, err := svc.List(context.Background())
Expand All @@ -95,7 +95,26 @@ func TestListWithLabels(t *testing.T) {
writeMRListResponse(t, w)
})

svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{"feature", "ready"})
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{"feature", "ready"}, "")
assert.NoError(t, err)

_, err = svc.List(context.Background())
assert.NoError(t, err)
}

func TestListWithState(t *testing.T) {
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()

path := "/api/v4/projects/278964/merge_requests"

mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, path+"?per_page=100&state=opened", r.URL.RequestURI())
writeMRListResponse(t, w)
})

svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{}, "opened")
assert.NoError(t, err)

_, err = svc.List(context.Background())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ spec:
# Labels is used to filter the MRs that you want to target. (optional)
labels:
- preview
# MR state is used to filter MRs only with a certain state. (optional)
pullRequestState: opened
requeueAfterSeconds: 1800
template:
# ...
Expand All @@ -91,6 +93,7 @@ spec:
* `api`: If using self-hosted GitLab, the URL to access it. (Optional)
* `tokenRef`: A `Secret` name and key containing the GitLab access token to use for requests. If not specified, will make anonymous requests which have a lower rate limit and can only see public repositories. (Optional)
* `labels`: Labels is used to filter the MRs that you want to target. (Optional)
* `pullRequestState`: PullRequestState is an additional MRs filter to get only those with a certain state. Default: "" (all states)

## Gitea

Expand Down
6 changes: 6 additions & 0 deletions manifests/core-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4555,6 +4555,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -6731,6 +6733,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -7772,6 +7776,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down
6 changes: 6 additions & 0 deletions manifests/crds/applicationset-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -4579,6 +4581,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -5620,6 +5624,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down
6 changes: 6 additions & 0 deletions manifests/ha/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4555,6 +4555,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -6731,6 +6733,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -7772,6 +7776,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down
6 changes: 6 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4555,6 +4555,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -6731,6 +6733,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down Expand Up @@ -7772,6 +7776,8 @@ spec:
type: array
project:
type: string
pullRequestState:
type: string
tokenRef:
properties:
key:
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/applicationset/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ type PullRequestGeneratorGitLab struct {
TokenRef *SecretRef `json:"tokenRef,omitempty"`
// Labels is used to filter the MRs that you want to target
Labels []string `json:"labels,omitempty"`
// PullRequestState is an additional MRs filter to get only those with a certain state. Default: "" (all states)
PullRequestState string `json:"pullRequestState,omitempty"`
}

// PullRequestGenerator defines connection info specific to BitbucketServer.
Expand Down