From 0308885d8cc83fb4090397c868f4e0427904ab30 Mon Sep 17 00:00:00 2001
From: Kyle Gach <kyle.gach@gmail.com>
Date: Thu, 11 Nov 2021 08:13:04 -0700
Subject: [PATCH] Add handle-release-branches-workflow

- Assuming "latest" is 6.3 & "next" is 6.4...
- On push to `main`
    - Use webhook to kick off production frontpage deploy
- On push to `next`
    - Creates & force-pushes `release-6-4` branch
    - Sends dispatch event to frontpage repo to create `release-6-4` branch
- On push to `release-x-x`
    - If pushing to `release-6-4`
        - Warns that changes will be lost on next push to `next`
    - Else
        - Sends dispatch event to frontpage repo to create `release-n-n` branch
- Remove `build-frontpage` script
    - Remove references from teamcity & circleci configs
---
 .circleci/config.yml                          |  14 --
 .github/workflows/handle-release-branches.yml | 131 ++++++++++++++++++
 .teamcity/settings.kts                        |  30 ----
 scripts/build-frontpage.js                    |  24 ----
 4 files changed, 131 insertions(+), 68 deletions(-)
 create mode 100644 .github/workflows/handle-release-branches.yml
 delete mode 100755 scripts/build-frontpage.js

diff --git a/.circleci/config.yml b/.circleci/config.yml
index 3736ab39090e..831dcd0f48bc 100644
--- a/.circleci/config.yml
+++ b/.circleci/config.yml
@@ -353,17 +353,6 @@ jobs:
           command: |
             cd examples/cra-react15
             yarn storybook --smoke-test --quiet
-  frontpage:
-    executor: sb_node_12_browsers
-    steps:
-      - git-shallow-clone/checkout_advanced:
-          clone_options: '--depth 1 --verbose'
-      - run:
-          name: Install dependencies
-          command: yarn install --immutable
-      - run:
-          name: Trigger build
-          command: ./scripts/build-frontpage.js
   lint:
     executor:
       class: small
@@ -446,6 +435,3 @@ workflows:
       - cra-bench:
           requires:
             - publish
