-
Notifications
You must be signed in to change notification settings - Fork 87
82 lines (75 loc) · 3.2 KB
/
preview.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
name: Preview modified pages
on: [pull_request]
jobs:
preview:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Get branch name
id: get-branch-name
uses: tj-actions/branch-names@v7
- name: Get changed markdown files
id: get-changed-markdown-files
uses: tj-actions/changed-files@v36
with:
files: |
**.md
**.mdx
- name: Fetch navigation.json
run: curl -o ./navigation.json https://developers.vtex.com/navigation.json
- name: Map slugs to paths
id: map-slugs-to-paths
uses: actions/github-script@v6
with:
script: |
const navigation = require('./navigation.json')
const paths = {}
const slugsToPaths = (slugPrefix, obj) => {
if (!obj || typeof obj !== 'object') return
if (obj.slug && obj.type === 'markdown') {
paths[obj.slug] = `${slugPrefix}${obj.slug}`
}
for (const key in obj) {
slugsToPaths(slugPrefix, obj[key])
}
}
navigation.navbar.forEach(({ slugPrefix, categories }) => slugsToPaths(slugPrefix.endsWith('/') ? slugPrefix : `${slugPrefix}/`, categories))
core.setOutput('slugs-to-paths', JSON.stringify(paths))
- name: Get preview links
id: get-preview-links
uses: actions/github-script@v6
env:
BRANCH: ${{ steps.get-branch-name.outputs.head_ref_branch }}
FILES: ${{ steps.get-changed-markdown-files.outputs.all_changed_files }}
PATHS: ${{ steps.map-slugs-to-paths.outputs.slugs-to-paths }}
with:
script: |
const branch = process.env.BRANCH
const paths = JSON.parse(process.env.PATHS)
const files = process.env.FILES.split(' ')
.map(file => file.trim())
.filter(file => file !== '')
let output = '# Preview Links\n\n'
if (files.length === 0) {
output += 'No changes detected in any markdown/MDX file'
} else {
output += `Open [this URL](https://developers.vtex.com/api/preview?branch=${branch}) to set up the portal with this branch changes.\n\n`
output += 'You can now access the edited pages with the following URLs:\n'
for (const file of files) {
const slug = file.split('/').pop().split('.')[0]
if (!paths[slug]) {
output += `* Couldn't find the corresponding page for \`${file}\`. Have you added \`${slug}\` to the navigation.json file? `
output += `Maybe you can find it in https://developers.vtex.com/docs/guides/${slug}\n`
} else {
output += `* https://developers.vtex.com/${paths[slug]}\n`
}
}
}
core.setOutput('preview-links', output)
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
with:
comment_tag: preview-links
message: ${{ steps.get-preview-links.outputs.preview-links }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}