🔄 Add auto-update functionality to playground comments

This commit is contained in:
Daniel Dudzic 2025-08-01 03:14:07 +02:00
parent bd51ff22aa
commit 37a465ca89
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
2 changed files with 32 additions and 25 deletions

View file

@ -78,8 +78,13 @@ async function run({ github, context, core }) {
return; return;
} }
// Comments are only added after the first successful build. // Check for existing playground comment to update instead of creating duplicate
// The caller should check for existing comments before calling this function. const comments = await github.rest.issues.listComments(commentInfo);
const existingComment = comments.data.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Test using WordPress Playground')
);
const defaultSchema = generateWordpressPlaygroundBlueprint( const defaultSchema = generateWordpressPlaygroundBlueprint(
context.runId, context.runId,
@ -111,19 +116,36 @@ The changes in this pull request can be previewed and tested using a [WordPress
> 💡 The demo environment resets each time you refresh. Perfect for testing! > 💡 The demo environment resets each time you refresh. Perfect for testing!
> >
> This URL is valid for 30 days from when this comment was last updated. You can refresh it by pushing a new commit. > 🔄 This link updates automatically with each new commit to the PR.
>
> This URL is valid for 30 days from when this comment was last updated.
--- ---
<sub>🤖 Auto-generated for commit ${context.payload.pull_request.head.sha}</sub>`; <sub>🤖 Auto-generated for commit ${context.payload.pull_request.head.sha} Last updated: ${new Date().toISOString()}</sub>`;
// Create the comment if (existingComment) {
commentInfo.body = body; // Update existing comment
await github.rest.issues.createComment(commentInfo); await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
core.info('Successfully created playground comment on PR'); core.info(`Successfully updated existing playground comment #${existingComment.id} on PR #${context.issue.number}`);
} else {
// Create new comment
await github.rest.issues.createComment({
...commentInfo,
body: body
});
core.info(`Successfully created new playground comment on PR #${context.issue.number}`);
}
} catch (error) { } catch (error) {
core.setFailed(`Failed to create playground comment: ${error.message}`); core.setFailed(`Failed to create/update playground comment: ${error.message}`);
core.error(`Error details: ${error.stack}`);
} }
} }

View file

@ -109,7 +109,7 @@ jobs:
- name: Unzip PR details - name: Unzip PR details
run: unzip pr-details.zip run: unzip pr-details.zip
- name: Comment on PR with WordPress Playground link - name: Create or update PR playground comment
uses: actions/github-script@v7 uses: actions/github-script@v7
with: with:
script: | script: |
@ -118,21 +118,6 @@ jobs:
const plugin_version = fs.readFileSync('./VERSION', 'utf8').trim(); const plugin_version = fs.readFileSync('./VERSION', 'utf8').trim();
const artifact_name = fs.readFileSync('./ARTIFACT', 'utf8').trim(); const artifact_name = fs.readFileSync('./ARTIFACT', 'utf8').trim();
// Check for existing comment to avoid duplicates
const commentInfo = {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
};
const comments = (await github.rest.issues.listComments(commentInfo)).data;
for (const currentComment of comments) {
if (currentComment.user.type === 'Bot' && currentComment.body.includes('Test using WordPress Playground')) {
return;
}
}
// Load the comment script // Load the comment script
const script = require('./.github/scripts/playground-comment.js'); const script = require('./.github/scripts/playground-comment.js');