From d8f7b45e384da7d97a95589e1055cad45422a4d2 Mon Sep 17 00:00:00 2001 From: Grace Park Date: Tue, 24 Aug 2021 09:45:52 -0700 Subject: [PATCH] =?UTF-8?q?Creating=20Action=20for=20adding=20staging=20an?= =?UTF-8?q?d=20article=20links=20to=20PR=20for=20conte=E2=80=A6=20(#19561)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Creating actions for adding staging and article links to PR for content writers * Ran prettier * update capitalization in yml file * Update .github/workflows/content-changes-table-comment.yml Co-authored-by: Rachael Sewell * Update .github/workflows/content-changes-table-comment.yml Co-authored-by: Rachael Sewell * Update .github/actions-scripts/get-content-changes-table.js Co-authored-by: Rachael Sewell * Updating fc to findComment * Update .github/actions-scripts/get-content-changes-table.js Co-authored-by: Rachael Sewell * updating constant * updating fetch depth * Updating yml file and removing script * Adding actions commit for github-script * updating table info * Using mel's wording * update allowed actions * Update .github/workflows/content-changes-table-comment.yml Co-authored-by: Felicity Chapman * linting * update * Testing content change * removing tests * updating npm install * back to ci Co-authored-by: Rachael Sewell Co-authored-by: Felicity Chapman --- .github/PULL_REQUEST_TEMPLATE.md | 2 +- .github/allowed-actions.js | 1 + .../content-changes-table-comment.yml | 95 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/content-changes-table-comment.yml diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index 07a9a39dec..51227417a2 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -20,7 +20,7 @@ Closes [issue link] ### What's being changed: - + ### Check off the following: diff --git a/.github/allowed-actions.js b/.github/allowed-actions.js index 512595d497..9dc96c82ef 100644 --- a/.github/allowed-actions.js +++ b/.github/allowed-actions.js @@ -27,6 +27,7 @@ export default [ 'peter-evans/create-issue-from-file@b4f9ee0a9d4abbfc6986601d9b1a4f8f8e74c77e', 'peter-evans/create-or-update-comment@5221bf4aa615e5c6e95bb142f9673a9c791be2cd', 'peter-evans/create-pull-request@8c603dbb04b917a9fc2dd991dc54fef54b640b43', + 'peter-evans/find-comment@0da1f4fc1f20cd898368bd56089d391df418f52f', 'rachmari/actions-add-new-issue-to-column@1a459ef92308ba7c9c9dc2fcdd72f232495574a9', 'rachmari/labeler@832d42ec5523f3c6d46e8168de71cd54363e3e2e', 'repo-sync/github-sync@3832fe8e2be32372e1b3970bbae8e7079edeec88', diff --git a/.github/workflows/content-changes-table-comment.yml b/.github/workflows/content-changes-table-comment.yml new file mode 100644 index 0000000000..3b10c4f38d --- /dev/null +++ b/.github/workflows/content-changes-table-comment.yml @@ -0,0 +1,95 @@ +name: Content Changes Table Comment + +# **What it does**: When a PR is opened in docs-internal or docs, it adds the staging preview and live article links in a Content Directory Changes table in a comment +# **Why we have it**: To help Docs Content team members and contributors automatically have their staging/live article links added to the table +# **Who does it impact**: docs-internal/docs maintainers and contributors + +on: + workflow_dispatch: + pull_request_target: + types: [opened, synchronize, reopened] + +jobs: + PR-Preview-Links: + name: Add staging/live links to PR + runs-on: ubuntu-latest + steps: + - name: check out repo content + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f + + - name: Install dependencies + run: npm ci + + - name: Get changes table + uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d + id: changes + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + script: | + const slugify = require('github-slugger').slug + + const HEROKU_APPNAME_MAX_LENGTH = 30 + + const repoName = context.payload.repository.name + const branchName = context.payload.pull_request.head.ref + const prNumber = context.payload.number + const stagingPrefix = getStagingPrefix(repoName, prNumber, branchName) + + const response = await github.repos.compareCommits({ + owner: context.repo.owner, + repo: context.repo.repo, + base: context.payload.pull_request.base.sha, + head: context.payload.pull_request.head.sha + }) + + let changedFiles = response.data.files + .map(e => e.raw_url) + .filter(file => file.includes('/content/')) + + let markdownTable = '| **Article in Staging** | **Live Article** | **Source** | **Changes** |\n| ----------- | ----------- | ----------- | ----------- |\n' + for (let file of changedFiles) { + file = file.split('/content')[1] + const fileURL = file.substring(0, file.length-3) + const stagingLink = `https://${stagingPrefix}.herokuapp.com${fileURL}` + let fileTitle = file.split('/').pop() + fileTitle = fileTitle.substring(0, fileTitle.length - 3) + const markdownLine = '| [' + fileTitle + '](' + stagingLink + ') | [' + fileTitle + '](https://docs.github.com' + fileURL + ') | | |\n' + markdownTable += markdownLine + } + + function getStagingPrefix (prefix, prNumber, branch) { + // Added a - in front of prNumber + return `${prefix}-${prNumber}--${slugify(branch)}` + .toLowerCase() + .slice(0, HEROKU_APPNAME_MAX_LENGTH) + .replace(/_/g, '-') + .replace(/-+$/, '') + } + core.setOutput('changesTable', markdownTable) + + - name: Find content directory changes comment + uses: peter-evans/find-comment@0da1f4fc1f20cd898368bd56089d391df418f52f + id: findComment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: Content Directory Changes + + - name: Update comment + uses: peter-evans/create-or-update-comment@5221bf4aa615e5c6e95bb142f9673a9c791be2cd + with: + comment-id: ${{ steps.findComment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: | + ## ⚠️ Automatically generated comment ⚠️ + **This comment is automatically generated and will be overwritten the 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. + + --- + + ### Content directory changes + _You may find it useful to copy this table into the pull request summary. There you can edit it to share links to important articles or changes and to give a high-level overview of how the changes in your pull request support the overall goals of the pull request._ + ${{ steps.changes.outputs.changesTable }} + edit-mode: replace