|
| 1 | +# replace-comment |
| 2 | + |
| 3 | +A GitHub Action that finds, deletes, and recreates a comment to move it to the bottom of the conversation. This is perfect for CI/CD status updates, bot notifications, and any content you want to keep highly visible. |
| 4 | + |
| 5 | +## Why Replace Instead of Update? |
| 6 | + |
| 7 | +When you **update** a comment, it stays in its original chronological position. When you **delete and recreate**, the new comment appears at the bottom of the conversation - much more visible! |
| 8 | + |
| 9 | +**Perfect for:** |
| 10 | +- ✅ CI/CD test results |
| 11 | +- ✅ Build status updates |
| 12 | +- ✅ Bot notifications |
| 13 | +- ✅ Status reports |
| 14 | +- ✅ Any content that should stay at the bottom for visibility |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Basic Example |
| 19 | + |
| 20 | +```yaml |
| 21 | +- name: Replace E2E Test Results Comment |
| 22 | + uses: johanwulf/replace-comment@v1 |
| 23 | + with: |
| 24 | + issue-number: ${{ github.event.pull_request.number }} |
| 25 | + body-includes: '<!-- e2e-test-results -->' |
| 26 | + comment-author: 'github-actions[bot]' |
| 27 | + body: | |
| 28 | + <!-- e2e-test-results --> |
| 29 | + ## 🧪 E2E Test Results |
| 30 | + |
| 31 | + ✅ All tests passed! |
| 32 | + |
| 33 | + - **Total tests:** 42 |
| 34 | + - **Duration:** 3m 24s |
| 35 | + - **Browser:** Chrome 120 |
| 36 | +``` |
| 37 | +
|
| 38 | +### Advanced Example with File Body |
| 39 | +
|
| 40 | +```yaml |
| 41 | +- name: Replace Build Report |
| 42 | + uses: johanwulf/replace-comment@v1 |
| 43 | + with: |
| 44 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + repository: ${{ github.repository }} |
| 46 | + issue-number: ${{ github.event.pull_request.number }} |
| 47 | + body-regex: '<!-- build-report-\\d+ -->' |
| 48 | + comment-author: 'build-bot[bot]' |
| 49 | + body-path: './build-report.md' |
| 50 | + reactions: 'rocket,eyes' |
| 51 | +``` |
| 52 | +
|
| 53 | +## Inputs |
| 54 | +
|
| 55 | +| Name | Description | Required | Default | |
| 56 | +|------|-------------|----------|---------| |
| 57 | +| `token` | GITHUB_TOKEN or a repo scoped PAT | No | `${{ github.token }}` | |
| 58 | +| `repository` | The full name of the repository | No | `${{ github.repository }}` | |
| 59 | +| `issue-number` | The number of the issue or pull request | **Yes** | | |
| 60 | +| `body-includes` | A string to search for in comment bodies | No* | | |
| 61 | +| `body-regex` | A regular expression to search for in comment bodies | No* | | |
| 62 | +| `comment-author` | The GitHub username of the comment author | No | | |
| 63 | +| `direction` | Search direction: `first` or `last` | No | `first` | |
| 64 | +| `nth` | 0-indexed number specifying which comment to replace if multiple found | No | `0` | |
| 65 | +| `body` | The comment body content | No** | | |
| 66 | +| `body-path` | Path to a file containing the comment body | No** | | |
| 67 | +| `reactions` | Comma-separated reactions to add (+1, -1, laugh, confused, heart, hooray, rocket, eyes) | No | | |
| 68 | + |
| 69 | +\* Either `body-includes` or `body-regex` must be provided |
| 70 | +\** Either `body` or `body-path` must be provided |
| 71 | + |
| 72 | +## Outputs |
| 73 | + |
| 74 | +| Name | Description | |
| 75 | +|------|-------------| |
| 76 | +| `comment-id` | The ID of the created comment | |
| 77 | +| `comment-url` | The URL of the created comment | |
| 78 | +| `comment-body` | The body of the created comment | |
| 79 | +| `found-comment-id` | The ID of the comment that was found and deleted (empty if none found) | |
| 80 | + |
| 81 | +## How It Works |
| 82 | + |
| 83 | +1. **🔍 Find**: Uses [peter-evans/find-comment](https://github.com/peter-evans/find-comment) to search for existing comments |
| 84 | +2. **🗑️ Delete**: Removes the found comment using GitHub's REST API (if any) |
| 85 | +3. **✨ Create**: Uses [peter-evans/create-or-update-comment](https://github.com/peter-evans/create-or-update-comment) to create a new comment at the bottom |
| 86 | + |
| 87 | +This action is a **composite action** that combines the excellent work of Peter Evans into a simple "find-delete-create" workflow. |
| 88 | + |
| 89 | +## Examples |
| 90 | + |
| 91 | +### CI/CD Test Results |
| 92 | + |
| 93 | +```yaml |
| 94 | +name: Tests |
| 95 | +on: [push, pull_request] |
| 96 | +
|
| 97 | +jobs: |
| 98 | + test: |
| 99 | + runs-on: ubuntu-latest |
| 100 | + steps: |
| 101 | + - uses: actions/checkout@v4 |
| 102 | + |
| 103 | + - name: Run tests |
| 104 | + id: tests |
| 105 | + run: | |
| 106 | + # Your test commands here |
| 107 | + echo "result=✅ All tests passed!" >> $GITHUB_OUTPUT |
| 108 | + |
| 109 | + - name: Update test results comment |
| 110 | + if: github.event_name == 'pull_request' |
| 111 | + uses: johanwulf/replace-comment@v1 |
| 112 | + with: |
| 113 | + issue-number: ${{ github.event.pull_request.number }} |
| 114 | + body-includes: '<!-- test-results -->' |
| 115 | + comment-author: 'github-actions[bot]' |
| 116 | + body: | |
| 117 | + <!-- test-results --> |
| 118 | + ## 🧪 Test Results |
| 119 | + |
| 120 | + ${{ steps.tests.outputs.result }} |
| 121 | + |
| 122 | + **Commit:** ${{ github.sha }} |
| 123 | + **Workflow:** [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) |
| 124 | +``` |
| 125 | + |
| 126 | +### Deployment Status |
| 127 | + |
| 128 | +```yaml |
| 129 | +- name: Update deployment status |
| 130 | + uses: johanwulf/replace-comment@v1 |
| 131 | + with: |
| 132 | + issue-number: ${{ github.event.pull_request.number }} |
| 133 | + body-regex: '<!-- deploy-status-.*? -->' |
| 134 | + body: | |
| 135 | + <!-- deploy-status-${{ github.run_id }} --> |
| 136 | + ## 🚀 Deployment Status |
| 137 | + |
| 138 | + **Environment:** staging |
| 139 | + **Status:** ${{ job.status }} |
| 140 | + **URL:** https://staging-${{ github.event.pull_request.number }}.example.com |
| 141 | + **Deploy time:** $(date) |
| 142 | + reactions: 'rocket' |
| 143 | +``` |
| 144 | + |
| 145 | +### Build Artifacts Report |
| 146 | + |
| 147 | +```yaml |
| 148 | +- name: Generate build report |
| 149 | + run: | |
| 150 | + echo "## 📦 Build Artifacts" > build-report.md |
| 151 | + echo "" >> build-report.md |
| 152 | + echo "| File | Size |" >> build-report.md |
| 153 | + echo "|------|------|" >> build-report.md |
| 154 | + ls -la dist/ | awk '{print "| " $9 " | " $5 " |"}' >> build-report.md |
| 155 | +
|
| 156 | +- name: Post build report |
| 157 | + uses: johanwulf/replace-comment@v1 |
| 158 | + with: |
| 159 | + issue-number: ${{ github.event.pull_request.number }} |
| 160 | + body-includes: '<!-- build-artifacts -->' |
| 161 | + body-path: './build-report.md' |
| 162 | +``` |
| 163 | + |
| 164 | +## Tips |
| 165 | + |
| 166 | +### Unique Comment Identification |
| 167 | + |
| 168 | +Use HTML comments to uniquely identify your comments: |
| 169 | + |
| 170 | +```yaml |
| 171 | +body: | |
| 172 | + <!-- my-unique-identifier --> |
| 173 | + Your visible content here |
| 174 | +``` |
| 175 | + |
| 176 | +### Multiple Bot Comments |
| 177 | + |
| 178 | +Use different identifiers for different types of comments: |
| 179 | + |
| 180 | +```yaml |
| 181 | +# Test results |
| 182 | +body-includes: '<!-- test-results -->' |
| 183 | +
|
| 184 | +# Build status |
| 185 | +body-includes: '<!-- build-status -->' |
| 186 | +
|
| 187 | +# Deploy status |
| 188 | +body-includes: '<!-- deploy-status -->' |
| 189 | +``` |
| 190 | + |
| 191 | +### Conditional Comments |
| 192 | + |
| 193 | +Only create comments when needed: |
| 194 | + |
| 195 | +```yaml |
| 196 | +- name: Replace comment |
| 197 | + if: github.event_name == 'pull_request' && failure() |
| 198 | + uses: johanwulf/replace-comment@v1 |
| 199 | + with: |
| 200 | + # ... your config |
| 201 | +``` |
| 202 | + |
| 203 | +## Comparison with Other Actions |
| 204 | + |
| 205 | +| Action | Find | Update | Delete+Create | Use Case | |
| 206 | +|--------|------|--------|---------------|----------| |
| 207 | +| `peter-evans/create-or-update-comment` | ✅ | ✅ | ❌ | Update existing content | |
| 208 | +| `edumserrano/find-create-or-update-comment` | ✅ | ✅ | ❌ | Simplified find+update | |
| 209 | +| `replace-comment` (this action) | ✅ | ❌ | ✅ | Keep comments at bottom | |
| 210 | + |
| 211 | +## Development |
| 212 | + |
| 213 | +This is a composite GitHub Action that leverages: |
| 214 | +- [peter-evans/find-comment](https://github.com/peter-evans/find-comment) for finding comments |
| 215 | +- [peter-evans/create-or-update-comment](https://github.com/peter-evans/create-or-update-comment) for creating comments |
| 216 | +- GitHub REST API for deleting comments |
| 217 | + |
| 218 | +No build step required! The action runs directly from the `action.yml` file. |
| 219 | + |
| 220 | +## Contributing |
| 221 | + |
| 222 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 223 | + |
| 224 | +## License |
| 225 | + |
| 226 | +This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 227 | + |
| 228 | +## Acknowledgments |
| 229 | + |
| 230 | +- Inspired by the excellent work of [Peter Evans](https://github.com/peter-evans) on GitHub Actions for comments |
| 231 | +- Built to fill the gap for delete+create comment workflows |
0 commit comments