Skip to content

Commit

Permalink
chore: keep co-authors in spread commit (#410)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Apr 21, 2022
1 parent 91dcfb3 commit f360137
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
19 changes: 17 additions & 2 deletions scripts/__tests__/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,32 @@ describe('gitCommit', () => {
});

it('commits with co-author', () => {
// This reflects how it can be retrieved from git commands.
const author = `Co-authored-by: them <[email protected]>
`.trim();
const coAuthors = `
Co-authored-by: me <[email protected]>
Co-authored-by: you <[email protected]>
`
.split('\n')
.map((coAuthor) => coAuthor.trim())
.filter(Boolean);

gitCommit({
message: 'chore: does something',
coauthor: { name: 'some', email: '[email protected]' },
coAuthors: [author, ...coAuthors],
});
expect(execa).toHaveBeenCalledTimes(1);
expect(execa).toHaveBeenCalledWith(
'git',
[
'commit',
'-m',
'chore: does something\n\n\nCo-authored-by: some <random@person.com>',
'chore: does something\n\n\nCo-authored-by: them <[email protected]>\nCo-authored-by: me <[email protected]>\nCo-authored-by: you <you@algolia.com>',
],
{ cwd: expect.any(String) }
);
Expand Down
16 changes: 12 additions & 4 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ async function spreadGeneration(): Promise<void> {
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 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);

const commitMessage = cleanUpCommitMessage(lastCommitMessage);
const langs = decideWhereToSpread(lastCommitMessage);

Expand Down Expand Up @@ -75,7 +83,7 @@ async function spreadGeneration(): Promise<void> {
await run(`git add .`, { cwd: tempGitDir });
await gitCommit({
message: commitMessage,
coauthor: { name, email },
coAuthors: [author, ...coAuthors],
cwd: tempGitDir,
});
await run(`git push`, { cwd: tempGitDir });
Expand Down
28 changes: 9 additions & 19 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,30 +186,20 @@ export async function runIfExists(

export async function gitCommit({
message,
coauthor,
coAuthors,
cwd = ROOT_DIR,
}: {
message: string;
coauthor?: {
name: string;
email: string;
};
coAuthors?: string[];
cwd?: string;
}): Promise<void> {
await execa(
'git',
[
'commit',
'-m',
message +
(coauthor
? `\n\n\nCo-authored-by: ${coauthor.name} <${coauthor.email}>`
: ''),
],
{
cwd,
}
);
const messageWithCoAuthors = coAuthors
? `${message}\n\n\n${coAuthors.join('\n')}`
: message;

await execa('git', ['commit', '-m', messageWithCoAuthors], {
cwd,
});
}

export async function checkForCache(
Expand Down

0 comments on commit f360137

Please sign in to comment.