Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b7fae6e
feat: add shared gh, topology and Slack block helpers for workflow sc…
cryptodev-2s Nov 17, 2026
a7945cb
fix: read Slack user group handles from topology instead of IDs
cryptodev-2s Nov 24, 2026
dcaf30a
chore: update coverage thresholds
cryptodev-2s Nov 24, 2026
0229af7
refactor: keep only the shared helpers that are used
cryptodev-2s Dec 2, 2026
746f14b
refactor: drop the unused block limit
cryptodev-2s Dec 2, 2026
788beb4
fix: address review feedback on the shared helpers
cryptodev-2s Dec 2, 2026
3b4d08d
fix: return nothing from escapeAndTruncate when there is no room
cryptodev-2s Dec 2, 2026
74f1615
test: structure the shared helper tests as arrange, act, assert
cryptodev-2s Dec 2, 2026
3ed173f
test: one describe per file and drop the test that only restated section
cryptodev-2s Dec 2, 2026
0a94c26
refactor: read topology.json from the default branch only
cryptodev-2s Dec 2, 2026
be9d590
refactor: use Octokit instead of the gh CLI in the shared helpers
cryptodev-2s Dec 3, 2026
16fe9c4
refactor: use the Octokit retry plugin instead of our own retry helper
cryptodev-2s Dec 3, 2026
88fbaae
refactor: use the octokit package for retries instead of the retry pl…
cryptodev-2s Dec 3, 2026
9de9149
chore: lower coverage thresholds to match CI
cryptodev-2s Dec 3, 2026
82019ba
refactor: document createOctokit and drop its library tests
cryptodev-2s Dec 3, 2026
6844f29
docs: use JSDoc on the shared helpers and lower coverage thresholds t…
cryptodev-2s Dec 3, 2026
56546c9
docs: document each property of the shared types
cryptodev-2s Dec 3, 2026
4b16bd5
docs: describe the shared type properties on the type itself
cryptodev-2s Dec 3, 2026
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
16 changes: 16 additions & 0 deletions .github/scripts/shared/octokit.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Octokit } from 'octokit';

/**
* Creates an Octokit client. It retries every method, writes included, so pass
* `request: { retries: 0 }` on a request that must not be repeated.
*
* @param token - The GitHub token to authenticate with.
* @param options - Extra Octokit options.
* @returns The Octokit client.
*/
export function createOctokit(
token: string,
options: ConstructorParameters<typeof Octokit>[0] = {},
): Octokit {
return new Octokit({ auth: token, ...options });
}
60 changes: 60 additions & 0 deletions .github/scripts/shared/slack-blocks.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* A Slack Block Kit block with a text object.
*
* @property type - The block type, such as `section`.
* @property text - The text shown in the block.
* @property text.type - The text format, such as `mrkdwn`.
* @property text.text - The text itself.
*/
export type SlackBlock = {
type: string;
text: { type: string; text: string };
};

/** Slack rejects section text over 3000 characters, so stay under it. */
export const MAX_SECTION_LENGTH = 2800;

/**
* Escapes the characters Slack treats as markup.
*
* @param text - The text to escape.
* @returns The escaped text.
*/
export function escapeSlack(text: string): string {
return text
.replace(/&/gu, '&amp;')
.replace(/</gu, '&lt;')
.replace(/>/gu, '&gt;');
}

/**
* Escapes the text and cuts it so the escaped text is at most `maxLength`. It
* goes by code point and escapes each one whole, so a cut can't land inside an
* entity like `&amp;` or an emoji.
*
* @param text - The text to escape and cut.
* @param maxLength - The most characters the escaped text may have.
* @returns The escaped text, ending in `…` when it was cut.
*/
export function escapeAndTruncate(text: string, maxLength: number): string {
if (maxLength <= 0) {
return '';
}
const escaped = escapeSlack(text);
if (escaped.length <= maxLength) {
return escaped;
}
let result = '';
for (const character of text) {
const next = escapeSlack(character);
if (result.length + next.length > maxLength - 1) {
break;
}
result += next;
}
return `${result}…`;
}

