Skip to content
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

feat(scripts): push to docs on release #3196

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

- name: Push specs to algolia documentation
if: github.ref == 'refs/heads/main'
run: yarn workspace scripts pushToAlgoliaDoc
env:
GITHUB_TOKEN: ${{ secrets.ALGOLIA_BOT_TOKEN }}

- name: Set output
id: setoutput
run: echo "success=true" >> "$GITHUB_OUTPUT"
Expand Down
8 changes: 7 additions & 1 deletion scripts/buildSpecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,15 @@ async function transformBundle({
: ({} as SnippetSamples);

if (docs) {
const snippets = transformCodeSamplesToGuideMethods(JSON.parse(JSON.stringify(snippetSamples)));
// the JS file will be removed once algolia/doc leverages the JSON one
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should support the JSON one you mean, the js one is for our doc.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes but the JS one will be removed once the doc is live that's what i meant

Copy link
Collaborator

Choose a reason for hiding this comment

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

so we plan on removing our doc entirely ? :(

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup at least for GA, @kai687 has great plans for it!

await fsp.writeFile(
toAbsolutePath(`website/src/generated/${clientName}-snippets.js`),
`export const snippets = ${transformCodeSamplesToGuideMethods(JSON.parse(JSON.stringify(snippetSamples)))}`,
`export const snippets = ${snippets}`,
);
await fsp.writeFile(
toAbsolutePath(`website/src/generated/${clientName}-snippets.json`),
snippets,
);
}

Expand Down
92 changes: 92 additions & 0 deletions scripts/ci/codegen/pushToAlgoliaDoc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-disable no-console */
Copy link
Collaborator

Choose a reason for hiding this comment

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

which version of the file did you take for this ? there was a lot of bug fixes done to the previous one

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the last but you are right I'll double check!

import fsp from 'fs/promises';
import { resolve } from 'path';

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

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

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

const repository = 'doc';
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.DRY_RUN && !lastCommitMessage.startsWith(commitStartRelease)) {
return;
}

console.log(`Pushing to ${OWNER}/${repository}`);

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}`);
await run(`git checkout -B ${targetBranch}`, { cwd: tempGitDir });

const dest = toAbsolutePath(`${tempGitDir}/app_data/api/specs`);
await emptyDirExceptForDotGit(dest);
await run(`cp ${toAbsolutePath('specs/bundled/*.doc.yml')} ${dest}`);
await run(`cp ${toAbsolutePath('config/release.config.json')} ${dest}`);
await run(`cp ${toAbsolutePath('website/src/generated/*.json')} ${dest}`);
Comment on lines +49 to +51
Copy link
Collaborator

Choose a reason for hiding this comment

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

the snippets are already contained in the bundled spec, is it necessary to also send the json file ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup it's so that they can leverage the JSONs for the guides like we did!

Copy link
Collaborator

Choose a reason for hiding this comment

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

we had to do this to have both the snippets for redoc, and the snippets for custom guide, are they using the same techno ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup! Given the time constraint it seems like the better, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't know how well this will work with ruby but why not !

Copy link
Member Author

@shortcuts shortcuts Jun 18, 2024

Choose a reason for hiding this comment

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

I believe fine, since it's simple json input but at least it gives @kai687 the opportunity to try the current solution

await run(`cp ${toAbsolutePath('website/static/img/*-sla.png')} ${dest}`);

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

return;
}

await configureGitHubAuthor(tempGitDir);

const message = 'feat(clients): automatic update from api-clients-automation repository';
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 released OpenAPI specs, the release SLA dates and PNGs, and the generated code snippets.',
].join('\n\n'),
base: 'master',
head: targetBranch,
});

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

if (import.meta.url.endsWith(process.argv[1])) {
setVerbose(false);
pushToAlgoliaDoc();
}
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"lint:deadcode": "knip",
"pre-commit": "node ./ci/husky/pre-commit.mjs",
"pushGeneratedCode": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/pushGeneratedCode.js",
"pushToAlgoliaDoc": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/pushToAlgoliaDoc.js",
"setRunVariables": "NODE_NO_WARNINGS=1 node dist/scripts/ci/githubActions/setRunVariables.js",
"spreadGeneration": "NODE_NO_WARNINGS=1 node dist/scripts/ci/codegen/spreadGeneration.js",
"start": "NODE_NO_WARNINGS=1 node dist/scripts/cli/index.js",
Expand Down
1 change: 1 addition & 0 deletions website/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ yarn-error.log*
specs

src/generated/*.js
src/generated/*.json
Loading