Skip to content

Commit

Permalink
add custom owner and repo inputs (#78)
Browse files Browse the repository at this point in the history
* add custom owner and repo inputs

* add test for comment in another repo
  • Loading branch information
ReenigneArcher committed Apr 24, 2023
1 parent 7ca8398 commit 1605572
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 46 deletions.
34 changes: 18 additions & 16 deletions README.md
Expand Up @@ -62,22 +62,24 @@ jobs:

## Configuration options

| Input | Location | Description | Required | Default |
| ------------------------ | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------------------ |
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters. | maybe | |
| message-path | with | Path to a message you'd like displayed. Will be read and displayed just like a normal message. | maybe | |
| message-success | with | A message override, printed in case of success. | no | |
| message-failure | with | A message override, printed in case of failure. | no | |
| message-cancelled | with | A message override, printed in case of cancelled. | no | |
| message-skipped | with | A message override, printed in case of skipped. | no | |
| status | with | Required if you want to use message status overrides. | no | {{ job.status }} |
| repo-token | with | Valid GitHub token, either the temporary token GitHub provides or a personal access token. | no | {{ github.token }} |
| message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
| refresh-message-position | with | Should the sticky message be the last one in the PR's feed. | no | false |
| allow-repeats | with | Boolean flag to allow identical messages to be posted each time this action is run. | no | false |
| proxy-url | with | String for your proxy service URL if you'd like this to work with fork-based PRs. | no | |
| issue | with | Optional issue number override. | no | |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
| Input | Location | Description | Required | Default |
|--------------------------|----------|------------------------------------------------------------------------------------------------------|----------|------------------------------------|
| message | with | The message you'd like displayed, supports Markdown and all valid Unicode characters. | maybe | |
| message-path | with | Path to a message you'd like displayed. Will be read and displayed just like a normal message. | maybe | |
| message-success | with | A message override, printed in case of success. | no | |
| message-failure | with | A message override, printed in case of failure. | no | |
| message-cancelled | with | A message override, printed in case of cancelled. | no | |
| message-skipped | with | A message override, printed in case of skipped. | no | |
| status | with | Required if you want to use message status overrides. | no | {{ job.status }} |
| repo-owner | with | Owner of the repo. | no | {{ github.repository_owner }} |
| repo-name | with | Name of the repo. | no | {{ github.event.repository.name }} |
| repo-token | with | Valid GitHub token, either the temporary token GitHub provides or a personal access token. | no | {{ github.token }} |
| message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
| refresh-message-position | with | Should the sticky message be the last one in the PR's feed. | no | false |
| allow-repeats | with | Boolean flag to allow identical messages to be posted each time this action is run. | no | false |
| proxy-url | with | String for your proxy service URL if you'd like this to work with fork-based PRs. | no | |
| issue | with | Optional issue number override. | no | |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |

## Advanced Uses

Expand Down
16 changes: 16 additions & 0 deletions __tests__/add-pr-comment.test.ts
Expand Up @@ -16,6 +16,8 @@ const simpleMessage = 'hello world'
type Inputs = {
message: string | undefined
'message-path': string | undefined
'repo-owner': string
'repo-name': string
'repo-token': string
'message-id': string
'allow-repeats': string
Expand All @@ -29,6 +31,8 @@ type Inputs = {
const defaultInputs: Inputs = {
message: '',
'message-path': undefined,
'repo-owner': 'foo',
'repo-name': 'bar',
'repo-token': repoToken,
'message-id': 'add-pr-comment',
'allow-repeats': 'false',
Expand Down Expand Up @@ -175,6 +179,18 @@ describe('add-pr-comment action', () => {
expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true')
})

it('creates a comment in another repo', async () => {
const repoOwner = 'my-owner'
const repoName = 'my-repo'
inputs['repo-owner'] = repoOwner
inputs['repo-name'] = repoName
repoFullName = `${repoOwner}/${repoName}`

await expect(run()).resolves.not.toThrow()
expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true')
expect(core.setOutput).toHaveBeenCalledWith('comment-id', postIssueCommentsResponse.id)
})

it('safely exits when no issue can be found [using GITHUB_TOKEN in env]', async () => {
process.env['GITHUB_TOKEN'] = repoToken

Expand Down
8 changes: 8 additions & 0 deletions action.yml
Expand Up @@ -15,6 +15,14 @@ inputs:
description: "If a message with the same id, this option allow to refresh the position of the message to be the last one posted."
default: "false"
required: true
repo-owner:
description: "The repo owner."
default: "${{ github.repository_owner }}"
required: true
repo-name:
description: "The repo name."
default: "${{ github.event.repository.name }}"
required: true
repo-token:
description: "A GitHub token for API access. Defaults to {{ github.token }}."
default: "${{ github.token }}"
Expand Down
18 changes: 8 additions & 10 deletions dist/index.js
Expand Up @@ -99,11 +99,13 @@ const core = __importStar(__nccwpck_require__(2186));
const github = __importStar(__nccwpck_require__(5438));
const promises_1 = __importDefault(__nccwpck_require__(3977));
async function getInputs() {
var _a, _b, _c;
var _a, _b;
const messageIdInput = core.getInput('message-id', { required: false });
const messageId = messageIdInput === '' ? 'add-pr-comment' : `add-pr-comment:${messageIdInput}`;
const messageInput = core.getInput('message', { required: false });
const messagePath = core.getInput('message-path', { required: false });
const repoOwner = core.getInput('repo-owner', { required: true });
const repoName = core.getInput('repo-name', { required: true });
const repoToken = core.getInput('repo-token', { required: true });
const status = core.getInput('status', { required: true });
const issue = core.getInput('issue', { required: false });
Expand Down Expand Up @@ -136,11 +138,7 @@ async function getInputs() {
throw new Error('no message, check your message inputs');
}
const { payload } = github.context;
const repoFullName = (_a = payload.repository) === null || _a === void 0 ? void 0 : _a.full_name;
if (!repoFullName) {
throw new Error('unable to determine repository from request type');
}
const [owner, repo] = repoFullName.split('/');

return {
refreshMessagePosition,
allowRepeats,
Expand All @@ -149,11 +147,11 @@ async function getInputs() {
proxyUrl,
repoToken,
status,
issue: issue ? Number(issue) : (_b = payload.issue) === null || _b === void 0 ? void 0 : _b.number,
pullRequestNumber: (_c = payload.pull_request) === null || _c === void 0 ? void 0 : _c.number,
issue: issue ? Number(issue) : (_a = payload.issue) === null || _a === void 0 ? void 0 : _a.number,
pullRequestNumber: (_b = payload.pull_request) === null || _b === void 0 ? void 0 : _b.number,
commitSha: github.context.sha,
owner,
repo,
owner: repoOwner || payload.repo.owner,
repo: repoName || payload.repo.repo,
};
}
exports.getInputs = getInputs;
Expand Down
17 changes: 7 additions & 10 deletions lib/config.js
Expand Up @@ -31,11 +31,13 @@ const core = __importStar(require("@actions/core"));
const github = __importStar(require("@actions/github"));
const promises_1 = __importDefault(require("node:fs/promises"));
async function getInputs() {
var _a, _b, _c;
var _a, _b;
const messageIdInput = core.getInput('message-id', { required: false });
const messageId = messageIdInput === '' ? 'add-pr-comment' : `add-pr-comment:${messageIdInput}`;
const messageInput = core.getInput('message', { required: false });
const messagePath = core.getInput('message-path', { required: false });
const repoOwner = core.getInput('repo-owner', { required: true });
const repoName = core.getInput('repo-name', { required: true });
const repoToken = core.getInput('repo-token', { required: true });
const status = core.getInput('status', { required: true });
const issue = core.getInput('issue', { required: false });
Expand Down Expand Up @@ -72,11 +74,6 @@ async function getInputs() {
throw new Error('no message, check your message inputs');
}
const { payload } = github.context;
const repoFullName = (_a = payload.repository) === null || _a === void 0 ? void 0 : _a.full_name;
if (!repoFullName) {
throw new Error('unable to determine repository from request type');
}
const [owner, repo] = repoFullName.split('/');
return {
refreshMessagePosition,
allowRepeats,
Expand All @@ -85,11 +82,11 @@ async function getInputs() {
proxyUrl,
repoToken,
status,
issue: issue ? Number(issue) : (_b = payload.issue) === null || _b === void 0 ? void 0 : _b.number,
pullRequestNumber: (_c = payload.pull_request) === null || _c === void 0 ? void 0 : _c.number,
issue: issue ? Number(issue) : (_a = payload.issue) === null || _a === void 0 ? void 0 : _a.number,
pullRequestNumber: (_b = payload.pull_request) === null || _b === void 0 ? void 0 : _b.number,
commitSha: github.context.sha,
owner,
repo,
owner: repoOwner || payload.repo.owner,
repo: repoName || payload.repo.repo,
};
}
exports.getInputs = getInputs;
14 changes: 4 additions & 10 deletions src/config.ts
Expand Up @@ -26,6 +26,8 @@ export async function getInputs(): Promise<Inputs> {
const messageId = messageIdInput === '' ? 'add-pr-comment' : `add-pr-comment:${messageIdInput}`
const messageInput = core.getInput('message', { required: false })
const messagePath = core.getInput('message-path', { required: false })
const repoOwner = core.getInput('repo-owner', { required: true })
const repoName = core.getInput('repo-name', { required: true })
const repoToken = core.getInput('repo-token', { required: true })
const status = core.getInput('status', { required: true })
const issue = core.getInput('issue', { required: false })
Expand Down Expand Up @@ -73,14 +75,6 @@ export async function getInputs(): Promise<Inputs> {

const { payload } = github.context

const repoFullName = payload.repository?.full_name

if (!repoFullName) {
throw new Error('unable to determine repository from request type')
}

const [owner, repo] = repoFullName.split('/')

return {
refreshMessagePosition,
allowRepeats,
Expand All @@ -92,7 +86,7 @@ export async function getInputs(): Promise<Inputs> {
issue: issue ? Number(issue) : payload.issue?.number,
pullRequestNumber: payload.pull_request?.number,
commitSha: github.context.sha,
owner,
repo,
owner: repoOwner || payload.repo.owner,
repo: repoName || payload.repo.repo,
}
}

0 comments on commit 1605572

Please sign in to comment.