Skip to content

Commit

Permalink
chore: fix specs run condition (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored May 10, 2022
1 parent f516c24 commit d22e05f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 114 deletions.
21 changes: 2 additions & 19 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ runs:
echo "::set-output name=RUN_JS_ALGOLIASEARCH::true"
fi
if [[ ${{ steps.gen-matrix.outputs.RUN_GEN_JAVASCRIPT || steps.diff.outputs.JS_UTILS_CHANGED > 0 }} ]]; then
if [[ ${{ steps.gen-matrix.outputs.RUN_GEN_JAVASCRIPT || steps.diff.outputs.JAVASCRIPT_UTILS_CHANGED > 0 }} ]]; then
echo "Running JavaScript utils: true"
echo "::set-output name=RUN_JS_UTILS::true"
fi
Expand All @@ -108,9 +108,6 @@ outputs:
description: Determine if the `scripts` job should run
value: ${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 }}

RUN_SPECS:
description: Determine if the `specs` job should run
value: ${{ steps.spec-matrix.outputs.RUN_SPECS }}
SPECS_MATRIX:
description: The generated `specs` matrix
value: ${{ steps.spec-matrix.outputs.MATRIX }}
Expand All @@ -132,20 +129,6 @@ outputs:
description: Determine if the `client_javascript_tests` job should run
value: ${{ steps.diff.outputs.JS_COMMON_TESTS_CHANGED > 0 }}

RUN_CTS:
description: Determine if the `cts` jobs should run
value: ${{
steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 ||
steps.diff.outputs.SCRIPTS_CHANGED > 0 ||
steps.diff.outputs.SHOULD_RUN_SPECS > 0 ||
steps.diff.outputs.TESTS_CHANGED > 0 ||
steps.diff.outputs.JS_CLIENT_CHANGED > 0 ||
steps.diff.outputs.JS_TEMPLATE_CHANGED > 0 ||
steps.diff.outputs.JAVA_CLIENT_CHANGED > 0 ||
steps.diff.outputs.JAVA_TEMPLATE_CHANGED > 0 }}

RUN_CODEGEN:
description: Determine if the `codegen` job should run
value: ${{
steps.spec-matrix.outputs.RUN_SPECS == 'true' ||
steps.gen-matrix.outputs.RUN_GEN == 'true' }}
value: ${{ steps.gen-matrix.outputs.RUN_GEN == 'true' }}
8 changes: 0 additions & 8 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ jobs:
outputs:
RUN_SCRIPTS: ${{ steps.setup.outputs.RUN_SCRIPTS }}

RUN_SPECS: ${{ steps.setup.outputs.RUN_SPECS }}
SPECS_MATRIX: ${{ steps.setup.outputs.SPECS_MATRIX }}

RUN_GEN: ${{ steps.setup.outputs.RUN_GEN }}
Expand All @@ -60,8 +59,6 @@ jobs:
RUN_JS_UTILS: ${{ steps.setup.outputs.RUN_JS_UTILS }}
RUN_JS_TESTS: ${{ steps.setup.outputs.RUN_JS_TESTS }}

RUN_CTS: ${{ steps.setup.outputs.RUN_CTS }}

RUN_CODEGEN: ${{ steps.setup.outputs.RUN_CODEGEN }}

scripts:
Expand Down Expand Up @@ -90,7 +87,6 @@ jobs:
runs-on: ubuntu-20.04
timeout-minutes: 10
needs: setup
if: ${{ needs.setup.outputs.RUN_SPECS == 'true' }}
strategy:
matrix: ${{ fromJSON(needs.setup.outputs.SPECS_MATRIX) }}
steps:
Expand Down Expand Up @@ -224,21 +220,17 @@ jobs:
run: yarn cli build clients ${{ matrix.client.language }} ${{ matrix.client.toRun }}

- name: Clean CTS output before generate
if: ${{ needs.setup.outputs.RUN_CTS == 'true' }}
run: rm -rf ${{ matrix.client.testsOutputPath }}

- name: Generate CTS
if: ${{ needs.setup.outputs.RUN_CTS == 'true' }}
run: yarn cli cts generate ${{ matrix.client.language }} ${{ matrix.client.toRun }}

- name: Check diff with pushed CTS
if: ${{ needs.setup.outputs.RUN_CTS == 'true' }}
run: |
git --no-pager diff
exit $(git diff --name-only --diff-filter=d ${{ matrix.client.testsOutputPath }} | wc -l)
- name: Run CTS
if: ${{ needs.setup.outputs.RUN_CTS == 'true' }}
run: yarn cli cts run ${{ matrix.client.language }}

- name: Zip artifact before storing
Expand Down
78 changes: 21 additions & 57 deletions scripts/ci/githubActions/createMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,13 @@ import { getLanguageFolder, getTestOutputFolder } from '../../config';
import type { Language } from '../../types';
import { getNbGitDiff } from '../utils';

