diff --git a/.github/discord-bot/github.mjs b/.github/discord-bot/github.mjs index 92b212f..6365c64 100644 --- a/.github/discord-bot/github.mjs +++ b/.github/discord-bot/github.mjs @@ -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( diff --git a/.github/discord-bot/handlers/pull_request.mjs b/.github/discord-bot/handlers/pull_request.mjs index ae1754f..60f010e 100644 --- a/.github/discord-bot/handlers/pull_request.mjs +++ b/.github/discord-bot/handlers/pull_request.mjs @@ -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 { @@ -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}**`)] @@ -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}.`, diff --git a/.github/discord-bot/sprint.mjs b/.github/discord-bot/sprint.mjs index cfa1ab7..93493b8 100644 --- a/.github/discord-bot/sprint.mjs +++ b/.github/discord-bot/sprint.mjs @@ -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, @@ -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. */ @@ -113,112 +102,9 @@ 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) { @@ -226,13 +112,4 @@ if (milestones.length === 0) { 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) diff --git a/.github/discord-bot/sprints.mjs b/.github/discord-bot/sprints.mjs index bfd6c26..3a8c878 100644 --- a/.github/discord-bot/sprints.mjs +++ b/.github/discord-bot/sprints.mjs @@ -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. * diff --git a/.github/workflows/discord-reminders.yml b/.github/workflows/discord-reminders.yml deleted file mode 100644 index 7817e9d..0000000 --- a/.github/workflows/discord-reminders.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Discord reminders - -on: - schedule: - # 13:00 UTC = 09:00 Montreal (EDT) / 08:00 (EST), every second day of the month. - # Chases sprints past their due date. - - cron: '0 13 */2 * *' - # Lets you run it on demand from the Actions tab without waiting for the schedule. - workflow_dispatch: - -permissions: - issues: read - contents: read - -jobs: - remind: - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v4 - with: - sparse-checkout: .github/discord-bot - sparse-checkout-cone-mode: true - fetch-depth: 1 - persist-credentials: false - - - name: Chase overdue sprints - run: node .github/discord-bot/sprint.mjs - env: - DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }} - DISCORD_USER_MAP: ${{ secrets.DISCORD_USER_MAP }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - MODE: overdue - # Only fires for overdue issues nobody has been assigned to. - ALLOW_EVERYONE: 'true' diff --git a/.github/workflows/discord-sprint.yml b/.github/workflows/discord-sprint.yml index 743a3fe..67c71fe 100644 --- a/.github/workflows/discord-sprint.yml +++ b/.github/workflows/discord-sprint.yml @@ -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 @@ -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'