Skip to content

Commit 7b6d645

Browse files
committed
chore: add support for ignoring PRs by labels
Add `does_not_have_labels` parameter (list of strings) which will make PR ignore PRs with those labels.
1 parent 00157ef commit 7b6d645

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

fixtures/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ github_pr_notifications:
2828
gh_pr_ignore_changes_requested: true
2929
gh_pr_conditions:
3030
older_than_seconds: 3600
31+
does_not_have_labels:
32+
- "WIP"
33+
- "do not merge"
3134
# Mon-Fri every 2 hours during business hours
3235
schedule: "CRON_TZ=Europe/Berlin 00 10-18/2 * * 1-5"
3336
notify:

internal/cfg/cfg.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ type Notification struct {
7979

8080
// PrConditions struct describes additional conditions for PRs
8181
type PrConditions struct {
82-
OlderThanSeconds int `yaml:"older_than_seconds"`
82+
OlderThanSeconds int `yaml:"older_than_seconds"`
83+
DoesNotHaveLabels []string `yaml:"does_not_have_labels"`
8384
}
8485

8586
// PrNotification is a struct for a single GH repo PRs notifications

internal/cfg/cfg_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,31 @@ func TestGetSlackUID(t *testing.T) {
8585
t.Errorf("Did not expect to find Slack user ID for GitHub login 'nonexistent-gh-user', but found one")
8686
}
8787
}
88+
89+
func TestPrConditions(t *testing.T) {
90+
config := AppConfig{}
91+
err := config.LoadConfig("../../fixtures/config.yaml")
92+
if err != nil {
93+
t.Errorf("Failed to load ./fixtures/config.yaml: %s", err.Error())
94+
}
95+
96+
if len(config.PrNotifications) < 1 {
97+
t.Errorf("Length of github_pr_notifications is %d, but it should be not empty", len(config.PrNotifications))
98+
}
99+
100+
conditions := config.PrNotifications[0].Conditions
101+
if conditions.OlderThanSeconds != 3600 {
102+
t.Errorf("Expected gh_pr_conditions.older_than_seconds=3600, but got %d", conditions.OlderThanSeconds)
103+
}
104+
105+
expectedLabels := []string{"WIP", "do not merge"}
106+
if len(conditions.DoesNotHaveLabels) != len(expectedLabels) {
107+
t.Errorf("Expected gh_pr_conditions.does_not_have_labels length=%d, but got %d", len(expectedLabels), len(conditions.DoesNotHaveLabels))
108+
} else {
109+
for i, label := range expectedLabels {
110+
if conditions.DoesNotHaveLabels[i] != label {
111+
t.Errorf("Expected gh_pr_conditions.does_not_have_labels[%d]=%q, but got %q", i, label, conditions.DoesNotHaveLabels[i])
112+
}
113+
}
114+
}
115+
}

internal/gh/gh.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ func (g *Github) MatchesConditions(pr *github.PullRequest, prn cfg.PrNotificatio
121121
prOlderThan := createdAt.Add(time.Duration(prn.Conditions.OlderThanSeconds) * time.Second)
122122
isAfter := time.Now().After(prOlderThan)
123123
if !isAfter {
124+
// PR is not older than the specified time
125+
return false
126+
}
127+
}
128+
if len(prn.Conditions.DoesNotHaveLabels) > 0 {
129+
if labelsMatched(pr.Labels, prn.Conditions.DoesNotHaveLabels) {
130+
// If the PR has any of the labels in DoesNotHaveLabels, it doesn't match the conditions
124131
return false
125132
}
126133
}

internal/gh/gh_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,26 @@ func TestMatchesConditions_OlderThanSeconds(t *testing.T) {
1919
// Condition: PR older than 1 hour
2020
prn := cfg.PrNotification{
2121
Conditions: cfg.PrConditions{
22-
OlderThanSeconds: 3600, // 1 hour
22+
OlderThanSeconds: 3600, // 1 hour
23+
DoesNotHaveLabels: []string{"WIP"},
2324
},
2425
}
2526

2627
if !g.MatchesConditions(pr, prn) {
2728
t.Error("Expected MatchesConditions to return true for PR older than OlderThanSeconds")
2829
}
30+
31+
label := "WIP"
32+
pr = &github.PullRequest{
33+
CreatedAt: &github.Timestamp{Time: time.Now().Add(-2 * time.Hour)},
34+
Labels: []*github.Label{
35+
{Name: &label},
36+
},
37+
}
38+
39+
if g.MatchesConditions(pr, prn) {
40+
t.Error("Expected MatchesConditions to return false for PR with labels in DoesNotHaveLabels")
41+
}
2942
}
3043

3144
func TestMatchesConditions_NotOlderThanSeconds(t *testing.T) {

0 commit comments

Comments
 (0)