Skip to content

Commit fe4b1c7

Browse files
Convert age matcher to use a duration config (#159)
* 🚀 Allow age with at-least/at-most options Marks the old `age: 2d` option as deprecated but keeps it for backward compatability
1 parent a8cc9f9 commit fe4b1c7

File tree

4 files changed

+89
-20
lines changed

4 files changed

+89
-20
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Please consider [sponsoring the project](https://github.com/sponsors/srvaroa) if
5353
### How to trigger action
5454

5555
To trigger the action on events, add a file `.github/workflows/main.yml`
56-
to your repository:
56+
to your repository:
5757

5858
```yaml
5959
name: Label PRs
@@ -294,7 +294,7 @@ With this config, the behaviour changes:
294294

295295
## Conditions
296296

297-
Below are the conditions currently supported in label matchers, in
297+
Below are the conditions currently supported in label matchers, in
298298
alphabetical order. Some important considerations:
299299

300300
* Conditions evaluate only when they are explicitly added in
@@ -308,20 +308,29 @@ alphabetical order. Some important considerations:
308308

309309
### Age (PRs and Issues) <a name="age" />
310310

311-
This condition is satisfied when the age of the PR or Issue are larger than
312-
the given one. The age is calculated from the creation date.
311+
This condition evaluates the creation date of the PR or Issue.
313312

314-
If you're looking to evaluate on the modification date of the issue or PR,
313+
If you're looking to evaluate on the modification date of the issue or PR,
315314
check on <a href="#last-modified" ></a>
316315

317316
This condition is best used when with a <a href="#schedule">schedule trigger</a>.
318317

319-
Example:
318+
Examples:
319+
320+
```yaml
321+
age-range:
322+
at-most: 1d
323+
```
324+
325+
Will label PRs or issues that were created at most one day ago.
320326

321327
```yaml
322-
age: 1d
328+
age-range:
329+
at-least: 1w
323330
```
324331

332+
Will label PRs or issues that were created at least one week ago.
333+
325334
The syntax for values is based on a number, followed by a suffix:
326335

327336
* s: seconds
@@ -412,7 +421,7 @@ This condition is satisfied when any of the PR files matches on the
412421
given regexs.
413422

414423
```yaml
415-
files:
424+
files:
416425
- "cmd\\/.*_tests.go"
417426
- ".*\\/subfolder\\/.*\\.md"
418427
```
@@ -425,9 +434,9 @@ and therefore must be escaped itself to appear as a literal in the regex.
425434

426435
### Last Modified (PRs and Issues) <a name="last-modified" />
427436

428-
This condition evaluates the modification date of the PR or Issue.
437+
This condition evaluates the modification date of the PR or Issue.
429438

430-
If you're looking to evaluate on the creation date of the issue or PR,
439+
If you're looking to evaluate on the creation date of the issue or PR,
431440
check on <a href="#age" ></a>
432441

433442
This condition is best used when with a <a href="#schedule">schedule trigger</a>.
@@ -462,19 +471,19 @@ For example, `2d` means 2 days, `4w` means 4 weeks, and so on.
462471

463472
This condition is satisfied when the [mergeable
464473
state](https://developer.github.com/v3/pulls/#response-1) matches that
465-
of the PR.
474+
of the PR.
466475

467476
```yaml
468477
mergeable: True
469478
```
470479

471-
Will match if the label is mergeable.
480+
Will match if the label is mergeable.
472481

473482
```yaml
474483
mergeable: False
475484
```
476485

477-
Will match if the label is not mergeable.
486+
Will match if the label is not mergeable.
478487

479488
### Size (PRs only) <a name="size" />
480489

@@ -518,7 +527,7 @@ file or a Regex expression:
518527
size:
519528
exclude-files: ["yarn.lock", "\\/root\\/.+\\/test.md"]
520529
above: 100
521-
```
530+
```
522531

523532
This condition will apply the `L` label if the diff is above 100 lines,
524533
but NOT taking into account changes in `yarn.lock`, or any `test.md`

pkg/condition_age.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,35 @@ func AgeCondition(l *Labeler) Condition {
1414
return target.ghIssue != nil || target.ghPR != nil
1515
},
1616
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) {
17-
// Parse the age from the configuration
18-
ageDuration, err := parseExtendedDuration(matcher.Age)
19-
if err != nil {
20-
return false, fmt.Errorf("failed to parse age parameter in configuration: %v", err)
17+
// Backward compatibility: If "age" is provided as a string, treat it as "at-least"
18+
var atLeastDuration, atMostDuration time.Duration
19+
var err error
20+
21+
// If they have specified a legacy "age" field, use that
22+
// and treat it is as "at-least"
23+
if matcher.Age != "" {
24+
atLeastDuration, err = parseExtendedDuration(matcher.Age)
25+
if err != nil {
26+
return false, fmt.Errorf("failed to parse age parameter in configuration: %v", err)
27+
}
28+
} else if matcher.AgeRange != nil {
29+
// Parse "at-least" if specified
30+
if matcher.AgeRange.AtLeast != "" {
31+
atLeastDuration, err = parseExtendedDuration(matcher.AgeRange.AtLeast)
32+
if err != nil {
33+
return false, fmt.Errorf("failed to parse `age.at-least` parameter in configuration: %v", err)
34+
}
35+
}
36+
37+
// Parse "at-most" if specified
38+
if matcher.AgeRange.AtMost != "" {
39+
atMostDuration, err = parseExtendedDuration(matcher.AgeRange.AtMost)
40+
if err != nil {
41+
return false, fmt.Errorf("failed to parse `age.at-most` parameter in configuration: %v", err)
42+
}
43+
}
44+
} else {
45+
return false, fmt.Errorf("no age conditions are set in config")
2146
}
2247

2348
// Determine the creation time of the issue or PR
@@ -30,7 +55,15 @@ func AgeCondition(l *Labeler) Condition {
3055

3156
age := time.Since(createdAt)
3257

33-
return age > ageDuration, nil
58+
// Check if the age of the issue/PR is within the specified range
59+
if atLeastDuration != 0 && age < atLeastDuration {
60+
return false, nil
61+
}
62+
if atMostDuration != 0 && age > atMostDuration {
63+
return false, nil
64+
}
65+
66+
return true, nil
3467
},
3568
}
3669
}

pkg/labeler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type SizeConfig struct {
1919
}
2020

2121
type LabelMatcher struct {
22-
Age string
22+
Age string `yaml:"age,omitempty"` // Deprecated age config.
23+
AgeRange *DurationConfig `yaml:"age-range,omitempty"`
2324
AuthorCanMerge string `yaml:"author-can-merge"`
2425
Authors []string
2526
AuthorInTeam string `yaml:"author-in-team"`

pkg/labeler_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,32 @@ func TestHandleEvent(t *testing.T) {
204204
initialLabels: []string{},
205205
expectedLabels: []string{"ThisIsOld"},
206206
},
207+
// Test the AgeRange configuration
208+
{
209+
event: "pull_request",
210+
payloads: []string{"create_pr"},
211+
name: "Age of a PR in the past",
212+
config: LabelerConfigV1{
213+
Version: 1,
214+
Labels: []LabelMatcher{
215+
{
216+
Label: "Getting Old",
217+
AgeRange: &DurationConfig{
218+
AtLeast: "7d",
219+
AtMost: "14d",
220+
},
221+
},
222+
{
223+
Label: "Getting Ancient",
224+
AgeRange: &DurationConfig{
225+
AtLeast: "14d",
226+
},
227+
},
228+
},
229+
},
230+
initialLabels: []string{},
231+
expectedLabels: []string{"Getting Ancient"},
232+
},
207233
{
208234
event: "pull_request",
209235
payloads: []string{"create_draft_pr"},

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy