Skip to content

Commit f95e8ac

Browse files
guides: add pushToAlgoliaWeb script (#3930)
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 0387209 commit f95e8ac

File tree

4 files changed

+157
-1
lines changed

4 files changed

+157
-1
lines changed

.github/workflows/check.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,12 @@ jobs:
689689
env:
690690
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
691691

692-
- name: Push generation to the Algolia docs
692+
- name: Push specs and snippets to algolia/doc
693693
run: yarn workspace scripts pushToAlgoliaDoc
694694
env:
695695
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
696+
697+
- name: Push guides to algolia/AlgoliaWeb
698+
run: yarn workspace scripts pushToAlgoliaWeb
699+
env:
700+
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Push snippets to AlgoliaWeb
2+
3+
on: workflow_dispatch
4+
5+
jobs:
6+
push:
7+
name: Manual trigger push for onboarding guides
8+
runs-on: ubuntu-22.04
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
fetch-depth: 0
13+
ref: main
14+
15+
- name: Setup
16+
id: setup
17+
uses: ./.github/actions/setup
18+
with:
19+
type: minimal
20+
21+
- run: yarn workspace scripts pushToAlgoliaWeb
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
24+
FORCE: true
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import fsp from 'fs/promises';
2+
import { resolve } from 'path';
3+
4+
import {
5+
configureGitHubAuthor,
6+
ensureGitHubToken,
7+
exists,
8+
getOctokit,
9+
gitBranchExists,
10+
gitCommit,
11+
LANGUAGES,
12+
OWNER,
13+
run,
14+
setVerbose,
15+
toAbsolutePath,
16+
} from '../../common.js';
17+
import { getNbGitDiff } from '../utils.js';
18+
19+
import { getClientsConfigField } from '../../config.js';
20+
import { commitStartRelease } from './text.js';
21+
22+
async function generateJSON(outputFile: string): Promise<void> {
23+
const guides = {};
24+
for (const language of LANGUAGES) {
25+
if (!(await exists(toAbsolutePath(`docs/guides/${language}`)))) {
26+
continue;
27+
}
28+
29+
const pathToGuides = toAbsolutePath(
30+
`docs/guides/${language}/${getClientsConfigField(language, ['snippets', 'outputFolder'])}`,
31+
);
32+
const files = await fsp.readdir(pathToGuides);
33+
for (const file of files) {
34+
const extension = getClientsConfigField(language, ['snippets', 'extension']);
35+
if (!file.endsWith(extension)) {
36+
continue;
37+
}
38+
39+
const guideName = file.replaceAll(extension, '');
40+
if (!guides[guideName]) {
41+
guides[guideName] = {};
42+
}
43+
44+
guides[guideName][language] = (await fsp.readFile(`${pathToGuides}/${file}`, 'utf-8'))
45+
.replace('ALGOLIA_APPLICATION_ID', 'YourApplicationID')
46+
.replace('ALGOLIA_API_KEY', 'YourWriteAPIKey')
47+
.replace('<YOUR_INDEX_NAME>', 'movies_index');
48+
}
49+
}
50+
51+
await fsp.writeFile(outputFile, JSON.stringify(guides, null, 2));
52+
}
53+
54+
async function pushToAlgoliaWeb(): Promise<void> {
55+
const githubToken = ensureGitHubToken();
56+
57+
const repository = 'AlgoliaWeb';
58+
const lastCommitMessage = await run('git log -1 --format="%s"');
59+
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
60+
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
61+
.split('\n')
62+
.map((coAuthor) => coAuthor.trim())
63+
.filter(Boolean);
64+
65+
if (!process.env.FORCE && !lastCommitMessage.startsWith(commitStartRelease)) {
66+
return;
67+
}
68+
69+
const targetBranch = 'feat/automated-update-from-api-clients-automation-repository';
70+
const githubURL = `https://${githubToken}:${githubToken}@github.com/${OWNER}/${repository}`;
71+
const tempGitDir = resolve(process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'), repository);
72+
await fsp.rm(tempGitDir, { force: true, recursive: true });
73+
await run(`git clone --depth 1 ${githubURL} ${tempGitDir}`);
74+
75+
const outputFile = toAbsolutePath(`${tempGitDir}/_client/src/routes/launchpad/onboarding-snippets.json`);
76+
77+
console.log(`Generating JSON output file from guides at path ${outputFile}`);
78+
79+
await generateJSON(outputFile);
80+
81+
console.log(`Pushing to ${OWNER}/${repository}`);
82+
if (await gitBranchExists(targetBranch, tempGitDir)) {
83+
await run(`git fetch origin ${targetBranch}`, { cwd: tempGitDir });
84+
await run(`git push -d origin ${targetBranch}`, { cwd: tempGitDir });
85+
}
86+
await run(`git checkout -B ${targetBranch}`, { cwd: tempGitDir });
87+
88+
if ((await getNbGitDiff({ head: null, cwd: tempGitDir })) === 0) {
89+
console.log('❎ Skipping push to AlgoliaWeb because there is no change.');
90+
91+
return;
92+
}
93+
94+
await configureGitHubAuthor(tempGitDir);
95+
96+
const message = 'feat: update generated guides';
97+
await run('git add .', { cwd: tempGitDir });
98+
await gitCommit({
99+
message,
100+
coAuthors: [author, ...coAuthors],
101+
cwd: tempGitDir,
102+
});
103+
await run(`git push -f -u origin ${targetBranch}`, { cwd: tempGitDir });
104+
105+
console.log(`Creating pull request on ${OWNER}/${repository}...`);
106+
const octokit = getOctokit();
107+
const { data } = await octokit.pulls.create({
108+
owner: OWNER,
109+
repo: repository,
110+
title: message,
111+
body: [
112+
'This PR is automatically created by https://github.com/algolia/api-clients-automation',
113+
'It contains the latest generated guides.',
114+
].join('\n\n'),
115+
base: 'develop',
116+
head: targetBranch,
117+
});
118+
119+
console.log(`Pull request created on ${OWNER}/${repository}`);
120+
console.log(` > ${data.url}`);
121+
}
122+
123+
if (import.meta.url.endsWith(process.argv[1])) {
124+
setVerbose(false);
125+
pushToAlgoliaWeb();
126+
}

scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"pre-commit": "node ./ci/husky/pre-commit.mjs",
1414
"pushGeneratedCode": "yarn runScript dist/ci/codegen/pushGeneratedCode.js",
1515
"pushToAlgoliaDoc": "yarn runScript dist/ci/codegen/pushToAlgoliaDoc.js",
16+
"pushToAlgoliaWeb": "yarn runScript dist/ci/codegen/pushToAlgoliaWeb.js",
1617
"runScript": "NODE_NO_WARNINGS=1 node --enable-source-maps",
1718
"setRunVariables": "yarn runScript dist/ci/githubActions/setRunVariables.js",
1819
"spreadGeneration": "yarn runScript dist/ci/codegen/spreadGeneration.js",

0 commit comments

Comments
 (0)