-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support labels based on PR / Issue age (#113)
Signed-off-by: Galo Navarro <[email protected]>
- Loading branch information
Showing
6 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
coverage.out | ||
action | ||
action.tar.gz | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package labeler | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func AgeCondition(l *Labeler) Condition { | ||
return Condition{ | ||
GetName: func() string { | ||
return "Age of issue/PR" | ||
}, | ||
CanEvaluate: func(target *Target) bool { | ||
return target.ghIssue != nil || target.ghPR != nil | ||
}, | ||
Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) { | ||
// Parse the age from the configuration | ||
ageDuration, err := parseExtendedDuration(matcher.Age) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to parse age parameter in configuration: %v", err) | ||
} | ||
|
||
// Determine the creation time of the issue or PR | ||
var createdAt time.Time | ||
if target.ghIssue != nil { | ||
createdAt = target.ghIssue.CreatedAt.Time | ||
} else if target.ghPR != nil { | ||
createdAt = target.ghPR.CreatedAt.Time | ||
} | ||
|
||
age := time.Since(createdAt) | ||
|
||
return age > ageDuration, nil | ||
}, | ||
} | ||
} | ||
|
||
func parseExtendedDuration(s string) (time.Duration, error) { | ||
multiplier := time.Hour * 24 // default to days | ||
|
||
if strings.HasSuffix(s, "w") { | ||
multiplier = time.Hour * 24 * 7 // weeks | ||
s = strings.TrimSuffix(s, "w") | ||
} else if strings.HasSuffix(s, "y") { | ||
multiplier = time.Hour * 24 * 365 // years | ||
s = strings.TrimSuffix(s, "y") | ||
} else if strings.HasSuffix(s, "d") { | ||
s = strings.TrimSuffix(s, "d") // days | ||
} else { | ||
return time.ParseDuration(s) // default to time.ParseDuration for hours, minutes, seconds | ||
} | ||
|
||
value, err := strconv.Atoi(s) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return time.Duration(value) * multiplier, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package labeler | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestParseExtendedDuration(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected time.Duration | ||
}{ | ||
{"1s", 1 * time.Second}, | ||
{"2m", 2 * time.Minute}, | ||
{"3h", 3 * time.Hour}, | ||
{"4d", 4 * 24 * time.Hour}, | ||
{"5w", 5 * 7 * 24 * time.Hour}, | ||
{"6y", 6 * 365 * 24 * time.Hour}, | ||
} | ||
|
||
for _, test := range tests { | ||
result, err := parseExtendedDuration(test.input) | ||
if err != nil { | ||
t.Errorf("failed to parse duration from %s: %v", test.input, err) | ||
} | ||
if result != test.expected { | ||
t.Errorf("expected %v, got %v", test.expected, result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters