|
| 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 | +} |
0 commit comments