Repository navigation
132 lines (120 loc) · 4.28 KB
/
Copy pathdocs-preview-comment.yml
File metadata and controls
132 lines (120 loc) · 4.28 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: docs preview comment
on:
pull_request_target:
branches:
- main
paths:
- 'docs/content/**'
types:
- opened
- reopened
- synchronize
- labeled
- unlabeled
permissions:
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
jobs:
comment:
runs-on: ubuntu-latest
steps:
- name: Create or update preview comment
uses: actions/github-script@v9
env:
SITE_URL: https://comark.dev
CONTENT_DIR: docs/content
PREVIEW_LABEL: preview:enabled
with:
script: |
const marker = '<!-- comark-docs-preview -->'
const { SITE_URL, CONTENT_DIR, PREVIEW_LABEL } = process.env
const { owner, repo } = context.repo
const pr = context.payload.pull_request
const internal = pr.head.repo && pr.head.repo.full_name === `${owner}/${repo}`
const enabled = internal || pr.labels.some((label) => label.name === PREVIEW_LABEL)
let body
if (!enabled) {
body = [
marker,
'## Documentation previews',
'',
'Previews are disabled for pull requests from forks.',
`A maintainer can add the \`${PREVIEW_LABEL}\` label to enable them.`,
].join('\n')
} else {
const previewRoot = `${SITE_URL}/pr/${pr.number}`
const files = await github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number: pr.number,
per_page: 100,
})
const prefix = `${CONTENT_DIR}/`
const pages = files
.filter((file) =>
file.status !== 'removed'
&& file.filename.startsWith(prefix)
&& file.filename.endsWith('.md'),
)
.map((file) => {
const relativePath = file.filename
.slice(prefix.length)
.replace(/\.md$/, '')
const routeSegments = relativePath
.split('/')
.map((segment) => segment.replace(/^\d+\./, ''))
.map(encodeURIComponent)
if (routeSegments.at(-1) === 'index') {
routeSegments.pop()
}
const route = routeSegments.join('/')
return {
filename: file.filename,
route,
url: route ? `${previewRoot}/${route}` : `${previewRoot}/`,
}
})
.sort((a, b) => a.filename.localeCompare(b.filename))
body = [
marker,
'## Documentation previews',
'',
`📚 [Preview all documentation changes](${previewRoot}) (follows new pushes)`,
...(pages.length
? [
'',
...pages.map((page) => `- [/${page.route}](${page.url})`),
]
: []),
'',
`Pinned to the current head: [\`${pr.head.sha.slice(0, 7)}\`](${SITE_URL}/blob/${pr.head.sha})`,
...(internal
? []
: ['', `Enabled by the \`${PREVIEW_LABEL}\` label — remove it to disable the preview.`]),
].join('\n')
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: pr.number,
per_page: 100,
})
const existingComment = comments.find((comment) =>
comment.user?.type === 'Bot' && comment.body?.includes(marker),
)
if (existingComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existingComment.id,
body,
})
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body,
})
}