From 919dca3fae63aea1e9ad0c82ee5b9e26c445395a Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Tue, 15 Nov 2022 11:21:23 +0100 Subject: [PATCH] add command to compose discord report message --- package.json | 1 + scripts/get-report-message.ts | 69 +++++++++++++++++++++++++++++++++++ scripts/package.json | 1 + 3 files changed, 71 insertions(+) create mode 100644 scripts/get-report-message.ts diff --git a/package.json b/package.json index bba2585efc3..f2bce525133 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "start": "yarn task --task dev --template react-vite/default-ts --start-from=install", "task": "echo 'Installing Script Dependencies...'; cd scripts; yarn install >/dev/null; yarn task", "get-template": "cd scripts; yarn get-template", + "get-report-message": "cd scripts; yarn get-report-message", "test": "cd code; yarn test", "lint": "cd code; yarn lint", "nx": "cd code; yarn nx", diff --git a/scripts/get-report-message.ts b/scripts/get-report-message.ts new file mode 100644 index 00000000000..7c0fdbc87fc --- /dev/null +++ b/scripts/get-report-message.ts @@ -0,0 +1,69 @@ +/* eslint-disable no-console */ +import { command } from 'execa'; +import { readJson } from 'fs-extra'; +import { join } from 'path'; + +type Branch = 'main' | 'next' | 'alpha'; +type Workflow = 'merged' | 'daily'; + +const getFooter = async (branch: Branch, workflow: Workflow, job: string) => { + if (job === 'chromatic-sandboxes') { + return `\n\nThis might not necessarily be a bug, it could be a visual diff that you have to review and approve. Please check it!`; + } + + if (branch === 'alpha') { + const packageJson = await readJson(join(__dirname, '..', 'code', 'package.json')); + + // running in alpha branch we should just show the version which failed + return `\n**Version: ${packageJson.version}**`; + } + + const mergeCommits = + workflow === 'merged' + ? // show single merge for merged workflow + `git log -1 --pretty=format:"\`%h\` %<(12)%ar %s [%an]"` + : // show last 24h merges for daily workflow + `git log --merges --since="24 hours ago" --pretty=format:"\`%h\` %<(12)%ar %s [%an]"`; + + const result = await command(mergeCommits, { shell: true }); + const formattedResult = result.stdout + // discord needs escaped line breaks + .replace(/\n/g, '\\n') + // make links out of pull request ids + .replace('Merge pull request #', 'https://github.com/storybookjs/storybook/pull/'); + + return `\n\n**Relevant PRs:**\n${formattedResult}`; +}; + +// This command is run in Circle CI on failures, to get a rich message to report to Discord +// Usage: yarn get-report-message type workflow branch +async function run() { + const [, , workflow = '', template = ''] = process.argv; + + if (!workflow || !template) { + throw new Error('[get-report-message] Missing workflow or template arguments.'); + } + + const { CIRCLE_BRANCH: currentBranch = '', CIRCLE_JOB: currentJob = '' } = process.env; + + if (!currentBranch || !currentJob) { + throw new Error( + '[get-report-message] Missing CIRCLE_BRANCH or CIRCLE_JOB environment variables.' + ); + } + + const title = `Oh no! The **${currentJob}** job has failed${ + template ? ` for **${template}**.` : '.' + }`; + const body = `\n\n**Branch**: ${currentBranch}\n**Workflow:** ${workflow}`; + const footer = await getFooter(currentBranch as Branch, workflow as Workflow, currentJob); + + console.log(`${title}${body}${footer}`); +} + +if (require.main === module) { + run().catch((err) => { + console.error(err); + process.exit(1); + }); +} diff --git a/scripts/package.json b/scripts/package.json index e7190c1c757..335de76f902 100644 --- a/scripts/package.json +++ b/scripts/package.json @@ -3,6 +3,7 @@ "version": "7.0.0-alpha.16", "private": true, "scripts": { + "get-report-message": "ts-node ./get-report-message.ts", "get-template": "ts-node ./get-template.ts", "lint": "yarn lint:js && yarn lint:md", "lint:js": "yarn lint:js:cmd . --quiet",