Repository navigation
feat: add reusable workflow to remind teams about open Dependabot PRs #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5a5a73b
a3731a0
c51d38c
f21a1be
0b14185
f650a00
3e82046
a06137a
2e8e3fd
d50c4e3
8dab76c
cffaa3e
26c86be
ee081e8
831f226
b1f0057
20b1ba0
67719e6
6b5f80c
344650e
7526b94
ecdff8a
1985cf6
f2be3da
cf11d15
1c7aa1d
45acfef
10b1532
d4bdbed
e15026b
2d08bb0
9d6c706
16f8aa0
ce9a1a6
f4a4cde
7e80bfc
bf5fab1
507794a
b2768ae
4a8bd15
1f09684
e47f083
b31f398
78d24ca
161f1f8
01c0373
bddbed6
819c17d
cbaacdc
3b79a3d
7f557f5
43ac938
ebe8385
fac0641
936323e
1a52d4e
2164cb6
066a5a0
4250f9b
7e04f21
977f6f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| name: Remind Dependabot PRs | ||
| description: 'Builds a Slack payload listing open Dependabot PRs grouped by owning team.' | ||
|
|
||
| inputs: | ||
| github-token: | ||
| description: 'Token used to read the pull requests of this repository.' | ||
| required: true | ||
| planning-token: | ||
| description: 'Token with read access to MetaMask/MetaMask-planning (for topology.json).' | ||
| required: true | ||
| owner-teams: | ||
| description: 'Comma-separated list of team slugs that own the invoking repository. May be empty.' | ||
| required: false | ||
| default: '' | ||
| maintainer-team: | ||
| description: 'Team that looks after the PRs no team is requested on, and coordinates the ones many teams share.' | ||
| required: true | ||
| shared-team-threshold: | ||
| description: 'A PR requested from more than this many teams is listed under the maintainer team.' | ||
| required: false | ||
| default: '5' | ||
| guide-url: | ||
| description: 'Link to a guide for handling these PRs, shown in the message. Empty for none.' | ||
| required: false | ||
| default: '' | ||
| reminder-note: | ||
| description: 'Note shown under the counts, as Slack mrkdwn. Empty uses the built in note.' | ||
| required: false | ||
| default: '' | ||
|
|
||
| outputs: | ||
| has-prs: | ||
| description: '"true" when there are open Dependabot PRs to report, otherwise "false".' | ||
| value: ${{ steps.reminder.outputs.has-prs }} | ||
| payload-path: | ||
| description: 'Path to the JSON file containing the Slack payload.' | ||
| value: ${{ steps.reminder.outputs.payload-path }} | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| # The script is a .mts file, which requires Node 22.18+ to run directly. | ||
| - name: Set up Node | ||
| uses: actions/setup-node@v6 | ||
| with: | ||
| node-version: '24' | ||
|
|
||
| # Corepack resolves Yarn from the working directory, so install from the | ||
| # github-tools root to use its own pinned Yarn and lockfile. | ||
| - name: Install github-tools dependencies | ||
| shell: bash | ||
| working-directory: ${{ github.action_path }}/../../.. | ||
| run: | | ||
| set -euo pipefail | ||
| corepack enable | ||
| corepack yarn install --immutable | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
|
|
||
| - name: Build Dependabot PR reminder | ||
| id: reminder | ||
| shell: bash | ||
| env: | ||
| GITHUB_REPOSITORY: ${{ github.repository }} | ||
| GH_TOKEN: ${{ inputs.github-token }} | ||
| PLANNING_TOKEN: ${{ inputs.planning-token }} | ||
| OWNER_TEAMS: ${{ inputs.owner-teams }} | ||
| MAINTAINER_TEAM: ${{ inputs.maintainer-team }} | ||
| SHARED_TEAM_THRESHOLD: ${{ inputs.shared-team-threshold }} | ||
| GUIDE_URL: ${{ inputs.guide-url }} | ||
| REMINDER_NOTE: ${{ inputs.reminder-note }} | ||
| run: | | ||
| set -euo pipefail | ||
| node "$GITHUB_ACTION_PATH/../../scripts/dependabot-pr-reminder/cli.mts" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about adding JSDoc to these types and functions? Also what do you think about calling this |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { normalizeTeamSlug } from '../shared/topology.mts'; | ||
| import type { PullRequest, PullRequests } from './types.mts'; | ||
|
|
||
| /** | ||
| * Groups PRs by the teams still requested as reviewers. A PR with more than | ||
| * `sharedTeamThreshold` owners has no single owner, so the maintainer team | ||
| * handles it, like a PR with no owner. | ||
| * | ||
| * @param pullRequests - The open Dependabot PRs. | ||
| * @param options - The options. | ||
| * @param options.ownerTeams - Only these teams are reminded, or every team when omitted. | ||
| * @param options.sharedTeamThreshold - The most owners a PR can have and still be owned by them. | ||
| * @returns The PRs grouped by owner. | ||
| */ | ||
| export function classifyPullRequests( | ||
| pullRequests: PullRequest[], | ||
| { | ||
| ownerTeams, | ||
| sharedTeamThreshold, | ||
| }: { ownerTeams?: string[] | undefined; sharedTeamThreshold: number }, | ||
| ): PullRequests { | ||
| const allowedOwners = ownerTeams?.map(normalizeTeamSlug); | ||
| const grouped: PullRequests = { | ||
| byOwner: new Map(), | ||
| sharedByManyOwners: [], | ||
| withNoOwners: [], | ||
| }; | ||
|
|
||
| for (const pullRequest of pullRequests) { | ||
| const requestedOwners = new Set( | ||
| pullRequest.requestedTeams | ||
| .map(normalizeTeamSlug) | ||
| .filter((team) => !allowedOwners || allowedOwners.includes(team)), | ||
| ); | ||
|
|
||
| if (requestedOwners.size > sharedTeamThreshold) { | ||
| grouped.sharedByManyOwners.push(pullRequest); | ||
| } else if (requestedOwners.size > 0) { | ||
| for (const owner of requestedOwners) { | ||
| const ownerPullRequests = grouped.byOwner.get(owner); | ||
| if (ownerPullRequests) { | ||
| ownerPullRequests.push(pullRequest); | ||
| } else { | ||
| grouped.byOwner.set(owner, [pullRequest]); | ||
| } | ||
| } | ||
| } else { | ||
| grouped.withNoOwners.push(pullRequest); | ||
| } | ||
| } | ||
|
|
||
| return grouped; | ||
| } |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about placing these tests side by side with the implementation files for consistency with other projects/repos? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { classifyPullRequests } from './classify-pull-requests.mts'; | ||
| import { pr } from './fixtures.mts'; | ||
|
|
||
| const options = { sharedTeamThreshold: 3 }; | ||
|
|
||
| describe('classify-pull-requests', () => { | ||
| describe('classifyPullRequests', () => { | ||
| it('lists a PR under each requested team', () => { | ||
| const pullRequest = pr({ requestedTeams: ['a', 'b'] }); | ||
|
|
||
| const { byOwner } = classifyPullRequests([pullRequest], options); | ||
|
|
||
| expect(byOwner).toStrictEqual( | ||
| new Map([ | ||
| ['a', [pullRequest]], | ||
| ['b', [pullRequest]], | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| it('puts a PR with no team with the maintainer team', () => { | ||
| const pullRequest = pr(); | ||
|
|
||
| const { withNoOwners } = classifyPullRequests([pullRequest], options); | ||
|
|
||
| expect(withNoOwners).toStrictEqual([pullRequest]); | ||
| }); | ||
|
|
||
| it('keeps a PR with exactly the threshold under its teams', () => { | ||
| const pullRequest = pr({ requestedTeams: ['a', 'b', 'c'] }); | ||
|
|
||
| const { byOwner, sharedByManyOwners } = classifyPullRequests( | ||
| [pullRequest], | ||
| options, | ||
| ); | ||
|
|
||
| expect([...byOwner.keys()]).toStrictEqual(['a', 'b', 'c']); | ||
| expect(sharedByManyOwners).toStrictEqual([]); | ||
| }); | ||
|
|
||
| it('puts a PR with more teams than the threshold with the maintainer team', () => { | ||
| const pullRequest = pr({ requestedTeams: ['a', 'b', 'c', 'd'] }); | ||
|
|
||
| const { byOwner, sharedByManyOwners } = classifyPullRequests( | ||
| [pullRequest], | ||
| options, | ||
| ); | ||
|
|
||
| expect(byOwner.size).toBe(0); | ||
| expect(sharedByManyOwners).toStrictEqual([pullRequest]); | ||
| }); | ||
|
|
||
| it('only counts the owner teams, ignoring case and repeats', () => { | ||
| const pullRequest = pr({ | ||
| requestedTeams: ['a', 'A', 'b', 'x', 'y', 'z'], | ||
| }); | ||
|
|
||
| const { byOwner, sharedByManyOwners } = classifyPullRequests( | ||
| [pullRequest], | ||
| { | ||
| ...options, | ||
| ownerTeams: ['@MetaMask/a', 'b'], | ||
| }, | ||
| ); | ||
|
|
||
| expect([...byOwner.keys()]).toStrictEqual(['a', 'b']); | ||
| expect(sharedByManyOwners).toStrictEqual([]); | ||
| }); | ||
|
|
||
| it('puts a PR with the maintainer team when no owner team is left', () => { | ||
| const pullRequest = pr({ requestedTeams: ['ui'] }); | ||
|
|
||
| const { byOwner, withNoOwners } = classifyPullRequests([pullRequest], { | ||
| ...options, | ||
| ownerTeams: ['extension-platform'], | ||
| }); | ||
|
|
||
| expect(byOwner.size).toBe(0); | ||
| expect(withNoOwners).toStrictEqual([pullRequest]); | ||
| }); | ||
|
|
||
| it('lists a team named like an object property', () => { | ||
| const pullRequest = pr({ requestedTeams: ['constructor'] }); | ||
|
|
||
| const { byOwner } = classifyPullRequests([pullRequest], options); | ||
|
|
||
| expect(byOwner.get('constructor')).toStrictEqual([pullRequest]); | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { appendFile, writeFile } from 'node:fs/promises'; | ||
| import * as path from 'node:path'; | ||
|
|
||
| import { createOctokit } from '../shared/octokit.mts'; | ||
| import { fetchTopology } from '../shared/topology.mts'; | ||
| import type { Topology } from '../shared/topology.mts'; | ||
| import { classifyPullRequests } from './classify-pull-requests.mts'; | ||
| import { fetchDependabotPullRequests } from './github-api.mts'; | ||
| import { buildSlackPayload } from './slack-payload.mts'; | ||
| import { parseOwnerTeams, parseSharedTeamThreshold } from './teams.mts'; | ||
|
|
||
| /** | ||
| * Builds the Slack payload for the open Dependabot PRs and writes it, with the | ||
| * step outputs, for the workflow to post. | ||
| */ | ||
| async function main(): Promise<void> { | ||
| const environment = process.env; | ||
| const repository = environment.GITHUB_REPOSITORY; | ||
| const maintainerTeam = environment.MAINTAINER_TEAM; | ||
| const githubToken = environment.GH_TOKEN; | ||
| const planningToken = environment.PLANNING_TOKEN; | ||
| const outputPath = environment.GITHUB_OUTPUT; | ||
| if ( | ||
| !repository || | ||
| !maintainerTeam || | ||
| !githubToken || | ||
| !planningToken || | ||
| !outputPath | ||
| ) { | ||
| throw new Error( | ||
| 'GITHUB_REPOSITORY, MAINTAINER_TEAM, GH_TOKEN, PLANNING_TOKEN and GITHUB_OUTPUT are required', | ||
| ); | ||
| } | ||
|
|
||
| const pullRequests = await fetchDependabotPullRequests( | ||
| createOctokit(githubToken), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GitHub has an |
||
| repository, | ||
| ); | ||
| if (pullRequests.length === 0) { | ||
| console.log('No open Dependabot pull requests to report.'); | ||
| await appendFile(outputPath, 'has-prs=false\n'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GitHub has an |
||
| return; | ||
| } | ||
|
|
||
| const grouped = classifyPullRequests(pullRequests, { | ||
| ownerTeams: parseOwnerTeams(environment.OWNER_TEAMS), | ||
| sharedTeamThreshold: | ||
| parseSharedTeamThreshold(environment.SHARED_TEAM_THRESHOLD) ?? 5, | ||
| }); | ||
|
|
||
| let topology: Topology = {}; | ||
| try { | ||
| topology = await fetchTopology(createOctokit(planningToken)); | ||
| } catch (error) { | ||
| console.log( | ||
| `::warning::Could not read topology.json, posting without Slack tags: ${String(error)}`, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can also output annotated messages using |
||
| ); | ||
| } | ||
|
|
||
| const payload = buildSlackPayload(grouped, topology, { | ||
| maintainerTeam, | ||
| repository, | ||
| guideUrl: environment.GUIDE_URL?.trim(), | ||
| note: environment.REMINDER_NOTE?.trim(), | ||
| }); | ||
| const payloadPath = path.join( | ||
| environment.RUNNER_TEMP ?? '.', | ||
| 'dependabot-reminder-payload.json', | ||
| ); | ||
| await writeFile(payloadPath, JSON.stringify(payload)); | ||
| await appendFile(outputPath, `has-prs=true\npayload-path=${payloadPath}\n`); | ||
| } | ||
|
|
||
| try { | ||
| await main(); | ||
| } catch (error) { | ||
| console.error(error instanceof Error ? error.message : String(error)); | ||
| process.exitCode = 1; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { execFile } from 'node:child_process'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { promisify } from 'node:util'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const execFileAsync = promisify(execFile); | ||
|
|
||
| const SCRIPT = fileURLToPath(new URL('./cli.mts', import.meta.url)); | ||
|
|
||
| describe('cli', () => { | ||
| it('stops with a message when the required settings are missing', async () => { | ||
| const run = execFileAsync(process.execPath, [SCRIPT], { | ||
| env: { PATH: process.env.PATH }, | ||
| }); | ||
|
|
||
| await expect(run).rejects.toMatchObject({ | ||
| stderr: expect.stringContaining('are required'), | ||
| }); | ||
| }); | ||
| }); |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about adding JSDoc to these constants and functions? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { escapeSlack } from '../shared/slack-blocks.mts'; | ||
|
|
||
| /** | ||
| * Matches `chore(deps-dev): bump foo from 1.0.0 to 2.0.0` and | ||
| * `chore(deps): bump the foo group across 1 directory with 7 updates`. The | ||
| * scope is captured to tell `deps-dev` from `deps`. | ||
| */ | ||
| const PREFIX = String.raw`(?:[a-z]+(?:\(([^)]*)\))?!?:\s*)?`; | ||
| const PACKAGE = new RegExp( | ||
| `^${PREFIX}bump\\s+(\\S+)\\s+from\\s+(\\S+)\\s+to\\s+(\\S+?)(?:\\s+in\\s.*)?$`, | ||
| 'iu', | ||
| ); | ||
| const GROUP = new RegExp( | ||
| `^${PREFIX}bump\\s+the\\s+(\\S+)\\s+group(?:\\s+(?:across\\s+\\d+\\s+director(?:y|ies)|in\\s+\\S+))?\\s+with\\s+(\\d+)\\s+updates?$`, | ||
| 'iu', | ||
| ); | ||
|
|
||
| const code = (value: string): string => | ||
| `\`${escapeSlack(value).replaceAll('`', "'")}\``; | ||
|
|
||
| /** | ||
| * Shortens a Dependabot title to `foo 1.0.0 → 2.0.0`. | ||
| * | ||
| * @param title - The pull request title. | ||
| * @returns The short description, or `undefined` if the title isn't a Dependabot one. | ||
| */ | ||
| export function describeUpdate(title: string): string | undefined { | ||
| const [, scope, name, from, to] = PACKAGE.exec(title.trim()) ?? []; | ||
| if (name && from && to) { | ||
| return `${code(name)} ${escapeSlack(from)} → ${escapeSlack(to)}${scope?.includes('dev') ? ' (dev)' : ''}`; | ||
| } | ||
|
|
||
| const [, , group, count] = GROUP.exec(title.trim()) ?? []; | ||
| if (group && count) { | ||
| return `${code(group)} group, ${count} ${count === '1' ? 'update' : 'updates'}`; | ||
| } | ||
|
|
||
| return undefined; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need an action if we already have a workflow? Can we just ship a workflow in this case?