Skip to content

Commit

Permalink
Merge pull request #226 from vixus0/pr-state-filtering
Browse files Browse the repository at this point in the history
Allow filtering PRs by state
  • Loading branch information
rickardl authored Oct 20, 2020
2 parents a506a61 + 2e9461a commit 39b1f9d
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 61 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Make sure to check out [#migrating](#migrating) to learn more.
| `access_token` | Yes | | A Github Access Token with repository access (required for setting status on commits). N.B. If you want github-pr-resource to work with a private repository. Set `repo:full` permissions on the access token you create on GitHub. If it is a public repository, `repo:status` is enough. |
| `v3_endpoint` | No | `https://api.github.com` | Endpoint to use for the V3 Github API (Restful). |
| `v4_endpoint` | No | `https://api.github.com/graphql` | Endpoint to use for the V4 Github API (Graphql). |
| `paths` | No | `["terraform/*/*.tf"]` | Only produce new versions if the PR includes changes to files that match one or more glob patterns or prefixes. |
| `ignore_paths` | No | `[".ci/"]` | Inverse of the above. Pattern syntax is documented in [filepath.Match](https://golang.org/pkg/path/filepath/#Match), or a path prefix can be specified (e.g. `.ci/` will match everything in the `.ci` directory). |
| `paths` | No | `["terraform/*/*.tf"]` | Only produce new versions if the PR includes changes to files that match one or more glob patterns or prefixes. |
| `ignore_paths` | No | `[".ci/"]` | Inverse of the above. Pattern syntax is documented in [filepath.Match](https://golang.org/pkg/path/filepath/#Match), or a path prefix can be specified (e.g. `.ci/` will match everything in the `.ci` directory). |
| `disable_ci_skip` | No | `true` | Disable ability to skip builds with `[ci skip]` and `[skip ci]` in commit message or pull request title. |
| `skip_ssl_verification` | No | `true` | Disable SSL/TLS certificate validation on git and API clients. Use with care! |
| `disable_forks` | No | `true` | Disable triggering of the resource if the pull request's fork repository is different to the configured repository. |
Expand All @@ -35,6 +35,7 @@ Make sure to check out [#migrating](#migrating) to learn more.
| `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. |
| `disable_git_lfs` | No | `true` | Disable Git LFS, skipping an attempt to convert pointers of files tracked into their corresponding objects when checked out into a working copy. |
| `states` | No | `["OPEN", "MERGED"]` | The PR states to select (`OPEN`, `MERGED` or `CLOSED`). The pipeline will only trigger on pull requests matching one of the specified states. Default is ["OPEN"]. |

Notes:
- If `v3_endpoint` is set, `v4_endpoint` must also be set (and the other way around).
Expand Down
24 changes: 23 additions & 1 deletion check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"regexp"
"sort"
"strings"

"github.com/shurcooL/githubv4"
)

// Check (business logic)
Expand All @@ -25,16 +27,36 @@ Loop:
if !disableSkipCI && ContainsSkipCI(p.Title) {
continue
}

// [ci skip]/[skip ci] in Commit message
if !disableSkipCI && ContainsSkipCI(p.Tip.Message) {
continue
}

// Filter pull request if the BaseBranch does not match the one specified in source
if request.Source.BaseBranch != "" && p.PullRequestObject.BaseRefName != request.Source.BaseBranch {
continue
}

// Filter out pull request if it does not have a filtered state
filterStates := []githubv4.PullRequestState{githubv4.PullRequestStateOpen}
if len(request.Source.States) > 0 {
filterStates = request.Source.States
}

stateFound := false
for _, state := range filterStates {
if p.State == state {
stateFound = true
break
}
}
if !stateFound {
continue
}

// Filter out commits that are too old.
if !p.Tip.CommittedDate.Time.After(request.Version.CommittedDate) {
if !p.UpdatedDate().Time.After(request.Version.CommittedDate) {
continue
}

Expand Down
66 changes: 57 additions & 9 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ package resource_test
import (
"testing"

"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
resource "github.com/telia-oss/github-pr-resource"
"github.com/telia-oss/github-pr-resource/fakes"
)

var (
testPullRequests = []*resource.PullRequest{
createTestPR(1, "master", true, false, 0, nil),
createTestPR(2, "master", false, false, 0, nil),
createTestPR(3, "master", false, false, 0, nil),
createTestPR(4, "master", false, false, 0, nil),
createTestPR(5, "master", false, true, 0, nil),
createTestPR(6, "master", false, false, 0, nil),
createTestPR(7, "develop", false, false, 0, []string{"enhancement"}),
createTestPR(8, "master", false, false, 1, []string{"wontfix"}),
createTestPR(9, "master", false, false, 0, nil),
createTestPR(1, "master", true, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(2, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(3, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(4, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(5, "master", false, true, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(6, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(7, "develop", false, false, 0, []string{"enhancement"}, githubv4.PullRequestStateOpen),
createTestPR(8, "master", false, false, 1, []string{"wontfix"}, githubv4.PullRequestStateOpen),
createTestPR(9, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
createTestPR(10, "master", false, false, 0, nil, githubv4.PullRequestStateClosed),
createTestPR(11, "master", false, false, 0, nil, githubv4.PullRequestStateMerged),
createTestPR(12, "master", false, false, 0, nil, githubv4.PullRequestStateOpen),
}
)

Expand Down Expand Up @@ -185,6 +189,50 @@ func TestCheck(t *testing.T) {
resource.NewVersion(testPullRequests[6]),
},
},

{
description: "check returns latest version from a PR with a single state filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateClosed},
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[9]),
},
},

{
description: "check filters out versions from a PR which do not match the state filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateOpen},
},
version: resource.Version{},
pullRequests: testPullRequests[9:11],
files: [][]string{},
expected: resource.CheckResponse(nil),
},

{
description: "check returns versions from a PR with multiple state filters",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
States: []githubv4.PullRequestState{githubv4.PullRequestStateClosed, githubv4.PullRequestStateMerged},
},
version: resource.NewVersion(testPullRequests[11]),
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[9]),
resource.NewVersion(testPullRequests[10]),
},
},
}

for _, tc := range tests {
Expand Down
2 changes: 1 addition & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (m *GithubClient) ListOpenPullRequests() ([]*PullRequest, error) {
"repositoryOwner": githubv4.String(m.Owner),
"repositoryName": githubv4.String(m.Repository),
"prFirst": githubv4.Int(100),
"prStates": []githubv4.PullRequestState{githubv4.PullRequestStateOpen},
"prStates": []githubv4.PullRequestState{githubv4.PullRequestStateOpen, githubv4.PullRequestStateClosed, githubv4.PullRequestStateMerged},
"prCursor": (*githubv4.String)(nil),
"commitsLast": githubv4.Int(1),
"prReviewStates": []githubv4.PullRequestReviewState{githubv4.PullRequestReviewStateApproved},
Expand Down
1 change: 1 addition & 0 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
metadata.Add("message", pull.Tip.Message)
metadata.Add("author", pull.Tip.Author.User.Login)
metadata.Add("author_email", pull.Tip.Author.Email)
metadata.Add("state", string(pull.State))

// Write version and metadata for reuse in PUT
path := filepath.Join(outputDir, ".git", "resource")
Expand Down
Loading

0 comments on commit 39b1f9d

Please sign in to comment.