-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ci): push to each repository on PR merge (#257)
* chore(ci): push to each repository on PR merge * chore: separate spreadGeneration workflow * chore: remove unnecessary eslint disable comment * chore: rename workflow * chore: adjust abstraction * chore: include PR url in commit message * chore: move generation workflow to after codegen * chore: spread generation only on main * chore: spread generation only when codegen is successful * chore: spread generation as a part of codegen * chore: apply author name and email to commits * chore: include coauthor in commit message * chore: add tests * chore: add tests
- Loading branch information
1 parent
ca222a7
commit 6a1b653
Showing
8 changed files
with
240 additions
and
15 deletions.
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,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 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,48 @@ | ||
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` | ||
); | ||
}); | ||
|
||
it('keeps the commit message even if it does not have PR number', () => { | ||
const commitMessage = `feat(ci): make ci push generated code`; | ||
expect(cleanUpCommitMessage(commitMessage)).toEqual(commitMessage); | ||
}); | ||
|
||
it('cleans up correctly even if the title contains a url', () => { | ||
const commitMessage = `fix(java): solve oneOf using a custom generator https://algolia.atlassian.net/browse/APIC-123 (#200)`; | ||
expect(cleanUpCommitMessage(commitMessage)).toMatchInlineSnapshot(` | ||
"fix(java): solve oneOf using a custom generator https://algolia.atlassian.net/browse/APIC-123 | ||
https://github.com/algolia/api-clients-automation/pull/200" | ||
`); | ||
}); | ||
}); |
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,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); | ||
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 }); | ||
await gitCommit({ | ||
message: commitMessage, | ||
coauthor: { name, email }, | ||
cwd: tempGitDir, | ||
}); | ||
await run(`git push`, { cwd: tempGitDir }); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
spreadGeneration(); | ||
} |
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
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