-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
guides: add pushToAlgoliaWeb script (#3930)
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
- Loading branch information
1 parent
0387209
commit f95e8ac
Showing
4 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Push snippets to AlgoliaWeb | ||
|
||
on: workflow_dispatch | ||
|
||
jobs: | ||
push: | ||
name: Manual trigger push for onboarding guides | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import fsp from 'fs/promises'; | ||
import { resolve } from 'path'; | ||
|
||
import { | ||
configureGitHubAuthor, | ||
ensureGitHubToken, | ||
exists, | ||
getOctokit, | ||
gitBranchExists, | ||
gitCommit, | ||
LANGUAGES, | ||
OWNER, | ||
run, | ||
setVerbose, | ||
toAbsolutePath, | ||
} from '../../common.js'; | ||
import { getNbGitDiff } from '../utils.js'; | ||
|
||
import { getClientsConfigField } from '../../config.js'; | ||
import { commitStartRelease } from './text.js'; | ||
|
||
async function generateJSON(outputFile: string): Promise<void> { | ||
const guides = {}; | ||
for (const language of LANGUAGES) { | ||
if (!(await exists(toAbsolutePath(`docs/guides/${language}`)))) { | ||
continue; | ||
} | ||
|
||
const pathToGuides = toAbsolutePath( | ||
`docs/guides/${language}/${getClientsConfigField(language, ['snippets', 'outputFolder'])}`, | ||
); | ||
const files = await fsp.readdir(pathToGuides); | ||
for (const file of files) { | ||
const extension = getClientsConfigField(language, ['snippets', 'extension']); | ||
if (!file.endsWith(extension)) { | ||
continue; | ||
} | ||
|
||
const guideName = file.replaceAll(extension, ''); | ||
if (!guides[guideName]) { | ||
guides[guideName] = {}; | ||
} | ||
|
||
guides[guideName][language] = (await fsp.readFile(`${pathToGuides}/${file}`, 'utf-8')) | ||
.replace('ALGOLIA_APPLICATION_ID', 'YourApplicationID') | ||
.replace('ALGOLIA_API_KEY', 'YourWriteAPIKey') | ||
.replace('<YOUR_INDEX_NAME>', 'movies_index'); | ||
} | ||
} | ||
|
||
await fsp.writeFile(outputFile, JSON.stringify(guides, null, 2)); | ||
} | ||
|
||
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; | ||
} | ||
|
||
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}`); | ||
|
||
const outputFile = toAbsolutePath(`${tempGitDir}/_client/src/routes/launchpad/onboarding-snippets.json`); | ||
|
||
console.log(`Generating JSON output file from guides at path ${outputFile}`); | ||
|
||
await generateJSON(outputFile); | ||
|
||
console.log(`Pushing to ${OWNER}/${repository}`); | ||
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 }); | ||
|
||
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 generated guides'; | ||
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 guides.', | ||
].join('\n\n'), | ||
base: 'develop', | ||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters