Skip to content

Commit ff8cb30

Browse files
philippwallersrvaroa
authored andcommitted
feat: Add new type condition
By setting the type attribute in the label configuration, the user can specify whether a rule applies exclusively to Pull Requests (PRs) or Issues. This functionality increases the adaptability of this GitHub Action, allowing users to create more tailored labeling strategies that differentiate between PRs and Issues or apply universally to both.
1 parent daa45e5 commit ff8cb30

File tree

6 files changed

+146
-0
lines changed

6 files changed

+146
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ configurable matching rules. Available conditions:
1818
* [Mergeable](#mergeable): label based on whether the PR is mergeable
1919
* [Size](#size): label based on the PR size
2020
* [Title](#title): label based on the PR/Issue title
21+
* [Type](#type): label based on record type (PR or Issue)
2122

2223
## Sponsors
2324

@@ -480,3 +481,34 @@ This condition is satisfied when the title matches on the given regex.
480481
```yaml
481482
title: "^WIP:.*"
482483
```
484+
485+
### Type <a name="type" />
486+
487+
By setting the type attribute in your label configuration, you can specify whether a rule applies exclusively to Pull
488+
Requests (PRs) or Issues. This allows for more precise label management based on the type of GitHub record. The
489+
type condition accepts one of two values:
490+
491+
- `pull_request`
492+
- `issue`
493+
494+
This functionality increases the adaptability of this GitHub Action, allowing users to create more tailored labeling
495+
strategies that differentiate between PRs and Issues or apply universally to both.
496+
497+
#### Pull-Request Only:
498+
499+
```yaml
500+
- label: "needs review"
501+
type: "pull_request"
502+
name: ".*bug.*"
503+
```
504+
This rule applies the label "needs review" to Pull Requests with "bug" in the title.
505+
506+
### Issue Only:
507+
508+
```yaml
509+
- label: "needs triage"
510+
type: "issue"
511+
name: ".*bug.*"
512+
```
513+
514+
This rule applies the label "needs triage" to Issues with "bug" in the title.

cmd/action_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ func TestGetLabelerConfig2V1(t *testing.T) {
264264
Label: "TestFileMatch",
265265
Files: []string{"cmd\\/.*.go", "pkg\\/.*.go"},
266266
},
267+
"TestTypePullRequest": {
268+
Label: "TestTypePullRequest",
269+
Type: "pull_request",
270+
},
271+
"TestTypeIssue": {
272+
Label: "TestTypeIssue",
273+
Type: "issue",
274+
},
267275
}
268276

269277
if !cmp.Equal(len(expectMatchers), len(c.Labels)) {

pkg/condition_type.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package labeler
2+
3+
import (
4+
"fmt"
5+
"log"
6+
)
7+
8+
func TypeCondition() Condition {
9+
return Condition{
10+
GetName: func() string {
11+
return "Target type matches defined type"
12+
},
13+
CanEvaluate: func(target *Target) bool {
14+
return true
15+
},
16+
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) {
17+
if len(matcher.Type) <= 0 {
18+
return false, fmt.Errorf("type is not set in config")
19+
} else if matcher.Type != "pull_request" && matcher.Type != "issue" {
20+
return false, fmt.Errorf("type musst be of value 'pull_request' or 'issue'")
21+
}
22+
23+
var targetType string
24+
if target.ghPR != nil {
25+
targetType = "pull_request"
26+
} else if target.ghIssue != nil {
27+
targetType = "issue"
28+
} else {
29+
return false, fmt.Errorf("target is neither pull_request nor issue")
30+
}
31+
32+
log.Printf("Matching `%s` against: `%s`", matcher.Type, targetType)
33+
return matcher.Type == targetType || matcher.Type == "all", nil
34+
},
35+
}
36+
}

pkg/labeler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type LabelMatcher struct {
3535
SizeBelow string `yaml:"size-below"`
3636
// size-legacy
3737
Title string
38+
Type string
3839
}
3940

4041
type LabelerConfigV0 map[string]LabelMatcher
@@ -226,6 +227,7 @@ func (l *Labeler) findMatches(target *Target, config *LabelerConfigV1) (LabelUpd
226227
IsMergeableCondition(),
227228
SizeCondition(l),
228229
TitleCondition(),
230+
TypeCondition(),
229231
}
230232

231233
for _, matcher := range config.Labels {

pkg/labeler_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,70 @@ func TestHandleEvent(t *testing.T) {
807807
initialLabels: []string{"Test", "WIP"},
808808
expectedLabels: []string{"Test", "WIP"},
809809
},
810+
{
811+
event: "issues",
812+
payloads: []string{"issue_open"},
813+
name: "Add a label to issue when type matches (issues neq pull_request)",
814+
config: LabelerConfigV1{
815+
Version: 1,
816+
Labels: []LabelMatcher{
817+
{
818+
Label: "Test",
819+
Type: "pull_request",
820+
},
821+
},
822+
},
823+
initialLabels: []string{"Meh"},
824+
expectedLabels: []string{"Meh"},
825+
},
826+
{
827+
event: "pull_request",
828+
payloads: []string{"create_pr"},
829+
name: "Add a label to pull request when type matches (pull_request neq issue)",
830+
config: LabelerConfigV1{
831+
Version: 1,
832+
Labels: []LabelMatcher{
833+
{
834+
Label: "Test",
835+
Type: "issue",
836+
},
837+
},
838+
},
839+
initialLabels: []string{"Meh"},
840+
expectedLabels: []string{"Meh"},
841+
},
842+
{
843+
event: "issues",
844+
payloads: []string{"issue_open"},
845+
name: "Add a label to issue when type matches (issues eq issue)",
846+
config: LabelerConfigV1{
847+
Version: 1,
848+
Labels: []LabelMatcher{
849+
{
850+
Label: "Test",
851+
Type: "issue",
852+
},
853+
},
854+
},
855+
initialLabels: []string{"Meh"},
856+
expectedLabels: []string{"Meh", "Test"},
857+
},
858+
{
859+
event: "pull_request",
860+
payloads: []string{"create_pr"},
861+
name: "Add a label to pull request when type matches (pull_request eq pull_request)",
862+
config: LabelerConfigV1{
863+
Version: 1,
864+
Labels: []LabelMatcher{
865+
{
866+
Label: "Test",
867+
Type: "pull_request",
868+
},
869+
},
870+
},
871+
initialLabels: []string{"Meh"},
872+
expectedLabels: []string{"Meh", "Test"},
873+
},
810874
}
811875

812876
for _, tc := range testCases {

test_data/config2_v1.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ labels:
77
files:
88
- "cmd\\/.*.go"
99
- "pkg\\/.*.go"
10+
- label: "TestTypePullRequest"
11+
type: "pull_request"
12+
- label: "TestTypeIssue"
13+
type: "issue"

0 commit comments

Comments
 (0)