storybook/scripts/get-report-message.ts

71 lines
2.6 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-console */
import { readJson } from 'fs-extra';
import { join } from 'path';
2022-11-21 17:06:53 +01:00
import { execaCommand } from './utils/exec';
type Branch = 'main' | 'next' | 'alpha' | 'next-release' | 'latest-release';
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!`;
}
// The CI workflows can run on release branches and we should display the version number
if (branch === 'next-release' || branch === 'latest-release') {
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]"`;
2022-11-21 17:06:53 +01:00
const result = await execaCommand(mergeCommits, { shell: true });
const formattedResult = result.stdout
// discord needs escaped line breaks
.replace(/\n/g, '\\n')
// make links out of pull request ids
2022-11-15 11:55:54 +01:00
.replace(/Merge pull request #/g, '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() {
2022-11-15 11:31:23 +01:00
const [, , workflow = '', template = 'none'] = process.argv;
2022-11-15 11:31:23 +01:00
if (!workflow) {
throw new Error('[get-report-message] Missing workflow argument.');
}
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${
2022-11-15 11:31:23 +01:00
template !== 'none' ? ` for **${template}**.` : '.'
}`;
2022-11-15 11:55:54 +01:00
const body = `\n\n**Branch**: \`${currentBranch}\`\n**Workflow:** ${workflow}`;
const footer = await getFooter(currentBranch as Branch, workflow as Workflow, currentJob);
2022-11-15 11:45:14 +01:00
console.log(`${title}${body}${footer}`.replace(/\n/g, '\\n'));
}
if (require.main === module) {
run().catch((err) => {
console.error(err);
process.exit(1);
});
}