Skip to content

Commit f180473

Browse files
authored
chore: use js scripts to set output variables (#348)
1 parent 98531d0 commit f180473

File tree

13 files changed

+182
-110
lines changed

13 files changed

+182
-110
lines changed

.github/.cache_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.0.2
1+
7.1.0

.github/actions/setup/action.yml

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,7 @@ runs:
4747
baseRef=${{ github.base_ref }}
4848
origin=$( [[ -z $baseRef ]] && echo $previousCommit || echo "origin/$baseRef" )
4949
50-
echo "Checking diff with branch: $origin"
51-
52-
echo "::set-output name=GITHUB_ACTIONS_CHANGED::$(git diff --shortstat $origin..HEAD -- .github/actions .github/workflows | wc -l)"
53-
54-
echo "::set-output name=SPECS_CHANGED::$(git diff --shortstat $origin..HEAD -- specs | wc -l)"
55-
echo "::set-output name=COMMON_SPECS_CHANGED::$(git diff --shortstat $origin..HEAD -- specs/common | wc -l)"
56-
57-
echo "::set-output name=TESTS_CHANGED::$(git diff --shortstat $origin..HEAD -- tests | wc -l)"
58-
59-
echo "::set-output name=SCRIPTS_CHANGED::$(git diff --shortstat $origin..HEAD -- scripts | wc -l)"
60-
61-
echo "::set-output name=GENERATORS_CHANGED::$(git diff --shortstat $origin..HEAD -- generators | wc -l)"
62-
63-
echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
64-
echo "::set-output name=JS_ALGOLIASEARCH_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/algoliasearch clients/algoliasearch-client-javascript/packages/client-search clients/algoliasearch-client-javascript/packages/client-analytics clients/algoliasearch-client-javascript/packages/client-personalization | wc -l)"
65-
echo "::set-output name=JS_COMMON_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common clients/algoliasearch-client-javascript/packages/requester-browser-xhr clients/algoliasearch-client-javascript/packages/requester-node-http | wc -l)"
66-
echo "::set-output name=JS_COMMON_TESTS_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common/src/__tests__ | wc -l)"
67-
echo "::set-output name=JS_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/javascript | wc -l)"
68-
69-
echo "::set-output name=JAVA_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-java-2 | wc -l)"
70-
echo "::set-output name=JAVA_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/java | wc -l)"
71-
72-
echo "::set-output name=PHP_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-php | wc -l)"
73-
echo "::set-output name=PHP_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/php | wc -l)"
74-
75-
echo "::set-output name=ORIGIN_BRANCH::$origin"
50+
yarn workspace scripts setRunVariables "$origin"
7651
7752
- name: Compute specs matrix
7853
if: inputs.type != 'minimal'

eslint/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
},
1414
"devDependencies": {
1515
"@types/jest": "27.4.1",
16-
"eslint": "8.11.0",
16+
"eslint": "8.12.0",
1717
"jest": "27.5.1",
1818
"ts-jest": "27.1.3",
1919
"ts-node": "10.7.0",

scripts/ci/codegen/__tests__/codegen.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
22
import { pushGeneratedCode } from '../pushGeneratedCode';
3-
import commentText from '../text';
3+
import commentText, { GENERATED_MAIN_BRANCH } from '../text';
44
import {
55
getCommentBody,
66
upsertGenerationComment,
@@ -66,7 +66,7 @@ describe('codegen', () => {
6666
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
6767
"### ✗ The generated branch has been deleted.
6868
69-
If the PR has been merged, you can check the generated code on the [\`generated/main\` branch](https://github.com/algolia/api-clients-automation/tree/generated/main)."
69+
If the PR has been merged, you can check the generated code on the [\`${GENERATED_MAIN_BRANCH}\` branch](https://github.com/algolia/api-clients-automation/tree/${GENERATED_MAIN_BRANCH})."
7070
`);
7171
});
7272

scripts/ci/codegen/pushGeneratedCode.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable no-console */
22
import { run } from '../../common';
33
import { configureGitHubAuthor } from '../../release/common';
4+
import { getNbGitDiff } from '../utils';
45

56
const PR_NUMBER = parseInt(process.env.PR_NUMBER || '0', 10);
67
const FOLDERS_TO_CHECK = 'yarn.lock openapitools.json clients specs/bundled';
@@ -22,9 +23,11 @@ export async function pushGeneratedCode(): Promise<void> {
2223
const generatedCodeBranch = `generated/${baseBranch}`;
2324

2425
if (
25-
(await run(
26-
`git status --porcelain ${FOLDERS_TO_CHECK} | wc -l | tr -d ' '`
27-
)) === '0'
26+
(await getNbGitDiff({
27+
branch: baseBranch,
28+
head: null,
29+
path: FOLDERS_TO_CHECK,
30+
})) === 0
2831
) {
2932
console.log(`No generated code changes found for '${baseBranch}'.`);
3033

scripts/ci/codegen/spreadGeneration.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import {
1010
toAbsolutePath,
1111
} from '../../common';
1212
import { getLanguageFolder } from '../../config';
13-
import {
14-
cloneRepository,
15-
configureGitHubAuthor,
16-
OWNER,
17-
REPO,
18-
} from '../../release/common';
13+
import { cloneRepository, configureGitHubAuthor } from '../../release/common';
1914

20-
const GENERATED_MAIN_BRANCH = `generated/main`;
15+
import { GENERATED_MAIN_BRANCH, REPO_URL } from './text';
2116

2217
export function decideWhereToSpread(commitMessage: string): string[] {
2318
if (commitMessage.startsWith('chore: release')) {
@@ -40,10 +35,7 @@ export function cleanUpCommitMessage(commitMessage: string): string {
4035
return commitMessage;
4136
}
4237

43-
return [
44-
result[1],
45-
`https://github.com/${OWNER}/${REPO}/pull/${result[2]}`,
46-
].join('\n\n');
38+
return [result[1], `${REPO_URL}/pull/${result[2]}`].join('\n\n');
4739
}
4840

4941
async function spreadGeneration(): Promise<void> {

scripts/ci/codegen/text.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { OWNER, REPO } from '../../release/common';
1+
import { MAIN_BRANCH, OWNER, REPO } from '../../release/common';
22

3-
const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
3+
export const REPO_URL = `https://github.com/${OWNER}/${REPO}`;
4+
export const GENERATED_MAIN_BRANCH = `generated/${MAIN_BRANCH}`;
45

56
export default {
67
notification: {
@@ -13,7 +14,7 @@ export default {
1314
},
1415
cleanup: {
1516
header: '### ✗ The generated branch has been deleted.',
16-
body: `If the PR has been merged, you can check the generated code on the [\`generated/main\` branch](${REPO_URL}/tree/generated/main).`,
17+
body: `If the PR has been merged, you can check the generated code on the [\`${GENERATED_MAIN_BRANCH}\` branch](${REPO_URL}/tree/${GENERATED_MAIN_BRANCH}).`,
1718
},
1819
codegen: {
1920
header: '### ✔️ Code generated!',

scripts/ci/createMatrix.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { CLIENTS, GENERATORS, run } from '../common';
1+
import { CLIENTS, GENERATORS } from '../common';
22
import type { Language } from '../types';
33

4+
import { getNbGitDiff } from './utils';
5+
46
type CreateMatrix = {
57
baseChanged: boolean;
68
baseBranch: string;
@@ -21,18 +23,6 @@ type Matrix<TMatrix> = {
2123
// This empty matrix is required by the CI, otherwise it throws
2224
const EMPTY_MATRIX = JSON.stringify({ client: ['no-run'] });
2325

24-
/**
25-
* Returns the number of diff between a `branch` and the current HEAD for the given `path`.
26-
*/
27-
async function getNbGitDiff(branch: string, path: string): Promise<number> {
28-
return parseInt(
29-
(
30-
await run(`git diff --shortstat ${branch}..HEAD -- ${path} | wc -l`)
31-
).trim(),
32-
10
33-
);
34-
}
35-
3626
async function getClientMatrix({
3727
language,
3828
baseBranch,
@@ -54,8 +44,14 @@ async function getClientMatrix({
5444
continue;
5545
}
5646

57-
const specChanges = await getNbGitDiff(baseBranch, `specs/${client}`);
58-
const clientChanges = await getNbGitDiff(baseBranch, output);
47+
const specChanges = await getNbGitDiff({
48+
branch: baseBranch,
49+
path: `specs/${client}`,
50+
});
51+
const clientChanges = await getNbGitDiff({
52+
branch: baseBranch,
53+
path: output,
54+
});
5955

6056
if (clientChanges === 0 && specChanges === 0 && !baseChanged) {
6157
continue;
@@ -89,7 +85,10 @@ async function getSpecMatrix({
8985
const matrix: Matrix<string> = { client: [] };
9086

9187
for (const client of CLIENTS) {
92-
const specChanges = await getNbGitDiff(baseBranch, `specs/${client}`);
88+
const specChanges = await getNbGitDiff({
89+
branch: baseBranch,
90+
path: `specs/${client}`,
91+
});
9392

9493
if (specChanges === 0 && !baseChanged) {
9594
continue;
@@ -101,6 +100,9 @@ async function getSpecMatrix({
101100
return matrix;
102101
}
103102

103+
/**
104+
* Creates a matrix for the CI jobs based on the files that changed.
105+
*/
104106
async function createMatrix(opts: CreateMatrix): Promise<void> {
105107
const matrix = opts.language
106108
? await getClientMatrix(opts)

scripts/ci/setRunVariables.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/* eslint-disable no-console */
2+
import { getNbGitDiff } from './utils';
3+
4+
const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript';
5+
6+
/**
7+
* Exhaustive list of output variables to use in the CI.
8+
*
9+
* Those variables are used to determine if jobs should run, based on the changes
10+
* made in their respective `path`s.
11+
*
12+
* Negative paths should start with `:!`.
13+
*
14+
* The variable will be accessible in the CI via `steps.diff.outputs.<name>`.
15+
*/
16+
const VARIABLES_TO_CHECK = [
17+
{
18+
name: 'GITHUB_ACTIONS_CHANGED',
19+
path: ['.github/actions', '.github/workflows'],
20+
},
21+
{
22+
name: 'SPECS_CHANGED',
23+
path: ['specs', ':!specs/bundled'],
24+
},
25+
{
26+
name: 'COMMON_SPECS_CHANGED',
27+
path: ['specs/common'],
28+
},
29+
{
30+
name: 'TESTS_CHANGED',
31+
path: ['tests'],
32+
},
33+
{
34+
name: 'SCRIPTS_CHANGED',
35+
path: ['scripts'],
36+
},
37+
{
38+
name: 'GENERATORS_CHANGED',
39+
path: ['generators'],
40+
},
41+
{
42+
name: 'JS_CLIENT_CHANGED',
43+
path: [JS_CLIENT_FOLDER, `:!${JS_CLIENT_FOLDER}/.github`],
44+
},
45+
{
46+
name: 'JS_ALGOLIASEARCH_CHANGED',
47+
path: [
48+
`${JS_CLIENT_FOLDER}/packages/algoliasearch`,
49+
`${JS_CLIENT_FOLDER}/packages/client-search`,
50+
`${JS_CLIENT_FOLDER}/packages/client-analytics`,
51+
`${JS_CLIENT_FOLDER}/packages/client-personalization`,
52+
],
53+
},
54+
{
55+
name: 'JS_COMMON_CHANGED',
56+
path: [
57+
`${JS_CLIENT_FOLDER}/packages/client-common`,
58+
`${JS_CLIENT_FOLDER}/packages/requester-browser-xhr`,
59+
`${JS_CLIENT_FOLDER}/packages/requester-node-http`,
60+
],
61+
},
62+
{
63+
name: 'JS_COMMON_TESTS_CHANGED',
64+
path: [`${JS_CLIENT_FOLDER}/packages/client-common/src/__tests__`],
65+
},
66+
{
67+
name: 'JS_TEMPLATE_CHANGED',
68+
path: ['templates/javascript'],
69+
},
70+
{
71+
name: 'JAVA_CLIENT_CHANGED',
72+
path: ['clients/algoliasearch-client-java-2'],
73+
},
74+
{
75+
name: 'JAVA_TEMPLATE_CHANGED',
76+
path: ['templates/java'],
77+
},
78+
{
79+
name: 'PHP_CLIENT_CHANGED',
80+
path: ['clients/algoliasearch-client-php'],
81+
},
82+
{
83+
name: 'PHP_TEMPLATE_CHANGED',
84+
path: ['templates/php'],
85+
},
86+
];
87+
88+
/**
89+
* Outputs variables used in the CI to determine if a job should run.
90+
*/
91+
async function setRunVariables({
92+
originBranch,
93+
}: {
94+
originBranch: string;
95+
}): Promise<void> {
96+
console.log(`Checking diff between ${originBranch} and HEAD`);
97+
98+
for (const check of VARIABLES_TO_CHECK) {
99+
const diff = await getNbGitDiff({
100+
branch: originBranch,
101+
path: check.path.join(' '),
102+
});
103+
104+
console.log(`Found ${diff} changes for '${check.name}'`);
105+
console.log(`::set-output name=${check.name}::${diff}`);
106+
}
107+
108+
console.log(`::set-output name=ORIGIN_BRANCH::${originBranch}`);
109+
}
110+
111+
if (require.main === module) {
112+
const [origin] = process.argv.slice(2);
113+
114+
if (!origin) {
115+
throw new Error(`Unable to retrieve the origin: ${origin}`);
116+
}
117+
118+
setRunVariables({ originBranch: origin });
119+
}

scripts/ci/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { run } from '../common';
2+
3+
/**
4+
* Returns the number of diff between a `branch` and its `HEAD` for the given `path`.
5+
* Head defaults to `HEAD`, providing `null` will check unstaged changes.
6+
*/
7+
export async function getNbGitDiff({
8+
branch,
9+
head = 'HEAD',
10+
path,
11+
}: {
12+
branch: string;
13+
head?: string | null;
14+
path: string;
15+
}): Promise<number> {
16+
const checkHead = head === null ? '' : `...${head}`;
17+
18+
return parseInt(
19+
(
20+
await run(`git diff --shortstat ${branch}${checkHead} -- ${path} | wc -l`)
21+
).trim(),
22+
10
23+
);
24+
}

scripts/cts/client/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ async function loadTests(client: string): Promise<TestsBlock[]> {
5454
});
5555
}
5656

57-
return testsBlocks;
57+
return testsBlocks.sort((a, b) => a.operationId.localeCompare(b.operationId));
5858
}
5959

6060
export async function generateClientTests(

scripts/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"cleanGeneratedBranch": "ts-node ci/codegen/cleanGeneratedBranch.ts",
1010
"spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts",
1111
"upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts",
12+
"setRunVariables": "ts-node ci/setRunVariables.ts",
1213
"test": "jest"
1314
},
1415
"devDependencies": {

0 commit comments

Comments
 (0)