-
Notifications
You must be signed in to change notification settings - Fork 27
guides: add pushToAlgoliaWeb script #3930
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
59d22e9
added pushToAlgoliaWeb ci script
levimichael 6ed658b
Update .github/workflows/push-to-algolia-web.yml
levimichael d2e2357
Update scripts/ci/codegen/pushToAlgoliaWeb.ts
levimichael 3da4814
pr review changes
levimichael fe9d94e
Merge branch 'main' into feat/ci-push-to-algolia-web
shortcuts 6c0d9c9
chore: programmatic file discovery
shortcuts cd815ad
chore: lint
shortcuts fe05323
Merge branch 'main' into feat/ci-push-to-algolia-web
shortcuts bbea872
Merge branch 'main' into feat/ci-push-to-algolia-web
shortcuts 00c3633
chore: push on release
shortcuts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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: | ||
| release: | ||
| name: Scheduled Release | ||
| 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 hidden or 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,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.
Outdated
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
|
||
|
|
||
| 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
|
||
| }; | ||
|
|
||
| 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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
| ].join('\n\n'), | ||
| base: 'master', | ||
levimichael marked this conversation as resolved.
Outdated
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(); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.