Skip to content

Commit c887f44

Browse files
Copilotmikepenz
andcommitted
Add documentation and edge case handling for pr_id parameter
Co-authored-by: mikepenz <[email protected]>
1 parent ec953bd commit c887f44

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ jobs:
114114
| `truncate_stack_traces` | Optional. Truncate stack traces from test output to 2 lines in annotations. Defaults to `true`. |
115115
| `resolve_ignore_classname` | Optional. Force ignore test case classname from the xml report (This can help fix issues with some tools/languages). Defaults to `false`. |
116116
| `skip_comment_without_tests` | Optional. Disable commenting if no tests are detected. Defaults to `false`. |
117+
| `pr_id` | Optional. PR number to comment on (useful for workflow_run contexts where the action runs outside the PR context). When provided, overrides the automatic PR detection. |
117118

118119
### Common Configurations
119120

@@ -247,10 +248,16 @@ jobs:
247248
with:
248249
commit: ${{github.event.workflow_run.head_sha}}
249250
report_paths: '**/build/test-results/test/TEST-*.xml'
251+
# Optional: if you want to add PR comments from workflow_run context
252+
# comment: true
253+
# pr_id: ${{ github.event.workflow_run.pull_requests[0].number }}
250254
```
251255

252256
This will securely post the check results from the privileged workflow onto the PR's checks report.
253257

258+
> [!TIP]
259+
> When running from `workflow_run` context, use the `pr_id` parameter to enable PR comments: `pr_id: ${{ github.event.workflow_run.pull_requests[0].number }}`
260+
254261
</p>
255262
</details>
256263

__tests__/annotator.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,82 @@ describe('attachComment', () => {
130130
})
131131
expect(mockOctokit.rest.issues.createComment).not.toHaveBeenCalled()
132132
})
133+
it('should warn and return early when pr_id is invalid', async () => {
134+
// Setup: no context issue number and invalid pr_id
135+
mockContext.issue.number = undefined
136+
137+
const checkName = ['Test Check']
138+
const table = [['Test', 'Result'], ['Example Test', 'Passed']]
139+
const prId = 'invalid-number'
140+
141+
await attachComment(mockOctokit, checkName, false, table, [], [], [], prId)
142+
143+
// Verify warning was called and no comment was created
144+
expect(mockWarning).toHaveBeenCalledWith(
145+
expect.stringContaining('Action requires a valid issue number (PR reference) or pr_id input')
146+
)
147+
expect(mockOctokit.rest.issues.createComment).not.toHaveBeenCalled()
148+
})
149+
150+
it('should handle pr_id with leading/trailing whitespace', async () => {
151+
// Setup: no context issue number
152+
mockContext.issue.number = undefined
153+
154+
mockOctokit.paginate.mockResolvedValue([])
155+
156+
const checkName = ['Test Check']
157+
const table = [['Test', 'Result'], ['Example Test', 'Passed']]
158+
const prId = ' 123 '
159+
160+
await attachComment(mockOctokit, checkName, false, table, [], [], [], prId)
161+
162+
// Verify comment was created with correct issue number (whitespace trimmed)
163+
expect(mockOctokit.rest.issues.createComment).toHaveBeenCalledWith({
164+
owner: 'test-owner',
165+
repo: 'test-repo',
166+
issue_number: 123,
167+
body: expect.stringContaining('Example Test')
168+
})
169+
170+
expect(mockWarning).not.toHaveBeenCalled()
171+
})
172+
173+
it('should update existing comment when pr_id is provided and updateComment is true', async () => {
174+
// Setup: no context issue number but pr_id provided
175+
mockContext.issue.number = undefined
176+
177+
const existingComment = {
178+
id: 888,
179+
body: 'Existing comment <!-- Summary comment for ["Test Check"] by mikepenz/action-junit-report -->'
180+
}
181+
mockOctokit.paginate.mockResolvedValue([existingComment])
182+
183+
const checkName = ['Test Check']
184+
const table = [['Test', 'Result'], ['Example Test', 'Updated']]
185+
const prId = '789'
186+
187+
await attachComment(mockOctokit, checkName, true, table, [], [], [], prId)
188+
189+
// Verify paginate was called with correct issue number
190+
expect(mockOctokit.paginate).toHaveBeenCalledWith(
191+
mockOctokit.rest.issues.listComments,
192+
{
193+
owner: 'test-owner',
194+
repo: 'test-repo',
195+
issue_number: 789
196+
}
197+
)
198+
199+
// Verify comment was updated
200+
expect(mockOctokit.rest.issues.updateComment).toHaveBeenCalledWith({
201+
owner: 'test-owner',
202+
repo: 'test-repo',
203+
comment_id: 888,
204+
body: expect.stringContaining('Example Test')
205+
})
206+
expect(mockOctokit.rest.issues.createComment).not.toHaveBeenCalled()
207+
})
208+
133209
})
134210

135211
describe('buildCommentIdentifier', () => {

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function run(): Promise<void> {
4141
const updateComment = core.getInput('updateComment') === 'true'
4242
const jobName = core.getInput('job_name')
4343
const skipCommentWithoutTests = core.getInput('skip_comment_without_tests') === 'true'
44-
const prId = core.getInput('pr_id')
44+
const prId = core.getInput('pr_id').trim() || undefined
4545

4646
const reportPaths = core.getMultilineInput('report_paths')
4747
const summary = core.getMultilineInput('summary')

0 commit comments

Comments
 (0)