generated from ExpediaGroup/new-project
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generate-matrix): add generate-matrix helper (#351)
* feat(generate-matrix): add generate-matrix helper * make batches optional
- Loading branch information
1 parent
b94eb72
commit 74438fc
Showing
8 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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,43 @@ | ||
name: Generate Matrix | ||
|
||
on: | ||
pull_request: | ||
branches: [ main ] | ||
paths: | ||
- 'src/helpers/generate-matrix.ts' | ||
|
||
jobs: | ||
scheduler: | ||
name: Determine packages to build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- uses: ./ | ||
id: matrix | ||
with: | ||
helper: generate-matrix | ||
paths: | | ||
test/path/1 | ||
test/path/2 | ||
test/path/3 | ||
outputs: | ||
matrix: ${{ steps.matrix.outputs.output }} | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
needs: scheduler | ||
strategy: | ||
matrix: ${{ fromJson(needs.scheduler.outputs.matrix) }} | ||
steps: | ||
- run: echo "Run each job using ${{ matrix.path }}" | ||
|
||
build-status: | ||
runs-on: ubuntu-latest | ||
if: always() | ||
needs: build | ||
steps: | ||
- name: Check build status | ||
run: exit ${{ (needs.build.result == 'failure' || needs.build.result == 'cancelled') && 1 || 0 }} |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,27 @@ | ||
/* | ||
Copyright 2021 Expedia, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { HelperInputs } from '../types/generated'; | ||
import { chunk } from 'lodash'; | ||
|
||
export class GenerateMatrix extends HelperInputs { | ||
paths = ''; | ||
batches?: string; | ||
} | ||
|
||
export const generateMatrix = ({ paths, batches = '1' }: GenerateMatrix) => { | ||
const matrixValues = paths.split(/[\n,]/); | ||
return { | ||
include: chunk(matrixValues, Math.ceil(matrixValues.length / Number(batches))).map(chunk => ({ path: chunk.join(',') })) | ||
}; | ||
}; |
This file contains 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,39 @@ | ||
/* | ||
Copyright 2021 Expedia, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { generateMatrix } from '../../src/helpers/generate-matrix'; | ||
|
||
jest.mock('@actions/core'); | ||
jest.mock('@actions/github', () => ({ | ||
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } }, | ||
getOctokit: jest.fn() | ||
})); | ||
|
||
describe('generateMatrix', () => { | ||
it('should generate matrix json with appropriate batching', () => { | ||
const result = generateMatrix({ paths: 'path/1,path/2,path/3,path/4,path/5', batches: '3' }); | ||
expect(result).toEqual({ | ||
include: [ | ||
{ | ||
path: 'path/1,path/2' | ||
}, | ||
{ | ||
path: 'path/3,path/4' | ||
}, | ||
{ | ||
path: 'path/5' | ||
} | ||
] | ||
}); | ||
}); | ||
}); |