-
Notifications
You must be signed in to change notification settings - Fork 193
65 lines (59 loc) · 2.69 KB
/
add-netlify-link.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
name: Add Netlify Link to PR
on:
pull_request_target:
paths:
- 'sites/**/src/pages/**'
- 'sites/**/src/content/**'
jobs:
add-comment:
runs-on: ubuntu-latest
steps:
- name: Check for changed files
id: changed-files
uses: tj-actions/changed-files@c65cd883420fd2eb864698a825fc4162dd94482c # v44
- name: Add Netlify link to PR
if: steps.changed-files.outputs.all_changed_files != ''
uses: actions/github-script@v7
with:
github-token: ${{ secrets.NF_CORE_BOT_AUTH_TOKEN }}
script: |
const changedFiles = `${{ steps.changed-files.outputs.all_changed_files }}`.split(' ').slice(0, 50);
console.log('Changed files:', changedFiles)
// handle normal pages
let netlifyLinks = changedFiles
.filter(file => file.startsWith('src/pages/'))
.filter(file => !file.endsWith('].astro')) // skip dynamic routes
.map(file => `@netlify ${file?.replace('src/pages/', '/')}`)[0]
?.replace(/\.md$/, '').replace(/\.mdx$/, '').replace(/\.astro$/, '').replace(/\/index$/, '');
// handle pages in content collections
if (!netlifyLinks) {
netlifyLinks = changedFiles
.filter(file => file.startsWith('src/content/'))
.map(file => `@netlify ${file?.replace('src/content/', '/')}`)[0]
?.replace(/\.md$/, '').replace(/\.mdx$/, '').replace(/\/index$/, '');
}
console.log('Netlify links:', netlifyLinks)
if (netlifyLinks) {
console.log('Adding Netlify link to PR body' ,context.payload.pull_request.number);
const { data: pullRequest } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.pull_request.number,
});
console.log('Current PR:', pullRequest);
console.log('Current PR body:', pullRequest.body);
const currentBody = pullRequest.body || '';
if (currentBody.includes('@netlify')) {
return; // Skip if the PR body already contains a Netlify link
}
const newBody = `${currentBody}\n\n${netlifyLinks}`;
console.log('New PR body:', newBody);
// Update the pull request body
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pullRequest.number,
body: newBody,
});
console.log('Netlify link added to PR body');
}