-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
045b8a1
feat(script): convert matrix scripts
shortcuts 5aeae9b
maybe less duplication
shortcuts 658cbe0
Merge branch 'main' into feat/convert-matrix-scripts
shortcuts f6e794d
apply changes from suggestion
shortcuts 7f4728c
remove scripts from cache
shortcuts 965cdbd
oops
shortcuts 4545b78
Merge branch 'main' into feat/convert-matrix-scripts
shortcuts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 || | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// `algoliasearch` is an aggregation of clients | ||
generatorOptions.client === 'algoliasearch' | ||
) { | ||
continue; | ||
} | ||
|
||
const folder = generatorOptions.output.replace('#{cwd}', ''); | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) { | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue; | ||
} | ||
|
||
matrix.client.push(client); | ||
} | ||
|
||
return matrix; | ||
} | ||
|
||
export async function createMatrix( | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"]}' | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
: JSON.stringify(matrix) | ||
); | ||
} | ||
|
||
const [job, ...args] = process.argv.slice(2); | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (require.main === module) { | ||
switch (job) { | ||
case 'spec': | ||
createMatrix(job, { | ||
baseChanged: args[0] === 'true', | ||
shortcuts marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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}`); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andscripts
, but maybe we need a better system, you can see the dependencies hereUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 folderThere was a problem hiding this comment.
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 😓There was a problem hiding this comment.
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