import { DEPENDENCIES } from './setRunVariables';
import { DEPENDENCIES, COMMON_DEPENDENCIES } from './setRunVariables';
import type { ClientMatrix, CreateMatrix, Matrix, SpecMatrix } from './types';
import { computeCacheKey, isBaseChanged } from './utils';

// This empty matrix is required by the CI, otherwise it throws
const EMPTY_MATRIX = { client: ['no-run'] };

/**
* List of dependencies based on the language, inherited from `./setRunVariables.ts` in a more dynamic form.
*/
const MATRIX_DEPENDENCIES = {
common: {
GITHUB_ACTIONS_CHANGED: DEPENDENCIES.GITHUB_ACTIONS_CHANGED,
SCRIPTS_CHANGED: DEPENDENCIES.SCRIPTS_CHANGED,
COMMON_SPECS_CHANGED: DEPENDENCIES.COMMON_SPECS_CHANGED,
},
clients: {
common: {
GENERATORS_CHANGED: DEPENDENCIES.GENERATORS_CHANGED,
},
javascript: {
JS_UTILS_CHANGED: DEPENDENCIES.JS_UTILS_CHANGED,
JS_TEMPLATE_CHANGED: DEPENDENCIES.JS_TEMPLATE_CHANGED,
},
php: {
PHP_TEMPLATE_CHANGED: DEPENDENCIES.PHP_TEMPLATE_CHANGED,
},
java: {
JAVA_TEMPLATE_CHANGED: DEPENDENCIES.JAVA_TEMPLATE_CHANGED,
},
},
};

