Skip to content

Commit

Permalink
chore(repo): skip hangar when not needed (#3791)
Browse files Browse the repository at this point in the history
### Goal
Skip e2e tests when the changes in the PR don't effect it. Notably, this usually means unit tests, README, or anything in the console.

"changes" here means one of 2 things:
- **If non-pr:** Last successful build workflow on the same branch
- **If pr:** git diff of the PR compared to the base, ignoring any commits from upstream

This high-level logic is the same that we've been using to determine whether or not we deploy the vscode extension, so this extends it to the e2e and benchmarking tests as well.
Unfortunately turbo itself was not sufficient for this goal (see note at the top of `tools/bump-pack/src/turbo-diff.ts`), so I had to write some junk to properly determine "what changed". I swear I've had to write something like this like 9 times already at various points in my life.

#### Limitations

The "Last successful build workflow on the same branch" does not apply to PRs, even though I wish it did. Implementing this safely is more difficult than it seems.

### The future of bump-pack

Part of the this PR is also effectively turning "bump-pack" into a general-purpose toolkit. In a followup PR I plan to simply turn the entire package into a full CLI to serve misc repo development purposes. My hope is that this will make it easier to actually add testing to it with a fixture based on https://github.com/kiegroup/mock-github and https://github.com/kiegroup/act-js

### Misc

- Added a script for the git patching stuff, didn't like so much repetitious bash in the workflow
- ~Effectively forked `nrwl/nx-set-shas` since it was not sufficient for the workflow I wanted here (It always assumed you wanted to compare against `main` in a PR)~ Nevermind, it turns out that the original action is actually good enough


*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
MarkMcCulloh authored Aug 30, 2023
1 parent 27aa725 commit bfbd81d
Show file tree
Hide file tree
Showing 14 changed files with 269 additions and 60 deletions.
53 changes: 21 additions & 32 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ jobs:
version: ${{ fromJson(steps.changelog.outputs.data).newVersion }}
last-version: ${{ fromJson(steps.changelog.outputs.data).lastVersion }}
changelog: ${{ fromJson(steps.changelog.outputs.data).changelog }}
vscode-wing-changed: ${{ steps.git-diff-vscode-wing.outputs.diff }}

vscode-wing-changed: ${{ fromJson(steps.turbo-diff.outputs.data)['vscode-wing#compile'].changes }}
e2e-changed: ${{ fromJson(steps.turbo-diff.outputs.data)['hangar#test'].changes }}
bench-changed: ${{ fromJson(steps.turbo-diff.outputs.data)['hangar#bench'].changes }}
steps:
- name: Checkout
uses: actions/checkout@v3
Expand Down Expand Up @@ -72,30 +73,25 @@ jobs:
- name: Install Dependencies
run: pnpm install --frozen-lockfile

- name: Derive appropriate SHAs for base and head
id: shas
uses: nrwl/nx-set-shas@v3

- name: Figure out what projects changed
id: turbo-diff
run: pnpm turbo-diff --startRef ${{ steps.shas.outputs.base }} --endRef ${{ steps.shas.outputs.head }}

- name: Changelog Generation
id: changelog
env:
GENERATE_VERSION: ${{ github.event_name != 'push' || github.repository != 'winglang/wing' }}
run: pnpm changelog
run: pnpm bump-pack

- name: Build and Package
run: pnpm package:ci
env:
SEGMENT_WRITE_KEY: ${{ secrets.SEGMENT_WRITE_KEY }}

- name: Derive appropriate SHAs for base and head
id: setSHAs
uses: nrwl/nx-set-shas@v3

- name: Check git diff of VSCode Extension
id: git-diff-vscode-wing
run: |
if git diff --quiet ${{ steps.setSHAs.outputs.base }} ${{ github.sha }} -- apps/vscode-wing; then
echo "diff=false" >> "$GITHUB_OUTPUT"
else
echo "diff=true" >> "$GITHUB_OUTPUT"
fi
- name: Upload Artifacts
uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -145,14 +141,7 @@ jobs:

- name: Create git patch
id: diff
run: |
git add --all
git diff --staged --binary --patch > build.diff
if [ -s build.diff ]; then
echo "Diff found, creating a patch to apply later"
cat build.diff
echo "diff=true" >> $GITHUB_OUTPUT
fi
run: scripts/create_patch.sh build.diff

- name: Upload patch
if: steps.diff.outputs.diff == 'true'
Expand All @@ -163,6 +152,7 @@ jobs:

benchmark:
name: Benchmark
if: needs.build.outputs.bench-changed == 'true' || contains(github.event.pull_request.labels.*.name, '🧪 pr/e2e-full')
runs-on: ubuntu-latest
needs:
- build
Expand Down Expand Up @@ -208,6 +198,7 @@ jobs:
runs-on: "${{ matrix.runner }}-latest"
needs:
- build
if: needs.build.outputs.e2e-changed == 'true' || contains(github.event.pull_request.labels.*.name, '🧪 pr/e2e-full')
strategy:
fail-fast: true
matrix:
Expand Down Expand Up @@ -263,13 +254,7 @@ jobs:
RAW_SHARD: ${{ matrix.shard }}
run: |
SHARD=$(echo $RAW_SHARD | sed 's/\//of/g')
DIFF_NAME="e2e-$SHARD.diff"
git add --all
git diff --staged --binary --patch > $DIFF_NAME
if [ -s $DIFF_NAME ]; then
echo "diff=true" >> $GITHUB_OUTPUT
echo "diff_name=$DIFF_NAME" >> $GITHUB_OUTPUT
fi
scripts/create_patch.sh "e2e-$SHARD.diff"
- name: Upload mutation
if: matrix.runner == 'ubuntu' && matrix.node == '18' && steps.diff.outputs.diff == 'true'
Expand Down Expand Up @@ -401,7 +386,11 @@ jobs:

- name: Compute Checksums
run: |
mv ./benchmarks/* ./dist
# if there is a ./benchmarks/report.json, move it to ./dist
if [ -f ./benchmarks/report.json ]; then
mv ./benchmarks/report.json ./dist
fi
cd dist
echo '' >> ../CHANGELOG.md
Expand Down
4 changes: 1 addition & 3 deletions apps/vscode-wing/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"outputs": ["lib/**"]
},
"dev": {
"dependsOn": ["compile"],
"cache": false,
"persistent": true
"dependsOn": ["compile"]
},
"package": {
"inputs": ["syntaxes/**", "resources/**"],
Expand Down
5 changes: 3 additions & 2 deletions libs/wingc/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
"extends": ["//"],
"pipeline": {
"compile": {
"dependsOn": ["@winglang/wingii#compile", "@winglang/tree-sitter-wing#compile"],
"outputs": [
"../../target/wasm32-wasi/release/wingc.wasm",
"../../target/wasm32-wasi/release/libwingc.*"
]
},
"test": {
"dependsOn": ["compile", "examples-valid#topo", "examples-invalid#topo"]
"dependsOn": ["compile", "@winglang/sdk#compile", "examples-valid#topo", "examples-invalid#topo"]
},
"dev": {
"cache": false
"dependsOn": ["@winglang/sdk#compile"]
}
}
}
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"private": true,
"devDependencies": {
"bump-pack": "workspace:^",
"changelogen": "^0.5.3",
"semver": "^7.3.8",
"turbo": "^1.10.12"
},
"scripts": {
Expand All @@ -15,7 +13,6 @@
"package:ci": "turbo package --color && tar -czvf dist/docs.tgz docs/*",
"test:ci": "turbo default --color --concurrency 1 && turbo compile post-compile lint eslint test test:playwright --color --filter=!hangar",
"docs": "./scripts/docsite.sh",
"changelog": "bump-pack",
"install": "bash scripts/setup_wasi.sh",
"postinstall": "link-bundles",
"wing": "turbo compile --filter=winglang --output-logs=errors-only && ./apps/wing/bin/wing"
Expand Down
49 changes: 36 additions & 13 deletions pnpm-lock.yaml

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

31 changes: 31 additions & 0 deletions scripts/create_patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

# Check if running in GitHub Actions
if [ -z "$GITHUB_ACTIONS" ]; then
echo "Not running in GitHub Actions, skipping patch creation"
exit 0
fi

# Get patch name from args
if [ -z "$1" ]; then
echo "No patch name specified, skipping patch creation"
exit 1
fi

PATCH_NAME=$1
START_SHA=$2
END_SHA=$3

git add --all
if [ -z "$START_SHA" ]; then
git diff --staged --binary --patch > $1
else
git diff --staged --binary --patch $START_SHA $END_SHA > $1
fi

if [ -s $1 ]; then
echo "Diff found, creating a patch to apply later"
cat $1
echo "diff=true" >> $GITHUB_OUTPUT
echo "diff_name=$1" >> $GITHUB_OUTPUT
fi
3 changes: 2 additions & 1 deletion scripts/docsite.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh
#!/usr/bin/env bash

scriptdir=$(cd $(dirname $0) && pwd)
workdir="$HOME/.winglang-docsite"
docsdir=$(cd $scriptdir/../docs && pwd)
Expand Down
2 changes: 1 addition & 1 deletion tools/bump-pack/bin/bump-pack.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { resolve, relative } = require("node:path");

const which = require("npm-which")(__dirname);
const tsx = relative(process.cwd(), which.sync("tsx"));
const cliSource = relative(process.cwd(), resolve(__dirname, "../src/cli.ts"));
const cliSource = relative(process.cwd(), resolve(__dirname, "../src/bump-pack.ts"));
execSync(`${tsx} ${cliSource} ${process.argv.slice(2).join(" ")}`, {
stdio: "inherit",
});
10 changes: 10 additions & 0 deletions tools/bump-pack/bin/turbo-diff.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env -S node
const { execSync } = require("node:child_process");
const { resolve, relative } = require("node:path");

const which = require("npm-which")(__dirname);
const tsx = relative(process.cwd(), which.sync("tsx"));
const cliSource = relative(process.cwd(), resolve(__dirname, "../src/turbo-diff.ts"));
execSync(`${tsx} ${cliSource} ${process.argv.slice(2).join(" ")}`, {
stdio: "inherit",
});
Loading

0 comments on commit bfbd81d

Please sign in to comment.