From bcbfdc934a49a67e719e50e4ec2d140396e7d9a9 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Fri, 28 Oct 2022 18:24:19 +0200 Subject: [PATCH] include docs-early-access in link checker (#27747) --- .../what-docs-early-access-branch.js | 38 ++++++++++++++ .github/workflows/link-check-daily.yml | 21 ++++++++ .github/workflows/link-check-on-pr.yml | 21 ++++++++ .github/workflows/test.yml | 52 +++++-------------- 4 files changed, 94 insertions(+), 38 deletions(-) create mode 100755 .github/actions-scripts/what-docs-early-access-branch.js diff --git a/.github/actions-scripts/what-docs-early-access-branch.js b/.github/actions-scripts/what-docs-early-access-branch.js new file mode 100755 index 0000000000..c076197df1 --- /dev/null +++ b/.github/actions-scripts/what-docs-early-access-branch.js @@ -0,0 +1,38 @@ +#!/usr/bin/env node + +import { getOctokit } from '@actions/github' +import { setOutput } from '@actions/core' + +async function main() { + // TODO Is there a lib function for this? + const { BRANCH_NAME, GITHUB_TOKEN } = process.env + if (!BRANCH_NAME) throw new Error("'BRANCH_NAME' env var not set") + if (!GITHUB_TOKEN) throw new Error("'GITHUB_TOKEN' env var not set") + + const OUTPUT_KEY = 'branch' + + // If being run from a PR, this becomes 'my-cool-branch'. + // If run on main, with the `workflow_dispatch` action for + // example, the value becomes 'main'. + const github = getOctokit(GITHUB_TOKEN) + try { + await github.rest.repos.getBranch({ + owner: 'github', + repo: 'docs-early-access', + branch: BRANCH_NAME, + }) + console.log(`Using docs-early-access branch called '${BRANCH_NAME}'.`) + setOutput(OUTPUT_KEY, BRANCH_NAME) + } catch (err) { + if (err.status === 404) { + console.log( + `There is no docs-early-access branch called '${BRANCH_NAME}' so checking out 'main' instead.` + ) + setOutput(OUTPUT_KEY, 'main') + } else { + throw err + } + } +} + +main() diff --git a/.github/workflows/link-check-daily.yml b/.github/workflows/link-check-daily.yml index 6e522c96e8..18f3360665 100644 --- a/.github/workflows/link-check-daily.yml +++ b/.github/workflows/link-check-daily.yml @@ -33,6 +33,27 @@ jobs: - name: Install dependencies run: npm ci + - name: Figure out which docs-early-access branch to checkout, if internal repo + if: ${{ github.repository == 'github/docs-internal' }} + id: check-early-access + env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }} + run: node .github/actions-scripts/what-docs-early-access-branch.js + + - name: Check out docs-early-access too, if internal repo + if: ${{ github.repository == 'github/docs-internal' }} + uses: actions/checkout@dcd71f646680f2efd8db4afa5ad64fdcba30e748 + with: + repository: github/docs-early-access + token: ${{ secrets.DOCUBOT_REPO_PAT }} + path: docs-early-access + ref: ${{ steps.check-early-access.outputs.branch }} + + - name: Merge docs-early-access repo's folders + if: ${{ github.repository == 'github/docs-internal' }} + run: .github/actions-scripts/merge-early-access.sh + - name: Run link checker env: LEVEL: 'critical' diff --git a/.github/workflows/link-check-on-pr.yml b/.github/workflows/link-check-on-pr.yml index 076140e4e5..42bfa83e81 100644 --- a/.github/workflows/link-check-on-pr.yml +++ b/.github/workflows/link-check-on-pr.yml @@ -40,6 +40,27 @@ jobs: - name: Install run: npm ci + - name: Figure out which docs-early-access branch to checkout, if internal repo + if: ${{ github.repository == 'github/docs-internal' }} + id: check-early-access + env: + BRANCH_NAME: ${{ github.head_ref || github.ref_name }} + GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }} + run: node .github/actions-scripts/what-docs-early-access-branch.js + + - name: Check out docs-early-access too, if internal repo + if: ${{ github.repository == 'github/docs-internal' }} + uses: actions/checkout@dcd71f646680f2efd8db4afa5ad64fdcba30e748 + with: + repository: github/docs-early-access + token: ${{ secrets.DOCUBOT_REPO_PAT }} + path: docs-early-access + ref: ${{ steps.check-early-access.outputs.branch }} + + - name: Merge docs-early-access repo's folders + if: ${{ github.repository == 'github/docs-internal' }} + run: .github/actions-scripts/merge-early-access.sh + # TODO: When we want to fix redirects on changed files we can uncomment everything below # Creates file "${{ env.HOME }}/files.json", among others # - name: Gather files changed diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 000c0ad107..a3bd592e2d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -77,35 +77,22 @@ jobs: # ->> Do we really need this? actions/checkout doesn't use it for the nested example: # https://github.com/actions/checkout#checkout-multiple-repos-nested + - name: Setup node + uses: actions/setup-node@17f8bd926464a1afa4c6a11669539e9c1ba77048 + with: + node-version: '16.17.0' + cache: npm + + - name: Install dependencies + run: npm ci + - name: Figure out which docs-early-access branch to checkout, if internal repo if: ${{ github.repository == 'github/docs-internal' }} id: check-early-access - uses: actions/github-script@d556feaca394842dc55e4734bf3bb9f685482fa0 env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} - with: - github-token: ${{ secrets.DOCUBOT_REPO_PAT }} - result-encoding: string - script: | - // If being run from a PR, this becomes 'my-cool-branch'. - // If run on main, with the `workflow_dispatch` action for - // example, the value becomes 'main'. - const { BRANCH_NAME } = process.env - try { - const response = await github.rest.repos.getBranch({ - owner: 'github', - repo: 'docs-early-access', - branch: BRANCH_NAME, - }) - console.log(`Using docs-early-access branch called '${BRANCH_NAME}'.`) - return BRANCH_NAME - } catch (err) { - if (err.status === 404) { - console.log(`There is no docs-early-access branch called '${BRANCH_NAME}' so checking out 'main' instead.`) - return 'main' - } - throw err - } + GITHUB_TOKEN: ${{ secrets.DOCUBOT_REPO_PAT }} + run: node .github/actions-scripts/what-docs-early-access-branch.js - name: Check out docs-early-access too, if internal repo if: ${{ github.repository == 'github/docs-internal' }} @@ -114,15 +101,13 @@ jobs: repository: github/docs-early-access token: ${{ secrets.DOCUBOT_REPO_PAT }} path: docs-early-access - ref: ${{ steps.check-early-access.outputs.result }} + ref: ${{ steps.check-early-access.outputs.branch }} - name: Merge docs-early-access repo's folders if: ${{ github.repository == 'github/docs-internal' }} run: | - mv docs-early-access/assets assets/images/early-access - mv docs-early-access/content content/early-access - mv docs-early-access/data data/early-access - rm -r docs-early-access + .github/actions-scripts/merge-early-access.sh + rm -fr docs-early-access # This is necessary when LFS files where cloned but does nothing # if actions/checkout was run with `lfs:false`. @@ -162,15 +147,6 @@ jobs: echo __ format, write to get_diff_files.txt __ echo $DIFF | tr '\n' ' ' > get_diff_files.txt - - name: Setup node - uses: actions/setup-node@17f8bd926464a1afa4c6a11669539e9c1ba77048 - with: - node-version: '16.17.0' - cache: npm - - - name: Install dependencies - run: npm ci - - name: Cache nextjs build uses: actions/cache@48af2dc4a9e8278b89d7fa154b955c30c6aaab09 with: