Skip to content

fix(scripts): add dry run release and fix major bump for python #3175

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

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
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
22 changes: 6 additions & 16 deletions scripts/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ const flags = {
flag: '-s, --skip-cache',
description: 'skip cache checking to force building specs',
},
outputType: {
flag: '-json, --output-json',
description: 'outputs the spec in JSON instead of yml',
},
docs: {
flag: '-d, --docs',
description: 'generates the doc specs with the code snippets',
},
major: {
flag: '-m, --major',
description: 'triggers a major release for the given language list',
},
};

program.name('cli');
Expand Down Expand Up @@ -97,8 +85,8 @@ buildCommand
.addArgument(args.clients)
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.skipCache.flag, flags.skipCache.description)
.option(flags.outputType.flag, flags.outputType.description)
.option(flags.docs.flag, flags.docs.description)
.option('-json, --output-json', 'outputs the spec in JSON instead of yml')
.option('-d, --docs', 'generates the doc specs with the code snippets')
.action(async (clientArg: string[], { verbose, skipCache, outputJson, docs }) => {
const { client, clientList } = transformSelection({
langArg: ALL,
Expand Down Expand Up @@ -212,8 +200,9 @@ program
.description('Releases the client')
.addArgument(args.languages)
.option(flags.verbose.flag, flags.verbose.description)
.option(flags.major.flag, flags.major.description)
.action(async (langArgs: LangArg[], { verbose, major }) => {
.option('-m, --major', 'triggers a major release for the given language list')
.option('-d, --dry-run', 'does not push anything to GitHub')
.action(async (langArgs: LangArg[], { verbose, major, dryRun }) => {
setVerbose(Boolean(verbose));

if (langArgs.length === 0) {
Expand All @@ -223,6 +212,7 @@ program
await createReleasePR({
languages: langArgs.includes(ALL) ? LANGUAGES : (langArgs as Language[]),
major,
dryRun,
});
});

Expand Down
57 changes: 29 additions & 28 deletions scripts/release/createReleasePR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ dotenv.config({ path: ROOT_ENV_PATH });

export const COMMON_SCOPES = ['specs', 'clients'];

// python pre-releases have a pattern like `X.Y.ZaN` for alpha or `X.Y.ZbN` for beta
// see https://peps.python.org/pep-0440/
// It also support ruby pre-releases like `X.Y.Z.alpha.N` for alpha or `X.Y.Z.beta.N` for beta
const preReleaseRegExp = new RegExp(/\d\.\d\.\d(\.?a(lpha\.)?\d+|\.?b(eta\.)?\d+)$/);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:face_vomiting:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahahah yes this one is pretty ugly I admit


// Prevent fetching the same user multiple times
const fetchedUsers: Record<string, string> = {};

Expand Down Expand Up @@ -181,14 +186,13 @@ export function getNextVersion(current: string, releaseType: semver.ReleaseType

let nextVersion: string | null = current;

// python pre-releases have a pattern like `X.Y.ZaN` for alpha or `X.Y.ZbN` for beta
// see https://peps.python.org/pep-0440/
// It also support ruby pre-releases like `X.Y.Z.alpha.N` for alpha or `X.Y.Z.beta.N` for beta
if (
releaseType !== 'major' &&
(/\d\.\d\.\d\.?a(lpha\.)?\d+$/.test(current) || /\d\.\d\.\d\.?b(eta\.)?\d+$/.test(current))
) {
nextVersion = current.replace(/\d+$/, (match) => `${parseInt(match, 10) + 1}`);
const preReleaseVersion = current.match(preReleaseRegExp);
if (preReleaseVersion?.length) {
if (releaseType === 'major') {
nextVersion = current.replace(preReleaseVersion[1], '');
} else {
nextVersion = current.replace(/\d+$/, (match) => `${parseInt(match, 10) + 1}`);
}
} else if (current.endsWith('-SNAPSHOT')) {
// snapshots should not be bumped
nextVersion = current;
Expand All @@ -210,11 +214,13 @@ export async function decideReleaseStrategy({
commits,
languages,
major,
dryRun,
}: {
versions: VersionsBeforeBump;
commits: PassedCommit[];
languages: Language[];
major?: boolean;
dryRun?: boolean;
}): Promise<Versions> {
const versionsToPublish: Versions = {};

Expand Down Expand Up @@ -247,8 +253,7 @@ export async function decideReleaseStrategy({

console.log(`Deciding next version bump for ${lang}.`);

// allows forcing a client release
if (process.env.LOCAL_TEST_DEV) {
if (dryRun) {
nbGitDiff = 1;
}

Expand Down Expand Up @@ -362,8 +367,6 @@ async function getCommits(): Promise<{

/**
* Ensure the release environment is correct before triggering.
*
* You can bypass blocking checks by setting LOCAL_TEST_DEV to true.
*/
async function prepareGitEnvironment(): Promise<void> {
ensureGitHubToken();
Expand All @@ -372,19 +375,11 @@ async function prepareGitEnvironment(): Promise<void> {
await configureGitHubAuthor();
}

if (
!process.env.LOCAL_TEST_DEV &&
(await run('git rev-parse --abbrev-ref HEAD')) !== MAIN_BRANCH
) {
if ((await run('git rev-parse --abbrev-ref HEAD')) !== MAIN_BRANCH) {
throw new Error(`You can run this script only from \`${MAIN_BRANCH}\` branch.`);
}

if (
!process.env.LOCAL_TEST_DEV &&
(await getNbGitDiff({
head: null,
})) !== 0
) {
if ((await getNbGitDiff({ head: null })) !== 0) {
throw new Error('Working directory is not clean. Commit all the changes first.');
}

Expand Down Expand Up @@ -483,11 +478,15 @@ async function updateLTS(versions: Versions, withGraphs?: boolean): Promise<void
export async function createReleasePR({
languages,
major,
dryRun,
}: {
languages: Language[];
major?: boolean;
dryRun?: boolean;
}): Promise<void> {
await prepareGitEnvironment();
if (!dryRun) {
await prepareGitEnvironment();
}

console.log('Searching for commits since last release...');
const { validCommits, skippedCommits } = await getCommits();
Expand Down Expand Up @@ -538,6 +537,12 @@ export async function createReleasePR({

await updateLTS(versions, true);

if (dryRun) {
console.log(' > asked for a dryrun, stopping here');

return;
}

const headBranch = `chore/prepare-release-${TODAY}`;
console.log(`Switching to branch: ${headBranch}`);
if (await gitBranchExists(headBranch)) {
Expand All @@ -551,11 +556,7 @@ export async function createReleasePR({
console.log(`Pushing updated changes to: ${headBranch}`);
const commitMessage = generationCommitText.commitPrepareReleaseMessage;
await run('git add .');
if (process.env.LOCAL_TEST_DEV) {
await run(`git commit -m "${commitMessage} [skip ci]"`);
} else {
await run(`CI=false git commit -m "${commitMessage}"`);
}
await run(`CI=false git commit -m "${commitMessage}"`);

// cleanup all the changes to the generated files (the ones not commited because of the pre-commit hook)
await run(`git checkout .`);
Expand Down
3 changes: 3 additions & 0 deletions website/docs/contributing/release-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Once setup, you can run:

```bash
apic release

# or if you just want to see what it does without pushing the releases
apic release --dry-run
```

It will create [a release PR](https://github.com/algolia/api-clients-automation/pull/545).
Expand Down
Loading