Skip to content

Commit

Permalink
[add] option ignore draft pull-request
Browse files Browse the repository at this point in the history
  • Loading branch information
ymktmk committed Dec 18, 2023
1 parent c5d5cdb commit 2181ac1
Show file tree
Hide file tree
Showing 15 changed files with 386 additions and 228 deletions.
1 change: 1 addition & 0 deletions applicationset/services/pull_request/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) {
TargetBranch: *pull.Base.Ref,
HeadSHA: *pull.Head.SHA,
Labels: getGithubPRLabelNames(pull.Labels),
Draft: *pull.Draft,
})
}
if resp.NextPage == 0 {
Expand Down
3 changes: 3 additions & 0 deletions applicationset/services/pull_request/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type PullRequest struct {
HeadSHA string
// Labels of the pull request.
Labels []string
// Draft is the draft state of the pull request.
Draft bool
}

type PullRequestService interface {
Expand All @@ -26,4 +28,5 @@ type PullRequestService interface {
type Filter struct {
BranchMatch *regexp.Regexp
TargetBranchMatch *regexp.Regexp
IgnoreDraft bool
}
6 changes: 6 additions & 0 deletions applicationset/services/pull_request/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ func compileFilters(filters []argoprojiov1alpha1.PullRequestGeneratorFilter) ([]
return nil, fmt.Errorf("error compiling TargetBranchMatch regexp %q: %v", *filter.TargetBranchMatch, err)
}
}
if filter.IgnoreDraft != nil {
outFilter.IgnoreDraft = *filter.IgnoreDraft
}
outFilters = append(outFilters, outFilter)
}
return outFilters, nil
Expand All @@ -37,6 +40,9 @@ func matchFilter(pullRequest *PullRequest, filter *Filter) bool {
if filter.TargetBranchMatch != nil && !filter.TargetBranchMatch.MatchString(pullRequest.TargetBranch) {
return false
}
if filter.IgnoreDraft && pullRequest.Draft {
return false
}

return true
}
Expand Down
70 changes: 70 additions & 0 deletions applicationset/services/pull_request/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ import (
func strp(s string) *string {
return &s
}

func boolp(b bool) *bool {
return &b
}

func TestFilterBranchMatchBadRegexp(t *testing.T) {
provider, _ := NewFakeService(
context.Background(),
[]*PullRequest{
{
Number: 1,
Branch: "branch1",
Draft: false,
TargetBranch: "master",
HeadSHA: "089d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand All @@ -40,24 +46,28 @@ func TestFilterBranchMatch(t *testing.T) {
{
Number: 1,
Branch: "one",
Draft: false,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "master",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 3,
Branch: "three",
Draft: false,
TargetBranch: "master",
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 4,
Branch: "four",
Draft: false,
TargetBranch: "master",
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand All @@ -75,31 +85,81 @@ func TestFilterBranchMatch(t *testing.T) {
assert.Equal(t, "two", pullRequests[0].Branch)
}

func TestFilterIgnoreDraft(t *testing.T) {
provider, _ := NewFakeService(
context.Background(),
[]*PullRequest{
{
Number: 1,
Branch: "one",
Draft: true,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "branch1",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 3,
Branch: "three",
Draft: true,
TargetBranch: "branch2",
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 4,
Branch: "four",
Draft: true,
TargetBranch: "branch3",
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
},
},
nil,
)
filters := []argoprojiov1alpha1.PullRequestGeneratorFilter{
{
IgnoreDraft: boolp(true),
},
}
pullRequests, err := ListPullRequests(context.Background(), provider, filters)
assert.NoError(t, err)
assert.Len(t, pullRequests, 1)
assert.Equal(t, false, pullRequests[0].Draft)
}

func TestFilterTargetBranchMatch(t *testing.T) {
provider, _ := NewFakeService(
context.Background(),
[]*PullRequest{
{
Number: 1,
Branch: "one",
Draft: false,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "branch1",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 3,
Branch: "three",
Draft: false,
TargetBranch: "branch2",
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 4,
Branch: "four",
Draft: false,
TargetBranch: "branch3",
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand All @@ -124,24 +184,28 @@ func TestMultiFilterOr(t *testing.T) {
{
Number: 1,
Branch: "one",
Draft: false,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "master",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 3,
Branch: "three",
Draft: false,
TargetBranch: "master",
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 4,
Branch: "four",
Draft: false,
TargetBranch: "master",
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand Down Expand Up @@ -171,24 +235,28 @@ func TestMultiFilterOrWithTargetBranchFilter(t *testing.T) {
{
Number: 1,
Branch: "one",
Draft: false,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "branch1",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 3,
Branch: "three",
Draft: false,
TargetBranch: "branch2",
HeadSHA: "389d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 4,
Branch: "four",
Draft: false,
TargetBranch: "branch3",
HeadSHA: "489d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand Down Expand Up @@ -219,12 +287,14 @@ func TestNoFilters(t *testing.T) {
{
Number: 1,
Branch: "one",
Draft: true,
TargetBranch: "master",
HeadSHA: "189d92cbf9ff857a39e6feccd32798ca700fb958",
},
{
Number: 2,
Branch: "two",
Draft: false,
TargetBranch: "master",
HeadSHA: "289d92cbf9ff857a39e6feccd32798ca700fb958",
},
Expand Down
3 changes: 3 additions & 0 deletions assets/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7802,6 +7802,9 @@
"branchMatch": {
"type": "string"
},
"ignoreDraft": {
"type": "boolean"
},
"targetBranchMatch": {
"type": "string"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ spec:
```

* `branchMatch`: A regexp matched against source branch names.
* `ignoreDraft`: A bool whether to exclude draft pull requests.
* `targetBranchMatch`: A regexp matched against target branch names.

[GitHub](#github) and [GitLab](#gitlab) also support a `labels` filter.
Expand Down
6 changes: 6 additions & 0 deletions manifests/core-install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10354,6 +10354,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -15381,6 +15383,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -18065,6 +18069,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
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 @@ -5414,6 +5414,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -10441,6 +10443,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -13125,6 +13129,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down
6 changes: 6 additions & 0 deletions manifests/ha/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10354,6 +10354,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -15381,6 +15383,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -18065,6 +18069,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down
6 changes: 6 additions & 0 deletions manifests/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10354,6 +10354,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -15381,6 +15383,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down Expand Up @@ -18065,6 +18069,8 @@ spec:
properties:
branchMatch:
type: string
ignoreDraft:
type: boolean
targetBranchMatch:
type: string
type: object
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/application/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ type BasicAuthBitbucketServer struct {
type PullRequestGeneratorFilter struct {
BranchMatch *string `json:"branchMatch,omitempty" protobuf:"bytes,1,opt,name=branchMatch"`
TargetBranchMatch *string `json:"targetBranchMatch,omitempty" protobuf:"bytes,2,opt,name=targetBranchMatch"`
IgnoreDraft *bool `json:"ignoreDraft,omitempty" protobuf:"varint,3,opt,name=ignoreDraft"`
}

type PluginConfigMapRef struct {
Expand Down
Loading

0 comments on commit 2181ac1

Please sign in to comment.