From cb98c6e66c03fca6a1b3ce09b7479d7f9ac399a8 Mon Sep 17 00:00:00 2001 From: Tyler Butler Date: Fri, 5 May 2023 12:12:48 -0700 Subject: [PATCH] feat: Support JSON output in check:changeset (#15465) The changeset workflows continue to challenge me. The simple 0/1 output isn't working for reasons I cannot explain, so I have added JSON support to the check:changeset command. When the `--json` flag is passed, the exit code will not be 1 in the case of a missing changeset. Instead the JSON output includes a boolean value that should be checked. I included the workflow changes in this PR so reviewers can see how these command changes will be consumed. They won't be merged with this PR - they'll be made separately (see #15472) after these changes are available in the client release group. --- .github/workflows/changeset-reporter.yml | 13 +++++----- .github/workflows/pr-check-changeset.yml | 17 ++++++------ build-tools/packages/build-cli/docs/check.md | 5 +++- .../build-cli/src/commands/check/changeset.ts | 26 +++++++++++++++++-- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/.github/workflows/changeset-reporter.yml b/.github/workflows/changeset-reporter.yml index 8e00c92003ec..a582ba401791 100644 --- a/.github/workflows/changeset-reporter.yml +++ b/.github/workflows/changeset-reporter.yml @@ -20,21 +20,20 @@ jobs: with: workflow: pr-check-changeset.yml run_id: ${{ github.event.workflow_run.id }} - name: changeset-status + name: changeset-metadata - - name: Load status - id: status - run: echo "status=$(cat changeset-status)" >> $GITHUB_OUTPUT + - name: Load changeset metadata into env variable + run: echo "CHANGESET=$(cat changeset-metadata.json)" >> $GITHUB_ENV - name: Required but missing - if: steps.status.outputs.status != '0' && contains(github.event.pull_request.labels.*.name, 'changeset-required') + if: fromJson(env.CHANGESET).changesetFound == false && contains(github.event.pull_request.labels.*.name, 'changeset-required') uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # ratchet:marocchino/sticky-pull-request-comment@v2.3.1 with: header: changeset path: ./.github/workflows/data/changeset-missing.md - name: Required and present - if: steps.status.outputs.status == '0' && contains(github.event.pull_request.labels.*.name, 'changeset-required') + if: fromJson(env.CHANGESET).changesetFound == true && contains(github.event.pull_request.labels.*.name, 'changeset-required') uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # ratchet:marocchino/sticky-pull-request-comment@v2.3.1 with: header: changeset @@ -43,7 +42,7 @@ jobs: This PR requires a changeset and it has one! Good job! - name: Changeset not required - if: steps.status.outputs.status == '0' && !contains(github.event.pull_request.labels.*.name, 'changeset-required') + if: fromJson(env.CHANGESET).changesetFound == true && !contains(github.event.pull_request.labels.*.name, 'changeset-required') uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # ratchet:marocchino/sticky-pull-request-comment@v2.3.1 with: header: changeset diff --git a/.github/workflows/pr-check-changeset.yml b/.github/workflows/pr-check-changeset.yml index 115076dc3964..687e0fef831e 100644 --- a/.github/workflows/pr-check-changeset.yml +++ b/.github/workflows/pr-check-changeset.yml @@ -48,15 +48,14 @@ jobs: - id: changeset-status name: Changeset status run: | - set +e # don't exit on failure - pnpm exec flub check changeset --branch=${{ github.base_ref }} - echo "$?" > ./changeset-status + # JSON output is piped through jq to compact it to a single line + pnpm exec flub check changeset --branch=${{ github.base_ref }} --json | jq -c > changeset-metadata.json - name: Upload changeset status uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # ratchet:actions/upload-artifact@v3 with: - name: changeset-status - path: ./changeset-status + name: changeset-metadata.json + path: ./changeset-metadata retention-days: 3 # Any PR without the changeset-required label will be ignored. @@ -68,12 +67,12 @@ jobs: - id: changeset-status name: Changeset disabled run: | - # Always output 0 to signal that changesets aren't needed - echo "0" > ./changeset-status + # Always output changesetFound=true to signal that changesets aren't needed + echo "{'branch': '${{ github.base_ref }}','changesetFound': true}" > ./changeset-metadata.json - name: Upload changeset status uses: actions/upload-artifact@0b7f8abb1508181956e8e162db84b466c27e18ce # ratchet:actions/upload-artifact@v3 with: - name: changeset-status - path: ./changeset-status + name: changeset-metadata.json + path: ./changeset-metadata retention-days: 3 diff --git a/build-tools/packages/build-cli/docs/check.md b/build-tools/packages/build-cli/docs/check.md index ac48e507f544..3b7f092e17d6 100644 --- a/build-tools/packages/build-cli/docs/check.md +++ b/build-tools/packages/build-cli/docs/check.md @@ -51,12 +51,15 @@ Checks if a changeset was added when compared against a branch. This is used in ``` USAGE - $ flub check changeset -b [-v] + $ flub check changeset -b [-v] [--json] FLAGS -b, --branch= (required) The branch to compare against. -v, --verbose Verbose logging. +GLOBAL FLAGS + --json Format output as json. + EXAMPLES Check if a changeset was added when compared to the 'main' branch. diff --git a/build-tools/packages/build-cli/src/commands/check/changeset.ts b/build-tools/packages/build-cli/src/commands/check/changeset.ts index 88ef84efeaf6..a40a3884ca64 100644 --- a/build-tools/packages/build-cli/src/commands/check/changeset.ts +++ b/build-tools/packages/build-cli/src/commands/check/changeset.ts @@ -4,6 +4,7 @@ */ import { Flags } from "@oclif/core"; import chalk from "chalk"; +import { sortPackageJson as sortJson } from "sort-package-json"; import { BaseCommand } from "../../base"; import { Repository } from "../../lib"; @@ -11,6 +12,8 @@ import { Repository } from "../../lib"; export default class CheckChangesetCommand extends BaseCommand { static summary = `Checks if a changeset was added when compared against a branch. This is used in CI to enforce that changesets are present for a PR.`; + static enableJsonFlag = true; + static flags = { branch: Flags.string({ char: "b", @@ -31,7 +34,11 @@ export default class CheckChangesetCommand extends BaseCommand { + public async run(): Promise<{ + changesetFound: boolean; + branch: string; + changesetPath?: string; + }> { const context = await this.getContext(); const repo = new Repository({ baseDir: context.gitRepo.resolvedRoot }); const remote = await repo.getRemote(context.originRemotePartialUrl); @@ -50,9 +57,24 @@ export default class CheckChangesetCommand extends BaseCommand