Skip to content

chore(scripts): take generated commits into account for the release #3695

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 3 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions scripts/release/__tests__/createReleasePR.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ describe('createReleasePR', () => {
getFileChangesMock.mockResolvedValueOnce('');
expect(await parseCommit(buildTestCommit())).toEqual(
expect.objectContaining({
error: 'missing-language-scope',
author: '[@algolia-bot](https://github.com/algolia-bot/)',
hash: 'b2501882',
languages: [],
message: 'fix: fix the thing',
prNumber: 123,
scope: undefined,
type: 'fix',
}),
);
});
Expand All @@ -110,7 +116,13 @@ describe('createReleasePR', () => {
getFileChangesMock.mockResolvedValueOnce('specs/search/something.json');
expect(await parseCommit(buildTestCommit())).toEqual(
expect.objectContaining({
error: 'missing-language-scope',
author: '[@algolia-bot](https://github.com/algolia-bot/)',
hash: 'b2501882',
languages: [],
message: 'fix: fix the thing',
prNumber: 123,
scope: undefined,
type: 'fix',
}),
);
});
Expand Down Expand Up @@ -138,7 +150,8 @@ describe('createReleasePR', () => {
});
});

it('returns error when it is a generated commit', async () => {
it('returns early when it is a generated commit', async () => {
getFileChangesMock.mockResolvedValueOnce('clients/algoliasearch-client-javascript/package.json');
expect(
await parseCommit(
buildTestCommit({
Expand All @@ -147,7 +160,9 @@ describe('createReleasePR', () => {
}),
),
).toEqual({
error: 'generation-commit',
generated: true,
languages: ['javascript'],
message: 'feat(specs): foo bar baz (generated)',
});
});
});
Expand Down
53 changes: 32 additions & 21 deletions scripts/release/createReleasePR.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add the default scope to client when it doesn't match? you know the comment from the previous pr

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah I have it in stash I forgot to push

Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,24 @@ export async function parseCommit(commit: string): Promise<Commit> {
const prNumberMatch = message.match(/#(\d+)/);
const prNumber = prNumberMatch ? parseInt(prNumberMatch[1], 10) : 0;

// We skip generation commits as they do not appear in changelogs
if (isGeneratedCommit(message)) {
return {
error: 'generation-commit',
};
let commitType = typeAndScope ? typeAndScope[1] : 'fix'; // default to fix.
if (!['feat', 'fix', 'chore'].includes(commitType)) {
commitType = 'fix';
}

// get the scope of the commit by checking the changes.
// any changes in the folder of a language will be scoped to that language
const diff = await getFileChanges(hash);
if (!diff) {
// for empty commits, they will be filtered out later
return {
error: 'missing-language-scope',
message,
hash,
type: commitType as CommitType,
languages: [],
scope: typeAndScope ? (typeAndScope[2] as Scope) : undefined,
message: message.replace(`(#${prNumber})`, '').trim(),
prNumber,
author: fetchedUsers[authorEmail],
};
}

Expand All @@ -111,9 +115,11 @@ export async function parseCommit(commit: string): Promise<Commit> {
}
}

if (languageScopes.size === 0) {
// for generated commits, we just report the languages so that the changes are attributed to the correct language and commit
if (isGeneratedCommit(message)) {
return {
error: 'missing-language-scope',
generated: true,
languages: [...languageScopes] as Language[],
message,
};
}
Expand All @@ -132,11 +138,6 @@ export async function parseCommit(commit: string): Promise<Commit> {
}
}

let commitType = typeAndScope ? typeAndScope[1] : 'fix'; // default to fix.
if (!['feat', 'fix', 'chore'].includes(commitType)) {
commitType = 'fix';
}

return {
hash,
type: commitType as CommitType, // `fix` | `feat` | `chore` | ..., default to `fix`
Expand Down Expand Up @@ -246,19 +247,23 @@ async function getCommits(force?: boolean): Promise<{
skippedCommits: string;
}> {
// Reading commits since last release
const latestCommits = (await run(`git log --pretty=format:"%h|%ae|%s" ${await getLastReleasedTag()}..${MAIN_BRANCH}`))
const latestCommits = (
await run(`git log --reverse --pretty=format:"%h|%ae|%s" ${await getLastReleasedTag()}..${MAIN_BRANCH}`)
)
.split('\n')
.filter(Boolean);

const commitsWithoutLanguageScope: string[] = [];
const validCommits: ParsedCommit[] = [];
let validCommits: ParsedCommit[] = [];

for (const commitMessage of latestCommits) {
const commit = await parseCommit(commitMessage);

if ('error' in commit) {
if (commit.error === 'missing-language-scope') {
commitsWithoutLanguageScope.push(commit.message);
if ('generated' in commit) {
const originalCommit = validCommits.findIndex((c) => commit.message.includes(c.message));
if (originalCommit !== -1) {
validCommits[originalCommit].languages = [
...new Set([...validCommits[originalCommit].languages, ...commit.languages]),
];
}

continue;
Expand All @@ -267,6 +272,12 @@ async function getCommits(force?: boolean): Promise<{
validCommits.push(commit);
}

// redo a pass to filter out commits without language scope
const commitsWithoutLanguageScope = validCommits
.filter((commit) => commit.languages.length === 0)
.map((commit) => commit.message);
validCommits = validCommits.filter((commit) => commit.languages.length > 0);

if (!force && validCommits.length === 0) {
console.log(
chalk.black.bgYellow('[INFO]'),
Expand Down Expand Up @@ -351,7 +362,7 @@ export async function createReleasePR({

// sometimes the scope of the commits is not set correctly and concerns another language, we can fix it.
if (LANGUAGES.includes(validCommit.scope as Language) && validCommit.scope !== lang) {
validCommit.message = validCommit.message.replace(`(${validCommit.scope}):`, `(${lang}):`);
validCommit.message = validCommit.message.replace(`(${validCommit.scope}):`, '(clients):');
}

const changelogCommit = [
Expand Down
5 changes: 1 addition & 4 deletions scripts/release/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ export type ParsedCommit = {
prNumber: number;
};

export type Commit =
| ParsedCommit
| { error: 'generation-commit' }
| { error: 'missing-language-scope'; message: string };
export type Commit = ParsedCommit | { generated: true; languages: Language[]; message: string };

export type Changelog = {
[lang in Language]?: string;
Expand Down
Loading