Skip to content

Commit cd5ac80

Browse files
committed
feat(scripts): push to docs on release
1 parent 7f53d10 commit cd5ac80

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

.github/workflows/check.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,12 @@ jobs:
431431
env:
432432
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
433433

434+
- name: Push specs to algolia documentation
435+
if: github.ref == 'refs/heads/main'
436+
run: yarn workspace scripts pushToAlgoliaDoc
437+
env:
438+
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}
439+
434440
- name: Set output
435441
id: setoutput
436442
run: echo "success=true" >> "$GITHUB_OUTPUT"
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* eslint-disable no-console */
2+
import fsp from 'fs/promises';
3+
import { resolve } from 'path';
4+
5+
import {
6+
emptyDirExceptForDotGit,
7+
gitCommit,
8+
run,
9+
toAbsolutePath,
10+
ensureGitHubToken,
11+
OWNER,
12+
configureGitHubAuthor,
13+
getOctokit,
14+
setVerbose,
15+
} from '../../common.js';
16+
import { getNbGitDiff } from '../utils.js';
17+
18+
import { commitStartRelease } from './text.js';
19+
20+
async function pushToAlgoliaDoc(): Promise<void> {
21+
const githubToken = ensureGitHubToken();
22+
23+
const repository = 'doc';
24+
const lastCommitMessage = await run('git log -1 --format="%s"');
25+
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
26+
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
27+
.split('\n')
28+
.map((coAuthor) => coAuthor.trim())
29+
.filter(Boolean);
30+
31+
if (!lastCommitMessage.startsWith(commitStartRelease)) {
32+
return;
33+
}
34+
35+
console.log(`Pushing to ${OWNER}/${repository}`);
36+
37+
const targetBranch = 'feat/automated-update-from-api-clients-automation-repository';
38+
const githubURL = `https://${githubToken}:${githubToken}@github.com/${OWNER}/${repository}`;
39+
const tempGitDir = resolve(
40+
process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'),
41+
repository,
42+
);
43+
await fsp.rm(tempGitDir, { force: true, recursive: true });
44+
await run(`git clone --depth 1 ${githubURL} ${tempGitDir}`);
45+
await run(`git checkout -B ${targetBranch}`, { cwd: tempGitDir });
46+
47+
const dest = toAbsolutePath(`${tempGitDir}/app_data/api/specs`);
48+
await emptyDirExceptForDotGit(dest);
49+
await run(`cp ${toAbsolutePath('specs/bundled/*.doc.yml')} ${dest}`);
50+
await run(`cp ${toAbsolutePath('config/release.config.json')} ${dest}`);
51+
await run(`cp ${toAbsolutePath('website/src/generated/*.js')} ${dest}`);
52+
await run(`cp ${toAbsolutePath('website/static/img/*-sla.png')} ${dest}`);
53+
54+
if ((await getNbGitDiff({ head: null, cwd: tempGitDir })) === 0) {
55+
console.log(`❎ Skipping push docs because there is no change.`);
56+
57+
return;
58+
}
59+
60+
await configureGitHubAuthor(tempGitDir);
61+
62+
const message = 'feat(clients): automatic update from api-clients-automation repository';
63+
await run('git add .', { cwd: tempGitDir });
64+
await gitCommit({
65+
message,
66+
coAuthors: [author, ...coAuthors],
67+
cwd: tempGitDir,
68+
});
69+
await run(`git push -f -u origin ${targetBranch}`, { cwd: tempGitDir });
70+
71+
console.log(`Creating pull request on ${OWNER}/${repository}...`);
72+
const octokit = getOctokit();
73+
const { data } = await octokit.pulls.create({
74+
owner: OWNER,
75+
repo: repository,
76+
title: message,
77+
body: [
78+
'This PR is automatically created by https://github.com/algolia/api-clients-automation',
79+
'It contains the latest released OpenAPI specs, the release SLA dates and PNGs, and the generated code snippets.',
80+
].join('\n\n'),
81+
base: 'master',
82+
head: targetBranch,
83+
});
84+
85+
console.log(`Pull request created on ${OWNER}/${repository}`);
86+
console.log(` > ${data.url}`);
87+
}
88+
89+
if (import.meta.url.endsWith(process.argv[1])) {
90+
setVerbose(false);
91+
pushToAlgoliaDoc();
92+
}

scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"lint:deadcode": "knip",
1313
"pre-commit": "node ./ci/husky/pre-commit.mjs",
1414
"pushGeneratedCode": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/pushGeneratedCode.js",
15+
"pushToAlgoliaDoc": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/pushToAlgoliaDoc.js",
1516
"setRunVariables": "NODE_NO_WARNINGS=1 node dist/scripts/ci/githubActions/setRunVariables.js",
1617
"spreadGeneration": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/spreadGeneration.js",
1718
"start": "NODE_NO_WARNINGS=1 node dist/scripts/cli/index.js",

0 commit comments

Comments
 (0)