Skip to content

Commit 716df0b

Browse files
authored
Merge pull request #22206 from storybookjs/fix/sandbox-publish-script
Build: Fix sandbox publish script
2 parents 6c20575 + 14a3d6f commit 716df0b

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

scripts/sandbox/utils/git.ts

+35-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { execaCommand } from '../../utils/exec';
33
// eslint-disable-next-line import/no-cycle
44
import { logger } from '../publish';
55

6+
const { version: storybookVersion } = require('../../../code/package.json');
7+
68
const getTheLastCommitHashThatUpdatedTheSandboxRepo = async (branch: string) => {
79
const owner = 'storybookjs';
810
const repo = 'sandboxes';
@@ -16,16 +18,23 @@ const getTheLastCommitHashThatUpdatedTheSandboxRepo = async (branch: string) =>
1618
await fetch(`https://api.github.com/repos/${owner}/${repo}/commits/${latestCommitSha}`)
1719
).json();
1820
const latestCommitMessage = commitData.commit.message;
19-
logger.log(
20-
`Latest commit message of ${owner}/${repo} on branch ${branch}: ${latestCommitMessage}`
21-
);
22-
// The commit message will look like this: Update examples - Thu Apr 13 2023 - 97dbc82537c3
21+
22+
// The commit message will look like this: 7.0.5 - Thu Apr 13 2023 - 97dbc82537c3
2323
// the hash at the end relates to the monorepo commit that updated the sandboxes
24-
return latestCommitMessage.split('\n')[0].split(' - ')[2];
24+
const lastCommitHash = latestCommitMessage.split('\n')[0].split(' - ')[2];
25+
if (!lastCommitHash) {
26+
throw new Error(
27+
`Could not find the last commit hash in the following commit message: "${latestCommitMessage}".\nDid someone manually push to the sandboxes repo?`
28+
);
29+
}
30+
31+
return lastCommitHash;
2532
} catch (error) {
26-
logger.error(
27-
`Error getting latest commit message of ${owner}/${repo} on branch ${branch}: ${error.message}`
28-
);
33+
if (!error.message.includes('Did someone manually push to the sandboxes repo')) {
34+
logger.error(
35+
`⚠️ Error getting latest commit message of ${owner}/${repo} on branch ${branch}: ${error.message}`
36+
);
37+
}
2938

3039
throw error;
3140
}
@@ -47,6 +56,7 @@ export async function commitAllToGit({ cwd, branch }: { cwd: string; branch: str
4756

4857
let gitCommitCommand;
4958

59+
logger.log('🔍 Determining commit message');
5060
try {
5161
const previousCommitHash = await getTheLastCommitHashThatUpdatedTheSandboxRepo(branch);
5262
const mergeCommits = (
@@ -66,20 +76,31 @@ export async function commitAllToGit({ cwd, branch }: { cwd: string; branch: str
6676

6777
const diffLink = `https://github.com/storybookjs/storybook/compare/${previousCommitHash}...${currentCommitHash}`;
6878

69-
const commitTitle = `Update sandboxes - ${new Date().toDateString()} - ${previousCommitHash}`;
70-
const commitBody = [...prLinks, `\nCheck the diff here: ${diffLink}`].join('\n');
79+
const commitTitle = `${storybookVersion} - ${new Date().toDateString()} - ${previousCommitHash}`;
80+
const commitBody = [
81+
`\nCheck the diff here: ${diffLink}`,
82+
'\nList of included PRs since previous version:',
83+
...prLinks,
84+
].join('\n');
7185
gitCommitCommand = `git commit -m "${commitTitle}" -m "${commitBody}"`;
7286
} catch (err) {
73-
gitCommitCommand = `git commit -m "Update sandboxes - ${new Date().toDateString()} - ${currentCommitHash}`;
87+
logger.log(
88+
`⚠️ Falling back to a simpler commit message because of an error while trying to get the previous commit hash: ${err.message}`
89+
);
90+
gitCommitCommand = `git commit -m "${storybookVersion} - ${new Date().toDateString()} - ${currentCommitHash}"`;
7491
}
7592

7693
await execaCommand(gitCommitCommand, {
7794
shell: true,
7895
cwd,
7996
});
8097
} catch (e) {
81-
logger.log(
82-
`🤷 Git found no changes between previous versions so there is nothing to commit. Skipping publish!`
83-
);
98+
if (e.message.includes('nothing to commit')) {
99+
logger.log(
100+
`🤷 Git found no changes between previous versions so there is nothing to commit. Skipping publish!`
101+
);
102+
} else {
103+
logger.error(`🤯 Something went wrong while committing to git: ${e.message}`);
104+
}
84105
}
85106
}

0 commit comments

Comments
 (0)