export function section(text: string): SlackBlock {
return { type: 'section', text: { type: 'mrkdwn', text } };
}
74 changes: 74 additions & 0 deletions .github/scripts/shared/topology.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { Octokit } from 'octokit';

/**
* A team's entry in `topology.json`.
*
* @property githubHandle - The team's GitHub handle, such as `@MetaMask/core-platform`.
* @property slackHandle - The team's Slack handles.
* @property slackHandle.team - The team's Slack user group handle, such as `@core-platform-team`.
*/
type TopologyTeam = {
githubHandle?: string;
slackHandle?: { team?: string };
};

/** `topology.json` from MetaMask-planning, keyed by team. */
export type Topology = Record<string, TopologyTeam | null>;

/**
* Turns `@MetaMask/Core-Platform` into `core-platform`.
*
* @param team - The team handle or slug.
* @returns The lowercase slug without the `@` or the `metamask/` prefix.
*/
export function normalizeTeamSlug(team: string): string {
return team
.trim()
.toLowerCase()
.replace(/^@/u, '')
.replace(/^metamask\//u, '');
}

export async function fetchTopology(octokit: Octokit): Promise<Topology> {
const { data } = await octokit.rest.repos.getContent({
owner: 'MetaMask',
repo: 'MetaMask-planning',
path: 'topology.json',
mediaType: { format: 'raw' },
});
if (typeof data !== 'string') {
throw new Error('topology.json was not returned as raw text');
}
const topology: unknown = JSON.parse(data);
if (
typeof topology !== 'object' ||
topology === null ||
Array.isArray(topology)
) {
throw new Error('topology.json is not a JSON object');
}
return topology as Topology;
}

/**
* Finds the team's Slack user group handle, such as `@core-platform-team`.
*
* @param topology - The parsed `topology.json`.
* @param slug - The team handle or slug to look up.
* @returns The Slack handle, or `undefined` when the team has none.
*/
export function getSlackHandle(
topology: Topology,
slug: string,
): string | undefined {
const wanted = normalizeTeamSlug(slug);
const team = Object.values(topology).find(
(candidate) =>
typeof candidate?.githubHandle === 'string' &&
normalizeTeamSlug(candidate.githubHandle) === wanted,
);
const handle = team?.slackHandle?.team;
return typeof handle === 'string' && handle.trim()
? handle.trim()
: undefined;
}
69 changes: 69 additions & 0 deletions .github/scripts/tests/slack-blocks.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, it } from 'vitest';

import {
MAX_SECTION_LENGTH,
escapeAndTruncate,
escapeSlack,
} from '../shared/slack-blocks.mts';

describe('slack-blocks', () => {
describe('escapeSlack', () => {
it('escapes &, < and >', () => {
const escaped = escapeSlack('a <b> & c');

expect(escaped).toBe('a &lt;b&gt; &amp; c');
});
});

describe('escapeAndTruncate', () => {
it('escapes text that fits', () => {
const result = escapeAndTruncate('a & b', 10);

expect(result).toBe('a &amp; b');
});

it('cuts by the escaped length, not the original length', () => {
const result = escapeAndTruncate('&'.repeat(601), MAX_SECTION_LENGTH);

expect(result.length).toBeLessThanOrEqual(MAX_SECTION_LENGTH);
expect(result.endsWith('…')).toBe(true);
});

it('never cuts an escape sequence in half', () => {
const result = escapeAndTruncate('&&&&', 8);

expect(result).toBe('&amp;…');
});

it.each([
[3, 'a…'],
[4, 'a😀…'],
])(
'never cuts an emoji in half, for a limit of %i',
(maxLength, expected) => {
const result = escapeAndTruncate('a😀bc', maxLength);

expect(result).toBe(expected);
},
);

it.each([
['x', 0],
['&', 0],
['x', -3],
])(
'returns nothing when there is no room, for %j and a limit of %i',
(text, maxLength) => {
const result = escapeAndTruncate(text, maxLength);

expect(result).toBe('');
},
);

it('fits the ellipsis alone in a limit of 1', () => {
const result = escapeAndTruncate('xyz', 1);

expect(result).toBe('…');
});
});
});
109 changes: 109 additions & 0 deletions .github/scripts/tests/topology.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import type { Octokit } from 'octokit';
import { describe, expect, it, vi } from 'vitest';

