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