-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
chore: add custom comment with link to playground for pkg.pr.new
#14151
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
6 commits
Select commit
Hold shift + click to select a range
530e3a8
chore: add custom comment with link to playground for `pkg.pr.new`
paoloricciuti 643b1a9
chore: small updates to the script output
paoloricciuti 2dced87
chore: refine comment a bit, include multiple packages
paoloricciuti fb68c5f
tweak comment
Rich-Harris 022040e
chore: two steps workflow
paoloricciuti 6f76685
chore: update workflow
paoloricciuti 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
name: Update pkg.pr.new comment | ||
|
||
on: | ||
workflow_run: | ||
workflows: ['Publish Any Commit'] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
build: | ||
name: 'Update comment' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Download artifact | ||
uses: actions/download-artifact@v4 | ||
with: | ||
name: output.json | ||
path: output.json | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
run-id: ${{ github.event.workflow_run.id }} | ||
- name: 'Post or update comment' | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const fs = require('fs'); | ||
const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); | ||
|
||
const bot_comment_identifier = `<!-- pkg.pr.new comment -->`; | ||
|
||
const body = (number) => `${bot_comment_identifier} | ||
|
||
[Playground](https://svelte.dev/playground?version=pr-${number}) | ||
|
||
\`\`\` | ||
${output.packages.map((p) => `pnpm add https://pkg.pr.new/${p.name}@${number}`).join('\n')} | ||
\`\`\` | ||
`; | ||
|
||
async function find_bot_comment(issue_number) { | ||
if (!issue_number) return null; | ||
const comments = await github.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issue_number, | ||
}); | ||
return comments.data.find((comment) => | ||
comment.body.includes(bot_comment_identifier) | ||
); | ||
} | ||
|
||
async function create_or_update_comment(issue_number) { | ||
if (!issue_number) { | ||
console.log('No issue number provided. Cannot post or update comment.'); | ||
return; | ||
} | ||
|
||
const existing_comment = await find_bot_comment(issue_number); | ||
if (existing_comment) { | ||
await github.rest.issues.updateComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
comment_id: existing_comment.id, | ||
body: body(issue_number), | ||
}); | ||
} else { | ||
await github.rest.issues.createComment({ | ||
issue_number: issue_number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: body(issue_number), | ||
}); | ||
} | ||
} | ||
|
||
async function log_publish_info() { | ||
console.log('\n' + '='.repeat(50)); | ||
console.log('Publish Information'); | ||
console.log('='.repeat(50)); | ||
console.log('\nPublished Packages:'); | ||
console.log(packages); | ||
console.log('\nPlayground URL:'); | ||
console.log(`\nhttps://svelte.dev/playground?version=commit-${output.sha.substring(0, 7)}`) | ||
console.log('\n' + '='.repeat(50)); | ||
} | ||
|
||
if (output.event_name === 'pull_request') { | ||
if (context.issue.number) { | ||
await create_or_update_comment(context.issue.number); | ||
} | ||
} else if (output.event_name === 'push') { | ||
const pull_requests = await github.rest.pulls.list({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
state: 'open', | ||
head: `${context.repo.owner}:${output.ref.replace('refs/heads/', '')}`, | ||
}); | ||
|
||
if (pull_requests.data.length > 0) { | ||
await create_or_update_comment(pull_requests.data[0].number); | ||
} else { | ||
console.log( | ||
'No open pull request found for this push. Logging publish information to console:' | ||
); | ||
await log_publish_info(); | ||
} | ||
} |
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
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.
@paoloricciuti This
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.
Actually it will never even run looking at this. None of these events types match.
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 event name is on the output not on the context...but yeah this I missed it...will fix
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.
Ah I see. Makes sense.