-
Notifications
You must be signed in to change notification settings - Fork 5
Add PR preview deployments with GitHub Pages #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Mark GitHub deployments as inactive when PR is closed | ||
| // This module exports a function that can be safely called by actions/github-script | ||
|
|
||
| module.exports = async function cleanupDeployments(github, context, core) { | ||
| // Validate inputs | ||
| if (!context.payload.pull_request) { | ||
| throw new Error('No pull request data available'); | ||
| } | ||
|
|
||
| const prNumber = context.payload.pull_request.number; | ||
| if (!Number.isInteger(prNumber) || prNumber < 1 || prNumber > 99999) { | ||
| throw new Error('Invalid PR number'); | ||
| } | ||
|
|
||
| const environment = `pr-preview-${prNumber}`; | ||
|
|
||
| // Get all deployments for this PR environment | ||
| const { data: deployments } = await github.rest.repos.listDeployments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| environment: environment, | ||
| }); | ||
|
|
||
| console.log(`Found ${deployments.length} deployments for environment: ${environment}`); | ||
|
|
||
| // Mark each deployment as inactive | ||
| for (const deployment of deployments) { | ||
| // Validate deployment ID | ||
| if (!deployment.id || !Number.isInteger(deployment.id)) { | ||
| console.log(`Skipping invalid deployment ID: ${deployment.id}`); | ||
| continue; | ||
| } | ||
|
|
||
| await github.rest.repos.createDeploymentStatus({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| deployment_id: deployment.id, | ||
| state: 'inactive', | ||
| description: 'PR closed - preview removed' | ||
| }); | ||
| console.log(`Marked deployment ${deployment.id} as inactive`); | ||
| } | ||
|
|
||
| console.log(`Cleanup completed for PR #${prNumber}`); | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // Comment on PR when preview is cleaned up | ||
| // This module exports a function that can be safely called by actions/github-script | ||
|
|
||
| module.exports = async function commentPrCleanup(github, context, core) { | ||
| // Validate inputs | ||
| if (!context.payload.pull_request) { | ||
| throw new Error('No pull request data available'); | ||
| } | ||
|
|
||
| const prNumber = context.payload.pull_request.number; | ||
| if (!Number.isInteger(prNumber) || prNumber < 1 || prNumber > 99999) { | ||
| throw new Error('Invalid PR number'); | ||
| } | ||
|
|
||
| // Read comment template with path validation | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const templatePath = '.github/templates/pr-cleanup-template.md'; | ||
|
|
||
| // Validate template path to prevent directory traversal | ||
| const resolvedPath = path.resolve(templatePath); | ||
| if (!resolvedPath.includes('.github/templates/pr-cleanup-template.md')) { | ||
| throw new Error('Invalid template path'); | ||
| } | ||
|
|
||
| if (!fs.existsSync(templatePath)) { | ||
| throw new Error(`Template file ${templatePath} not found`); | ||
| } | ||
|
|
||
| const commentBody = fs.readFileSync(templatePath, 'utf8'); | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body: commentBody | ||
| }); | ||
|
|
||
| console.log(`Posted cleanup comment on PR #${prNumber}`); | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // Comment on PR with preview deployment information | ||
| // This module exports a function that can be safely called by actions/github-script | ||
|
|
||
| module.exports = async function commentPrPreview(github, context, core) { | ||
| // Validate inputs | ||
| if (!context.payload.pull_request) { | ||
| throw new Error('No pull request data available'); | ||
| } | ||
|
|
||
| const prNumber = context.payload.pull_request.number; | ||
| if (!Number.isInteger(prNumber) || prNumber < 1 || prNumber > 99999) { | ||
| throw new Error('Invalid PR number'); | ||
| } | ||
|
|
||
| const rawCommitSha = context.payload.pull_request.head.sha; | ||
| if (!rawCommitSha || !rawCommitSha.match(/^[a-f0-9]{40}$/)) { | ||
| throw new Error('Invalid commit SHA format'); | ||
| } | ||
|
|
||
| const previewUrl = `https://preview.wafer.space/pr-${prNumber}/`; | ||
| const commitSha = rawCommitSha.substring(0, 7); | ||
|
|
||
| // Read comment template with path validation | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const templatePath = '.github/templates/pr-comment-template.md'; | ||
|
|
||
| // Validate template path to prevent directory traversal | ||
| const resolvedPath = path.resolve(templatePath); | ||
| if (!resolvedPath.includes('.github/templates/pr-comment-template.md')) { | ||
| throw new Error('Invalid template path'); | ||
| } | ||
|
|
||
| if (!fs.existsSync(templatePath)) { | ||
| throw new Error(`Template file ${templatePath} not found`); | ||
| } | ||
|
|
||
| let commentBody = fs.readFileSync(templatePath, 'utf8'); | ||
|
|
||
| // Sanitize and replace placeholders in template | ||
| const sanitizedPreviewUrl = previewUrl.replace(/[<>&"']/g, (match) => { | ||
| const entityMap = { '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' }; | ||
| return entityMap[match]; | ||
| }); | ||
|
|
||
| const sanitizedCommitSha = commitSha.replace(/[<>&"']/g, (match) => { | ||
| const entityMap = { '<': '<', '>': '>', '&': '&', '"': '"', "'": ''' }; | ||
| return entityMap[match]; | ||
| }); | ||
|
|
||
| commentBody = commentBody | ||
| .replace(/\{\{PREVIEW_URL\}\}/g, sanitizedPreviewUrl) | ||
| .replace(/\{\{COMMIT_SHA\}\}/g, sanitizedCommitSha); | ||
|
|
||
| // Find existing comment | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| }); | ||
|
|
||
| const botComment = comments.find(comment => | ||
| comment.user.type === 'Bot' && | ||
| (comment.body.includes('Preview Deployment Ready!') || | ||
| comment.body.includes('Preview Deployment Partially Ready') || | ||
| comment.body.includes('Preview Deployment Failed')) | ||
| ); | ||
|
|
||
| if (botComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: botComment.id, | ||
| body: commentBody | ||
| }); | ||
| console.log(`Updated existing comment ${botComment.id} on PR #${prNumber}`); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body: commentBody | ||
| }); | ||
| console.log(`Created new comment on PR #${prNumber}`); | ||
| } | ||
| }; | ||
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Create GitHub deployment for PR preview | ||
| // This module exports a function that can be safely called by actions/github-script | ||
|
|
||
| module.exports = async function createDeployment(github, context, core) { | ||
| // Validate inputs | ||
| if (!context.payload.pull_request) { | ||
| throw new Error('No pull request data available'); | ||
| } | ||
|
|
||
| const prNumber = context.payload.pull_request.number; | ||
| if (!Number.isInteger(prNumber) || prNumber < 1 || prNumber > 99999) { | ||
| throw new Error('Invalid PR number'); | ||
| } | ||
|
|
||
| // Sanitize PR title to prevent code injection | ||
| const rawTitle = context.payload.pull_request.title || ''; | ||
| const prTitle = rawTitle.replace(/[^\w\s-_.]/g, '').substring(0, 100); | ||
|
|
||
| // Validate ref format | ||
| const ref = context.payload.pull_request.head.ref; | ||
| if (!ref || ref.length > 255) { | ||
| throw new Error('Invalid ref format'); | ||
| } | ||
|
|
||
| const deployment = await github.rest.repos.createDeployment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| ref: ref, | ||
| environment: `pr-preview-${prNumber}`, | ||
| transient_environment: true, | ||
| production_environment: false, | ||
| required_contexts: [], | ||
| description: `PR #${prNumber}: ${prTitle}`, | ||
| auto_merge: false | ||
| }); | ||
|
|
||
| await github.rest.repos.createDeploymentStatus({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| deployment_id: deployment.data.id, | ||
| state: 'in_progress', | ||
| description: 'Building preview...' | ||
| }); | ||
|
|
||
| core.setOutput('deployment_id', deployment.data.id); | ||
| console.log(`Created deployment ${deployment.data.id} for PR #${prNumber}`); | ||
| }; |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Generate user-friendly directory name for PR previews | ||
| // This module exports a function that creates a slugified directory name from PR title | ||
|
|
||
| module.exports = function generatePrDirectoryName(prNumber, prTitle) { | ||
| // Validate inputs | ||
| if (!Number.isInteger(prNumber) || prNumber < 1 || prNumber > 99999) { | ||
| throw new Error('Invalid PR number'); | ||
| } | ||
|
|
||
| if (typeof prTitle !== 'string') { | ||
| throw new Error('PR title must be a string'); | ||
| } | ||
|
|
||
| // Slugify the PR title | ||
| const slugifiedTitle = prTitle | ||
| .toLowerCase() | ||
| .trim() | ||
| // Replace special characters and spaces with hyphens | ||
| .replace(/[^a-z0-9\s-]/g, '') | ||
| .replace(/\s+/g, '-') | ||
| .replace(/-+/g, '-') | ||
| .replace(/^-+|-+$/g, '') | ||
| // Limit length to reasonable size | ||
| .substring(0, 50) | ||
| .replace(/-+$/, ''); // Remove trailing hyphens after truncation | ||
|
|
||
| // Ensure we have a valid slug | ||
| const finalSlug = slugifiedTitle || 'untitled'; | ||
|
|
||
| // Format: pr-{number}-{slug} | ||
| return `pr-${prNumber}-${finalSlug}`; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
includescheck may be too permissive—use a strict equality or path comparison (e.g.,resolvedPath === path.resolve(templatePath)) to prevent directory traversal risks.