-
Notifications
You must be signed in to change notification settings - Fork 25
Token Diff PR Comment Workflow
Garth Braithwaite edited this page Jul 16, 2025
·
1 revision
This document describes the GitHub Action workflow that automatically generates and posts token change reports as comments on pull requests.
The token-diff-pr-comment.yml
workflow automatically:
- Triggers on pull request events (opened, synchronized, reopened)
- Analyzes token changes between the base branch and PR branch
- Posts or updates a comment on the PR with the diff report
- Handles cases with no changes and error scenarios
The workflow runs when:
- A pull request is opened, synchronized (updated), or reopened
- Changes are made to token-related files:
packages/tokens/src/**
packages/tokens/schemas/**
-
Toolchain: Uses
moonrepo/setup-toolchain@v0
for automatic Node.js and pnpm setup - Versions: Automatically determined from project configuration (moon.yml/package.json)
- Runner: ubuntu-latest
-
contents: read
- To checkout the repository -
pull-requests: write
- To create and update PR comments
-
Repository Checkout: Fetches the full git history with
fetch-depth: 0
- Toolchain Setup: Uses moonrepo to automatically configure Node.js and pnpm
- Dependencies: Installs diff-generator dependencies using pnpm
-
Diff Generation:
- Compares base branch vs head branch
- Outputs markdown-formatted report
- Handles success, no-changes, and error scenarios
-
Comment Management:
- Finds existing token diff comments (if any)
- Creates new comment or updates existing one
- Uses unique HTML comment marker for identification
## 🎨 Token Changes Report
**Tokens Changed (5)**
main | **feature-branch**
-------------------------------------------------------------------------------------------
<details open><summary>
**Added (2)**
</summary>
- `color-blue-100`
- `color-blue-200`
</details>
<details open><summary>
**Updated (3)**
</summary>
- `color-red-500`: `#ff0000` -> `#dc2626`
- `spacing-large`: `16px` -> `20px`
- `font-size-body`: `14px` -> `16px`
</details>
---
*This comment was automatically generated by the token diff tool. 🤖*
## 🎨 Token Changes Report
No token changes detected in this pull request.
---
*This comment was automatically generated by the token diff tool. 🤖*
## 🎨 Token Changes Report
⚠️ **Error**: Error generating token diff report. Exit code: 1
Please check the [workflow logs](link-to-logs) for more details.
---
*This comment was automatically generated by the token diff tool. 🤖*
To modify which file changes trigger the workflow, update the paths
section:
on:
pull_request:
types: [opened, synchronize, reopened]
paths:
- 'packages/tokens/src/**' # Token source files
- 'packages/tokens/schemas/**' # Schema files
- 'custom/path/**' # Add custom paths
The workflow uses the diff-generator's markdown format. To use a different format:
- Change the
--format
parameter in the workflow - Update the comment body template accordingly
Available formats:
-
markdown
(default) - Rich markdown with collapsible sections -
handlebars
with templates:-
json
- Structured JSON output -
plain
- Plain text format -
summary
- High-level statistics only
-
Example for JSON format:
pnpm tdiff report \
--otb ${{ github.base_ref }} \
--ntb ${{ github.head_ref }} \
--repo ${{ github.repository }} \
--format handlebars \
--template json \
--output /tmp/token-diff.json
The workflow handles several error scenarios:
- Diff Generation Failures: Captures exit codes and displays user-friendly error messages
- No Changes: Displays appropriate "no changes" message
- Permission Issues: Workflow requires proper permissions for PR comments
- Network Issues: GitHub API failures are handled by the comment actions
- peter-evans/find-comment@v3: Finds existing comments to update
- peter-evans/create-or-update-comment@v4: Creates or updates PR comments
- actions/checkout@v4: Repository checkout
- moonrepo/setup-toolchain@v0: Automatic Node.js and pnpm setup using project configuration
- Check that the PR contains changes to files in the specified paths
- Verify the workflow file is on the default branch (main)
- Ensure the repository has Actions enabled
- Verify the workflow has
pull-requests: write
permission - Check that the repository settings allow Actions to create/update PR comments
- For organization repositories, check organization security policies
- Check the workflow logs for specific error messages
- Verify the diff-generator dependencies are correctly installed
- Ensure the base and head branches exist and are accessible
- Check if the workflow completed successfully
- Verify PR permissions allow bot comments
- Look for existing comments that might have been updated instead of creating new ones
When updating GitHub Actions or tool versions:
- Update action versions in the workflow file
- Node.js and pnpm versions are automatically managed by moonrepo toolchain
- Update project configuration (moon.yml/package.json) to change tool versions
- Test the workflow in a development environment
The comment includes an HTML marker <!-- token-diff-comment -->
for identification. When modifying the comment template:
- Keep the HTML marker intact for proper comment updating
- Maintain the section structure for consistency
- Test formatting with various diff scenarios
This workflow follows the Spectrum Tokens project standards:
- Package Management: Uses pnpm instead of npm
- Node.js Version: Matches project requirement (~20.12)
- Tool Integration: Uses the project's diff-generator tool
- Code Quality: Includes proper error handling and logging
- Documentation: Comprehensive documentation following project patterns