Skip to content

Commit

Permalink
guides: add pushToAlgoliaWeb script (#3930)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Vannicatte <[email protected]>
  • Loading branch information
levimichael and shortcuts authored Oct 9, 2024
1 parent 0387209 commit f95e8ac
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,12 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

- name: Push generation to the Algolia docs
- name: Push specs and snippets to algolia/doc
run: yarn workspace scripts pushToAlgoliaDoc
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

- name: Push guides to algolia/AlgoliaWeb
run: yarn workspace scripts pushToAlgoliaWeb
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
24 changes: 24 additions & 0 deletions .github/workflows/push-to-algolia-web.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Push snippets to AlgoliaWeb

on: workflow_dispatch

jobs:
push:
name: Manual trigger push for onboarding guides
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: main

- name: Setup
id: setup
uses: ./.github/actions/setup
with:
type: minimal

- run: yarn workspace scripts pushToAlgoliaWeb
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
FORCE: true
126 changes: 126 additions & 0 deletions scripts/ci/codegen/pushToAlgoliaWeb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import fsp from 'fs/promises';
import { resolve } from 'path';

import {
configureGitHubAuthor,
ensureGitHubToken,
exists,
getOctokit,
gitBranchExists,
gitCommit,
LANGUAGES,
OWNER,
run,
setVerbose,
toAbsolutePath,
} from '../../common.js';
import { getNbGitDiff } from '../utils.js';

import { getClientsConfigField } from '../../config.js';
import { commitStartRelease } from './text.js';

async function generateJSON(outputFile: string): Promise<void> {
const guides = {};
for (const language of LANGUAGES) {
if (!(await exists(toAbsolutePath(`docs/guides/${language}`)))) {
continue;
}

const pathToGuides = toAbsolutePath(
`docs/guides/${language}/${getClientsConfigField(language, ['snippets', 'outputFolder'])}`,
);
const files = await fsp.readdir(pathToGuides);
for (const file of files) {
const extension = getClientsConfigField(language, ['snippets', 'extension']);
if (!file.endsWith(extension)) {
continue;
}

const guideName = file.replaceAll(extension, '');
if (!guides[guideName]) {
guides[guideName] = {};
}

guides[guideName][language] = (await fsp.readFile(`${pathToGuides}/${file}`, 'utf-8'))
.replace('ALGOLIA_APPLICATION_ID', 'YourApplicationID')
.replace('ALGOLIA_API_KEY', 'YourWriteAPIKey')
.replace('<YOUR_INDEX_NAME>', 'movies_index');
}
}

await fsp.writeFile(outputFile, JSON.stringify(guides, null, 2));
}

async function pushToAlgoliaWeb(): Promise<void> {
const githubToken = ensureGitHubToken();

const repository = 'AlgoliaWeb';
const lastCommitMessage = await run('git log -1 --format="%s"');
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
.split('\n')
.map((coAuthor) => coAuthor.trim())
.filter(Boolean);

if (!process.env.FORCE && !lastCommitMessage.startsWith(commitStartRelease)) {
return;
}

const targetBranch = 'feat/automated-update-from-api-clients-automation-repository';
const githubURL = `https://${githubToken}:${githubToken}@github.com/${OWNER}/${repository}`;
const tempGitDir = resolve(process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'), repository);
await fsp.rm(tempGitDir, { force: true, recursive: true });
await run(`git clone --depth 1 ${githubURL} ${tempGitDir}`);

const outputFile = toAbsolutePath(`${tempGitDir}/_client/src/routes/launchpad/onboarding-snippets.json`);

console.log(`Generating JSON output file from guides at path ${outputFile}`);

await generateJSON(outputFile);

console.log(`Pushing to ${OWNER}/${repository}`);
if (await gitBranchExists(targetBranch, tempGitDir)) {
await run(`git fetch origin ${targetBranch}`, { cwd: tempGitDir });
await run(`git push -d origin ${targetBranch}`, { cwd: tempGitDir });
}
await run(`git checkout -B ${targetBranch}`, { cwd: tempGitDir });

if ((await getNbGitDiff({ head: null, cwd: tempGitDir })) === 0) {
console.log('❎ Skipping push to AlgoliaWeb because there is no change.');

return;
}

await configureGitHubAuthor(tempGitDir);

const message = 'feat: update generated guides';
await run('git add .', { cwd: tempGitDir });
await gitCommit({
message,
coAuthors: [author, ...coAuthors],
cwd: tempGitDir,
});
await run(`git push -f -u origin ${targetBranch}`, { cwd: tempGitDir });

console.log(`Creating pull request on ${OWNER}/${repository}...`);
const octokit = getOctokit();
const { data } = await octokit.pulls.create({
owner: OWNER,
repo: repository,
title: message,
body: [
'This PR is automatically created by https://github.com/algolia/api-clients-automation',
'It contains the latest generated guides.',
].join('\n\n'),
base: 'develop',
head: targetBranch,
});

console.log(`Pull request created on ${OWNER}/${repository}`);
console.log(` > ${data.url}`);
}

if (import.meta.url.endsWith(process.argv[1])) {
setVerbose(false);
pushToAlgoliaWeb();
}
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"pre-commit": "node ./ci/husky/pre-commit.mjs",
"pushGeneratedCode": "yarn runScript dist/ci/codegen/pushGeneratedCode.js",
"pushToAlgoliaDoc": "yarn runScript dist/ci/codegen/pushToAlgoliaDoc.js",
"pushToAlgoliaWeb": "yarn runScript dist/ci/codegen/pushToAlgoliaWeb.js",
"runScript": "NODE_NO_WARNINGS=1 node --enable-source-maps",
"setRunVariables": "yarn runScript dist/ci/githubActions/setRunVariables.js",
"spreadGeneration": "yarn runScript dist/ci/codegen/spreadGeneration.js",
Expand Down

0 comments on commit f95e8ac

Please sign in to comment.