Skip to content

Commit 59d22e9

Browse files
committed
added pushToAlgoliaWeb ci script
1 parent 6a2f9f8 commit 59d22e9

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
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+
release:
7+
name: Scheduled Release
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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import fsp from 'fs/promises';
2+
3+
import { resolve } from 'path';
4+
5+
import {
6+
configureGitHubAuthor,
7+
ensureGitHubToken,
8+
getOctokit,
9+
gitBranchExists,
10+
gitCommit,
11+
OWNER,
12+
run,
13+
setVerbose,
14+
toAbsolutePath,
15+
} from '../../common.js';
16+
import { getNbGitDiff } from '../utils.js';
17+
18+
import { commitStartRelease } from './text.js';
19+
20+
const languageFiles = {
21+
csharp: 'guides/csharp/src/saveObjectsMovies.cs',
22+
go: 'guides/go/src/saveObjectsMovies.go',
23+
java: 'guides/java/src/test/java/com/algolia/saveObjectsMovies.java',
24+
javascript: 'guides/javascript/src/saveObjectsMovies.ts',
25+
kotlin: 'guides/kotlin/src/main/kotlin/com/algolia/snippets/saveObjectsMovies.kt',
26+
php: 'guides/php/src/saveObjectsMovies.php',
27+
python: 'guides/python/saveObjectsMovies.py',
28+
ruby: 'guides/ruby/saveObjectsMovies.rb',
29+
scala: 'guides/scala/src/main/scala/saveObjectsMovies.scala',
30+
swift: 'guides/swift/Sources/saveObjectsMovies.swift',
31+
};
32+
const generateJSON = async (outputFile: string): Promise<void> => {
33+
const filesPromises = Object.entries(languageFiles).map(async (p) => {
34+
const snippet = await fsp.readFile(toAbsolutePath(p[1]), 'utf-8');
35+
36+
return [
37+
[p[0]],
38+
snippet
39+
.replace('ALGOLIA_APPLICATION_ID', 'YourApplicationID')
40+
.replace('ALGOLIA_API_KEY', 'YourWriteAPIKey')
41+
.replace('<YOUR_INDEX_NAME>', 'movies_index'),
42+
];
43+
});
44+
45+
const files = await Promise.all(filesPromises);
46+
47+
await fsp.writeFile(outputFile, JSON.stringify(Object.fromEntries(files), null, 2));
48+
};
49+
50+
async function pushToAlgoliaWeb(): Promise<void> {
51+
const githubToken = ensureGitHubToken();
52+
53+
const repository = 'AlgoliaWeb';
54+
const lastCommitMessage = await run('git log -1 --format="%s"');
55+
const author = (await run('git log -1 --format="Co-authored-by: %an <%ae>"')).trim();
56+
const coAuthors = (await run('git log -1 --format="%(trailers:key=Co-authored-by)"'))
57+
.split('\n')
58+
.map((coAuthor) => coAuthor.trim())
59+
.filter(Boolean);
60+
61+
if (!process.env.FORCE && !lastCommitMessage.startsWith(commitStartRelease)) {
62+
return;
63+
}
64+
65+
console.log(`Pushing to ${OWNER}/${repository}`);
66+
67+
const targetBranch = 'feat/automated-update-from-api-clients-automation-repository';
68+
const githubURL = `https://${githubToken}:${githubToken}@github.com/${OWNER}/${repository}`;
69+
const tempGitDir = resolve(process.env.RUNNER_TEMP! || toAbsolutePath('foo/local/test'), repository);
70+
await fsp.rm(tempGitDir, { force: true, recursive: true });
71+
await run(`git clone --depth 1 ${githubURL} ${tempGitDir}`);
72+
if (await gitBranchExists(targetBranch, tempGitDir)) {
73+
await run(`git fetch origin ${targetBranch}`, { cwd: tempGitDir });
74+
await run(`git push -d origin ${targetBranch}`, { cwd: tempGitDir });
75+
}
76+
await run(`git checkout -B ${targetBranch}`, { cwd: tempGitDir });
77+
78+
const pathToSnippets = toAbsolutePath(`${tempGitDir}/_client/src/routes/launchpad/onboarding-snippets.json`);
79+
80+
await generateJSON(pathToSnippets);
81+
82+
if ((await getNbGitDiff({ head: null, cwd: tempGitDir })) === 0) {
83+
console.log('❎ Skipping push to AlgoliaWeb because there is no change.');
84+
85+
return;
86+
}
87+
88+
await configureGitHubAuthor(tempGitDir);
89+
90+
const message = 'feat: update specs and supported versions';
91+
await run('git add .', { cwd: tempGitDir });
92+
await gitCommit({
93+
message,
94+
coAuthors: [author, ...coAuthors],
95+
cwd: tempGitDir,
96+
});
97+
await run(`git push -f -u origin ${targetBranch}`, { cwd: tempGitDir });
98+
99+
console.log(`Creating pull request on ${OWNER}/${repository}...`);
100+
const octokit = getOctokit();
101+
const { data } = await octokit.pulls.create({
102+
owner: OWNER,
103+
repo: repository,
104+
title: message,
105+
body: [
106+
'This PR is automatically created by https://github.com/algolia/api-clients-automation',
107+
'It contains the latest generated code snippets.',
108+
].join('\n\n'),
109+
base: 'master',
110+
head: targetBranch,
111+
});
112+
113+
console.log(`Pull request created on ${OWNER}/${repository}`);
114+
console.log(` > ${data.url}`);
115+
}
116+
117+
if (import.meta.url.endsWith(process.argv[1])) {
118+
setVerbose(false);
119+
pushToAlgoliaWeb();
120+
}

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)