From f27c521efe4434af2ab3c58ad3cc88fb2bb23629 Mon Sep 17 00:00:00 2001 From: Hector Alfaro Date: Wed, 22 Jan 2025 10:10:55 -0500 Subject: [PATCH] Use review server URL for content changes table (#53812) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Kevin Heis Co-authored-by: Evan Bonsignori --- .github/workflows/code-changes-comment.yml | 66 +++++++++++++++++++ .../content-changes-table-comment.yml | 29 ++++---- .../content-changes-table-comment.ts | 45 ++++++++----- 3 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 .github/workflows/code-changes-comment.yml diff --git a/.github/workflows/code-changes-comment.yml b/.github/workflows/code-changes-comment.yml new file mode 100644 index 000000000000..060d4467c9b0 --- /dev/null +++ b/.github/workflows/code-changes-comment.yml @@ -0,0 +1,66 @@ +name: Code changes comment + +# **What it does**: When a PR is opened in docs-internal or docs containing code, it comments with instructions on how to deploy and preview the changes. +# **Why we have it**: To help Docs contributors understand how to preview their changes. +# **Who does it impact**: docs-internal and docs maintainers and contributors + +on: + # Required in lieu of `pull_request` so that the comment can be posted to PRs opened from a fork. + pull_request_target: + types: + - opened + - synchronize + # this applies to any non-content files, this workflow will run for all changed files unless it's ONLY these content-related changes + paths-ignore: + - 'content/**' + - 'data/reusables/**' + - 'assets/images/**' + +permissions: + contents: read + pull-requests: write + +# This allows a subsequently queued workflow run to interrupt previous runs +concurrency: + group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}' + cancel-in-progress: true + +jobs: + postOnCodeChanges: + if: ${{ github.event.pull_request.user.login != 'docs-bot' && (github.repository == 'github/docs-internal' || github.repository == 'github/docs') }} + runs-on: ubuntu-latest + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + steps: + - name: check out repo content + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + + - name: Find code changes comment + uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e + id: findComment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: '' + + # TODO: We'll update this comment at a later time with permament-ish links to deployment instructions. + - name: Comment on code changes + if: ${{ !steps.findComment.outputs.comment-id }} + run: | + if [[ "${{ github.repository }}" == "github/docs" ]]; then + COMMENT_BODY=" + ## 🚀 Manual Deployment Required + + It looks like this pull request contains code changes. To preview these changes, an internal Hubber will need to manually deploy the changes to one of our staging servers. + + Thank you for your contribution!" + else + COMMENT_BODY=" + ## 🚀 Manual Deployment Required + + It looks like this pull request contains code changes. To preview these changes, you will need to deploy them manually. + + Thank you for your contribution!" + fi + + gh pr comment ${{ github.event.pull_request.number }} --body "$COMMENT_BODY" diff --git a/.github/workflows/content-changes-table-comment.yml b/.github/workflows/content-changes-table-comment.yml index d394b59e7cdf..a9d28f872d30 100644 --- a/.github/workflows/content-changes-table-comment.yml +++ b/.github/workflows/content-changes-table-comment.yml @@ -28,6 +28,7 @@ on: paths: - 'content/**' - 'data/reusables/**' + - 'assets/images/**' permissions: contents: read @@ -48,21 +49,28 @@ jobs: - name: check out repo content uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - - name: Get preview app info - env: - APP_NAME_SEED: ${{ secrets.PREVIEW_ENV_NAME_SEED }} - run: src/workflows/get-preview-app-info.sh - - uses: ./.github/actions/node-npm-setup + # We're setting the URLs manually until we merge in-progress work. We can utilize review server and its helper functions in a later iteration. + - name: Set APP_URL + run: | + if [[ "${{ github.repository }}" == "github/docs-internal" ]]; then + echo "APP_URL=https://docs-review.github.com" >> $GITHUB_ENV + elif [[ "${{ github.repository }}" == "github/docs" ]]; then + echo "APP_URL=https://os-docs-review.github.com" >> $GITHUB_ENV + fi + - name: Get changes table id: changes timeout-minutes: 30 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REVIEW_SERVER_ACCESS_TOKEN: ${{ secrets.REVIEW_SERVER_ACCESS_TOKEN }} APP_URL: ${{ env.APP_URL }} BASE_SHA: ${{ github.event.pull_request.base.sha || inputs.BASE_SHA }} HEAD_SHA: ${{ github.event.pull_request.head.sha || inputs.HEAD_SHA }} + # HEAD_BRANCH isn't necessary for the table comment, but lets us list the branch in the review server menu bar + HEAD_BRANCH: ${{ github.event.pull_request.head.ref }} run: npm run content-changes-table-comment - name: Find content directory changes comment @@ -81,10 +89,10 @@ jobs: issue-number: ${{ github.event.pull_request.number || inputs.PR_NUMBER }} body: | - ## Automatically generated comment ℹī¸ - **This comment is automatically generated and will be overwritten every time changes are committed to this branch.** + ## 👓 Previews for changed content + **This comment is 🤖 automatically generated and will be overwritten every time changes are committed to this branch.** - The table contains an overview of files in the `content` directory that have been changed in this pull request. It's provided to make it easy to review your changes on the staging site. Please note that changes to the `data` directory will not show up in this table. + The table contains an overview of files in the `content` directory that have been changed in this pull request. It's provided to make it easy to review your changes on the review server. Please note that changes to the `data` directory will not show up in this table. --- @@ -93,7 +101,6 @@ jobs: ${{ steps.changes.outputs.changesTable }} --- - fpt: Free, Pro, Team - ghec: GitHub Enterprise Cloud - ghes: GitHub Enterprise Server + Key: **fpt**: Free, Pro, Team; **ghec**: GitHub Enterprise Cloud; **ghes**: GitHub Enterprise Server + _This table is posted from the [${{ github.workflow }}](https://github.com/${{ github.repository }}/blob/${{ github.sha }}/.github/workflows/content-changes-table-comment.yml) workflow._ edit-mode: replace diff --git a/src/workflows/content-changes-table-comment.ts b/src/workflows/content-changes-table-comment.ts index d9531d0f6b0f..28647cb4c382 100755 --- a/src/workflows/content-changes-table-comment.ts +++ b/src/workflows/content-changes-table-comment.ts @@ -1,5 +1,3 @@ -#!/usr/bin/env node - /** * Hi there! 👋 * To test this code locally, outside of Actions, you need to run @@ -23,11 +21,10 @@ import { getContents } from './git-utils.js' import getApplicableVersions from '@/versions/lib/get-applicable-versions.js' import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-version.js' import { allVersionShortnames } from '@/versions/lib/all-versions.js' -import { waitUntilUrlIsHealthy } from './wait-until-url-is-healthy.js' import readFrontmatter from '@/frame/lib/read-frontmatter.js' import { inLiquid } from './lib/in-liquid' -const { GITHUB_TOKEN, APP_URL } = process.env +const { GITHUB_TOKEN, REVIEW_SERVER_ACCESS_TOKEN, APP_URL } = process.env const context = github.context // the max size of the comment (in bytes) @@ -44,13 +41,8 @@ if (import.meta.url.endsWith(process.argv[1])) { const baseSHA = process.env.BASE_SHA || context.payload.pull_request!.base.sha const headSHA = process.env.HEAD_SHA || context.payload.pull_request!.head.sha - const isHealthy = await waitUntilUrlIsHealthy(new URL('/healthz', APP_URL).toString()) - if (!isHealthy) { - core.setFailed(`Timeout waiting for preview environment: ${APP_URL}`) - } else { - const markdownTable = await main(owner, repo, baseSHA, headSHA) - core.setOutput('changesTable', markdownTable) - } + const markdownTable = await main(owner, repo, baseSHA, headSHA) + core.setOutput('changesTable', markdownTable) } async function main(owner: string, repo: string, baseSHA: string, headSHA: string) { @@ -60,10 +52,30 @@ async function main(owner: string, repo: string, baseSHA: string, headSHA: strin if (!APP_URL) { throw new Error(`APP_URL environment variable not set`) } + const headBranch = process.env.HEAD_BRANCH + const RetryingOctokit = Octokit.plugin(retry) const octokit = new RetryingOctokit({ auth: `token ${GITHUB_TOKEN}`, }) + + // we'll attach the branch or sha right after this + const searchParams = new URLSearchParams({ + 'review-server-repository': `${owner}/${repo}`, + }) + + // this token will be available in the internal repo only, skip it for the open source repo + if (REVIEW_SERVER_ACCESS_TOKEN) + searchParams.append('review-server-access-token', REVIEW_SERVER_ACCESS_TOKEN) + + // this script compares with SHAs only, so this allows us + // to surface the branch name for the review server bar + headBranch + ? searchParams.append('review-server-branch', headBranch) + : searchParams.append('review-server-sha', headSHA) + + const queryParams = `?${searchParams.toString()}` + // get the list of file changes from the PR const response = await octokit.rest.repos.compareCommitsWithBasehead({ owner, @@ -134,7 +146,7 @@ async function main(owner: string, repo: string, baseSHA: string, headSHA: strin return } - return makeRow({ file, fileName, sourceUrl, fileUrl, data }) + return makeRow({ file, fileName, sourceUrl, fileUrl, queryParams, data }) }), ) @@ -149,6 +161,7 @@ async function main(owner: string, repo: string, baseSHA: string, headSHA: strin fileName, sourceUrl: file.blob_url, fileUrl, + queryParams, data, fromReusable: true, }) @@ -194,6 +207,7 @@ type File = { function makeRow({ file, fileUrl, + queryParams, fileName, sourceUrl, data, @@ -201,6 +215,7 @@ function makeRow({ }: { file: File fileUrl: string + queryParams: string fileName: string sourceUrl: string data: any @@ -235,12 +250,12 @@ function makeRow({ if (versions.toString() === nonEnterpriseDefaultVersion) { // omit version from fpt url - previewCell += `[${plan}](${APP_URL}/${fileUrl})
` + previewCell += `[${plan}](${APP_URL}/${fileUrl}${queryParams})
` prodCell += `[${plan}](${PROD_URL}/${fileUrl})
` } else { // for non-versioned releases (ghec) use full url - previewCell += `[${plan}](${APP_URL}/${versions}/${fileUrl})
` + previewCell += `[${plan}](${APP_URL}/${versions}/${fileUrl}${queryParams})
` prodCell += `[${plan}](${PROD_URL}/${versions}/${fileUrl})
` } } else if (versions.length) { @@ -250,7 +265,7 @@ function makeRow({ prodCell += `${plan}@ ` versions.forEach((version) => { - previewCell += `[${version.split('@')[1]}](${APP_URL}/${version}/${fileUrl}) ` + previewCell += `[${version.split('@')[1]}](${APP_URL}/${version}/${fileUrl}${queryParams}) ` prodCell += `[${version.split('@')[1]}](${PROD_URL}/${version}/${fileUrl}) ` }) previewCell += '
'