Skip to content
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
22 changes: 14 additions & 8 deletions pkg/labeler.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,13 @@ func (l *Labeler) ProcessAllIssues(owner, repo string) {
return
}

for _, pr := range issues {
err = l.ExecuteOn(wrapIssueAsTarget(pr))
log.Printf("Unable to execute action: %+v", err)
}
for _, issue := range issues {
if issue.State != nil && strings.ToLower(*issue.State) != "open" {
continue
}
err = l.ExecuteOn(wrapIssueAsTarget(issue))
log.Printf("Unable to execute action: %+v", err)
}
}

func (l *Labeler) ProcessAllPRs(owner, repo string) {
Expand All @@ -332,9 +335,12 @@ func (l *Labeler) ProcessAllPRs(owner, repo string) {
return
}

for _, pr := range prs {
err = l.ExecuteOn(wrapPrAsTarget(pr))
log.Printf("Unable to execute action: %+v", err)
}
for _, pr := range prs {
if pr.State != nil && strings.ToLower(*pr.State) != "open" {
continue
}
err = l.ExecuteOn(wrapPrAsTarget(pr))
log.Printf("Unable to execute action: %+v", err)
}

}
53 changes: 53 additions & 0 deletions pkg/labeler_open_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package labeler

import (
"testing"
gh "github.com/google/go-github/v50/github"
)

func TestProcessAllIssuesAndPRsOnlyOpen(t *testing.T) {
calls := struct{
issues []int
prs []int
}{[]int{}, []int{}}

fakeIssues := []*gh.Issue{
{Number: gh.Int(1), State: gh.String("open"), User: &gh.User{Login: gh.String("user1")}, RepositoryURL: gh.String("http://x/y/repo")},
{Number: gh.Int(2), State: gh.String("closed"), User: &gh.User{Login: gh.String("user2")}, RepositoryURL: gh.String("http://x/y/repo")},
}
fakePRs := []*gh.PullRequest{
{Number: gh.Int(10), State: gh.String("open"), User: &gh.User{Login: gh.String("user3")}, Base: &gh.PullRequestBranch{Repo: &gh.Repository{Name: gh.String("repo"), Owner: &gh.User{Login: gh.String("y")}}}},
{Number: gh.Int(11), State: gh.String("closed"), User: &gh.User{Login: gh.String("user4")}, Base: &gh.PullRequestBranch{Repo: &gh.Repository{Name: gh.String("repo"), Owner: &gh.User{Login: gh.String("y")}}}},
}


l := &Labeler{
FetchRepoConfig: func() (*LabelerConfigV1, error) {
return &LabelerConfigV1{Version: 1, Issues: true, Labels: []LabelMatcher{}}, nil
},
ReplaceLabels: func(target *Target, labels []string) error {
if target.ghIssue != nil {
calls.issues = append(calls.issues, target.IssueNo)
}
if target.ghPR != nil {
calls.prs = append(calls.prs, target.IssueNo)
}
return nil
},
GetCurrentLabels: func(target *Target) ([]string, error) { return nil, nil },
GitHubFacade: &GitHubFacade{
ListIssuesByRepo: func(owner, repo string) ([]*gh.Issue, error) { return fakeIssues, nil },
ListPRs: func(owner, repo string) ([]*gh.PullRequest, error) { return fakePRs, nil },
},
}

l.ProcessAllIssues("y", "repo")
l.ProcessAllPRs("y", "repo")

if len(calls.issues) != 1 || calls.issues[0] != 1 {
t.Errorf("Expected only open issue #1 to be processed, got %+v", calls.issues)
}
if len(calls.prs) != 1 || calls.prs[0] != 10 {
t.Errorf("Expected only open PR #10 to be processed, got %+v", calls.prs)
}
}
8 changes: 8 additions & 0 deletions pkg/labeler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ func TestProcessAllIssues(t *testing.T) {
func TestHandleEvent(t *testing.T) {
// These all use the payload in payload files
testCases := []TestCase{
{
event: "issues",
payloads: []string{"issue_open"},
name: "Do not process issues if Issues flag is not set",
config: LabelerConfigV1{Version: 1, Labels: []LabelMatcher{{Label: "Test", Title: "^Testy.*t"}}},
initialLabels: []string{"ShouldStay"},
expectedLabels: []string{"ShouldStay"},
},
{
event: "issues",
payloads: []string{"issue_open"},
Expand Down
Loading