Skip to content

Commit 29df864

Browse files
authored
feat: add title matching filter for Pull Request Generator (#23569)
Signed-off-by: nitishfy <[email protected]>
1 parent 7d0820f commit 29df864

File tree

16 files changed

+991
-762
lines changed

16 files changed

+991
-762
lines changed

applicationset/generators/pull_request.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
argoprojiov1alpha1 "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
1919
)
2020

21-
var _ Generator = (*PullRequestGenerator)(nil)
22-
2321
const (
2422
DefaultPullRequestRequeueAfter = 30 * time.Minute
2523
)

applicationset/services/pull_request/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ type PullRequestService interface {
3030
type Filter struct {
3131
BranchMatch *regexp.Regexp
3232
TargetBranchMatch *regexp.Regexp
33+
TitleMatch *regexp.Regexp
3334
}

applicationset/services/pull_request/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ func compileFilters(filters []argoprojiov1alpha1.PullRequestGeneratorFilter) ([]
2525
return nil, fmt.Errorf("error compiling TargetBranchMatch regexp %q: %w", *filter.TargetBranchMatch, err)
2626
}
2727
}
28+
if filter.TitleMatch != nil {
29+
outFilter.TitleMatch, err = regexp.Compile(*filter.TitleMatch)
30+
if err != nil {
31+
return nil, fmt.Errorf("error compiling TitleMatch regexp %q: %w", *filter.TitleMatch, err)
32+
}
33+
}
2834
outFilters = append(outFilters, outFilter)
2935
}
3036
return outFilters, nil
@@ -37,6 +43,9 @@ func matchFilter(pullRequest *PullRequest, filter *Filter) bool {
3743
if filter.TargetBranchMatch != nil && !filter.TargetBranchMatch.MatchString(pullRequest.TargetBranch) {
3844
return false
3945
}
46+
if filter.TitleMatch != nil && !filter.TitleMatch.MatchString(pullRequest.Title) {
47+
return false
48+
}
4049

4150
return true
4251
}

applicationset/services/pull_request/utils_test.go

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,110 @@ func TestFilterTargetBranchMatch(t *testing.T) {
137137
assert.Equal(t, "two", pullRequests[0].Branch)
138138
}
139139

140+
func TestFilterTitleMatch(t *testing.T) {
141+
provider, _ := NewFakeService(
142+
t.Context(),
143+
[]*PullRequest{
144+
{
145+
Number: 1,
146+
Title: "PR one - filter",
147+
Branch: "one",
148+
TargetBranch: "master",
149+
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
150+
Author: "name1",
151+
},
152+
{
153+
Number: 2,
154+
Title: "PR two - ignore",
155+
Branch: "two",
156+
TargetBranch: "branch1",
157+
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
158+
Author: "name2",
159+
},
160+
{
161+
Number: 3,
162+
Title: "[filter] PR three",
163+
Branch: "three",
164+
TargetBranch: "branch2",
165+
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
166+
Author: "name3",
167+
},
168+
{
169+
Number: 4,
170+
Title: "[ignore] PR four",
171+
Branch: "four",
172+
TargetBranch: "branch3",
173+
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
174+
Author: "name4",
175+
},
176+
},
177+
nil,
178+
)
179+
filters := []argoprojiov1alpha1.PullRequestGeneratorFilter{
180+
{
181+
TitleMatch: strp("\\[filter]"),
182+
},
183+
}
184+
pullRequests, err := ListPullRequests(t.Context(), provider, filters)
185+
require.NoError(t, err)
186+
assert.Len(t, pullRequests, 1)
187+
assert.Equal(t, "three", pullRequests[0].Branch)
188+
}
189+
190+
func TestMultiFilterOrWithTitle(t *testing.T) {
191+
provider, _ := NewFakeService(
192+
t.Context(),
193+
[]*PullRequest{
194+
{
195+
Number: 1,
196+
Title: "PR one - filter",
197+
Branch: "one",
198+
TargetBranch: "master",
199+
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
200+
Author: "name1",
201+
},
202+
{
203+
Number: 2,
204+
Title: "PR two - ignore",
205+
Branch: "two",
206+
TargetBranch: "branch1",
207+
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
208+
Author: "name2",
209+
},
210+
{
211+
Number: 3,
212+
Title: "[filter] PR three",
213+
Branch: "three",
214+
TargetBranch: "branch2",
215+
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
216+
Author: "name3",
217+
},
218+
{
219+
Number: 4,
220+
Title: "[ignore] PR four",
221+
Branch: "four",
222+
TargetBranch: "branch3",
223+
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
224+
Author: "name4",
225+
},
226+
},
227+
nil,
228+
)
229+
filters := []argoprojiov1alpha1.PullRequestGeneratorFilter{
230+
{
231+
TitleMatch: strp("\\[filter]"),
232+
},
233+
{
234+
TitleMatch: strp("- filter"),
235+
},
236+
}
237+
pullRequests, err := ListPullRequests(t.Context(), provider, filters)
238+
require.NoError(t, err)
239+
assert.Len(t, pullRequests, 2)
240+
assert.Equal(t, "one", pullRequests[0].Branch)
241+
assert.Equal(t, "three", pullRequests[1].Branch)
242+
}
243+
140244
func TestMultiFilterOr(t *testing.T) {
141245
provider, _ := NewFakeService(
142246
t.Context(),
@@ -192,7 +296,7 @@ func TestMultiFilterOr(t *testing.T) {
192296
assert.Equal(t, "four", pullRequests[2].Branch)
193297
}
194298

195-
func TestMultiFilterOrWithTargetBranchFilter(t *testing.T) {
299+
func TestMultiFilterOrWithTargetBranchFilterOrWithTitleFilter(t *testing.T) {
196300
provider, _ := NewFakeService(
197301
t.Context(),
198302
[]*PullRequest{
@@ -228,6 +332,14 @@ func TestMultiFilterOrWithTargetBranchFilter(t *testing.T) {
228332
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
229333
Author: "name4",
230334
},
335+
{
336+
Number: 5,
337+
Title: "PR title is different than branch name",
338+
Branch: "five",
339+
TargetBranch: "branch3",
340+
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
341+
Author: "name5",
342+
},
231343
},
232344
nil,
233345
)
@@ -240,12 +352,21 @@ func TestMultiFilterOrWithTargetBranchFilter(t *testing.T) {
240352
BranchMatch: strp("r"),
241353
TargetBranchMatch: strp("3"),
242354
},
355+
{
356+
TitleMatch: strp("two"),
357+
},
358+
{
359+
BranchMatch: strp("five"),
360+
TitleMatch: strp("PR title is different than branch name"),
361+
},
243362
}
244363
pullRequests, err := ListPullRequests(t.Context(), provider, filters)
245364
require.NoError(t, err)
246-
assert.Len(t, pullRequests, 2)
365+
assert.Len(t, pullRequests, 3)
247366
assert.Equal(t, "two", pullRequests[0].Branch)
248367
assert.Equal(t, "four", pullRequests[1].Branch)
368+
assert.Equal(t, "five", pullRequests[2].Branch)
369+
assert.Equal(t, "PR title is different than branch name", pullRequests[2].Title)
249370
}
250371

251372
func TestNoFilters(t *testing.T) {

assets/swagger.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/core-install-with-hydrator.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/core-install.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/crds/applicationset-crd.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/ha/install-with-hydrator.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/ha/install.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)