Skip to content

Commit b77b195

Browse files
authored
Migrate Content Changes Table Workflow github-script code (github#23198)
* moving content changes table to actions-script * rename js file * using context for neater code
1 parent 840de67 commit b77b195

2 files changed

Lines changed: 55 additions & 46 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
import createStagingAppName from '../../script/deployment/create-staging-app-name.js'
4+
import * as github from '@actions/github'
5+
import { setOutput } from '@actions/core'
6+
7+
const context = github.context
8+
9+
const githubToken = process.env.GITHUB_TOKEN
10+
if (!githubToken) {
11+
throw new Error(`GITHUB_TOKEN environment variable not set`)
12+
}
13+
14+
const stagingPrefix = createStagingAppName({
15+
repo: context.payload.repository.name,
16+
pullNumber: context.payload.number,
17+
branch: context.payload.pull_request.head.ref,
18+
})
19+
20+
const octokit = github.getOctokit(githubToken)
21+
22+
const response = await octokit.rest.repos.compareCommits({
23+
owner: context.repo.owner,
24+
repo: context.payload.repository.name,
25+
base: context.payload.pull_request.base.sha,
26+
head: context.payload.pull_request.head.sha,
27+
})
28+
29+
const { files } = response.data
30+
31+
let markdownTable =
32+
'| **Source** | **Staging** | **Production** | **What Changed** |\n|:----------- |:----------- |:----------- |:----------- |\n'
33+
34+
const pathPrefix = 'content/'
35+
const articleFiles = files.filter(
36+
({ filename }) => filename.startsWith(pathPrefix) && !filename.endsWith('/index.md')
37+
)
38+
for (const file of articleFiles) {
39+
const sourceUrl = file.blob_url
40+
const fileName = file.filename.slice(pathPrefix.length)
41+
const fileUrl = fileName.slice(0, fileName.lastIndexOf('.'))
42+
const stagingLink = `https://${stagingPrefix}.herokuapp.com/${fileUrl}`
43+
const productionLink = `https://docs.github.com/${fileUrl}`
44+
let markdownLine = ''
45+
46+
if (file.status === 'modified') {
47+
markdownLine = `| [content/${fileName}](${sourceUrl}) | [Modified](${stagingLink}) | [Original](${productionLink}) | |\n`
48+
} else if (file.status === 'added') {
49+
markdownLine = `| New file: [content/${fileName}](${sourceUrl}) | [Modified](${stagingLink}) | | |\n`
50+
}
51+
markdownTable += markdownLine
52+
}
53+
54+
setOutput('changesTable', markdownTable)

.github/workflows/content-changes-table-comment.yml

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,57 +49,12 @@ jobs:
4949
- name: Install temporary dependencies
5050
run: |
5151
npm install --no-save github-slugger
52-
npm install --no-save --include=optional esm
5352
5453
- name: Get changes table
55-
uses: actions/github-script@2b34a689ec86a68d8ab9478298f91d5401337b7d
5654
id: changes
5755
env:
5856
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59-
with:
60-
script: |
61-
// Workaround to allow us to load ESM files with `require(...)`
62-
const esm = require('esm')
63-
require = esm({})
64-
65-
const { default: createStagingAppName } = require('./script/deployment/create-staging-app-name')
66-
67-
const stagingPrefix = createStagingAppName({
68-
repo: context.payload.repository.name,
69-
pullNumber: context.payload.number,
70-
branch: context.payload.pull_request.head.ref,
71-
})
72-
73-
const response = await github.repos.compareCommits({
74-
owner: context.repo.owner,
75-
repo: context.repo.repo,
76-
base: context.payload.pull_request.base.sha,
77-
head: context.payload.pull_request.head.sha
78-
})
79-
80-
const files = response.data.files
81-
82-
let markdownTable = '| **Source** | **Staging** | **Production** | **What Changed** |\n|:----------- |:----------- |:----------- |:----------- |\n'
83-
84-
const pathPrefix = 'content/'
85-
const articleFiles = files.filter(({ filename }) => filename.startsWith(pathPrefix) && !filename.endsWith('/index.md'))
86-
for (const file of articleFiles) {
87-
const sourceUrl = file.blob_url
88-
const fileName = file.filename.slice(pathPrefix.length)
89-
const fileUrl = fileName.slice(0, fileName.lastIndexOf('.'))
90-
const stagingLink = `https://${stagingPrefix}.herokuapp.com/${fileUrl}`
91-
const productionLink = `https://docs.github.com/${fileUrl}`
92-
let markdownLine = ''
93-
94-
if (file.status === 'modified') {
95-
markdownLine = `| [content/${fileName}](${sourceUrl}) | [Modified](${stagingLink}) | [Original](${productionLink}) | |\n`
96-
} else if (file.status === 'added') {
97-
markdownLine = `| New file: [content/${fileName}](${sourceUrl}) | [Modified](${stagingLink}) | | |\n`
98-
}
99-
markdownTable += markdownLine
100-
}
101-
102-
core.setOutput('changesTable', markdownTable)
57+
run: .github/actions-scripts/content-changes-table-comment.js
10358

10459
- name: Find content directory changes comment
10560
uses: peter-evans/find-comment@d2dae40ed151c634e4189471272b57e76ec19ba8

0 commit comments

Comments
 (0)