diff --git a/applicationset/generators/pull_request.go b/applicationset/generators/pull_request.go index 06610067ff927..2091af4ccc3b7 100644 --- a/applicationset/generators/pull_request.go +++ b/applicationset/generators/pull_request.go @@ -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 diff --git a/applicationset/services/pull_request/gitlab.go b/applicationset/services/pull_request/gitlab.go index 21d429f5682ab..7d3b1138d7b8e 100644 --- a/applicationset/services/pull_request/gitlab.go +++ b/applicationset/services/pull_request/gitlab.go @@ -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 @@ -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 } @@ -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) diff --git a/applicationset/services/pull_request/gitlab_test.go b/applicationset/services/pull_request/gitlab_test.go index d64dd5313f96d..7c42e6f9f9269 100644 --- a/applicationset/services/pull_request/gitlab_test.go +++ b/applicationset/services/pull_request/gitlab_test.go @@ -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()) @@ -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()) @@ -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()) @@ -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()) diff --git a/docs/operator-manual/applicationset/Generators-Pull-Request.md b/docs/operator-manual/applicationset/Generators-Pull-Request.md index d37764e49f4f3..24b68850fa9f4 100644 --- a/docs/operator-manual/applicationset/Generators-Pull-Request.md +++ b/docs/operator-manual/applicationset/Generators-Pull-Request.md @@ -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: # ... @@ -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 diff --git a/manifests/core-install.yaml b/manifests/core-install.yaml index b4dfb8d651542..bc721b5595ae9 100644 --- a/manifests/core-install.yaml +++ b/manifests/core-install.yaml @@ -4555,6 +4555,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -6731,6 +6733,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -7772,6 +7776,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: diff --git a/manifests/crds/applicationset-crd.yaml b/manifests/crds/applicationset-crd.yaml index 0e201f61ad8f4..cc1e8938541d2 100644 --- a/manifests/crds/applicationset-crd.yaml +++ b/manifests/crds/applicationset-crd.yaml @@ -2403,6 +2403,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -4579,6 +4581,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -5620,6 +5624,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: diff --git a/manifests/ha/install.yaml b/manifests/ha/install.yaml index c4c1fe526b626..cd444f6015c28 100644 --- a/manifests/ha/install.yaml +++ b/manifests/ha/install.yaml @@ -4555,6 +4555,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -6731,6 +6733,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -7772,6 +7776,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: diff --git a/manifests/install.yaml b/manifests/install.yaml index f7d3095369b6d..748ca70b68f85 100644 --- a/manifests/install.yaml +++ b/manifests/install.yaml @@ -4555,6 +4555,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -6731,6 +6733,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: @@ -7772,6 +7776,8 @@ spec: type: array project: type: string + pullRequestState: + type: string tokenRef: properties: key: diff --git a/pkg/apis/applicationset/v1alpha1/applicationset_types.go b/pkg/apis/applicationset/v1alpha1/applicationset_types.go index 73b7f65c0a90c..8da9e62a9ce19 100644 --- a/pkg/apis/applicationset/v1alpha1/applicationset_types.go +++ b/pkg/apis/applicationset/v1alpha1/applicationset_types.go @@ -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.