Skip to content

Commit

Permalink
chore: use js scripts to set output variables (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Apr 7, 2022
1 parent 98531d0 commit f180473
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.2
7.1.0
27 changes: 1 addition & 26 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,7 @@ runs:
baseRef=${{ github.base_ref }}
origin=$( [[ -z $baseRef ]] && echo $previousCommit || echo "origin/$baseRef" )
echo "Checking diff with branch: $origin"
echo "::set-output name=GITHUB_ACTIONS_CHANGED::$(git diff --shortstat $origin..HEAD -- .github/actions .github/workflows | wc -l)"
echo "::set-output name=SPECS_CHANGED::$(git diff --shortstat $origin..HEAD -- specs | wc -l)"
echo "::set-output name=COMMON_SPECS_CHANGED::$(git diff --shortstat $origin..HEAD -- specs/common | wc -l)"
echo "::set-output name=TESTS_CHANGED::$(git diff --shortstat $origin..HEAD -- tests | wc -l)"
echo "::set-output name=SCRIPTS_CHANGED::$(git diff --shortstat $origin..HEAD -- scripts | wc -l)"
echo "::set-output name=GENERATORS_CHANGED::$(git diff --shortstat $origin..HEAD -- generators | wc -l)"
echo "::set-output name=JS_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript | wc -l)"
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)"
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)"
echo "::set-output name=JS_COMMON_TESTS_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-javascript/packages/client-common/src/__tests__ | wc -l)"
echo "::set-output name=JS_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/javascript | wc -l)"
echo "::set-output name=JAVA_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-java-2 | wc -l)"
echo "::set-output name=JAVA_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/java | wc -l)"
echo "::set-output name=PHP_CLIENT_CHANGED::$(git diff --shortstat $origin..HEAD -- clients/algoliasearch-client-php | wc -l)"
echo "::set-output name=PHP_TEMPLATE_CHANGED::$(git diff --shortstat $origin..HEAD -- templates/php | wc -l)"
echo "::set-output name=ORIGIN_BRANCH::$origin"
yarn workspace scripts setRunVariables "$origin"
- name: Compute specs matrix
if: inputs.type != 'minimal'
Expand Down
2 changes: 1 addition & 1 deletion eslint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"devDependencies": {
"@types/jest": "27.4.1",
"eslint": "8.11.0",
"eslint": "8.12.0",
"jest": "27.5.1",
"ts-jest": "27.1.3",
"ts-node": "10.7.0",
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci/codegen/__tests__/codegen.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cleanGeneratedBranch } from '../cleanGeneratedBranch';
import { pushGeneratedCode } from '../pushGeneratedCode';
import commentText from '../text';
import commentText, { GENERATED_MAIN_BRANCH } from '../text';
import {
getCommentBody,
upsertGenerationComment,
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('codegen', () => {
expect(await getCommentBody('cleanup')).toMatchInlineSnapshot(`
"### ✗ The generated branch has been deleted.
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)."
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})."
`);
});

Expand Down
9 changes: 6 additions & 3 deletions scripts/ci/codegen/pushGeneratedCode.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-console */
import { run } from '../../common';
import { configureGitHubAuthor } from '../../release/common';
import { getNbGitDiff } from '../utils';

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

if (
(await run(
`git status --porcelain ${FOLDERS_TO_CHECK} | wc -l | tr -d ' '`
)) === '0'
(await getNbGitDiff({
branch: baseBranch,
head: null,
path: FOLDERS_TO_CHECK,
})) === 0
) {
console.log(`No generated code changes found for '${baseBranch}'.`);

Expand Down
14 changes: 3 additions & 11 deletions scripts/ci/codegen/spreadGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import {
toAbsolutePath,
} from '../../common';
import { getLanguageFolder } from '../../config';
import {
cloneRepository,
configureGitHubAuthor,
OWNER,
REPO,
} from '../../release/common';
import { cloneRepository, configureGitHubAuthor } from '../../release/common';

const GENERATED_MAIN_BRANCH = `generated/main`;
import { GENERATED_MAIN_BRANCH, REPO_URL } from './text';

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

return [
result[1],
`https://github.com/${OWNER}/${REPO}/pull/${result[2]}`,
].join('\n\n');
return [result[1], `${REPO_URL}/pull/${result[2]}`].join('\n\n');
}

async function spreadGeneration(): Promise<void> {
Expand Down
7 changes: 4 additions & 3 deletions scripts/ci/codegen/text.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { OWNER, REPO } from '../../release/common';
import { MAIN_BRANCH, OWNER, REPO } from '../../release/common';

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

export default {
notification: {
Expand All @@ -13,7 +14,7 @@ export default {
},
cleanup: {
header: '### ✗ The generated branch has been deleted.',
body: `If the PR has been merged, you can check the generated code on the [\`generated/main\` branch](${REPO_URL}/tree/generated/main).`,
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}).`,
},
codegen: {
header: '### ✔️ Code generated!',
Expand Down
34 changes: 18 additions & 16 deletions scripts/ci/createMatrix.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { CLIENTS, GENERATORS, run } from '../common';
import { CLIENTS, GENERATORS } from '../common';
import type { Language } from '../types';

import { getNbGitDiff } from './utils';

type CreateMatrix = {
baseChanged: boolean;
baseBranch: string;
Expand All @@ -21,18 +23,6 @@ type Matrix<TMatrix> = {
// This empty matrix is required by the CI, otherwise it throws
const EMPTY_MATRIX = JSON.stringify({ client: ['no-run'] });

/**
* Returns the number of diff between a `branch` and the current HEAD for the given `path`.
*/
async function getNbGitDiff(branch: string, path: string): Promise<number> {
return parseInt(
(
await run(`git diff --shortstat ${branch}..HEAD -- ${path} | wc -l`)
).trim(),
10
);
}

async function getClientMatrix({
language,
baseBranch,
Expand All @@ -54,8 +44,14 @@ async function getClientMatrix({
continue;
}

const specChanges = await getNbGitDiff(baseBranch, `specs/${client}`);
const clientChanges = await getNbGitDiff(baseBranch, output);
const specChanges = await getNbGitDiff({
branch: baseBranch,
path: `specs/${client}`,
});
const clientChanges = await getNbGitDiff({
branch: baseBranch,
path: output,
});

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

for (const client of CLIENTS) {
const specChanges = await getNbGitDiff(baseBranch, `specs/${client}`);
const specChanges = await getNbGitDiff({
branch: baseBranch,
path: `specs/${client}`,
});

if (specChanges === 0 && !baseChanged) {
continue;
Expand All @@ -101,6 +100,9 @@ async function getSpecMatrix({
return matrix;
}

/**
* Creates a matrix for the CI jobs based on the files that changed.
*/
async function createMatrix(opts: CreateMatrix): Promise<void> {
const matrix = opts.language
? await getClientMatrix(opts)
Expand Down
119 changes: 119 additions & 0 deletions scripts/ci/setRunVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* eslint-disable no-console */
import { getNbGitDiff } from './utils';

const JS_CLIENT_FOLDER = 'clients/algoliasearch-client-javascript';

/**
* Exhaustive list of output variables to use in the CI.
*
* Those variables are used to determine if jobs should run, based on the changes
* made in their respective `path`s.
*
* Negative paths should start with `:!`.
*
* The variable will be accessible in the CI via `steps.diff.outputs.<name>`.
*/
const VARIABLES_TO_CHECK = [
{
name: 'GITHUB_ACTIONS_CHANGED',
path: ['.github/actions', '.github/workflows'],
},
{
name: 'SPECS_CHANGED',
path: ['specs', ':!specs/bundled'],
},
{
name: 'COMMON_SPECS_CHANGED',
path: ['specs/common'],
},
{
name: 'TESTS_CHANGED',
path: ['tests'],
},
{
name: 'SCRIPTS_CHANGED',
path: ['scripts'],
},
{
name: 'GENERATORS_CHANGED',
path: ['generators'],
},
{
name: 'JS_CLIENT_CHANGED',
path: [JS_CLIENT_FOLDER, `:!${JS_CLIENT_FOLDER}/.github`],
},
{
name: 'JS_ALGOLIASEARCH_CHANGED',
path: [
`${JS_CLIENT_FOLDER}/packages/algoliasearch`,
`${JS_CLIENT_FOLDER}/packages/client-search`,
`${JS_CLIENT_FOLDER}/packages/client-analytics`,
`${JS_CLIENT_FOLDER}/packages/client-personalization`,
],
},
{
name: 'JS_COMMON_CHANGED',
path: [
`${JS_CLIENT_FOLDER}/packages/client-common`,
`${JS_CLIENT_FOLDER}/packages/requester-browser-xhr`,
`${JS_CLIENT_FOLDER}/packages/requester-node-http`,
],
},
{
name: 'JS_COMMON_TESTS_CHANGED',
path: [`${JS_CLIENT_FOLDER}/packages/client-common/src/__tests__`],
},
{
name: 'JS_TEMPLATE_CHANGED',
path: ['templates/javascript'],
},
{
name: 'JAVA_CLIENT_CHANGED',
path: ['clients/algoliasearch-client-java-2'],
},
{
name: 'JAVA_TEMPLATE_CHANGED',
path: ['templates/java'],
},
{
name: 'PHP_CLIENT_CHANGED',
path: ['clients/algoliasearch-client-php'],
},
{
name: 'PHP_TEMPLATE_CHANGED',
path: ['templates/php'],
},
];

/**
* Outputs variables used in the CI to determine if a job should run.
*/
async function setRunVariables({
originBranch,
}: {
originBranch: string;
}): Promise<void> {
console.log(`Checking diff between ${originBranch} and HEAD`);

for (const check of VARIABLES_TO_CHECK) {
const diff = await getNbGitDiff({
branch: originBranch,
path: check.path.join(' '),
});

console.log(`Found ${diff} changes for '${check.name}'`);
console.log(`::set-output name=${check.name}::${diff}`);
}

console.log(`::set-output name=ORIGIN_BRANCH::${originBranch}`);
}

if (require.main === module) {
const [origin] = process.argv.slice(2);

if (!origin) {
throw new Error(`Unable to retrieve the origin: ${origin}`);
}

setRunVariables({ originBranch: origin });
}
24 changes: 24 additions & 0 deletions scripts/ci/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { run } from '../common';

/**
* Returns the number of diff between a `branch` and its `HEAD` for the given `path`.
* Head defaults to `HEAD`, providing `null` will check unstaged changes.
*/
export async function getNbGitDiff({
branch,
head = 'HEAD',
path,
}: {
branch: string;
head?: string | null;
path: string;
}): Promise<number> {
const checkHead = head === null ? '' : `...${head}`;

return parseInt(
(
await run(`git diff --shortstat ${branch}${checkHead} -- ${path} | wc -l`)
).trim(),
10
);
}
2 changes: 1 addition & 1 deletion scripts/cts/client/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function loadTests(client: string): Promise<TestsBlock[]> {
});
}

return testsBlocks;
return testsBlocks.sort((a, b) => a.operationId.localeCompare(b.operationId));
}

export async function generateClientTests(
Expand Down
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"cleanGeneratedBranch": "ts-node ci/codegen/cleanGeneratedBranch.ts",
"spreadGeneration": "ts-node ci/codegen/spreadGeneration.ts",
"upsertGenerationComment": "ts-node ci/codegen/upsertGenerationComment.ts",
"setRunVariables": "ts-node ci/setRunVariables.ts",
"test": "jest"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit f180473

Please sign in to comment.