Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into tom/add-task-name
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Sep 6, 2023
2 parents 9389834 + 9bb3918 commit 424f395
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 7.0.0 - 2023-09-04

- [789](https://github.com/chromaui/chromatic-cli/pull/789) Use `@antfu/ni` to support `pnpm` for Storybook build
- [805](https://github.com/chromaui/chromatic-cli/pull/805) Add a `onTaskProgress` option and report progress on it

This is a potentially breaking change due to the introduction of [@antfu/ni](https://github.com/antfu/ni) to handle running the `storybook build` command in the **Build Storybook** step.

# 6.24.1 - 2023-08-25

- [803](https://github.com/chromaui/chromatic-cli/pull/803) Support Mode Name as Suffix for Build Progress Indicator
Expand Down
10 changes: 9 additions & 1 deletion node-src/git/getCommitAndBranch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,19 @@ describe('getCommitAndBranch', () => {
expect(info).toMatchObject({ branch: 'master' });
});

it('throws when there is only one commit', async () => {
it('throws when there is only one commit, CI', async () => {
envCi.mockReturnValue({ isCi: true });
hasPreviousCommit.mockResolvedValue(false);
await expect(getCommitAndBranch({ log })).rejects.toThrow('Found only one commit');
});

it('does NOT throw when there is only one commit, non-CI', async () => {
envCi.mockReturnValue({ isCi: false });
hasPreviousCommit.mockResolvedValue(false);
const info = await getCommitAndBranch({ log });
expect(info).toMatchObject({});
});

describe('with branchName', () => {
it('uses provided branchName as branch', async () => {
const info = await getCommitAndBranch({ log }, { branchName: 'foobar' });
Expand Down
9 changes: 7 additions & 2 deletions node-src/git/getCommitAndBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ export default async function getCommitAndBranch(
CHROMATIC_PULL_REQUEST_SHA,
CHROMATIC_SLUG,
} = process.env;
const { isCi, service, prBranch, branch: ciBranch, commit: ciCommit, slug: ciSlug } = envCi();

const isFromEnvVariable = CHROMATIC_SHA && CHROMATIC_BRANCH; // Our GitHub Action also sets these
const isTravisPrBuild = TRAVIS_EVENT_TYPE === 'pull_request';
const isGitHubAction = GITHUB_ACTIONS === 'true';
const isGitHubPrBuild = GITHUB_EVENT_NAME === 'pull_request';

if (!(await hasPreviousCommit())) {
throw new Error(gitOneCommit(isGitHubAction));
const message = gitOneCommit(isGitHubAction);
if (isCi) {
throw new Error(message);
} else {
log.warn(message);
}
}

if (isFromEnvVariable) {
Expand Down Expand Up @@ -117,7 +123,6 @@ export default async function getCommitAndBranch(
slug = GITHUB_REPOSITORY;
}

const { isCi, service, prBranch, branch: ciBranch, commit: ciCommit, slug: ciSlug } = envCi();
const ciService = process.env.CHROMATIC_ACTION ? 'chromaui/action' : service;
slug = slug || ciSlug;

Expand Down
21 changes: 21 additions & 0 deletions node-src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ const getContext = (
},
packagePath: '',
statsPath: 'preview-stats.json',
options: {},
...parseArgs(argv),
} as any;
};
Expand All @@ -351,6 +352,26 @@ it('fails on missing project token', async () => {
expect(ctx.testLogger.errors[0]).toMatch(/Missing project token/);
});

// Note this tests options errors, but not fatal task or runtime errors.
it('passes options error to onTaskError', async () => {
const ctx = getContext([]);
ctx.options = {
onTaskError: jest.fn(),
} as any;

ctx.options.onTaskError = jest.fn();
ctx.env.CHROMATIC_PROJECT_TOKEN = '';
await runBuild(ctx);

await expect(ctx.options.onTaskError).toHaveBeenCalledWith(
expect.anything(), // Context
expect.objectContaining({
formattedError: expect.stringContaining('Missing project token'), // Long formatted error fatalError https://github.com/chromaui/chromatic-cli/blob/217e77671179748eb4ddb8becde78444db93d067/node-src/ui/messages/errors/fatalError.ts#L11
originalError: expect.any(Error),
})
);
});

it('runs in simple situations', async () => {
const ctx = getContext(['--project-token=asdf1234']);
await runBuild(ctx);
Expand Down
5 changes: 5 additions & 0 deletions node-src/runBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function runBuild(ctx: Context, extraOptions?: Partial<Options>) {
} catch (e) {
ctx.log.info('');
ctx.log.error(fatalError(ctx, [e]));
ctx.options.onTaskError?.(ctx, { formattedError: fatalError(ctx, [e]), originalError: e });
setExitCode(ctx, exitCodes.INVALID_OPTIONS, true);
return;
}
Expand Down Expand Up @@ -71,6 +72,10 @@ export async function runBuild(ctx: Context, extraOptions?: Partial<Options>) {
}
} catch (error) {
const errors = [].concat(error); // GraphQLClient might throw an array of errors
ctx.options.onTaskError?.(ctx, {
formattedError: fatalError(ctx, errors),
originalError: error,
});

if (errors.length && !ctx.userError) {
ctx.log.info('');
Expand Down
6 changes: 6 additions & 0 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ export interface Options {
/** A callback that is called at the start of each task */
experimental_onTaskStart?: (ctx: Context) => void;

/** A callback that is called if a task fails */
onTaskError?: (
ctx: Context,
{ formattedError, originalError }: { formattedError: string; originalError: Error | Error[] }
) => void;

/** A callback that is called during tasks that have incremental progress */
experimental_onTaskProgress?: (
ctx: Context,
Expand Down

0 comments on commit 424f395

Please sign in to comment.