Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Filter for a specific pull request #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Make sure to check out [#migrating](#migrating) to learn more.
| `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. |
| `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.

| `pr` | No | `34` | Only return commits from specified pull request number (ignores all commits from other pull requests) |

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

// Check (business logic)
Expand All @@ -18,9 +19,15 @@ func Check(request CheckRequest, manager Github) (CheckResponse, error) {
}

disableSkipCI := request.Source.DisableCISkip
specifiedPr := request.Source.PR

Loop:
for _, p := range pulls {
// If PR not the filtered
if !MatchesSpecifiedPR(specifiedPr, strconv.Itoa(p.Number)) {
continue
}

// [ci skip]/[skip ci] in Pull request title
if !disableSkipCI && ContainsSkipCI(p.Title) {
continue
Expand Down Expand Up @@ -83,7 +90,7 @@ Loop:
sort.Sort(response)

// If there are no new but an old version = return the old
if len(response) == 0 && request.Version.PR != "" {
if len(response) == 0 && request.Version.PR != "" && MatchesSpecifiedPR(specifiedPr, request.Version.PR) {
response = append(response, request.Version)
}
// If there are new versions and no previous = return just the latest
Expand All @@ -99,6 +106,11 @@ func ContainsSkipCI(s string) bool {
return re.MatchString(s)
}

// Returns true if request.Source.PR matches pr number
func MatchesSpecifiedPR(specifiedPr string, prNumber string) bool {
return specifiedPr == "" || specifiedPr == prNumber
}

// FilterIgnorePath ...
func FilterIgnorePath(files []string, pattern string) ([]string, error) {
var out []string
Expand Down
28 changes: 28 additions & 0 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,34 @@ func TestCheck(t *testing.T) {
resource.NewVersion(testPullRequests[1]),
},
},
{
description: "check returns pr if pr number does match specified one and no previous version",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableForks: true,
PR: "3",
},
version: resource.Version{},
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2]),
},
},
{
description: "check returns pr if pr number does match specified one and has previous version",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
DisableForks: true,
PR: "3",
},
version: resource.NewVersion(testPullRequests[2]),
pullRequests: testPullRequests,
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2]),
},
},
}

for _, tc := range tests {
Expand Down
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Source struct {
SkipSSLVerification bool `json:"skip_ssl_verification"`
DisableForks bool `json:"disable_forks"`
GitCryptKey string `json:"git_crypt_key"`
PR string `json:"pr"`
}

// Validate the source configuration.
Expand Down