Skip to content

feat(script): convert matrix scripts #318

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 7 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
95 changes: 91 additions & 4 deletions .github/actions/cache/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,32 +339,119 @@ runs:
)}}

# Restore Java clients: used during 'cts' or 'codegen'
- name: Restore built Java client
- name: Restore built Java search client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-java-2
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-java-2/**',
'clients/algoliasearch-client-java-2/search/**',
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should also include generators and scripts, but maybe we need a better system, you can see the dependencies here

Copy link
Member Author

@shortcuts shortcuts Apr 4, 2022

Choose a reason for hiding this comment

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

I think we will need to at least re-arrange our folder structure for the CTS and Scripts in order to be able to scope those more easily, I'll put the cache to the scripts folder for now but this should be in a generation/build related folder

Copy link
Member Author

Choose a reason for hiding this comment

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

There is node_modules in scripts, mostly because of different versions in our package.json. I'll fix it in an other PR and add back the keys 😓

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay, but changes to package version should also invalidate the cache

'clients/algoliasearch-client-java-2/model/search/**',
'templates/java/**',
'specs/bundled/search.yml'
)}}

# Restore PHP clients: used during 'cts' or 'codegen'
- name: Restore built PHP client
- name: Restore built PHP search client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/*',
'clients/algoliasearch-client-php/lib/Api/SearchApi.php',
'clients/algoliasearch-client-php/lib/Configuration/SearchConfig.php',
'templates/php/**',
'specs/bundled/search.yml'
)}}

- name: Restore built PHP recommend client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/RecommendApi.php',
'clients/algoliasearch-client-php/lib/Configuration/RecommendConfig.php',
'templates/php/**',
'specs/bundled/recommend.yml'
)}}

- name: Restore built PHP personalization client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/PersonalizationApi.php',
'clients/algoliasearch-client-php/lib/Configuration/PersonalizationConfig.php',
'templates/php/**',
'specs/bundled/personalization.yml'
)}}

- name: Restore built PHP analytics client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/AnalyticsApi.php',
'clients/algoliasearch-client-php/lib/Configuration/AnalyticsConfig.php',
'templates/php/**',
'specs/bundled/analytics.yml'
)}}

- name: Restore built PHP insights client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/InsightsApi.php',
'clients/algoliasearch-client-php/lib/Configuration/InsightsConfig.php',
'templates/php/**',
'specs/bundled/insights.yml'
)}}

- name: Restore built PHP abtesting client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/AbTestingApi.php',
'clients/algoliasearch-client-php/lib/Configuration/AbTestingConfig.php',
'templates/php/**',
'specs/bundled/abtesting.yml'
)}}

- name: Restore built PHP query-suggestions client
if: ${{ inputs.job == 'cts' || inputs.job == 'codegen' }}
uses: actions/cache@v2
with:
path: clients/algoliasearch-client-php
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
'clients/algoliasearch-client-php/lib/Api/QuerySuggestionsApi.php',
'clients/algoliasearch-client-php/lib/Configuration/QuerySuggestionsConfig.php',
'templates/php/**',
'specs/bundled/query-suggestions.yml'
)}}

- name: Install JavaScript dependencies
shell: bash
run: YARN_ENABLE_IMMUTABLE_INSTALLS=false yarn install
8 changes: 4 additions & 4 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ runs:
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 }}

matrix=$(./scripts/ci/create-spec-matrix.sh $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})
matrix=$(yarn workspace scripts createMatrix spec $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

if [[ $matrix == '{"client":["no-run"]}' ]]; then
run="false"
Expand All @@ -101,7 +101,7 @@ runs:
algoliasearch_changed=${{ steps.diff.outputs.JS_ALGOLIASEARCH_CHANGED > 0 }}
common_changed=${{ steps.diff.outputs.JS_COMMON_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh javascript $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})
matrix=$(yarn workspace scripts createMatrix client javascript $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

if [[ $algoliasearch_changed == 'true' || $base_changed == 'true' ]]; then
echo "Running algoliasearch: true"
Expand Down Expand Up @@ -132,7 +132,7 @@ runs:
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.GENERATORS_CHANGED > 0 || steps.diff.outputs.JAVA_TEMPLATE_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh java $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})
matrix=$(yarn workspace scripts createMatrix client java $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

if [[ $matrix == '{"client":["no-run"]}' ]]; then
run="false"
Expand All @@ -153,7 +153,7 @@ runs:
run: |
base_changed=${{ steps.diff.outputs.GITHUB_ACTIONS_CHANGED > 0 || steps.diff.outputs.COMMON_SPECS_CHANGED > 0 || steps.diff.outputs.SCRIPTS_CHANGED > 0 || steps.diff.outputs.GENERATORS_CHANGED > 0 || steps.diff.outputs.PHP_TEMPLATE_CHANGED > 0 }}

matrix=$(./scripts/ci/create-client-matrix.sh php $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})
matrix=$(yarn workspace scripts createMatrix client php $base_changed ${{ steps.diff.outputs.ORIGIN_BRANCH }})

if [[ $matrix == '{"client":["no-run"]}' ]]; then
run="false"
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ jobs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('{0}/**', matrix.client.folder),
format('{0}/{1}/**', matrix.client.folder, matrix.client.name),
format('{0}/model/{1}/**', matrix.client.folder, matrix.client.name),
'templates/java/**',
format('specs/bundled/{0}.yml', matrix.client.name)
)}}
Expand Down Expand Up @@ -288,7 +289,9 @@ jobs:
key: |
${{ env.CACHE_VERSION }}-${{
hashFiles(
format('{0}/**', matrix.client.folder),
format('{0}/lib/Api/{1}.php', matrix.client.folder, matrix.client.api),
format('{0}/lib/Configuration/{1}.php', matrix.client.folder, matrix.client.config),
'templates/php/**',
format('specs/bundled/{0}.yml', matrix.client.name)
)}}

Expand Down
41 changes: 0 additions & 41 deletions scripts/ci/create-client-matrix.sh

This file was deleted.

29 changes: 0 additions & 29 deletions scripts/ci/create-spec-matrix.sh

This file was deleted.

129 changes: 129 additions & 0 deletions scripts/ci/createMatrix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { CLIENTS, GENERATORS, run } from '../common';
import type { Language } from '../types';

type CreateMatrix = {
language?: Language;
baseChanged: boolean;
baseBranch: string;
};

type Client = {
name: string;
folder: string;
config?: string;
api?: string;
};

type Matrix<TMatrix> = {
client: TMatrix[];
};

async function getClientMatrix({
language,
baseBranch,
baseChanged,
}: CreateMatrix): Promise<Matrix<Client>> {
const matrix: Matrix<Client> = { client: [] };

for (const [generatorKey, generatorOptions] of Object.entries(GENERATORS)) {
if (
generatorKey.startsWith(`${language}-`) === false ||
// `algoliasearch` is an aggregation of clients
generatorOptions.client === 'algoliasearch'
) {
continue;
}

const folder = generatorOptions.output.replace('#{cwd}', '');
const specChanges = await run(
`git diff --shortstat ${baseBranch}..HEAD -- specs/${generatorOptions.client} | wc -l | tr -d ' '`
);
const clientChanges = await run(
`git diff --shortstat ${baseBranch}..HEAD -- ${folder} | wc -l | tr -d ' '`
);

if (clientChanges === '0' && specChanges === '0' && baseChanged === false) {
continue;
}

const matchedGenerator: Client = {
name: generatorOptions.client,
folder,
};

// Extra informations for the PHP matrix in order to properly scope the
// GitHub action cache
if (language === 'php') {
matchedGenerator.config =
generatorOptions.additionalProperties.configClassname;
matchedGenerator.api =
generatorOptions.additionalProperties.configClassname.replace(
'Config',
'Api'
);
}

matrix.client.push(matchedGenerator);
}

return matrix;
}

async function getSpecMatrix({
baseBranch,
baseChanged,
}: CreateMatrix): Promise<Matrix<string>> {
const matrix: Matrix<string> = { client: [] };

for (const client of CLIENTS) {
const specChanges = await run(
`git diff --shortstat ${baseBranch}..HEAD -- specs/${client} | wc -l | tr -d ' '`
);

if (specChanges === '0' && baseChanged === false) {
continue;
}

matrix.client.push(client);
}

return matrix;
}

export async function createMatrix(
job: 'client' | 'spec',
opts: CreateMatrix
): Promise<void> {
const matrix =
job === 'spec' ? await getSpecMatrix(opts) : await getClientMatrix(opts);

// eslint-disable-next-line no-console
console.log(
// client cannot be empty or the matrix will fail
matrix.client.length === 0
? '{"client":["no-run"]}'
: JSON.stringify(matrix)
);
}

const [job, ...args] = process.argv.slice(2);

if (require.main === module) {
switch (job) {
case 'spec':
createMatrix(job, {
baseChanged: args[0] === 'true',
baseBranch: args[1],
});
break;
case 'client':
createMatrix(job, {
language: args[0] as Language,
baseChanged: args[1] === 'true',
baseBranch: args[2],
});
break;
default:
throw new Error(`Unknown job: ${job}`);
}
}
1 change: 1 addition & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "scripts",
"version": "1.0.0",
"scripts": {
"createMatrix": "ts-node ci/createMatrix.ts",
"createReleaseIssue": "ts-node release/create-release-issue.ts",
"processRelease": "ts-node release/process-release.ts",
"pushGeneratedCode": "ts-node ci/codegen/pushGeneratedCode.ts",
Expand Down