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 branch (headRefName) source filter #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Check(request CheckRequest, manager Github) (CheckResponse, error) {
filterStates = request.Source.States
}

pulls, err := manager.ListPullRequests(filterStates)
pulls, err := manager.ListPullRequests(filterStates, request.Source.Branch)
if err != nil {
return nil, fmt.Errorf("failed to get last commits: %s", err)
}
Expand Down
25 changes: 22 additions & 3 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,21 @@ func TestCheck(t *testing.T) {
resource.NewVersion(testPullRequests[10]),
},
},

{
description: "check filters out versions from a PR which do not match the branch filter",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: "oauthtoken",
Branch: "pr3",
},
version: resource.Version{},
pullRequests: testPullRequests,
files: [][]string{},
expected: resource.CheckResponse{
resource.NewVersion(testPullRequests[2]),
},
},
}

for _, tc := range tests {
Expand All @@ -274,10 +289,14 @@ func TestCheck(t *testing.T) {
}
for i := range tc.pullRequests {
for j := range filterStates {
if filterStates[j] == tc.pullRequests[i].PullRequestObject.State {
pullRequests = append(pullRequests, tc.pullRequests[i])
break
if filterStates[j] != tc.pullRequests[i].PullRequestObject.State {
continue
}
if tc.source.Branch != "" && tc.source.Branch != tc.pullRequests[i].PullRequestObject.HeadRefName {
continue
}
pullRequests = append(pullRequests, tc.pullRequests[i])
break
}
}
github.ListPullRequestsReturns(pullRequests, nil)
Expand Down
13 changes: 13 additions & 0 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ func TestCheckE2E(t *testing.T) {
},
},

{
description: "check will only return versions that match the specified branch",
source: resource.Source{
Repository: "itsdalmo/test-repository",
AccessToken: os.Getenv("GITHUB_ACCESS_TOKEN"),
Branch: "my_second_pull",
},
version: resource.Version{},
expected: resource.CheckResponse{
resource.Version{PR: targetPullRequestID, Commit: targetCommitID, CommittedDate: targetDateTime},
},
},

{
description: "check will skip versions which only match the ignore paths",
source: resource.Source{
Expand Down
18 changes: 10 additions & 8 deletions fakes/fake_github.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// Github for testing purposes.
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -o fakes/fake_github.go . Github
type Github interface {
ListPullRequests([]githubv4.PullRequestState) ([]*PullRequest, error)
ListPullRequests([]githubv4.PullRequestState, string) ([]*PullRequest, error)
ListModifiedFiles(int) ([]string, error)
PostComment(string, string) error
GetPullRequest(string, string) (*PullRequest, error)
Expand Down Expand Up @@ -98,7 +98,7 @@ func NewGithubClient(s *Source) (*GithubClient, error) {
}

// ListPullRequests gets the last commit on all pull requests with the matching state.
func (m *GithubClient) ListPullRequests(prStates []githubv4.PullRequestState) ([]*PullRequest, error) {
func (m *GithubClient) ListPullRequests(prStates []githubv4.PullRequestState, prHeadRefName string) ([]*PullRequest, error) {
var query struct {
Repository struct {
PullRequests struct {
Expand Down Expand Up @@ -128,7 +128,7 @@ func (m *GithubClient) ListPullRequests(prStates []githubv4.PullRequestState) ([
EndCursor githubv4.String
HasNextPage bool
}
} `graphql:"pullRequests(first:$prFirst,states:$prStates,after:$prCursor)"`
} `graphql:"pullRequests(first:$prFirst,states:$prStates,after:$prCursor,headRefName:$prHeadRefName)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}

Expand All @@ -137,12 +137,17 @@ func (m *GithubClient) ListPullRequests(prStates []githubv4.PullRequestState) ([
"repositoryName": githubv4.String(m.Repository),
"prFirst": githubv4.Int(100),
"prStates": prStates,
"prHeadRefName": (*githubv4.String)(nil),
"prCursor": (*githubv4.String)(nil),
"commitsLast": githubv4.Int(1),
"prReviewStates": []githubv4.PullRequestReviewState{githubv4.PullRequestReviewStateApproved},
"labelsFirst": githubv4.Int(100),
}

if len(prHeadRefName) > 0 {
vars["prHeadRefName"] = githubv4.String(prHeadRefName)
}

var response []*PullRequest
for {
if err := m.V4.Query(context.TODO(), &query, vars); err != nil {
Expand Down
1 change: 1 addition & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Source struct {
RequiredReviewApprovals int `json:"required_review_approvals"`
Labels []string `json:"labels"`
States []githubv4.PullRequestState `json:"states"`
Branch string `json:"branch"`
}

// Validate the source configuration.
Expand Down