Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ For more advanced usage, there are several inputs available.
summary: true # Post report to the job summary. Default is true
pull-request: false # Comment on pull request with report. Default is false
issue: '' # Issue number to comment on. Works with standard issues and pull-request. Default is no issue
issue-repo: '' # Repository containing the issue or pull request to comment on, in owner/repo format. Default is the repository running the workflow
status-check: false # Create a status check for the workflow. Default is false
status-check-name: 'Test Reporter Results' # Name of the status check. Default is GitHub Test Reporter Results
community-report-name: 'summary-short' # Name of the community report to use. Default is summary-short
Expand Down Expand Up @@ -273,6 +274,24 @@ input and providing the issue number ():

Requires a `GITHUB_TOKEN` with issue or pull request write permission.

The issue or pull request can live in a different repository than the one
running the workflow — useful when an orchestration repository runs the tests
but the results belong on a pull request in another repository. Set
`issue-repo` in `owner/repo` format and provide a token with write access to
that repository:

```yaml
- name: Publish Test Report
uses: ctrf-io/github-test-reporter@v1
with:
report-path: './ctrf/*.json'
issue: '123'
issue-repo: 'my-org/my-other-repo'
env:
GITHUB_TOKEN: ${{ secrets.CROSS_REPO_TOKEN }}
if: always()
```

### Comment Management Inputs

`update-comment` An existing tagged comment is found, the new report is
Expand Down
26 changes: 26 additions & 0 deletions __tests__/github/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
findExistingMarkedComment,
handleComment,
handleViewsAndComments,
resolveIssueRepo,
} from "../../src/github/handler.js";
import type { Inputs } from "../../src/types/index.js";
import type { Report } from "../../src/ctrf/core/types/ctrf.js";
Expand Down Expand Up @@ -740,3 +741,28 @@ describe("handleViewsAndComments", () => {
expect(mockCore.setOutput).toHaveBeenCalledTimes(2);
});
});

describe("resolveIssueRepo", () => {
it("should fall back to the workflow repository when issue-repo is empty", () => {
expect(resolveIssueRepo("")).toEqual({
owner: "test-owner",
repo: "test-repo",
});
});

it("should resolve owner and repo from an owner/repo value", () => {
expect(resolveIssueRepo("some-org/some-repo")).toEqual({
owner: "some-org",
repo: "some-repo",
});
});

it.each(["no-slash", "too/many/slashes", "/missing-owner", "missing-repo/"])(
"should throw for malformed issue-repo %s",
(value) => {
expect(() => resolveIssueRepo(value)).toThrow(
`Invalid issue-repo "${value}" - expected "owner/repo" format`,
);
},
);
});
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ inputs:
description: 'Add a comment to the issue number with the generated report.'
required: false
default: ''
issue-repo:
description:
'Repository containing the issue or pull request to comment on, in
owner/repo format. Defaults to the repository running the workflow.
Commenting on another repository requires a token with write access to
it.'
required: false
default: ''
annotate:
description: 'Add annotations to failed tests in the GitHub Actions UI.'
required: false
Expand Down
19 changes: 17 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/core/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function getInputs(): Inputs {
githubReport: core.getInput("github-report").toLowerCase() === "true",
pullRequest: core.getInput("pull-request").toLowerCase() === "true",
issue: core.getInput("issue").toLowerCase() || "",
issueRepo: core.getInput("issue-repo") || "",
collapseLargeReports:
core.getInput("collapse-large-reports").toLowerCase() === "true",
summaryReport: core.getInput("summary-report").toLowerCase() === "true",
Expand Down
31 changes: 29 additions & 2 deletions src/github/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ async function postOrUpdatePRComment(
}
}

/**
* Resolves the repository that an issue comment should be posted to.
*
* @param issueRepo - The user-provided `issue-repo` input in `owner/repo` format, or an empty string.
* @returns The owner and repository name to comment on.
* @throws {Error} If `issueRepo` is set but not in `owner/repo` format.
*/
export function resolveIssueRepo(issueRepo: string): {
owner: string;
repo: string;
} {
if (!issueRepo) {
return { owner: context.repo.owner, repo: context.repo.repo };
}

const parts = issueRepo.split("/");
if (parts.length !== 2 || !parts[0] || !parts[1]) {
throw new Error(
`Invalid issue-repo "${issueRepo}" - expected "owner/repo" format`,
);
}

return { owner: parts[0], repo: parts[1] };
}

/**
* Posts or updates a comment on an issue.
*
Expand All @@ -231,9 +256,10 @@ async function postOrUpdateIssueComment(
const newSummary = core.summary.stringify();

try {
const { owner, repo } = resolveIssueRepo(inputs.issueRepo);
await handleComment(
context.repo.owner,
context.repo.repo,
owner,
repo,
parseInt(inputs.issue, 10),
newSummary,
marker,
Expand All @@ -259,6 +285,7 @@ async function postOrUpdateIssueComment(
" runs-on: ubuntu-latest\n" +
" permissions:\n" +
" issues: write\n\n" +
"When commenting on an issue in another repository via the issue-repo input, GITHUB_TOKEN must be a token with write access to that repository.\n" +
"See documentation: https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token",
);
} else if (error instanceof Error) {
Expand Down
1 change: 1 addition & 0 deletions src/types/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Inputs {
summary: boolean;
pullRequest: boolean;
issue: string;
issueRepo: string;
collapseLargeReports: boolean;
summaryReport: boolean;
summaryDeltaReport: boolean;
Expand Down
Loading