From 40980b3ada13cb85199d1869b4a0ac69f75520d4 Mon Sep 17 00:00:00 2001
From: Gao Sun <gao@silverhand.io>
Date: Sat, 7 Jan 2023 21:14:17 +0800
Subject: [PATCH] ci: manually dispatch and add sanity check in workflow
 (#2838)

---
 .github/workflows/changesets.yml | 24 +++++++++++++++++++-----
 .scripts/version.js              |  7 ++++++-
 packages/schemas/package.json    |  1 +
 3 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/.github/workflows/changesets.yml b/.github/workflows/changesets.yml
index 97f62648a..8cc756c31 100644
--- a/.github/workflows/changesets.yml
+++ b/.github/workflows/changesets.yml
@@ -1,9 +1,17 @@
 name: Changesets
 
 on:
-  push:
-    branches:
-      - master
+  workflow_dispatch:
+    inputs:
+      releaseGroup:
+        description: 'The release group to bump version and create pull request'
+        required: true
+        type: choice
+        # This should be synced with `/.scripts/version.js`
+        options:
+          - core
+          - toolkit
+
   pull_request:
 
 concurrency:
@@ -13,7 +21,13 @@ jobs:
   changesets:
     strategy:
       matrix:
-        group: [core, toolkit]
+        # Multiline expression https://stackoverflow.com/a/67532120/12514940
+        group: |-
+          ${{
+            github.event_name == 'pull_request' &&
+            fromJSON('["core", "toolkit"]') ||
+            fromJSON(format('["{0}"]', inputs.releaseGroup))
+          }}
 
     runs-on: ubuntu-latest
 
@@ -46,7 +60,7 @@ jobs:
           git status
 
       - name: Create pull request
-        if: github.event_name == 'push'
+        if: github.event_name == 'workflow_dispatch'
         uses: peter-evans/create-pull-request@v4
         with:
           token: ${{ secrets.BOT_PAT }}
diff --git a/.scripts/version.js b/.scripts/version.js
index 0ab170533..8bbd13d1c 100644
--- a/.scripts/version.js
+++ b/.scripts/version.js
@@ -9,6 +9,7 @@ if (process.argv.length > 3) {
 }
 
 // This is configured based on our practice. Change with care.
+// Should be synced with `/.github/workflows/changesets.yml`
 const allowedGroups = { core: 'core', toolkit: 'toolkit' };
 if (!Object.values(allowedGroups).includes(versionGroup)) {
   throw new Error('Version group is invalid. Should be one of ' + Object.values(allowedGroups).join(', ') + '.');
@@ -33,7 +34,8 @@ const ignoreCmd = getIgnoreGroup()
   .join('');
 const cmd = ('pnpm changeset version' + ignoreCmd);
 
-const catchCmdError = ({ stderr, code }) => {
+const catchCmdError = ({ stderr, stdout, code }) => {
+  console.log(stdout);
   console.error(stderr);
   process.exit(code ?? 1);
 };
@@ -44,3 +46,6 @@ await execAsync(cmd).catch(catchCmdError);
 
 // Manually run lifecycle script since changesets didn't
 await execAsync('pnpm -r version').catch(catchCmdError);
+
+// Sanity check for prepublish scripts
+await execAsync('pnpm -r prepublishOnly').catch(catchCmdError);
diff --git a/packages/schemas/package.json b/packages/schemas/package.json
index 33a0021c7..429f81cab 100644
--- a/packages/schemas/package.json
+++ b/packages/schemas/package.json
@@ -30,6 +30,7 @@
     "dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput --incremental",
     "lint": "eslint --ext .ts src",
     "lint:report": "pnpm lint --format json --output-file report.json",
+    "prepublishOnly": "! ls alterations/next-*",
     "prepack": "pnpm build",
     "test:only": "NODE_OPTIONS=--experimental-vm-modules jest",
     "test": "pnpm build:test && pnpm test:only",