-
Notifications
You must be signed in to change notification settings - Fork 194
81 lines (67 loc) · 2.98 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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@c3a1bb2c992d77180ae65be6ae6c166cf40f857c # v45
- 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: |
let changedFiles = `${{ steps.changed-files.outputs.all_changed_files }}`.split(' ').slice(0, 50);
console.log('Changed files:', changedFiles)
// remove the sites/** prefix until src
const processedFiles = changedFiles.map(file => file.replace(/^sites\/[^/]+\//, ''));
console.log('Processed files:', processedFiles);
// handle normal pages
let netlifyLinks = processedFiles
.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 = processedFiles
.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');
}