-  deploy:
-    jobs:
-      - frontpage
diff --git a/.github/workflows/handle-release-branches.yml b/.github/workflows/handle-release-branches.yml
new file mode 100644
index 000000000000..a75d487e5d06
--- /dev/null
+++ b/.github/workflows/handle-release-branches.yml
@@ -0,0 +1,131 @@
+name: Handle Release Branches
+
+on:
+  push:
+
+jobs:
+  branch-checks:
+    runs-on: ubuntu-latest
+    steps:
+      - id: get-branch
+        run: |
+          BRANCH=($(echo ${{ github.ref }} | sed -E 's/refs\/heads\///'))
+          echo "branch=$BRANCH" >> $GITHUB_ENV
+    outputs:
+      branch: ${{ env.branch }}
+      is-latest-branch: ${{ env.branch == 'main' }}
+      is-next-branch: ${{ env.branch == 'next' }}
+      is-release-branch: ${{ startsWith(env.branch, 'release-') }}
+      is-actionable-branch: ${{ env.branch == 'main' || env.branch == 'next' || startsWith(env.branch, 'release-') }}
+
+  handle-latest:
+    needs: branch-checks
+    if: ${{ needs.branch-checks.outputs.is-latest-branch == 'true' }}
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+
+      - run: curl -X POST "https://api.netlify.com/build_hooks/${{ secrets.FRONTPAGE_HOOK }}"
+
+  get-latest-release-branch:
+    needs: branch-checks
+    if: ${{ needs.branch-checks.outputs.is-release-branch == 'true' }}
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+        with:
+          path: main
+
+      - id: latest-version
+        uses: notiz-dev/github-action-json-property@release
+        with:
+          path: ${{ github.workspace }}/main/docs/versions/latest.json
+          prop_path: version
+
+      - run: |
+          LATEST_RELEASE_BRANCH=($(echo ${{ steps.latest-version.outputs.prop }} | sed -E 's/([0-9]+)\.([0-9]+).*/release-\1-\2/'))
+          echo "latest-release-branch=$LATEST_RELEASE_BRANCH" >> $GITHUB_ENV
+    outputs:
+      branch: ${{ env.latest-release-branch }}
+
+  get-next-release-branch:
+    needs: branch-checks
+    if: ${{ needs.branch-checks.outputs.is-next-branch == 'true' || needs.branch-checks.outputs.is-release-branch == 'true' }}
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+        with:
+          ref: next
+          path: next
+
+      - id: next-version
+        uses: notiz-dev/github-action-json-property@release
+        with:
+          path: ${{ github.workspace }}/next/docs/versions/next.json
+          prop_path: version
+
+      - run: |
+          NEXT_RELEASE_BRANCH=($(echo ${{ steps.next-version.outputs.prop }} | sed -E 's/([0-9]+)\.([0-9]+).*/release-\1-\2/'))
+          echo "next-release-branch=$NEXT_RELEASE_BRANCH" >> $GITHUB_ENV
+    outputs:
+      branch: ${{ env.next-release-branch }}
+
+  create-next-release-branch:
+    needs: [branch-checks, get-next-release-branch]
+    if: ${{ needs.branch-checks.outputs.is-next-branch == 'true' }}
+    runs-on: ubuntu-latest
+    steps:
+      - uses: actions/checkout@v2
+
+      - run: |
+          git branch temp
+          git push -f origin temp:${{ needs.get-next-release-branch.outputs.branch }}
+          git branch -D temp
+    outputs:
+      branch: ${{ needs.get-next-release-branch.outputs.branch }}
+
+  latest-or-next-release-branch-check:
+    if: ${{ always() }}
+    needs: [branch-checks, get-latest-release-branch, get-next-release-branch]
+    runs-on: ubuntu-latest
+    steps:
+      - run: |
+          IS_LATEST_RELEASE_BRANCH=${{ needs.branch-checks.outputs.branch == needs.get-latest-release-branch.outputs.branch }}
+          IS_NEXT_RELEASE_BRANCH=${{ needs.branch-checks.outputs.branch == needs.get-next-release-branch.outputs.branch }}
+          echo "is-latest-release-branch=$IS_LATEST_RELEASE_BRANCH" >> $GITHUB_ENV
+          echo "is-next-release-branch=$IS_NEXT_RELEASE_BRANCH" >> $GITHUB_ENV
+
+      - run: |
+          IS_LATEST_OR_NEXT_RELEASE_BRANCH=${{ env.is-latest-release-branch == 'true' || env.is-next-release-branch == 'true' }}
+          echo "is-latest-or-next-release-branch=$IS_LATEST_OR_NEXT_RELEASE_BRANCH" >> $GITHUB_ENV
+
+      - if: ${{ env.is-latest-release-branch == 'true' }}
+        run: echo "relevant-base-branch=main" >> $GITHUB_ENV
+
+      - if: ${{ env.is-next-release-branch == 'true' }}
+        run: echo "relevant-base-branch=next" >> $GITHUB_ENV
+
+      - if: ${{ env.is-latest-or-next-release-branch == 'true' }}
+        run: |
+          echo 'WARNING: Do not push directly to the `${{ needs.branch-checks.outputs.branch }}` branch. This branch is created and force-pushed over after pushing to the `${{ env.relevant-base-branch }}` branch and the changes you just pushed will be lost.'
+          exit 1
+    outputs:
+      check: ${{ env.is-latest-or-next-release-branch }}
+
+  request-create-frontpage-branch:
+    if: ${{ always() }}
+    needs:
+      [
+        branch-checks,
+        latest-or-next-release-branch-check,
+        create-latest-release-branch,
+        create-next-release-branch,
+      ]
+    runs-on: ubuntu-latest
+    steps:
+      - if: ${{ needs.branch-checks.outputs.is-actionable-branch == 'true' && needs.latest-or-next-release-branch-check.outputs.check == 'false' }}
+        run: |
+          curl -X POST https://api.github.com/repos/storybookjs/frontpage/dispatches \
+          -H 'Accept: application/vnd.github.v3+json' \
+          -u ${{ secrets.FRONTPAGE_ACCESS_TOKEN }} \
+          --data '{"event_type": "request-create-frontpage-branch", "client_payload": { "branch": "${{ needs.create-latest-release-branch.outputs.branch || needs.create-next-release-branch.outputs.branch || needs.branch-checks.outputs.branch }}" }}'
diff --git a/.teamcity/settings.kts b/.teamcity/settings.kts
index 243818648924..f99734ae1ce8 100644
--- a/.teamcity/settings.kts
+++ b/.teamcity/settings.kts
@@ -45,7 +45,6 @@ project {
     buildType(Build)
     buildType(E2E)
     buildType(SmokeTests)
-    buildType(Frontpage)
     buildType(Test)
     buildType(Coverage)
 
@@ -56,7 +55,6 @@ project {
             RelativeId("Build"),
             RelativeId("E2E"),
             RelativeId("SmokeTests"),
-            RelativeId("Frontpage"),
             RelativeId("Test"),
             RelativeId("Coverage")
     )
@@ -403,34 +401,6 @@ object SmokeTests : BuildType({
     }
 })
 
-object Frontpage : BuildType({
-    name = "Frontpage"
-    type = Type.DEPLOYMENT
-
-    steps {
-        script {
-            scriptContent = """
-                #!/bin/bash
-                set -e -x
-
-                yarn install --immutable
-                yarn bootstrap --install
-                node ./scripts/build-frontpage.js
-            """.trimIndent()
-            dockerImage = "node:12"
-            dockerImagePlatform = ScriptBuildStep.ImagePlatform.Linux
-        }
-    }
-
-    triggers {
-        vcs {
-            quietPeriodMode = VcsTrigger.QuietPeriodMode.USE_DEFAULT
-            triggerRules = "-:.teamcity/**"
-            branchFilter = "+:main"
-        }
-    }
-})
-
 object Test : BuildType({
     name = "Test"
 
diff --git a/scripts/build-frontpage.js b/scripts/build-frontpage.js
deleted file mode 100755
index b7c7bdd72f36..000000000000
--- a/scripts/build-frontpage.js
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env node
-/* eslint-disable no-console */
-
-const fetch = require('node-fetch');
-const { execSync } = require('child_process');
-
-const { FRONTPAGE_WEBHOOK, FRONTPAGE_WEBHOOK_NEXT } = process.env;
-
-const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
-
-const branchToHook = {
-  main: FRONTPAGE_WEBHOOK,
-  next: FRONTPAGE_WEBHOOK_NEXT,
-};
-
-console.log('build-frontpage');
-const hook = branchToHook[branch];
-if (hook) {
-  console.log('triggering frontpage build');
-  const url = `https://api.netlify.com/build_hooks/${hook}`;
-  fetch(url, { method: 'post' }).then((res) => console.log('result', res.status));
-} else {
-  console.log('skipping branch', branch);
-}