Skip to content

Commit

Permalink
post diff to PR
Browse files Browse the repository at this point in the history
  • Loading branch information
mrgrain committed Oct 24, 2023
1 parent aea3e57 commit d651036
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 1 deletion.
73 changes: 73 additions & 0 deletions .github/workflows/build.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion .projenrc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as pj from 'projen';
import { AwsCdkIntegrationTest, TypeScriptWorkspace, YarnMonorepo } from './projenrc';
import { AwsCdkIntegrationTest, DiffDb, TypeScriptWorkspace, YarnMonorepo } from './projenrc';
import { RegionalSource, Role, SingleSource, SourceProcessing } from './projenrc/update-sources';

const workflowRunsOn = [
Expand Down Expand Up @@ -178,6 +178,13 @@ new AwsCdkIntegrationTest(repo, {
serviceSpecTypes,
});

// Post diff of database
new DiffDb(repo, {
workflowRunsOn,
serviceSpec: awsServiceSpec,
serviceSpecImporters,
});

// Update sources
new SingleSource(repo, {
name: 'documentation',
Expand Down
146 changes: 146 additions & 0 deletions projenrc/diff-db.ts
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',
},
},
],
});
}
}
1 change: 1 addition & 0 deletions projenrc/index.ts
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';

0 comments on commit d651036

Please sign in to comment.