Skip to content

Commit

Permalink
Refactor to support positive and negative head branch filters
Browse files Browse the repository at this point in the history
This replaces the proposed `head_branch_filter` option with two
options: `head_branches` selects PRs that match the specified glob,
and `ignore_head_branches` selects PRs that do not match the
specified glob.

This provides more control over filtering PRs based on the head
branch, and brings head branch filtering in line with what already
exists for path filtering.
  • Loading branch information
ctreatma committed May 6, 2020
1 parent 733c994 commit 889c714
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Make sure to check out [#migrating](#migrating) to learn more.
| `git_crypt_key` | No | `AEdJVENSWVBUS0VZAAAAA...` | Base64 encoded git-crypt key. Setting this will unlock / decrypt the repository with git-crypt. To get the key simply execute `git-crypt export-key -- - | base64` in an encrypted repository. |
| `base_branch` | No | `master` | Name of a branch. The pipeline will only trigger on pull requests against the specified branch. |
| `labels` | No | `["bug", "enhancement"]` | The labels on the PR. The pipeline will only trigger on pull requests having at least one of the specified labels. |
| `head_branch_filter` | No | `release/*` | If specified, the pipeline will only trigger on pull requests for which the head branch name matches the specified glob pattern. |
| `head_branches` | No | `release/*` | If specified, the pipeline will only trigger on pull requests for which the head branch name matches the specified glob pattern. |
| `ignore_head_branches` | No | `release/*` | Inverse of the above |

Notes:
- If `v3_endpoint` is set, `v4_endpoint` must also be set (and the other way around).
Expand Down
17 changes: 14 additions & 3 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ Loop:
continue
}

// Filter pull request if the HeadRefName does not match the HeadBranchFilter specified in source
if request.Source.HeadBranchFilter != "" {
matched, err := filepath.Match(request.Source.HeadBranchFilter, p.PullRequestObject.HeadRefName)
// Skip pull request if the HeadRefName does not match the head_branches glob specified in source
if request.Source.HeadBranches != "" {
matched, err := filepath.Match(request.Source.HeadBranches, p.PullRequestObject.HeadRefName)
if err != nil {
return nil, fmt.Errorf("failed to apply head branch filter: %s", err)
}
Expand All @@ -49,6 +49,17 @@ Loop:
}
}

// Skip pull request if the HeadRefName matches the ignore_head_branches glob specified in source
if request.Source.IgnoreHeadBranches != "" {
matched, err := filepath.Match(request.Source.IgnoreHeadBranches, p.PullRequestObject.HeadRefName)
if err != nil {
return nil, fmt.Errorf("failed to apply ignore head branch filter: %s", err)
}
if matched {
continue
}
}

// Filter out pull request if it does not contain at least one of the desired labels
if len(request.Source.Labels) > 0 {
labelFound := false
Expand Down
23 changes: 19 additions & 4 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,30 @@ func TestCheck(t *testing.T) {
{
description: "check correctly ignores PRs that do not match the head branch filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
HeadBranchFilter: "pr2*",
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
HeadBranches: "pr8*",
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[1]),
resource.NewVersion(testPullRequests[7]),
},
},

{
description: "check correctly ignores PRs that do match the ignore head branch filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
IgnoreHeadBranches: "pr2*",
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2]),
},
},
}
Expand Down
56 changes: 44 additions & 12 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ func TestCheckE2E(t *testing.T) {
{
description: "check returns latest PR that matches the head branch filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
HeadBranchFilter: "my*",
DisableCISkip: true,
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
HeadBranches: "my*",
DisableCISkip: true,
},
version: resource.Version{},
expected: resource.CheckResponse{
Expand All @@ -193,16 +193,48 @@ func TestCheckE2E(t *testing.T) {
{
description: "check works when head branch filter doesn't match any PRs",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
HeadBranchFilter: "feature/*",
DisableCISkip: true,
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
HeadBranches: "feature/*",
DisableCISkip: true,
},
version: resource.Version{},
expected: resource.CheckResponse(nil),
},

{
description: "check returns latest PR that matches the ignore head branch filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
IgnoreHeadBranches: "test*",
DisableCISkip: true,
},
version: resource.Version{},
expected: resource.CheckResponse{
resource.Version{PR: targetPullRequestID, Commit: targetCommitID, CommittedDate: targetDateTime},
},
},

{
description: "check works when ignore head branch filter doesn't match any PRs",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
V3Endpoint: "https://api.github.com/",
V4Endpoint: "https://api.github.com/graphql",
IgnoreHeadBranches: "feature/*",
DisableCISkip: true,
},
version: resource.Version{},
expected: resource.CheckResponse{
resource.Version{PR: developPullRequestID, Commit: developCommitID, CommittedDate: developDateTime},
},
},
}

for _, tc := range tests {
Expand Down
3 changes: 2 additions & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type Source struct {
DisableForks bool `json:"disable_forks"`
GitCryptKey string `json:"git_crypt_key"`
BaseBranch string `json:"base_branch"`
HeadBranchFilter string `json:"head_branch_filter"`
HeadBranches string `json:"head_branches"`
IgnoreHeadBranches string `json:"ignore_head_branches"`
RequiredReviewApprovals int `json:"required_review_approvals"`
Labels []string `json:"labels"`
}
Expand Down

0 comments on commit 889c714

Please sign in to comment.