Skip to content

Commit

Permalink
feat(node): mutable pull request builds (projen#624)
Browse files Browse the repository at this point in the history
Introduce "mutablke builds". When enabled (by default) it will cause PR builds to automatically push any changes to synthesized files or snapshots to the PR branch. This only works for branches and will fail for forks (similar behavior to the anti-tamper check).

See attached documentation.

This means that if a PR is submitted and requires updates to generated files, the PR branch will automatically get updated. It also implies that anti-tamper check is disabled for PR builds.

This is done as part of the PR build which means that if the build passes and changes are pushed successfully, the PR check will be successful (doing that as a result of a different workflow will not trigger the PR build).

Related to projen#602 and projen#604
  • Loading branch information
Elad Ben-Israel authored Mar 17, 2021
1 parent 7997b69 commit 757f8b1
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 86 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Setup Node.js
uses: actions/setup-node@v1
with:
Expand All @@ -20,8 +23,6 @@ jobs:
run: yarn install --check-files --frozen-lockfile
- name: Synthesize project files
run: /bin/bash ./projen.bash
- name: Anti-tamper check
run: git diff --exit-code
- name: Set git identity
run: |-
git config user.name "Auto-bump"
Expand All @@ -32,7 +33,8 @@ jobs:
uses: codecov/codecov-action@v1
with:
directory: coverage
- name: Anti-tamper check
run: git diff --exit-code
- name: Commit and push changes (if any)
run: 'git diff --exit-code || (git commit -am "chore: self mutation" && git push
origin HEAD:${{ github.event.pull_request.head.ref }})'
container:
image: jsii/superchain
8 changes: 2 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ jobs:
- name: Anti-tamper check
run: git diff --exit-code
- name: Push commits
run: git push origin $BRANCH
env:
BRANCH: ${{ github.ref }}
run: git push origin HEAD:${{ github.ref }}
- name: Push tags
run: git push --follow-tags origin $BRANCH
env:
BRANCH: ${{ github.ref }}
run: git push --follow-tags origin ${{ github.ref }}
- name: Upload artifact
uses: actions/upload-artifact@v2.1.1
with:
Expand Down
4 changes: 4 additions & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const project = new JsiiProject({
authorEmail: 'benisrae@amazon.com',
stability: 'experimental',

// this will cause PR builds to auto update the
// branch with changes to generated files (experimental)
buildWorkflowMutable: true,

pullRequestTemplateContents: [
'---',
'By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.',
Expand Down
27 changes: 27 additions & 0 deletions API.md

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions docs/mutable-builds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Mutable Builds

Projen synthesizes files that are part of your source repository. This means
that when you change you projenrc file, and execute `projen`, other files in
your repo may change as a result.

This is also relevant in other situations where your build process _mutates_
your repository. For example, if you use **snapshot testing**, your repository
includes snapshots which represent expected test results. When your code
changes, you will likely need to update those snapshots as well.

To ensure that a pull request branch always represent the final state of the
repository, you can enable the `mutableBuild` option in your project
configuration (currently only supported for projects derived from
`NodeProject`).

When enabled, the PR build workflow (also called `build`) will push any modified
files to the PR branch after a successful build, so that the branch will always
reflect the most up-to-date version of all generated files.

## Forks

This feature does not work for forks since it is impossible to safely push
changes to a fork from a PR build. If a PR is created from a fork of the
repository and there are build mutations, the PR build will fail (indicating
that it cannot push to the fork). To fix this, the branch needs to be updated
(same behavior as if mutable builds was disabled).
36 changes: 16 additions & 20 deletions src/__tests__/__snapshots__/integ.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: \${{ github.event.pull_request.head.ref }}
repository: \${{ github.event.pull_request.head.repo.full_name }}
- name: Setup Node.js
uses: actions/setup-node@v1
with:
Expand All @@ -267,16 +270,15 @@ jobs:
run: yarn install --check-files --frozen-lockfile
- name: Synthesize project files
run: npx projen
- name: Anti-tamper check
run: git diff --exit-code
- name: Set git identity
run: |-
git config user.name \\"Auto-bump\\"
git config user.email \\"github-actions@github.com\\"
- name: Build
run: npx projen build
- name: Anti-tamper check
run: git diff --exit-code
- name: Commit and push changes (if any)
run: 'git diff --exit-code || (git commit -am \\"chore: self mutation\\" && git push
origin HEAD:\${{ github.event.pull_request.head.ref }})'
container:
image: jsii/superchain
",
Expand Down Expand Up @@ -352,13 +354,9 @@ jobs:
- name: Anti-tamper check
run: git diff --exit-code
- name: Push commits
run: git push origin $BRANCH
env:
BRANCH: \${{ github.ref }}
run: git push origin HEAD:\${{ github.ref }}
- name: Push tags
run: git push --follow-tags origin $BRANCH
env:
BRANCH: \${{ github.ref }}
run: git push --follow-tags origin \${{ github.ref }}
- name: Upload artifact
uses: actions/upload-artifact@v2.1.1
with:
Expand Down Expand Up @@ -4119,20 +4117,22 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
with:
ref: \${{ github.event.pull_request.head.ref }}
repository: \${{ github.event.pull_request.head.repo.full_name }}
- name: Install dependencies
run: pnpm i --frozen-lockfile
- name: Synthesize project files
run: npx projen
- name: Anti-tamper check
run: git diff --exit-code
- name: Set git identity
run: |-
git config user.name \\"Auto-bump\\"
git config user.email \\"github-actions@github.com\\"
- name: Build
run: npx projen build
- name: Anti-tamper check
run: git diff --exit-code
- name: Commit and push changes (if any)
run: 'git diff --exit-code || (git commit -am \\"chore: self mutation\\" && git push
origin HEAD:\${{ github.event.pull_request.head.ref }})'
",
".github/workflows/release.yml": "# ~~ Generated by projen. To modify, edit .projenrc.js and run \\"npx projen\\".
Expand Down Expand Up @@ -4169,13 +4169,9 @@ jobs:
- name: Anti-tamper check
run: git diff --exit-code
- name: Push commits
run: git push origin $BRANCH
env:
BRANCH: \${{ github.ref }}
run: git push origin HEAD:\${{ github.ref }}
- name: Push tags
run: git push --follow-tags origin $BRANCH
env:
BRANCH: \${{ github.ref }}
run: git push --follow-tags origin \${{ github.ref }}
- name: Upload artifact
uses: actions/upload-artifact@v2.1.1
with:
Expand Down
132 changes: 132 additions & 0 deletions src/__tests__/__snapshots__/inventory.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -1802,6 +1814,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -2927,6 +2951,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -4354,6 +4390,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -5364,6 +5412,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -6413,6 +6473,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -7401,6 +7473,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -8790,6 +8874,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -9815,6 +9911,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -10887,6 +10995,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down Expand Up @@ -11959,6 +12079,18 @@ Array [
"switch": "min-node-version",
"type": "string",
},
Object {
"default": "true",
"docs": "Automatically update files modified during builds to pull-request branches.",
"name": "mutableBuild",
"optional": true,
"parent": "NodeProjectOptions",
"path": Array [
"mutableBuild",
],
"switch": "mutable-build",
"type": "boolean",
},
Object {
"default": "$BASEDIR",
"docs": "This is the name of your project.",
Expand Down
Loading

0 comments on commit 757f8b1

Please sign in to comment.