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

guides: add pushToAlgoliaWeb script #3930

Merged
merged 10 commits into from
Oct 9, 2024
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:
release:
name: Scheduled Release
levimichael marked this conversation as resolved.
Show resolved Hide resolved
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
120 changes: 120 additions & 0 deletions scripts/ci/codegen/pushToAlgoliaWeb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import fsp from 'fs/promises';

import { resolve } from 'path';

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

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

const languageFiles = {
levimichael marked this conversation as resolved.
Show resolved Hide resolved
csharp: 'guides/csharp/src/saveObjectsMovies.cs',
go: 'guides/go/src/saveObjectsMovies.go',
java: 'guides/java/src/test/java/com/algolia/saveObjectsMovies.java',
javascript: 'guides/javascript/src/saveObjectsMovies.ts',
kotlin: 'guides/kotlin/src/main/kotlin/com/algolia/snippets/saveObjectsMovies.kt',
php: 'guides/php/src/saveObjectsMovies.php',
python: 'guides/python/saveObjectsMovies.py',
ruby: 'guides/ruby/saveObjectsMovies.rb',
scala: 'guides/scala/src/main/scala/saveObjectsMovies.scala',
swift: 'guides/swift/Sources/saveObjectsMovies.swift',
};
const generateJSON = async (outputFile: string): Promise<void> => {
const filesPromises = Object.entries(languageFiles).map(async (p) => {
const snippet = await fsp.readFile(toAbsolutePath(p[1]), 'utf-8');

Check warning on line 34 in scripts/ci/codegen/pushToAlgoliaWeb.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

scripts/ci/codegen/pushToAlgoliaWeb.ts#L34

Detected that function argument `p` has entered the fs module.

Check warning on line 34 in scripts/ci/codegen/pushToAlgoliaWeb.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

scripts/ci/codegen/pushToAlgoliaWeb.ts#L34

The application dynamically constructs file or path information.

return [
[p[0]],
snippet
.replace('ALGOLIA_APPLICATION_ID', 'YourApplicationID')
.replace('ALGOLIA_API_KEY', 'YourWriteAPIKey')
.replace('<YOUR_INDEX_NAME>', 'movies_index'),
];
});

const files = await Promise.all(filesPromises);

await fsp.writeFile(outputFile, JSON.stringify(Object.fromEntries(files), null, 2));

Check warning on line 47 in scripts/ci/codegen/pushToAlgoliaWeb.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

scripts/ci/codegen/pushToAlgoliaWeb.ts#L47

Detected that function argument `outputFile` has entered the fs module.

Check warning on line 47 in scripts/ci/codegen/pushToAlgoliaWeb.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

scripts/ci/codegen/pushToAlgoliaWeb.ts#L47

The application dynamically constructs file or path information.
};

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;
}

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}`);
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 });

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

await generateJSON(pathToSnippets);

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 specs and supported versions';
levimichael marked this conversation as resolved.
Show resolved Hide resolved
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 code snippets.',
levimichael marked this conversation as resolved.
Show resolved Hide resolved
].join('\n\n'),
base: 'master',
levimichael marked this conversation as resolved.
Show resolved Hide resolved
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
Loading