- Notifications
You must be signed in to change notification settings - Fork 27
chore(ci): push to each repository on PR merge #257
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 15 commits
Commits
Show all changes
17 commits Select commit Hold shift + click to select a range
4bf4738 chore(ci): push to each repository on PR merge
eunjae-lee de75fd7 chore: separate spreadGeneration workflow
eunjae-lee 600fdc2 chore: remove unnecessary eslint disable comment
eunjae-lee 62a80fd chore: rename workflow
eunjae-lee 8f8539a chore: adjust abstraction
eunjae-lee 9b1ed93 Merge branch 'main' into chore/push
eunjae-lee 8950266 Merge branch 'main' into chore/push
eunjae-lee c9c4014 chore: include PR url in commit message
eunjae-lee 12c71f1 chore: move generation workflow to after codegen
eunjae-lee 9a38b2d chore: spread generation only on main
eunjae-lee 0657214 chore: spread generation only when codegen is successful
eunjae-lee 15d0d65 chore: spread generation as a part of codegen
eunjae-lee 3d7965a chore: apply author name and email to commits
eunjae-lee fab9012 chore: include coauthor in commit message
eunjae-lee e041d58 chore: add tests
eunjae-lee 3d5055d chore: add tests
eunjae-lee 73e3c04 Merge branch 'main' into chore/push
eunjae-lee 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
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,38 @@ | ||
| import execa from 'execa'; | ||
| | ||
| import { gitCommit } from '../common'; | ||
| | ||
| jest.mock('execa'); | ||
| | ||
| describe('gitCommit', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
| | ||
| it('commits with message', () => { | ||
| gitCommit({ message: 'chore: does something' }); | ||
| expect(execa).toHaveBeenCalledTimes(1); | ||
| expect(execa).toHaveBeenCalledWith( | ||
| 'git', | ||
| ['commit', '-m', 'chore: does something'], | ||
| { cwd: expect.any(String) } | ||
| ); | ||
| }); | ||
| | ||
| it('commits with co-author', () => { | ||
| gitCommit({ | ||
| message: 'chore: does something', | ||
| coauthor: { name: 'some', email: 'random@person.com' }, | ||
| }); | ||
| expect(execa).toHaveBeenCalledTimes(1); | ||
| expect(execa).toHaveBeenCalledWith( | ||
| 'git', | ||
| [ | ||
| 'commit', | ||
| '-m', | ||
| 'chore: does something\n\n\nCo-authored-by: some <random@person.com>', | ||
| ], | ||
| { cwd: expect.any(String) } | ||
| ); | ||
| }); | ||
| }); |
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,34 @@ | ||
| import { LANGUAGES } from '../../../common'; | ||
| import { decideWhereToSpread, cleanUpCommitMessage } from '../spreadGeneration'; | ||
| | ||
| describe('spread generation', () => { | ||
| it('skips in case of release commit', () => { | ||
| expect(decideWhereToSpread('chore: release 2022-03-15')).toEqual([]); | ||
| }); | ||
| | ||
| it('spreads to all if scope is missing', () => { | ||
| expect(decideWhereToSpread('chore: do something')).toEqual(LANGUAGES); | ||
| }); | ||
| | ||
| it('spreads to javascript if the scope is javascript', () => { | ||
| expect(decideWhereToSpread('fix(javascript): fix something')).toEqual([ | ||
| 'javascript', | ||
| ]); | ||
| }); | ||
| | ||
| it('spreads to all if scope is not specific language', () => { | ||
| ['cts', 'spec', 'script', 'ci'].forEach((scope) => { | ||
| expect(decideWhereToSpread(`fix(${scope}): fix something`)).toEqual( | ||
| LANGUAGES | ||
| ); | ||
| }); | ||
| }); | ||
| | ||
| it('removes pull-request number from commit message', () => { | ||
| expect( | ||
| cleanUpCommitMessage(`feat(ci): make ci push generated code (#244)`) | ||
| ).toEqual( | ||
| `feat(ci): make ci push generated code\n\nhttps://github.com/algolia/api-clients-automation/pull/244` | ||
| ); | ||
| }); | ||
| }); | ||
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,75 @@ | ||
| import { gitCommit, LANGUAGES, run, toAbsolutePath } from '../../common'; | ||
| import { getLanguageFolder } from '../../config'; | ||
| import { | ||
| cloneRepository, | ||
| configureGitHubAuthor, | ||
| OWNER, | ||
| REPO, | ||
| } from '../../release/common'; | ||
| | ||
| const GENERATED_MAIN_BRANCH = `generated/main`; | ||
| | ||
| export function decideWhereToSpread(commitMessage: string): string[] { | ||
| if (commitMessage.startsWith('chore: release')) { | ||
| return []; | ||
| } | ||
| | ||
| const result = commitMessage.match(/(.+)\((.+)\):/); | ||
| if (!result) { | ||
| // no scope | ||
| return LANGUAGES; | ||
| } | ||
| | ||
| const scope = result[2]; | ||
| return LANGUAGES.includes(scope) ? [scope] : LANGUAGES; | ||
| } | ||
| | ||
| export function cleanUpCommitMessage(commitMessage: string): string { | ||
| const result = commitMessage.match(/(.+)\s\(#(\d+)\)$/); | ||
| if (!result) { | ||
| return commitMessage; | ||
| } | ||
| | ||
| return [ | ||
| result[1], | ||
| `https://github.com/${OWNER}/${REPO}/pull/${result[2]}`, | ||
| ].join('\n\n'); | ||
| } | ||
| | ||
| async function spreadGeneration(): Promise<void> { | ||
| if (!process.env.GITHUB_TOKEN) { | ||
| throw new Error('Environment variable `GITHUB_TOKEN` does not exist.'); | ||
| } | ||
| | ||
| const lastCommitMessage = await run(`git log -1 --format="%s"`); | ||
| const name = (await run(`git log -1 --format="%an"`)).trim(); | ||
| const email = (await run(`git log -1 --format="%ae"`)).trim(); | ||
| const commitMessage = cleanUpCommitMessage(lastCommitMessage); | ||
shortcuts marked this conversation as resolved. Show resolved Hide resolved | ||
| const langs = decideWhereToSpread(lastCommitMessage); | ||
| | ||
| await run(`git checkout ${GENERATED_MAIN_BRANCH}`); | ||
| | ||
| for (const lang of langs) { | ||
| const { tempGitDir } = await cloneRepository({ | ||
| lang, | ||
| githubToken: process.env.GITHUB_TOKEN, | ||
| tempDir: process.env.RUNNER_TEMP!, | ||
| }); | ||
| | ||
| const clientPath = toAbsolutePath(getLanguageFolder(lang)); | ||
| await run(`cp -r ${clientPath}/ ${tempGitDir}`); | ||
| | ||
| await configureGitHubAuthor(tempGitDir); | ||
| await run(`git add .`, { cwd: tempGitDir }); | ||
shortcuts marked this conversation as resolved. Show resolved Hide resolved | ||
| await gitCommit({ | ||
| message: commitMessage, | ||
| coauthor: { name, email }, | ||
| cwd: tempGitDir, | ||
| }); | ||
| await run(`git push`, { cwd: tempGitDir }); | ||
| } | ||
| } | ||
| | ||
| if (require.main === module) { | ||
| spreadGeneration(); | ||
| } | ||
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
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
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
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
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.