import {
fetchTopology,
getSlackHandle,
normalizeTeamSlug,
} from '../shared/topology.mts';
import type { Topology } from '../shared/topology.mts';

const topology: Topology = {
bad: null,
core: {
githubHandle: '@MetaMask/core-platform',
slackHandle: { team: '@core-platform-team' },
},
ui: { githubHandle: '@MetaMask/ui', slackHandle: { team: '' } },
none: { githubHandle: '@MetaMask/none' },
};

describe('topology', () => {
describe('normalizeTeamSlug', () => {
it('lowercases and drops the @ and the org', () => {
const slug = normalizeTeamSlug('@MetaMask/Core-Platform');

expect(slug).toBe('core-platform');
});
});

describe('fetchTopology', () => {
function octokitReturning(data: string): {
octokit: Octokit;
getContent: ReturnType<typeof vi.fn>;
} {
const getContent = vi.fn().mockResolvedValue({ data });
const octokit = { rest: { repos: { getContent } } } as unknown as Octokit;
return { octokit, getContent };
}

it('reads topology.json with one request', async () => {
const { octokit, getContent } = octokitReturning(
JSON.stringify(topology),
);

const result = await fetchTopology(octokit);

expect(result).toStrictEqual(topology);
expect(getContent).toHaveBeenCalledTimes(1);
expect(getContent).toHaveBeenCalledWith({
owner: 'MetaMask',
repo: 'MetaMask-planning',
path: 'topology.json',
mediaType: { format: 'raw' },
});
});

it('rejects a response that is not raw text', async () => {
const getContent = vi.fn().mockResolvedValue({ data: [] });
const octokit = { rest: { repos: { getContent } } } as unknown as Octokit;

const result = fetchTopology(octokit);

await expect(result).rejects.toThrow('not returned as raw text');
});

it('rejects a response that is not a JSON object', async () => {
const { octokit } = octokitReturning('[]');

const result = fetchTopology(octokit);

await expect(result).rejects.toThrow('not a JSON object');
});
});

describe('getSlackHandle', () => {
it('matches the team ignoring case, @ and the org', () => {
const handle = getSlackHandle(topology, 'MetaMask/Core-Platform');

expect(handle).toBe('@core-platform-team');
});

it.each(['ui', 'none', 'unknown'])('has no handle for %s', (slug) => {
const handle = getSlackHandle(topology, slug);

expect(handle).toBeUndefined();
});

it('skips a team whose github handle is not a string', () => {
const malformed = {
broken: { githubHandle: 5, slackHandle: { team: '@x' } },
good: { githubHandle: '@MetaMask/c', slackHandle: { team: '@c-team' } },
} as unknown as Topology;

const handle = getSlackHandle(malformed, 'c');

expect(handle).toBe('@c-team');
});

it('ignores a Slack handle that is not a string', () => {
const malformed = {
wrongTeam: { githubHandle: '@MetaMask/b', slackHandle: { team: 123 } },
} as unknown as Topology;

const handle = getSlackHandle(malformed, 'b');

expect(handle).toBeUndefined();
});
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"execa": "^9.5.2",
"googleapis": "144.0.0",
"luxon": "^3.3.0",
"octokit": "5.0.5",
"ora": "^5.4.1",
"prettier": "^3.6.2",
"semver": "^7.7.2",
Expand Down
8 changes: 4 additions & 4 deletions vitest.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export default defineConfig({
thresholds: {
// Raises the thresholds in this file when coverage improves.
autoUpdate: true,
branches: 41.75,
functions: 50.79,
lines: 41.37,
statements: 41.56,
branches: 42.22,
functions: 51.1,
lines: 41.78,
statements: 41.95,
},
},
},
Expand Down
Loading