-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
228 additions
and
1 deletion.
There are no files selected for viewing
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
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,146 @@ | ||
import * as pj from 'projen'; | ||
import { yarn } from 'cdklabs-projen-project-types'; | ||
import { relative } from 'node:path'; | ||
|
||
export interface DiffDbOptions { | ||
readonly workflowRunsOn: string[]; | ||
readonly serviceSpec: yarn.TypeScriptWorkspace; | ||
readonly serviceSpecImporters: yarn.TypeScriptWorkspace; | ||
} | ||
|
||
export class DiffDb extends pj.Component { | ||
private readonly workflow: pj.github.GithubWorkflow; | ||
private readonly serviceSpec: yarn.TypeScriptWorkspace; | ||
private readonly importers: yarn.TypeScriptWorkspace; | ||
private readonly workflowRunsOn: string[]; | ||
|
||
public constructor(root: yarn.Monorepo, options: DiffDbOptions) { | ||
super(root); | ||
|
||
this.workflow = root.github?.tryFindWorkflow('build')!; | ||
this.serviceSpec = options.serviceSpec; | ||
this.importers = options.serviceSpecImporters; | ||
this.workflowRunsOn = options.workflowRunsOn; | ||
|
||
this.uploadHeadDatabase(); | ||
this.buildBaseDatabase(); | ||
this.diffDatabase(); | ||
} | ||
|
||
private path(path: string) { | ||
return relative(this.project.outdir, path); | ||
} | ||
|
||
private uploadHeadDatabase() { | ||
this.workflow.file?.patch( | ||
pj.JsonPatch.add('/jobs/build/steps/-', { | ||
name: 'Upload head database', | ||
uses: 'actions/upload-artifact@v3', | ||
with: { | ||
name: 'db.head.json.gz', | ||
path: this.path(this.serviceSpec.outdir + '/db.json.gz'), | ||
}, | ||
}), | ||
); | ||
} | ||
|
||
private buildBaseDatabase() { | ||
this.workflow.addJob('base-database', { | ||
runsOn: this.workflowRunsOn, | ||
env: { CI: 'true' }, | ||
permissions: {}, | ||
steps: [ | ||
{ | ||
name: 'Checkout', | ||
uses: 'actions/checkout@v3', | ||
with: { | ||
ref: '${{ github.event.pull_request.base.ref }}', | ||
repository: '${{ github.event.pull_request.base.repo.full_name }}', | ||
lfs: true, | ||
}, | ||
}, | ||
{ | ||
name: 'Install dependencies', | ||
run: 'yarn install --check-files', | ||
}, | ||
{ | ||
name: 'Build base database', | ||
workingDirectory: this.path(this.serviceSpec.outdir), | ||
run: 'npx projen nx compile', | ||
}, | ||
{ | ||
name: 'Upload base database', | ||
uses: 'actions/upload-artifact@v3', | ||
with: { | ||
name: 'db.base.json.gz', | ||
path: this.path(this.serviceSpec.outdir + '/db.json.gz'), | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
private diffDatabase() { | ||
this.workflow.addJob('diff-db', { | ||
needs: ['build', 'base-database'], | ||
if: '!(needs.build.outputs.self_mutation_happened)', | ||
runsOn: this.workflowRunsOn, | ||
env: { CI: 'true' }, | ||
permissions: { | ||
contents: pj.github.workflows.JobPermission.WRITE, | ||
}, | ||
steps: [ | ||
{ | ||
name: 'Checkout', | ||
uses: 'actions/checkout@v3', | ||
with: { | ||
ref: '${{ github.event.pull_request.head.ref }}', | ||
repository: '${{ github.event.pull_request.head.repo.full_name }}', | ||
lfs: false, | ||
}, | ||
}, | ||
{ | ||
name: 'Install dependencies', | ||
run: 'yarn install --check-files', | ||
}, | ||
{ | ||
name: 'Build diff-db', | ||
workingDirectory: this.path(this.importers.outdir), | ||
run: 'npx projen nx compile', | ||
}, | ||
{ | ||
name: 'Download base database', | ||
uses: 'actions/download-artifact@v3', | ||
with: { | ||
name: 'db.base.json.gz', | ||
path: '${{ runner.temp }}', | ||
}, | ||
}, | ||
{ | ||
name: 'Download head database', | ||
uses: 'actions/download-artifact@v3', | ||
with: { | ||
name: 'db.head.json.gz', | ||
path: '${{ runner.temp }}', | ||
}, | ||
}, | ||
{ | ||
id: 'diff-db', | ||
name: 'Diff databases', | ||
workingDirectory: this.path(this.importers.outdir), | ||
run: 'echo "diff=$(./bin/import-db ${{ runner.temp }}/db.base.json.gz ${{ runner.temp }}/db.head.json.gz)" >> "$GITHUB_OUTPUT"', | ||
}, | ||
{ | ||
name: 'Comment diff', | ||
if: '${{ steps.diff-db.outputs.diff }}', | ||
uses: 'thollander/actions-comment-pull-request@v2', | ||
with: { | ||
comment_tag: 'diff-db', | ||
message: | ||
'Model database diff introduced between base and head branch:\n```\n${{ steps.diff-db.outputs.diff }}\n```\n', | ||
}, | ||
}, | ||
], | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './aws-cdk-integration-test'; | ||
export * from './diff-db'; | ||
export * from './monorepo'; | ||
export * from './workspace'; |