Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/discord-bot/github.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export async function getIssue(owner, repo, number) {
return gh(`/repos/${owner}/${repo}/issues/${number}`)
}

export async function getPullRequest(owner, repo, number) {
return gh(`/repos/${owner}/${repo}/pulls/${number}`)
}

/** Issues that must close before this one can start (GitHub's native dependencies). */
export async function nativeBlockedBy(owner, repo, number) {
const data = await gh(
Expand Down
53 changes: 51 additions & 2 deletions .github/discord-bot/handlers/pull_request.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
import { COLORS, clamp, sendDiscord } from '../discord.mjs'
import { mention } from '../config.mjs'
import { getPullRequest } from '../github.mjs'

// Reviewers picked when a PR is created each fire their own review_requested event,
// at the same time as `opened`. Requests made this soon after the PR was created are
// left to the "opened" message, which names every reviewer in one message.
const OPENING_WINDOW_MS = 60_000

const sleep = (ms) => new Promise((r) => setTimeout(r, ms))

/**
* The reviewers at the end of the opening window. `gh pr create --reviewer` asks for
* them a few seconds after the PR exists, so the `opened` payload can miss them.
*/
async function reviewersAfterOpening(pr, repository) {
const wait =
Date.parse(pr.created_at) + OPENING_WINDOW_MS + 5_000 - Date.now()
if (wait > 0) await sleep(wait)

try {
const fresh = await getPullRequest(
repository.owner.login,
repository.name,
pr.number,
)
if (fresh) return fresh
} catch (err) {
console.warn(
`[notify] could not re-read the reviewers of #${pr.number}: ${err.message}`,
)
}
return pr
}

function prEmbed(pr, repository, color, extraFields = []) {
return {
Expand Down Expand Up @@ -50,10 +82,16 @@ async function onOpened(payload) {
}

const author = pr.user?.login
const reviewers = (pr.requested_reviewers ?? [])
const current =
payload.action === 'opened'
? await reviewersAfterOpening(pr, repository)
: pr
const reviewers = (current.requested_reviewers ?? [])
.map((r) => r.login)
.filter(Boolean)
const teams = (pr.requested_teams ?? []).map((t) => t.name).filter(Boolean)
const teams = (current.requested_teams ?? [])
.map((t) => t.name)
.filter(Boolean)
const closes = closesIssues(pr)

const asked = [...reviewers.map(mention), ...teams.map((t) => `**@${t}**`)]
Expand Down Expand Up @@ -92,6 +130,17 @@ async function onReviewRequested(payload) {
const team = payload.requested_team?.name
const author = pr.user?.login ?? 'someone'

// A draft gets no "opened" message, so its review requests are still announced.
if (
!pr.draft &&
Date.now() - Date.parse(pr.created_at) < OPENING_WINDOW_MS
) {
console.log(
`[notify] #${pr.number}: review request made with the opening — the "opened" message names ${reviewer ?? team}`,
)
return
}

await sendDiscord({
review: true,
content: `${reviewer ? mention(reviewer) : `**@${team}**`} — code review requested by ${mention(author)} on #${pr.number}.`,
Expand Down
133 changes: 5 additions & 128 deletions .github/discord-bot/sprint.mjs
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
/**
* Sprint reporting. Two modes, both driven by cron from a workflow.
*
* MODE=progress Wednesday and Saturday. Tells the whole team, with an @everyone,
* how far through the current sprint they are.
* MODE=overdue Every second day. Names everyone still holding an open issue in
* a sprint whose due date has passed, and escalates to @everyone
* when overdue work has nobody assigned at all — an unassigned
* late task is the team's problem, not one person's.
* Sprint check-in, run by cron on Wednesday and Saturday. Tells the whole team, with
* an @everyone, how far through the current sprint they are.
*
* A "sprint" is a GitHub milestone. Percentages come from the issues in it, not the
* milestone's own counters, which also count pull requests.
*
* progress speaks on schedule, because a status update people expect is not noise.
* overdue stays silent when nothing is late.
*/
import { assertConfig, everyoneMention, mention } from './config.mjs'
import { assertConfig, everyoneMention } from './config.mjs'
import { COLORS, clamp, sendDiscord } from './discord.mjs'
import { listMilestoneIssues, listMilestones } from './github.mjs'
import {
dueState,
isOverdue,
percent,
pickCurrentSprint,
progressBar,
Expand All @@ -36,7 +26,6 @@ if (!repoFullName) {
}
const [owner, repo] = repoFullName.split('/')

const mode = (process.env.MODE || 'progress').toLowerCase()
const now = Date.now()

/** Up to `max` issue links, with an honest "+N more" rather than a silent cut. */
Expand Down Expand Up @@ -113,126 +102,14 @@ async function reportProgress(milestones) {
})
}

/** Every second day: who is late, and what nobody has claimed. */
async function reportOverdue(milestones) {
const late = milestones.filter((m) => isOverdue(m, now))
if (late.length === 0) {
console.log('[sprint] no sprint is past its due date — staying quiet')
return
}

const openIssues = []
const finished = []
for (const milestone of late) {
const issues = await listMilestoneIssues(
owner,
repo,
milestone.number,
'open',
)
// A late sprint with nothing open is finished; somebody just never closed it.
if (issues.length === 0) finished.push(milestone)
else openIssues.push(...issues)
}

const { byPerson, unassignedOpen, open } = tallyIssues(openIssues)
const people = sortByRemaining(byPerson)

console.log(
`[sprint] ${late.length} overdue sprint(s), ${open} open issue(s), ` +
`${people.length} person(s) behind, ${unassignedOpen.length} unassigned`,
)

if (open === 0) {
console.log(
'[sprint] overdue sprints have no open issues left — staying quiet',
)
return
}

const everyone = everyoneMention()
const needsEveryone = unassignedOpen.length > 0

const pings = [
...(needsEveryone ? [everyone.text] : []),
...people.map(([login]) => mention(login)),
].join(' ')

const worst = late
.slice()
.sort((a, b) => new Date(a.due_on) - new Date(b.due_on))[0]
const worstDue = dueState(worst, now)

const fields = people
.slice(0, needsEveryone ? 24 : 25)
.map(([login, row]) => ({
name: `@${login} — ${row.open} still open`,
value: issueList(row.openIssues),
}))

if (needsEveryone) {
fields.push({
name: `⚠️ Unassigned — ${unassignedOpen.length} overdue with no owner`,
value: `These are late and nobody is on them. Someone has to take them:\n${issueList(unassignedOpen)}`,
})
}

await sendDiscord({
content:
`${pings} — ⏰ **${worst.title}** is ${worstDue.text}` +
(late.length > 1
? ` (and ${late.length - 1} other sprint past due)`
: '') +
` with **${open}** issue${open === 1 ? '' : 's'} still open.` +
(needsEveryone
? ` ${unassignedOpen.length} of them ${unassignedOpen.length === 1 ? 'has' : 'have'} nobody assigned.`
: ''),
everyone: needsEveryone,
pingLogins: [
...people.map(([login]) => login),
...(needsEveryone ? everyone.logins : []),
],
embed: {
title:
late.length === 1
? `Overdue sprint — ${worst.title}`
: `${late.length} overdue sprints`,
url: worst.html_url,
color: COLORS.review,
description:
late
.map((m) => `• **${m.title}** — ${dueState(m, now).text}`)
.join('\n') +
'\n\nClose what is done, move what is not into the next sprint, or say where it stands. ' +
'A sprint that quietly runs past its date stops meaning anything.',
fields,
footer: {
text: finished.length
? `${finished.map((m) => m.title).join(', ')}: every issue closed — the milestone itself is still open.`
: 'Overdue check runs every second day.',
},
timestamp: new Date().toISOString(),
},
})
}

const milestones = await listMilestones(owner, repo, 'open')
console.log(
`[sprint:${mode}] ${milestones.length} open milestone(s) in ${repoFullName}`,
`[sprint] ${milestones.length} open milestone(s) in ${repoFullName}`,
)

if (milestones.length === 0) {
console.log('[sprint] no open milestones — nothing to report on')
process.exit(0)
}

if (mode === 'progress') {
await reportProgress(milestones)
} else if (mode === 'overdue') {
await reportOverdue(milestones)
} else {
console.error(
`::error::Unknown MODE "${mode}" — expected "progress" or "overdue".`,
)
process.exit(1)
}
await reportProgress(milestones)
3 changes: 0 additions & 3 deletions .github/discord-bot/sprints.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export function dueState(milestone, now = Date.now()) {
}
}

export const isOverdue = (milestone, now = Date.now()) =>
dueState(milestone, now).overdue

/**
* The sprint the team is in right now.
*
Expand Down
10 changes: 6 additions & 4 deletions .github/workflows/discord-notify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ on:
issues:
# No `assigned` on purpose: taking an issue is not announced on Discord.
types: [closed, unlabeled, edited]
# pull_request_target (not pull_request) so notifications also work for PRs opened
# from forks, which are not given access to secrets. Safe here because the job only
# ever runs code from the base branch — it never checks out the PR's own code.
pull_request_target:
# pull_request, not pull_request_target: GitHub now blocks pull_request_target on
# public repositories by default. PRs from forks get no secrets with pull_request,
# so the job below skips them.
pull_request:
# ready_for_review pairs with opened: drafts are skipped when they appear and
# announced when the author says they are ready, so neither is missed or early.
types: [opened, ready_for_review, review_requested, closed]
Expand All @@ -25,6 +25,8 @@ permissions:

jobs:
notify:
# Skip PRs and reviews from forks: their runs get no secrets, so the bot would fail.
if: github.event_name == 'issues' || github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
Expand Down
35 changes: 0 additions & 35 deletions .github/workflows/discord-reminders.yml

This file was deleted.

7 changes: 0 additions & 7 deletions .github/workflows/discord-sprint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ on:
- cron: '0 13 * * 3,6'
# Lets you post a check-in on demand from the Actions tab.
workflow_dispatch:
inputs:
mode:
description: 'progress (% complete) or overdue (who is late)'
type: choice
options: [progress, overdue]
default: progress

permissions:
issues: read
Expand All @@ -36,6 +30,5 @@ jobs:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
DISCORD_USER_MAP: ${{ secrets.DISCORD_USER_MAP }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MODE: ${{ inputs.mode || 'progress' }}
# Set to false to name each teammate instead of firing a real @everyone.
ALLOW_EVERYONE: 'true'
Loading