Skip to content
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

chore(scripts): improve release script with git diff [skip-bc] #3686

Merged
merged 7 commits into from
Sep 11, 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
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# See ./website/docs/releaseProcess.md for more information.
# See ./website/docs/release-process.md for more information.

GITHUB_TOKEN=
GITHUB_TOKEN=
1 change: 0 additions & 1 deletion config/release.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"mainBranch": "main",
"owner": "algolia",
"repo": "api-clients-automation",
"teamSlug": "api-clients-automation",
"targetBranch": {
"csharp": "main",
"dart": "main",
Expand Down
3 changes: 2 additions & 1 deletion scripts/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
ignorePatterns: ['*.test.ts', '**.yml', 'tsconfig.json'],
ignorePatterns: ['**.yml', 'tsconfig.json'],

extends: "../.eslintrc.cjs",

Expand Down Expand Up @@ -35,6 +35,7 @@ module.exports = {
complexity: 0,
'no-param-reassign': 0,
'@typescript-eslint/consistent-type-assertions': 0,
'@typescript-eslint/consistent-type-imports': 0,
curly: ['error', 'all'],
},
};
43 changes: 16 additions & 27 deletions scripts/__tests__/common.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { execa } from 'execa';
import { afterEach, describe, expect, it, vi } from 'vitest';

import { capitalize, createClientName, gitCommit } from '../common.js';
import { getClientsConfigField } from '../config.js';

vi.mock('execa', () => {
Expand All @@ -9,11 +11,6 @@ vi.mock('execa', () => {
};
});

const { capitalize, createClientName, gitCommit } = await import(
'../common.js'
);
const { execa } = await import('execa');

describe('gitCommit', () => {
afterEach(() => {
vi.clearAllMocks();
Expand All @@ -22,10 +19,7 @@ describe('gitCommit', () => {
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) }
);
expect(execa).toHaveBeenCalledWith('git', ['commit', '-m', 'chore: does something'], { cwd: expect.any(String) });
});

it('commits with co-author', () => {
Expand All @@ -50,8 +44,13 @@ describe('gitCommit', () => {
});
expect(execa).toHaveBeenCalledTimes(1);
expect(execa).toHaveBeenCalledWith(
'git', ["commit", "-m", "chore: does something\n\n\nCo-authored-by: them <them@algolia.com>\nCo-authored-by: me <me@algolia.com>\nCo-authored-by: you <you@algolia.com>"],
{ cwd: expect.any(String) }
'git',
[
'commit',
'-m',
'chore: does something\n\n\nCo-authored-by: them <them@algolia.com>\nCo-authored-by: me <me@algolia.com>\nCo-authored-by: you <you@algolia.com>',
],
{ cwd: expect.any(String) },
);
});
});
Expand All @@ -61,15 +60,11 @@ describe('config', () => {
it('throws if the field is not found', () => {
expect(() => {
getClientsConfigField('javascript', 'foofoo');
}).toThrowErrorMatchingInlineSnapshot(
`[Error: Unable to find 'foofoo' for 'javascript']`
);
}).toThrowErrorMatchingInlineSnapshot(`[Error: Unable to find 'foofoo' for 'javascript']`);
});

it('find the field if it exists', () => {
expect(getClientsConfigField('java', ['tests', 'extension'])).toEqual(
'.test.java'
);
expect(getClientsConfigField('java', ['tests', 'extension'])).toEqual('.test.java');
});
});
});
Expand All @@ -95,20 +90,14 @@ describe('utils', () => {
describe('createClientName', () => {
it('does not capitalize every part for JavaScript', () => {
expect(createClientName('search', 'javascript')).toEqual('search');
expect(createClientName('search-client', 'javascript')).toEqual(
'searchClient'
);
expect(createClientName('search-cli!nt-complex', 'javascript')).toEqual(
'searchCli!ntComplex'
);
expect(createClientName('search-client', 'javascript')).toEqual('searchClient');
expect(createClientName('search-cli!nt-complex', 'javascript')).toEqual('searchCli!ntComplex');
});

it('capitalize every part for other languages', () => {
expect(createClientName('search', 'java')).toEqual('Search');
expect(createClientName('search-client', 'java')).toEqual('SearchClient');
expect(createClientName('search-cli!nt-complex', 'java')).toEqual(
'SearchCli!ntComplex'
);
expect(createClientName('search-cli!nt-complex', 'java')).toEqual('SearchCli!ntComplex');
});
});
});
13 changes: 6 additions & 7 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { describe, expect, it, vi } from "vitest";
import { describe, expect, it, vi } from 'vitest';

