|
| 1 | +name: Content Changes Table Comment |
| 2 | + |
| 3 | +# **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 |
| 4 | +# **Why we have it**: To help Docs Content team members and contributors automatically have their staging/live article links added to the table |
| 5 | +# **Who does it impact**: docs-internal/docs maintainers and contributors |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + pull_request_target: |
| 10 | + types: [opened, synchronize, reopened] |
| 11 | + |
| 12 | +jobs: |
| 13 | + PR-Preview-Links: |
| 14 | + name: Add staging/live links to PR |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: check out repo content |
| 18 | + uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f |
| 19 | + |
| 20 | + - name: Setup Node |
| 21 | + uses: actions/setup-node@38d90ce44d5275ad62cc48384b3d8a58c500bb5f |
| 22 | + with: |
| 23 | + node-version: 16.x |
| 24 | + cache: npm |
| 25 | + |
| 26 | + - name: Install dependencies |
| 27 | + run: npm ci |
| 28 | + |
| 29 | + - name: Get changes table |
| 30 | + uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d |
| 31 | + id: changes |
| 32 | + env: |
| 33 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const slugify = require('github-slugger').slug |
| 37 | +
|
| 38 | + const HEROKU_APPNAME_MAX_LENGTH = 30 |
| 39 | +
|
| 40 | + const repoName = context.payload.repository.name |
| 41 | + const branchName = context.payload.pull_request.head.ref |
| 42 | + const prNumber = context.payload.number |
| 43 | + const stagingPrefix = getStagingPrefix(repoName, prNumber, branchName) |
| 44 | +
|
| 45 | + const response = await github.repos.compareCommits({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + base: context.payload.pull_request.base.sha, |
| 49 | + head: context.payload.pull_request.head.sha |
| 50 | + }) |
| 51 | +
|
| 52 | + let changedFiles = response.data.files |
| 53 | + .map(e => e.raw_url) |
| 54 | + .filter(file => file.includes('/content/')) |
| 55 | +
|
| 56 | + let markdownTable = '| **Article in Staging** | **Live Article** | **Source** | **Changes** |\n| ----------- | ----------- | ----------- | ----------- |\n' |
| 57 | + for (let file of changedFiles) { |
| 58 | + file = file.split('/content')[1] |
| 59 | + const fileURL = file.substring(0, file.length-3) |
| 60 | + const stagingLink = `https://${stagingPrefix}.herokuapp.com${fileURL}` |
| 61 | + let fileTitle = file.split('/').pop() |
| 62 | + fileTitle = fileTitle.substring(0, fileTitle.length - 3) |
| 63 | + const markdownLine = '| [' + fileTitle + '](' + stagingLink + ') | [' + fileTitle + '](https://docs.github.com' + fileURL + ') | | |\n' |
| 64 | + markdownTable += markdownLine |
| 65 | + } |
| 66 | +
|
| 67 | + function getStagingPrefix (prefix, prNumber, branch) { |
| 68 | + // Added a - in front of prNumber |
| 69 | + return `${prefix}-${prNumber}--${slugify(branch)}` |
| 70 | + .toLowerCase() |
| 71 | + .slice(0, HEROKU_APPNAME_MAX_LENGTH) |
| 72 | + .replace(/_/g, '-') |
| 73 | + .replace(/-+$/, '') |
| 74 | + } |
| 75 | + core.setOutput('changesTable', markdownTable) |
| 76 | +
|
| 77 | + - name: Find content directory changes comment |
| 78 | + uses: peter-evans/find-comment@0da1f4fc1f20cd898368bd56089d391df418f52f |
| 79 | + id: findComment |
| 80 | + with: |
| 81 | + issue-number: ${{ github.event.pull_request.number }} |
| 82 | + comment-author: 'github-actions[bot]' |
| 83 | + body-includes: Content Directory Changes |
| 84 | + |
| 85 | + - name: Update comment |
| 86 | + uses: peter-evans/create-or-update-comment@5221bf4aa615e5c6e95bb142f9673a9c791be2cd |
| 87 | + with: |
| 88 | + comment-id: ${{ steps.findComment.outputs.comment-id }} |
| 89 | + issue-number: ${{ github.event.pull_request.number }} |
| 90 | + body: | |
| 91 | + ## ⚠️ Automatically generated comment ⚠️ |
| 92 | + **This comment is automatically generated and will be overwritten the every time changes are committed to this branch.** |
| 93 | +
|
| 94 | + 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. |
| 95 | +
|
| 96 | + --- |
| 97 | +
|
| 98 | + ### Content directory changes |
| 99 | + _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._ |
| 100 | + ${{ steps.changes.outputs.changesTable }} |
| 101 | + edit-mode: replace |
0 commit comments