|
| 1 | +package labeler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/google/go-github/v50/github" |
| 8 | +) |
| 9 | + |
| 10 | +func LastModifiedCondition(l *Labeler) Condition { |
| 11 | + return Condition{ |
| 12 | + GetName: func() string { |
| 13 | + return "Last modification of issue/PR" |
| 14 | + }, |
| 15 | + CanEvaluate: func(target *Target) bool { |
| 16 | + return target.ghIssue != nil || target.ghPR != nil |
| 17 | + }, |
| 18 | + Evaluate: func(target *Target, matcher LabelMatcher) (bool, error) { |
| 19 | + if matcher.LastModified == nil { |
| 20 | + return false, fmt.Errorf("no last modified conditions are set in config") |
| 21 | + } |
| 22 | + // Determine the last modification time of the issue or PR |
| 23 | + var lastModifiedAt *github.Timestamp |
| 24 | + if target.ghIssue != nil { |
| 25 | + lastModifiedAt = target.ghIssue.UpdatedAt |
| 26 | + } else if target.ghPR != nil { |
| 27 | + lastModifiedAt = target.ghPR.UpdatedAt |
| 28 | + } else { |
| 29 | + return false, fmt.Errorf("no issue or PR found in target") |
| 30 | + } |
| 31 | + duration := time.Since(lastModifiedAt.Time) |
| 32 | + |
| 33 | + if matcher.LastModified.AtMost != "" { |
| 34 | + maxDuration, err := parseExtendedDuration(matcher.LastModified.AtMost) |
| 35 | + if err != nil { |
| 36 | + return false, fmt.Errorf("failed to parse `last-modified.at-most` parameter in configuration: %v", err) |
| 37 | + } |
| 38 | + return duration <= maxDuration, nil |
| 39 | + } |
| 40 | + |
| 41 | + if matcher.LastModified.AtLeast != "" { |
| 42 | + minDuration, err := parseExtendedDuration(matcher.LastModified.AtLeast) |
| 43 | + if err != nil { |
| 44 | + return false, fmt.Errorf("failed to parse `last-modified.at-least` parameter in configuration: %v", err) |
| 45 | + } |
| 46 | + return duration >= minDuration, nil |
| 47 | + } |
| 48 | + |
| 49 | + return false, fmt.Errorf("no last modified conditions are set in config") |
| 50 | + |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
0 commit comments