Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build: Fix sandbox publish script #22206

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Changes from all commits
Commits
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
49 changes: 35 additions & 14 deletions scripts/sandbox/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { execaCommand } from '../../utils/exec';
// eslint-disable-next-line import/no-cycle
import { logger } from '../publish';

const { version: storybookVersion } = require('../../../code/package.json');

const getTheLastCommitHashThatUpdatedTheSandboxRepo = async (branch: string) => {
const owner = 'storybookjs';
const repo = 'sandboxes';
Expand All @@ -16,16 +18,23 @@ const getTheLastCommitHashThatUpdatedTheSandboxRepo = async (branch: string) =>
await fetch(`https://api.github.com/repos/${owner}/${repo}/commits/${latestCommitSha}`)
).json();
const latestCommitMessage = commitData.commit.message;
logger.log(
`Latest commit message of ${owner}/${repo} on branch ${branch}: ${latestCommitMessage}`
);
// The commit message will look like this: Update examples - Thu Apr 13 2023 - 97dbc82537c3

// The commit message will look like this: 7.0.5 - Thu Apr 13 2023 - 97dbc82537c3
// the hash at the end relates to the monorepo commit that updated the sandboxes
return latestCommitMessage.split('\n')[0].split(' - ')[2];
const lastCommitHash = latestCommitMessage.split('\n')[0].split(' - ')[2];
if (!lastCommitHash) {
throw new Error(
`Could not find the last commit hash in the following commit message: "${latestCommitMessage}".\nDid someone manually push to the sandboxes repo?`
);
}

return lastCommitHash;
} catch (error) {
logger.error(
`Error getting latest commit message of ${owner}/${repo} on branch ${branch}: ${error.message}`
);
if (!error.message.includes('Did someone manually push to the sandboxes repo')) {
logger.error(
`⚠️ Error getting latest commit message of ${owner}/${repo} on branch ${branch}: ${error.message}`
);
}

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

let gitCommitCommand;

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

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

const commitTitle = `Update sandboxes - ${new Date().toDateString()} - ${previousCommitHash}`;
const commitBody = [...prLinks, `\nCheck the diff here: ${diffLink}`].join('\n');
const commitTitle = `${storybookVersion} - ${new Date().toDateString()} - ${previousCommitHash}`;
const commitBody = [
`\nCheck the diff here: ${diffLink}`,
'\nList of included PRs since previous version:',
...prLinks,
].join('\n');
gitCommitCommand = `git commit -m "${commitTitle}" -m "${commitBody}"`;
} catch (err) {
gitCommitCommand = `git commit -m "Update sandboxes - ${new Date().toDateString()} - ${currentCommitHash}`;
logger.log(
`⚠️ Falling back to a simpler commit message because of an error while trying to get the previous commit hash: ${err.message}`
);
gitCommitCommand = `git commit -m "${storybookVersion} - ${new Date().toDateString()} - ${currentCommitHash}"`;
}

await execaCommand(gitCommitCommand, {
shell: true,
cwd,
});
} catch (e) {
logger.log(
`🤷 Git found no changes between previous versions so there is nothing to commit. Skipping publish!`
);
if (e.message.includes('nothing to commit')) {
logger.log(
`🤷 Git found no changes between previous versions so there is nothing to commit. Skipping publish!`
);
} else {
logger.error(`🤯 Something went wrong while committing to git: ${e.message}`);
}
}
}