type ToRunMatrix = {
path: string;
toRun: string[];
Expand All @@ -62,6 +36,20 @@ async function getClientMatrix(baseBranch: string): Promise<void> {
continue;
}

const languageDependencies = Object.entries(DEPENDENCIES).reduce(
(finalDeps, [key, deps]) => {
if (key.startsWith(`${language.toUpperCase()}_`)) {
return {
...finalDeps,
[key]: deps,
};
}

return finalDeps;
},
{} as Record<string, string[]>
);

const bundledSpec = client === 'algoliasearch-lite' ? 'search' : client;
const specChanges = await getNbGitDiff({
branch: baseBranch,
Expand All @@ -72,17 +60,15 @@ async function getClientMatrix(baseBranch: string): Promise<void> {
path: output,
});
const baseChanged = await isBaseChanged(baseBranch, {
...MATRIX_DEPENDENCIES.common,
...MATRIX_DEPENDENCIES.clients.common,
...MATRIX_DEPENDENCIES.clients[language],
...COMMON_DEPENDENCIES,
...languageDependencies,
});

// No changes found, we don't put this job in the matrix
if (clientChanges === 0 && specChanges === 0 && !baseChanged) {
continue;
}

console.log(`::set-output name=RUN_GEN_${language.toUpperCase()}::true`);
matrix[language].toRun.push(client);
matrix[language].cacheToCompute.push(`specs/${bundledSpec}`);
}
Expand Down Expand Up @@ -111,6 +97,7 @@ async function getClientMatrix(baseBranch: string): Promise<void> {
language
)}`,
});
console.log(`::set-output name=RUN_GEN_${language.toUpperCase()}::true`);
}

const shouldRun = clientMatrix.client.length > 0;
Expand All @@ -123,7 +110,7 @@ async function getClientMatrix(baseBranch: string): Promise<void> {
);
}

async function getSpecMatrix(baseBranch: string): Promise<void> {
async function getSpecMatrix(): Promise<void> {
const matrix: ToRunMatrix = {
path: 'specs/bundled',
toRun: [],
Expand All @@ -133,31 +120,9 @@ async function getSpecMatrix(baseBranch: string): Promise<void> {
for (const client of CLIENTS) {
// The `algoliasearch-lite` spec is created by the `search` spec
const bundledSpecName = client === 'algoliasearch-lite' ? 'search' : client;
const path = `specs/${bundledSpecName}`;
const specChanges = await getNbGitDiff({
branch: baseBranch,
path,
});
const baseChanged = await isBaseChanged(baseBranch, {
...MATRIX_DEPENDENCIES.common,
...MATRIX_DEPENDENCIES.clients.common,
});

// No changes found, we don't put this job in the matrix
if (specChanges === 0 && !baseChanged) {
continue;
}

matrix.toRun.push(client);
matrix.cacheToCompute.push(path);
}

// We have nothing to run
if (matrix.toRun.length === 0) {
console.log('::set-output name=RUN_SPECS::false');
console.log(`::set-output name=MATRIX::${JSON.stringify(EMPTY_MATRIX)}`);

return;
matrix.cacheToCompute.push(`specs/${bundledSpecName}`);
}

const ciMatrix: Matrix<SpecMatrix> = {
Expand All @@ -174,7 +139,6 @@ async function getSpecMatrix(baseBranch: string): Promise<void> {
],
};

console.log('::set-output name=RUN_SPECS::true');
console.log(`::set-output name=MATRIX::${JSON.stringify(ciMatrix)}`);
}

Expand All @@ -186,7 +150,7 @@ async function createMatrix(opts: CreateMatrix): Promise<void> {
return await getClientMatrix(opts.baseBranch);
}

return await getSpecMatrix(opts.baseBranch);
return await getSpecMatrix();
}

if (require.main === module) {
Expand Down
72 changes: 42 additions & 30 deletions scripts/ci/githubActions/setRunVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,72 @@ const PHP_CLIENT_FOLDER = getLanguageFolder('php');
const CLIENTS_COMMON_FILES = [
'config/openapitools.json',
'config/clients.config.json',
'generators/src/main/java/com/algolia/codegen/Utils.java',
];

/**
* Dependencies that are common to every specs, clients or CTS jobs.
*/
export const COMMON_DEPENDENCIES = {
GITHUB_ACTIONS_CHANGED: [
'.github/actions',
'.github/workflows',
'.github/.cache_version',
],
SCRIPTS_CHANGED: ['scripts'],
COMMON_SPECS_CHANGED: ['specs/common'],
};

/**
* 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.
* made in their respective dependencies.
*
* Negative paths should start with `:!`.
*
* The variable will be accessible in the CI via `steps.diff.outputs.<name>`.
*
* Variables starting by `LANGUAGENAME_` will be used in the `createMatrix` to determine
* if a job should be added.
*/
export const DEPENDENCIES = {
GITHUB_ACTIONS_CHANGED: [
'.github/actions',
'.github/workflows',
'.github/.cache_version',
],
SHOULD_RUN_SPECS: [
...CLIENTS_COMMON_FILES,
'specs',
'templates',
'generators',
':!specs/bundled',
],
COMMON_SPECS_CHANGED: ['specs/common'],
...COMMON_DEPENDENCIES,
TESTS_CHANGED: ['tests'],
SCRIPTS_CHANGED: ['scripts'],
GENERATORS_CHANGED: ['generators'],
JS_CLIENT_CHANGED: [
...CLIENTS_COMMON_FILES,
JS_CLIENT_FOLDER,
`:!${JS_CLIENT_FOLDER}/.github`,
`:!${JS_CLIENT_FOLDER}/README.md`,
],
JS_ALGOLIASEARCH_CHANGED: [
`${JS_CLIENT_FOLDER}/packages/algoliasearch`,
`${JS_CLIENT_FOLDER}/packages/client-search`,
`${JS_CLIENT_FOLDER}/packages/client-analytics`,
`${JS_CLIENT_FOLDER}/packages/client-personalization`,
],
JS_UTILS_CHANGED: [
JS_COMMON_TESTS_CHANGED: [
`${JS_CLIENT_FOLDER}/packages/client-common/src/__tests__`,
],
JAVASCRIPT_UTILS_CHANGED: [
`${JS_CLIENT_FOLDER}/packages/client-common`,
`${JS_CLIENT_FOLDER}/packages/requester-browser-xhr`,
`${JS_CLIENT_FOLDER}/packages/requester-node-http`,
],
JS_COMMON_TESTS_CHANGED: [
`${JS_CLIENT_FOLDER}/packages/client-common/src/__tests__`,
JAVASCRIPT_CLIENT_CHANGED: [
...CLIENTS_COMMON_FILES,
JS_CLIENT_FOLDER,
'templates/javascript',
'generators/src/main/java/com/algolia/codegen/AlgoliaJavascriptGenerator.java',
`:!${JS_CLIENT_FOLDER}/.github`,
`:!${JS_CLIENT_FOLDER}/README.md`,
],
JAVA_CLIENT_CHANGED: [
...CLIENTS_COMMON_FILES,
JAVA_CLIENT_FOLDER,
'templates/java',
'generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java',
],
PHP_CLIENT_CHANGED: [
...CLIENTS_COMMON_FILES,
PHP_CLIENT_FOLDER,
'templates/php',
'generators/src/main/java/com/algolia/codegen/AlgoliaPhpGenerator.java',
],
JS_TEMPLATE_CHANGED: ['templates/javascript'],
JAVA_CLIENT_CHANGED: [...CLIENTS_COMMON_FILES, JAVA_CLIENT_FOLDER],
JAVA_TEMPLATE_CHANGED: ['templates/java'],
PHP_CLIENT_CHANGED: [...CLIENTS_COMMON_FILES, PHP_CLIENT_FOLDER],
PHP_TEMPLATE_CHANGED: ['templates/php'],
};

/**
Expand Down

0 comments on commit d22e05f

Please sign in to comment.