import { pushGeneratedCode } from '../pushGeneratedCode.js';

vi.mock('../../../common.js', async () => {
const mod = await vi.importActual<typeof import('../../../common.js')>('../../../common.js')
vi.mock('../../../common.js', async (importOriginal) => {
const mod = await importOriginal<typeof import('../../../common.js')>();
return {
...mod,
run: vi.fn().mockResolvedValue(''),
}
};
});

describe('pushGeneratedCode', () => {
it('throws without GITHUB_TOKEN environment variable', async () => {
process.env.GITHUB_TOKEN = '';
await expect(pushGeneratedCode()).rejects.toThrow(
'Environment variable `GITHUB_TOKEN` does not exist.'
);
await expect(pushGeneratedCode()).rejects.toThrow('Environment variable `GITHUB_TOKEN` does not exist.');
});
});
24 changes: 10 additions & 14 deletions scripts/ci/codegen/__tests__/spreadGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it } from 'vitest';

import { cleanUpCommitMessage } from '../spreadGeneration.js';
import text from '../text.js';

describe('spread generation', () => {
describe('cleanUpCommitMessage', () => {
it('returns a release commit message with the version', () => {
expect(
cleanUpCommitMessage(`${text.commitReleaseMessage} [skip ci]`, '1.0.0')
).toEqual('chore: release 1.0.0');
expect(cleanUpCommitMessage(`${text.commitReleaseMessage} [skip ci]`, '1.0.0')).toEqual('chore: release 1.0.0');
});

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 (generated)\n\nhttps://github.com/algolia/api-clients-automation/pull/244`
expect(cleanUpCommitMessage('feat(ci): make ci push generated code (#244)', '')).toEqual(
'feat(ci): make ci push generated code (generated)\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`;
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)`;
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 (generated)

Expand All @@ -33,10 +31,8 @@ describe('spread generation', () => {
});

it('generated commits have a link to the origin pull request', () => {
expect(
cleanUpCommitMessage('feat(ci): make ci push generated code (#244) (generated)', '')
).toEqual(
`feat(ci): make ci push generated code (generated)\n\nhttps://github.com/algolia/api-clients-automation/pull/244`
expect(cleanUpCommitMessage('feat(ci): make ci push generated code (#244) (generated)', '')).toEqual(
'feat(ci): make ci push generated code (generated)\n\nhttps://github.com/algolia/api-clients-automation/pull/244',
);
});
});
Expand Down
8 changes: 1 addition & 7 deletions scripts/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ program
program
.command('release')
.description('Releases the client')
.addArgument(args.languages)
.option(flags.verbose.flag, flags.verbose.description)
.option<semver.ReleaseType>(
'-rt --releaseType <type>',
Expand All @@ -258,7 +257,7 @@ program
.option('-d, --dry-run', 'does not push anything to GitHub')
.option('-sla, --sla-only', 'only generates the sla policy', false)
.option('-b --breaking', 'allow breaking change on the CI', false)
.action(async (langArgs: LangArg[], { verbose, releaseType, dryRun, slaOnly, breaking }) => {
.action(async ({ verbose, releaseType, dryRun, slaOnly, breaking }) => {
setVerbose(Boolean(verbose));

if (slaOnly) {
Expand All @@ -267,12 +266,7 @@ program
return;
}

if (langArgs.length === 0) {
langArgs = [ALL];
}

await createReleasePR({
languages: langArgs.includes(ALL) ? LANGUAGES : (langArgs as Language[]),
releaseType,
dryRun,
breaking,
Expand Down
4 changes: 1 addition & 3 deletions scripts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ export const TODAY = new Date().toISOString().split('T')[0];
export const CI = Boolean(process.env.CI);

// This script is run by `yarn workspace ...`, which means the current working directory is `./script`
const ROOT_DIR = path.resolve(process.cwd(), '..');

export const ROOT_ENV_PATH = path.resolve(ROOT_DIR, '.env');
export const ROOT_DIR = path.resolve(process.cwd(), '..');

// Build `GENERATORS` from the `clients.config.json` file
export const GENERATORS = Object.entries(clientsConfig).reduce(
Expand Down
Loading
Loading