Skip to content

Commit

Permalink
Merge branch 'GoogleCloudPlatform:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-mitchell authored May 21, 2024
2 parents 670868c + 430f650 commit 25af7fb
Show file tree
Hide file tree
Showing 19 changed files with 849 additions and 93 deletions.
7 changes: 5 additions & 2 deletions .ci/magician/cmd/SCHEDULED_PR_WAITING_FOR_CONTRIBUTOR.md.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{{ if lt .SinceDays 42 -}}
{{ if lt .SinceDays 30 -}}
@{{.PullRequest.User.Login}}, this PR is waiting for action from you. Please address any comments or change requests, or [re-request review](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/requesting-a-pull-request-review) from a core reviewer if no action is required.

![Image showing the re-request review button](https://docs.github.com/assets/cb-28785/mw-1440/images/help/pull_requests/request-re-review.webp)

If no action is taken, this PR will be closed in {{minus 42 .SinceDays}} days.
If no action is taken, this PR will be closed in
{{- if eq .SinceDays 10 }} 28 days.{{end}}
{{- if eq .SinceDays 20 }} 14 days.{{end}}
{{- if eq .SinceDays 28 }} 2 weekdays.{{end}}

This notification can be disabled with the `disable-automatic-closure` label.
{{ else -}}
Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/SCHEDULED_PR_WAITING_FOR_MERGE.md.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This PR is approved and has been waiting for merge for {{.SinceDays}} days. Is it ready to merge? Use the label `disable-review-reminders` to disable these notifications.
This PR is approved and has been waiting for merge for {{if eq .SinceDays 5}}1 week{{else}}{{weekdaysToWeeks .SinceDays}} weeks{{end}}. Is it ready to merge? Use the label `disable-review-reminders` to disable these notifications.
2 changes: 1 addition & 1 deletion .ci/magician/cmd/SCHEDULED_PR_WAITING_FOR_REVIEW.md.tmpl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{if ge .SinceDays 7}}@GoogleCloudPlatform/terraform-team {{end}}This PR has been waiting for review for {{.SinceDays}} days. Please take a look! Use the label `disable-review-reminders` to disable these notifications.
{{if ge .SinceDays 5}}@GoogleCloudPlatform/terraform-team {{end}}This PR has been waiting for review for {{if lt .SinceDays 5}}{{.SinceDays}} weekdays{{else if eq .SinceDays 5}}1 week{{else}}{{weekdaysToWeeks .SinceDays}} weeks{{end}}. Please take a look! Use the label `disable-review-reminders` to disable these notifications.
55 changes: 43 additions & 12 deletions .ci/magician/cmd/scheduled_pr_reminders.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func execScheduledPrReminders(gh *github.Client) error {
state,
since,
)
sinceDays := daysDiff(since, time.Now())
sinceDays := businessDaysDiff(since, time.Now())
if shouldNotify(pr, state, sinceDays) {
comment, err := formatReminderComment(state, reminderCommentData{
PullRequest: pr,
Expand Down Expand Up @@ -353,8 +353,10 @@ func notificationState(pr *github.PullRequest, issueEvents []*github.IssueEvent,
}

// Calculates the number of PDT days between from and to (by calendar date, not # of hours).
func daysDiff(from, to time.Time) int {
// Set minimum time here
func businessDaysDiff(from, to time.Time) int {
if to.Before(from) {
from, to = to, from
}
pdtLoc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
Expand All @@ -364,11 +366,40 @@ func daysDiff(from, to time.Time) int {
from = minFrom
}
from = from.In(pdtLoc)
to = to.In(pdtLoc) //.Truncate(24 * time.Hour)
// Timezone-aware truncation to day
to = to.In(pdtLoc)
// Timezone-aware truncation to day start
from = time.Date(from.Year(), from.Month(), from.Day(), 0, 0, 0, 0, from.Location())
to = time.Date(to.Year(), to.Month(), to.Day(), 0, 0, 0, 0, to.Location())
return int(math.Floor(from.Sub(to).Abs().Hours() / 24))

// Calculate offset from previous Monday & truncate dates
fromOffset := int(from.Weekday()) - 1
if fromOffset == -1 {
fromOffset = 6
}
toOffset := int(to.Weekday()) - 1
if toOffset == -1 {
toOffset = 6
}

// Calculate # of business days for full weeks
from = from.AddDate(0, 0, -fromOffset)
to = to.AddDate(0, 0, -toOffset)
daysDiff := int(math.Floor(to.Sub(from).Hours()/24/7)) * 5

// Adjust days based on weekdays from the offsets. For "from", count weekends as
// 5 days (that is, they "become" the following Monday). For "to", count weekends
// as 4 days (that is, they "become" the previous Friday).
fromOffset = int(math.Min(float64(fromOffset), 5))
toOffset = int(math.Min(float64(toOffset), 4))
daysDiff += toOffset - fromOffset

// Special case: daysDiff may be < 0 if from & to are a saturday and sunday
// from the same weekend. Count this as 0.
if daysDiff < 0 {
daysDiff = 0
}

return daysDiff
}

func shouldNotify(pr *github.PullRequest, state pullRequestReviewState, sinceDays int) bool {
Expand All @@ -381,17 +412,17 @@ func shouldNotify(pr *github.PullRequest, state pullRequestReviewState, sinceDay
if _, ok := labels["disable-review-reminders"]; ok {
return false
}
return sinceDays > 0 && sinceDays%7 == 0
return sinceDays > 0 && sinceDays%5 == 0
case waitingForContributor:
if _, ok := labels["disable-automatic-closure"]; ok {
return false
}
return slices.Contains([]int{14, 28, 40, 42}, sinceDays)
return slices.Contains([]int{10, 20, 28, 30}, sinceDays)
case waitingForReview:
if _, ok := labels["disable-review-reminders"]; ok {
return false
}
return sinceDays == 2 || (sinceDays > 0 && sinceDays%7 == 0)
return sinceDays == 2 || (sinceDays > 0 && sinceDays%5 == 0)
}
return false
}
Expand All @@ -409,8 +440,8 @@ func formatReminderComment(state pullRequestReviewState, data reminderCommentDat
return "", fmt.Errorf("state does not have corresponding template: %s", state.String())
}
tmpl, err := template.New("").Funcs(template.FuncMap{
"minus": func(a, b int) int {
return a - b
"weekdaysToWeeks": func(a int) int {
return a / 5
},
}).Parse(embeddedTemplate)
if err != nil {
Expand All @@ -430,7 +461,7 @@ func shouldClose(pr *github.PullRequest, state pullRequestReviewState, sinceDays
return false
}
}
return state == waitingForContributor && sinceDays >= 42
return state == waitingForContributor && sinceDays >= 30
}

func init() {
Expand Down
Loading

0 comments on commit 25af7fb

Please sign in to comment.