Skip to content

Commit

Permalink
add post CR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
sirineJ committed Dec 26, 2024
1 parent b42d2bf commit cccaaea
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion .github/workflows/cr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,85 @@ jobs:
run: npm run build

- name: Publish packages
run: npx pkg-pr-new publish './packages/circuit-ui' './packages/design-tokens' './packages/icons'
run: npx pkg-pr-new publish './packages/circuit-ui' './packages/design-tokens' './packages/icons' --json output.json --comment=off

- 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 packages = output.packages
.map((p) => `- ${p.name}: ${p.url}`)
.join('\n');
const templates = output.templates
.map((t) => `- [${t.name}](${t.url})`)
.join('\n');

const sha = context.payload.pull_request.head.sha

const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`;

const body = `## Packages were published 🚀

### Published Packages:

${packages}

### Templates:

${templates}

[View Commit](${commitUrl})`;

const botCommentIdentifier = '## Packages were published';

async function findBotComment(issueNumber) {
if (!issueNumber) return null;
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
});
return comments.data.find((comment) =>
comment.body.includes(botCommentIdentifier)
);
}

async function createOrUpdateComment(issueNumber) {
if (!issueNumber) {
console.log('No issue number provided. Cannot post or update comment.');
return;
}

const existingComment = await findBotComment(issueNumber);
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: body,
});
}
}
if (context.issue.number) {
await createOrUpdateComment(context.issue.number);
}

- name: Delete label
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER:
${{ github.event.pull_request.number }}
run: |
echo "Pull Request Number: $PR_NUMBER"
gh pr edit $PR_NUMBER --remove-label "preview"

0 comments on commit cccaaea

Please sign in to comment.