Improve recommendations on publication credit #1247
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
} |