Skip to content

feat(scripts): add script to generate github releases #3523

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 3 commits into from
Aug 19, 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
5 changes: 5 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,11 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

- name: Create GitHub release
run: yarn workspace scripts createGitHubReleases ${{ steps.spreadGeneration.outputs.PUSHED_LANGUAGES }}
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

- name: Push generation to the Algolia docs
run: yarn workspace scripts pushToAlgoliaDoc
env:
Expand Down
101 changes: 101 additions & 0 deletions scripts/ci/codegen/createGitHubReleases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-disable no-console */

import {
ensureGitHubToken,
getOctokit,
OWNER,
run,
setVerbose,
toAbsolutePath,
} from '../../common';
import { isPreRelease } from '../../release/sla';
import type { Language } from '../../types';
import { cloneRepository } from '../utils';

import { commitStartRelease } from './text';

async function createGitHubRelease(lang: Language): Promise<void> {
// **Full Changelog**:

const githubToken = ensureGitHubToken();

// fetch all release tags for this lang
const { tempGitDir } = await cloneRepository({
lang,
githubToken,
tempDir: process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'),
});
await run('git fetch --all --tags', { cwd: tempGitDir });

let tags = (
await run('git describe --abbrev=0 --tags $(git rev-list --tags) --always', { cwd: tempGitDir })
).split('\n');

if (tags.length === 0) {
throw new Error(`unable to find tags for language ${lang}`);
}

const newVersion = tags[0];
const isMajor = newVersion.endsWith('0.0');
let previousVersion = '';

// if we are not on the major, we create a release for the last version, otherwise we get every versions of the pre release
if (!isMajor) {
previousVersion = tags[1];
} else {
tags = tags.filter((tag) => tag.startsWith(newVersion) && isPreRelease(tag));
previousVersion = tags[tags.length - 1];
}

const repository = `algoliasearch-client-${lang}`;
const repositoryLink = `https://github.com/${OWNER}/${repository}`;
const content = `
New ${isMajor ? '**major** ' : ''}version released!

→ [Browse the Algolia documentation](https://www.algolia.com/doc/libraries/${lang})
→ [Browse the changelog](${repositoryLink}/blob/main/CHANGELOG.md)
→ [Browse the commits](${repositoryLink}/compare/${previousVersion}...${newVersion})`;

try {
await getOctokit().repos.createRelease({
owner: OWNER,
repo: repository,
tag_name: newVersion,
name: newVersion,
body: content,
});

console.log(`release for ${lang} created: ${repositoryLink}/releases/tag/${newVersion}`);
} catch (e: any) {
if (e.status === 422) {
console.log(
`release for ${lang} already exist: ${repositoryLink}/releases/tag/${newVersion}`,
);
} else {
throw new Error(e);
}
}
}

async function createGitHubReleases(languagesReleased: Language[]): Promise<void> {
const lastCommitMessage = await run('git log -1 --format="%s"');

if (!lastCommitMessage.startsWith(commitStartRelease)) {
console.log('No release commit found, skipping release generation');

return;
}

await Promise.all(
languagesReleased.map(async (lang) => {
console.log(`> creating GitHub release for ${lang}`);

return await createGitHubRelease(lang);
}),
);
}

if (import.meta.url.endsWith(process.argv[1])) {
setVerbose(false);
createGitHubReleases(process.argv.slice(2) as Language[]);
}
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"build": "yarn build:cli & yarn build:actions",
"build:actions": "cd ci/actions/restore-artifacts && esbuild --bundle --format=cjs --minify --platform=node --outfile=builddir/index.cjs --log-level=error src/index.ts",
"build:cli": "esbuild --bundle --format=esm --sourcemap --platform=node --packages=external --outdir=dist --log-level=error cli/index.ts ci/codegen/*.ts ci/githubActions/*.ts",
"createGitHubReleases": "yarn runScript dist/ci/codegen/createGitHubReleases.js",
"createMatrix": "yarn runScript dist/ci/githubActions/createMatrix.js",
"lint": "eslint --ext=ts,js,mjs,cjs .",
"lint:deadcode": "knip",
Expand Down
2 changes: 1 addition & 1 deletion scripts/release/sla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function getMajorMinor(lang: Language, version: string): { major: number; minor:
return { major: parseInt(matches[1], 10), minor: parseInt(matches[2], 10) };
}

function isPreRelease(version: string): boolean {
export function isPreRelease(version: string): boolean {
return (
version.match(preReleaseRegExp) !== null ||
semver.prerelease(version) !== null ||
Expand Down
Loading