mirror of
https://github.com/mshick/add-pr-comment.git
synced 2025-12-31 22:29:45 +11:00
Allow message overrides for statuses (#62)
This commit is contained in:
parent
75079b41e7
commit
f116a1a828
10 changed files with 407 additions and 87 deletions
3
.github/workflows/ci.yml
vendored
3
.github/workflows/ci.yml
vendored
|
|
@ -54,7 +54,7 @@ jobs:
|
|||
|
||||
- name: Build action
|
||||
run: |
|
||||
npm run build
|
||||
npm run release
|
||||
|
||||
- uses: ./
|
||||
with:
|
||||
|
|
@ -63,3 +63,4 @@ jobs:
|
|||
🌏
|
||||
!
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
status: ${{ job.status }}
|
||||
|
|
|
|||
38
README.md
38
README.md
|
|
@ -12,6 +12,7 @@
|
|||
- Supports emoji 😂😂😂!
|
||||
- Supports a proxy for fork-based PRs. [See below](#proxy-for-fork-based-prs).
|
||||
- Supports creating a message from a file path.
|
||||
- Optional message / status overrides.
|
||||
|
||||
## Usage
|
||||
|
||||
|
|
@ -66,13 +67,19 @@ jobs:
|
|||
| -------------------- | -------- | ---------------------------------------------------------------------------------------------------- | -------- | ------- |
|
||||
| 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. | maybe | |
|
||||
| message-failure | with | A message override, printed in case of failure. | maybe | |
|
||||
| message-cancelled | with | A message override, printed in case of cancelled. | maybe | |
|
||||
| status | with | Required if you want to use message status overrides. | maybe | |
|
||||
| repo-token | with | Valid GitHub token, either the temporary token GitHub provides or a personal access token. | maybe | |
|
||||
| message-id | with | Message id to use when searching existing comments. If found, updates the existing (sticky comment). | no | |
|
||||
| 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 | |
|
||||
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | maybe | |
|
||||
|
||||
## Proxy for Fork-based PRs
|
||||
## Advanced Uses
|
||||
|
||||
### Proxy for Fork-based PRs
|
||||
|
||||
GitHub limits `GITHUB_TOKEN` and other API access token permissions when creating a PR from a fork. This precludes adding comments when your PRs are coming from forks, which is the norm for open source projects. To work around this situation I've created a simple companion app you can deploy to Cloud Run or another host to proxy the create comment requests with a personal access token you provide.
|
||||
|
||||
|
|
@ -99,3 +106,32 @@ jobs:
|
|||
proxy-url: https://add-pr-comment-proxy-94idvmwyie-uc.a.run.app
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
```
|
||||
|
||||
### Status Message Overrides
|
||||
|
||||
You can override your messages based on your job status. This can be helpful
|
||||
if you don't anticipate having the data required to create a helpful message in
|
||||
case of failure, but you still want a message to be sent to the PR comment.
|
||||
|
||||
**Example**
|
||||
|
||||
```yaml
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
pr:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
pull-requests: write
|
||||
steps:
|
||||
- uses: mshick/add-pr-comment@v2
|
||||
with:
|
||||
if: always()
|
||||
message: |
|
||||
**Howdie!**
|
||||
message-failure: |
|
||||
Uh oh!
|
||||
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
status: ${{ job.status }}
|
||||
```
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@ type Inputs = {
|
|||
'repo-token': string
|
||||
'message-id': string
|
||||
'allow-repeats': string
|
||||
'message-success'?: string
|
||||
'message-failure'?: string
|
||||
'message-cancelled'?: string
|
||||
status?: 'success' | 'failure' | 'cancelled'
|
||||
}
|
||||
|
||||
const inputs: Inputs = {
|
||||
|
|
@ -36,18 +40,27 @@ let postIssueCommentsResponse = {
|
|||
id: 42,
|
||||
}
|
||||
|
||||
type MessagePayload = {
|
||||
comment_id?: number
|
||||
body: string
|
||||
}
|
||||
|
||||
let messagePayload: MessagePayload | undefined
|
||||
|
||||
vi.mock('@actions/core')
|
||||
|
||||
export const handlers = [
|
||||
rest.post(
|
||||
`https://api.github.com/repos/${repoFullName}/issues/:issueNumber/comments`,
|
||||
(req, res, ctx) => {
|
||||
async (req, res, ctx) => {
|
||||
messagePayload = await req.json<MessagePayload>()
|
||||
return res(ctx.status(200), ctx.json(postIssueCommentsResponse))
|
||||
},
|
||||
),
|
||||
rest.patch(
|
||||
`https://api.github.com/repos/${repoFullName}/issues/comments/:commentId`,
|
||||
(req, res, ctx) => {
|
||||
async (req, res, ctx) => {
|
||||
messagePayload = await req.json<MessagePayload>()
|
||||
return res(ctx.status(200), ctx.json(postIssueCommentsResponse))
|
||||
},
|
||||
),
|
||||
|
|
@ -225,4 +238,73 @@ describe('add-pr-comment action', () => {
|
|||
expect(core.setOutput).toHaveBeenCalledWith('comment-updated', 'true')
|
||||
expect(core.setOutput).toHaveBeenCalledWith('comment-id', commentId)
|
||||
})
|
||||
|
||||
it('overrides the default message with a success message on success', async () => {
|
||||
inputs.message = simpleMessage
|
||||
inputs['message-path'] = undefined
|
||||
inputs['repo-token'] = repoToken
|
||||
inputs['allow-repeats'] = 'false'
|
||||
inputs['message-success'] = '666'
|
||||
inputs.status = 'success'
|
||||
|
||||
const commentId = 123
|
||||
|
||||
getIssueCommentsResponse = [
|
||||
{
|
||||
id: commentId,
|
||||
},
|
||||
]
|
||||
postIssueCommentsResponse = {
|
||||
id: commentId,
|
||||
}
|
||||
|
||||
await run()
|
||||
expect(messagePayload?.body).toContain('666')
|
||||
})
|
||||
|
||||
it('overrides the default message with a failure message on failure', async () => {
|
||||
inputs.message = simpleMessage
|
||||
inputs['message-path'] = undefined
|
||||
inputs['repo-token'] = repoToken
|
||||
inputs['allow-repeats'] = 'false'
|
||||
inputs['message-failure'] = '666'
|
||||
inputs.status = 'failure'
|
||||
|
||||
const commentId = 123
|
||||
|
||||
getIssueCommentsResponse = [
|
||||
{
|
||||
id: commentId,
|
||||
},
|
||||
]
|
||||
postIssueCommentsResponse = {
|
||||
id: commentId,
|
||||
}
|
||||
|
||||
await run()
|
||||
expect(messagePayload?.body).toContain('666')
|
||||
})
|
||||
|
||||
it('overrides the default message with a cancelled message on cancelled', async () => {
|
||||
inputs.message = simpleMessage
|
||||
inputs['message-path'] = undefined
|
||||
inputs['repo-token'] = repoToken
|
||||
inputs['allow-repeats'] = 'false'
|
||||
inputs['message-cancelled'] = '666'
|
||||
inputs.status = 'cancelled'
|
||||
|
||||
const commentId = 123
|
||||
|
||||
getIssueCommentsResponse = [
|
||||
{
|
||||
id: commentId,
|
||||
},
|
||||
]
|
||||
postIssueCommentsResponse = {
|
||||
id: commentId,
|
||||
}
|
||||
|
||||
await run()
|
||||
expect(messagePayload?.body).toContain('666')
|
||||
})
|
||||
})
|
||||
|
|
|
|||
12
action.yml
12
action.yml
|
|
@ -21,6 +21,18 @@ inputs:
|
|||
proxy-url:
|
||||
description: "Proxy URL for comment creation"
|
||||
required: false
|
||||
status:
|
||||
description: "Provide a job status for status headers."
|
||||
required: false
|
||||
message-success:
|
||||
description: "Override the message when a run is successful."
|
||||
required: false
|
||||
message-failure:
|
||||
description: "Override the message when a run fails."
|
||||
required: false
|
||||
message-cancelled:
|
||||
description: "Override the message when a run is cancelled."
|
||||
required: false
|
||||
outputs:
|
||||
comment-created:
|
||||
description: "Whether a comment was created."
|
||||
|
|
|
|||
72
dist/index.js
vendored
72
dist/index.js
vendored
|
|
@ -38,49 +38,73 @@ const github = __importStar(__nccwpck_require__(5438));
|
|||
const http_client_1 = __nccwpck_require__(6255);
|
||||
const promises_1 = __importDefault(__nccwpck_require__(3977));
|
||||
const getIssueNumberFromCommitPullsList = (commitPullsList) => (commitPullsList.length ? commitPullsList[0].number : null);
|
||||
const createCommentProxy = async (params) => {
|
||||
async function createCommentProxy(params) {
|
||||
const { repoToken, owner, repo, issueNumber, body, commentId, proxyUrl } = params;
|
||||
const http = new http_client_1.HttpClient('http-client-add-pr-comment');
|
||||
const response = await http.postJson(`${proxyUrl}/repos/${owner}/${repo}/issues/${issueNumber}/comments`, { comment_id: commentId, body }, {
|
||||
['temporary-github-token']: repoToken,
|
||||
});
|
||||
return response.result;
|
||||
};
|
||||
const getExistingCommentId = (comments, messageId) => {
|
||||
}
|
||||
function getExistingCommentId(comments, messageId) {
|
||||
const found = comments.find(({ body }) => {
|
||||
var _a;
|
||||
return ((_a = body === null || body === void 0 ? void 0 : body.search(messageId)) !== null && _a !== void 0 ? _a : -1) > -1;
|
||||
});
|
||||
return found === null || found === void 0 ? void 0 : found.id;
|
||||
};
|
||||
const getInputs = () => {
|
||||
}
|
||||
async function getInputs() {
|
||||
const messageId = core.getInput('message-id');
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message: core.getInput('message'),
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
messagePath: core.getInput('message-path'),
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken: core.getInput('repo-token') || process.env['GITHUB_TOKEN'],
|
||||
};
|
||||
};
|
||||
const run = async () => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, messagePath, repoToken, proxyUrl } = getInputs();
|
||||
const messageIdComment = `<!-- ${messageId} -->`;
|
||||
const messageInput = core.getInput('message');
|
||||
const messagePath = core.getInput('message-path');
|
||||
const repoToken = core.getInput('repo-token') || process.env['GITHUB_TOKEN'];
|
||||
const status = core.getInput('status');
|
||||
if (!repoToken) {
|
||||
throw new Error('no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable');
|
||||
}
|
||||
if (message && messagePath) {
|
||||
if (messageInput && messagePath) {
|
||||
throw new Error('must specify only one, message or message-path');
|
||||
}
|
||||
let messageText = message;
|
||||
let message;
|
||||
if (messagePath) {
|
||||
messageText = await promises_1.default.readFile(messagePath, { encoding: 'utf8' });
|
||||
message = await promises_1.default.readFile(messagePath, { encoding: 'utf8' });
|
||||
}
|
||||
if (!messageText) {
|
||||
throw new Error('could not get message text, check your message-path');
|
||||
else {
|
||||
message = messageInput;
|
||||
}
|
||||
const messageSuccess = core.getInput(`message-success`);
|
||||
const messageFailure = core.getInput(`message-failure`);
|
||||
const messageCancelled = core.getInput(`message-cancelled`);
|
||||
if ((messageSuccess || messageFailure || messageCancelled) && !status) {
|
||||
throw new Error('to use a status message you must provide a status input');
|
||||
}
|
||||
if (status) {
|
||||
if (status === 'success' && messageSuccess) {
|
||||
message = messageSuccess;
|
||||
}
|
||||
if (status === 'failure' && messageFailure) {
|
||||
message = messageFailure;
|
||||
}
|
||||
if (status === 'cancelled' && messageCancelled) {
|
||||
message = messageCancelled;
|
||||
}
|
||||
}
|
||||
if (!message) {
|
||||
throw new Error('no message, check your message inputs');
|
||||
}
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message,
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken,
|
||||
status,
|
||||
};
|
||||
}
|
||||
const run = async () => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, repoToken, proxyUrl } = await getInputs();
|
||||
const messageIdComment = `<!-- ${messageId} -->`;
|
||||
const { payload: { pull_request: pullRequest, issue, repository }, sha: commitSha, } = github.context;
|
||||
if (!repository) {
|
||||
core.info('unable to determine repository from request type');
|
||||
|
|
@ -130,7 +154,7 @@ const run = async () => {
|
|||
}
|
||||
}
|
||||
let comment;
|
||||
const body = `${messageIdComment}\n\n${messageText}`;
|
||||
const body = `${messageIdComment}\n\n${message}`;
|
||||
if (proxyUrl) {
|
||||
comment = await createCommentProxy({
|
||||
commentId: existingCommentId,
|
||||
|
|
|
|||
2
dist/index.js.map
vendored
2
dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
108
file.json
Normal file
108
file.json
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
{
|
||||
"POWERSHELL_DISTRIBUTION_CHANNEL": "GitHub-Actions-ubuntu20",
|
||||
"GOROOT_1_19_X64": "/opt/hostedtoolcache/go/1.19.2/x64",
|
||||
"CONDA": "/usr/share/miniconda",
|
||||
"PERFLOG_LOCATION_SETTING": "RUNNER_PERFLOG",
|
||||
"DOTNET_SKIP_FIRST_TIME_EXPERIENCE": "1",
|
||||
"JAVA_HOME_17_X64": "/usr/lib/jvm/temurin-17-jdk-amd64",
|
||||
"GOROOT_1_18_X64": "/opt/hostedtoolcache/go/1.18.7/x64",
|
||||
"GOROOT_1_17_X64": "/opt/hostedtoolcache/go/1.17.13/x64",
|
||||
"XDG_RUNTIME_DIR": "/run/user/1001",
|
||||
"RUNNER_TRACKING_ID": "github_62467ebe-e6ca-495e-8b05-f84807e1e273",
|
||||
"ANDROID_HOME": "/usr/local/lib/android/sdk",
|
||||
"JAVA_HOME": "/usr/lib/jvm/temurin-11-jdk-amd64",
|
||||
"GECKOWEBDRIVER": "/usr/local/share/gecko_driver",
|
||||
"ANDROID_NDK_LATEST_HOME": "/usr/local/lib/android/sdk/ndk/25.1.8937393",
|
||||
"DEPLOYMENT_BASEPATH": "/opt/runner",
|
||||
"JAVA_HOME_11_X64": "/usr/lib/jvm/temurin-11-jdk-amd64",
|
||||
"HOMEBREW_CLEANUP_PERIODIC_FULL_DAYS": "3650",
|
||||
"CHROMEWEBDRIVER": "/usr/local/share/chrome_driver",
|
||||
"DEBIAN_FRONTEND": "noninteractive",
|
||||
"STATS_RDCL": "true",
|
||||
"ImageVersion": "20221027.1",
|
||||
"INVOCATION_ID": "5351e7c0520b4f1097f40d5c672f3184",
|
||||
"DOTNET_NOLOGO": "1",
|
||||
"ANT_HOME": "/usr/share/ant",
|
||||
"RUNNER_PERFLOG": "/home/runner/perflog",
|
||||
"JAVA_HOME_8_X64": "/usr/lib/jvm/temurin-8-jdk-amd64",
|
||||
"SGX_AESM_ADDR": "1",
|
||||
"XDG_CONFIG_HOME": "/home/runner/.config",
|
||||
"ANDROID_NDK": "/usr/local/lib/android/sdk/ndk/25.1.8937393",
|
||||
"CI": "true",
|
||||
"NVM_DIR": "/home/runner/.nvm",
|
||||
"ImageOS": "ubuntu20",
|
||||
"GRADLE_HOME": "/usr/share/gradle-7.5.1",
|
||||
"ACCEPT_EULA": "Y",
|
||||
"STATS_KEEPALIVE": "false",
|
||||
"DOTNET_MULTILEVEL_LOOKUP": "0",
|
||||
"CHROME_BIN": "/usr/bin/google-chrome",
|
||||
"GRAALVM_11_ROOT": "/usr/local/graalvm/graalvm-ce-java11-22.3.0",
|
||||
"HOMEBREW_NO_AUTO_UPDATE": "1",
|
||||
"ANDROID_NDK_ROOT": "/usr/local/lib/android/sdk/ndk/25.1.8937393",
|
||||
"LANG": "C.UTF-8",
|
||||
"JOURNAL_STREAM": "8:23784",
|
||||
"ANDROID_SDK_ROOT": "/usr/local/lib/android/sdk",
|
||||
"LEIN_JAR": "/usr/local/lib/lein/self-installs/leiningen-2.9.10-standalone.jar",
|
||||
"RUNNER_USER": "runner",
|
||||
"AZURE_EXTENSION_DIR": "/opt/az/azcliextensions",
|
||||
"PATH": "/opt/hostedtoolcache/node/16.18.0/x64/bin:/home/runner/.local/bin:/opt/pipx_bin:/home/runner/.cargo/bin:/home/runner/.config/composer/vendor/bin:/usr/local/.ghcup/bin:/home/runner/.dotnet/tools:/snap/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin",
|
||||
"USER": "runner",
|
||||
"HOME": "/home/runner",
|
||||
"ANDROID_NDK_HOME": "/usr/local/lib/android/sdk/ndk/25.1.8937393",
|
||||
"SWIFT_PATH": "/usr/share/swift/usr/bin",
|
||||
"VCPKG_INSTALLATION_ROOT": "/usr/local/share/vcpkg",
|
||||
"EDGEWEBDRIVER": "/usr/local/share/edge_driver",
|
||||
"AGENT_TOOLSDIRECTORY": "/opt/hostedtoolcache",
|
||||
"LEIN_HOME": "/usr/local/lib/lein",
|
||||
"STATS_PFS": "true",
|
||||
"GITHUB_ACTIONS": "true",
|
||||
"PIPX_HOME": "/opt/pipx",
|
||||
"SELENIUM_JAR_PATH": "/usr/share/java/selenium-server.jar",
|
||||
"BOOTSTRAP_HASKELL_NONINTERACTIVE": "1",
|
||||
"RUNNER_TOOL_CACHE": "/opt/hostedtoolcache",
|
||||
"PIPX_BIN_DIR": "/opt/pipx_bin",
|
||||
"INPUT_MESSAGE": "**STATUS**\nsuccess\n",
|
||||
"INPUT_REPO-TOKEN": "***",
|
||||
"INPUT_MESSAGE-PATH": "",
|
||||
"INPUT_MESSAGE-ID": "add-pr-comment",
|
||||
"INPUT_ALLOW-REPEATS": "false",
|
||||
"INPUT_PROXY-URL": "",
|
||||
"GITHUB_JOB": "dogfood",
|
||||
"GITHUB_REF": "refs/pull/62/merge",
|
||||
"GITHUB_SHA": "5dc16813ec0548c00ae0354f28b6189dcd149428",
|
||||
"GITHUB_REPOSITORY": "mshick/add-pr-comment",
|
||||
"GITHUB_REPOSITORY_OWNER": "mshick",
|
||||
"GITHUB_RUN_ID": "3414492873",
|
||||
"GITHUB_RUN_NUMBER": "13",
|
||||
"GITHUB_RETENTION_DAYS": "90",
|
||||
"GITHUB_RUN_ATTEMPT": "1",
|
||||
"GITHUB_ACTOR": "mshick",
|
||||
"GITHUB_TRIGGERING_ACTOR": "mshick",
|
||||
"GITHUB_WORKFLOW": "ci",
|
||||
"GITHUB_HEAD_REF": "status",
|
||||
"GITHUB_BASE_REF": "main",
|
||||
"GITHUB_EVENT_NAME": "pull_request",
|
||||
"GITHUB_SERVER_URL": "https://github.com",
|
||||
"GITHUB_API_URL": "https://api.github.com",
|
||||
"GITHUB_GRAPHQL_URL": "https://api.github.com/graphql",
|
||||
"GITHUB_REF_NAME": "62/merge",
|
||||
"GITHUB_REF_PROTECTED": "false",
|
||||
"GITHUB_REF_TYPE": "branch",
|
||||
"GITHUB_WORKSPACE": "/home/runner/work/add-pr-comment/add-pr-comment",
|
||||
"GITHUB_ACTION": "__self",
|
||||
"GITHUB_EVENT_PATH": "/home/runner/work/_temp/_github_workflow/event.json",
|
||||
"GITHUB_ACTION_REPOSITORY": "",
|
||||
"GITHUB_ACTION_REF": "",
|
||||
"GITHUB_PATH": "/home/runner/work/_temp/_runner_file_commands/add_path_d8db3eba-2bf0-4bf3-91b7-11870ad9cdbb",
|
||||
"GITHUB_ENV": "/home/runner/work/_temp/_runner_file_commands/set_env_d8db3eba-2bf0-4bf3-91b7-11870ad9cdbb",
|
||||
"GITHUB_STEP_SUMMARY": "/home/runner/work/_temp/_runner_file_commands/step_summary_d8db3eba-2bf0-4bf3-91b7-11870ad9cdbb",
|
||||
"GITHUB_STATE": "/home/runner/work/_temp/_runner_file_commands/save_state_d8db3eba-2bf0-4bf3-91b7-11870ad9cdbb",
|
||||
"GITHUB_OUTPUT": "/home/runner/work/_temp/_runner_file_commands/set_output_d8db3eba-2bf0-4bf3-91b7-11870ad9cdbb",
|
||||
"RUNNER_OS": "Linux",
|
||||
"RUNNER_ARCH": "X64",
|
||||
"RUNNER_NAME": "Hosted Agent",
|
||||
"RUNNER_TEMP": "/home/runner/work/_temp",
|
||||
"RUNNER_WORKSPACE": "/home/runner/work/add-pr-comment",
|
||||
"ACTIONS_RUNTIME_URL": "https://pipelines.actions.githubusercontent.com/EAM1G72fAPPgq08iwwmZdnyKsgvgCWrZJHS1pztwsfVXAQXHOT/",
|
||||
"ACTIONS_CACHE_URL": "https://artifactcache.actions.githubusercontent.com/EAM1G72fAPPgq08iwwmZdnyKsgvgCWrZJHS1pztwsfVXAQXHOT/"
|
||||
}
|
||||
72
lib/main.js
72
lib/main.js
|
|
@ -31,49 +31,73 @@ const github = __importStar(require("@actions/github"));
|
|||
const http_client_1 = require("@actions/http-client");
|
||||
const promises_1 = __importDefault(require("node:fs/promises"));
|
||||
const getIssueNumberFromCommitPullsList = (commitPullsList) => (commitPullsList.length ? commitPullsList[0].number : null);
|
||||
const createCommentProxy = async (params) => {
|
||||
async function createCommentProxy(params) {
|
||||
const { repoToken, owner, repo, issueNumber, body, commentId, proxyUrl } = params;
|
||||
const http = new http_client_1.HttpClient('http-client-add-pr-comment');
|
||||
const response = await http.postJson(`${proxyUrl}/repos/${owner}/${repo}/issues/${issueNumber}/comments`, { comment_id: commentId, body }, {
|
||||
['temporary-github-token']: repoToken,
|
||||
});
|
||||
return response.result;
|
||||
};
|
||||
const getExistingCommentId = (comments, messageId) => {
|
||||
}
|
||||
function getExistingCommentId(comments, messageId) {
|
||||
const found = comments.find(({ body }) => {
|
||||
var _a;
|
||||
return ((_a = body === null || body === void 0 ? void 0 : body.search(messageId)) !== null && _a !== void 0 ? _a : -1) > -1;
|
||||
});
|
||||
return found === null || found === void 0 ? void 0 : found.id;
|
||||
};
|
||||
const getInputs = () => {
|
||||
}
|
||||
async function getInputs() {
|
||||
const messageId = core.getInput('message-id');
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message: core.getInput('message'),
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
messagePath: core.getInput('message-path'),
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken: core.getInput('repo-token') || process.env['GITHUB_TOKEN'],
|
||||
};
|
||||
};
|
||||
const run = async () => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, messagePath, repoToken, proxyUrl } = getInputs();
|
||||
const messageIdComment = `<!-- ${messageId} -->`;
|
||||
const messageInput = core.getInput('message');
|
||||
const messagePath = core.getInput('message-path');
|
||||
const repoToken = core.getInput('repo-token') || process.env['GITHUB_TOKEN'];
|
||||
const status = core.getInput('status');
|
||||
if (!repoToken) {
|
||||
throw new Error('no github token provided, set one with the repo-token input or GITHUB_TOKEN env variable');
|
||||
}
|
||||
if (message && messagePath) {
|
||||
if (messageInput && messagePath) {
|
||||
throw new Error('must specify only one, message or message-path');
|
||||
}
|
||||
let messageText = message;
|
||||
let message;
|
||||
if (messagePath) {
|
||||
messageText = await promises_1.default.readFile(messagePath, { encoding: 'utf8' });
|
||||
message = await promises_1.default.readFile(messagePath, { encoding: 'utf8' });
|
||||
}
|
||||
if (!messageText) {
|
||||
throw new Error('could not get message text, check your message-path');
|
||||
else {
|
||||
message = messageInput;
|
||||
}
|
||||
const messageSuccess = core.getInput(`message-success`);
|
||||
const messageFailure = core.getInput(`message-failure`);
|
||||
const messageCancelled = core.getInput(`message-cancelled`);
|
||||
if ((messageSuccess || messageFailure || messageCancelled) && !status) {
|
||||
throw new Error('to use a status message you must provide a status input');
|
||||
}
|
||||
if (status) {
|
||||
if (status === 'success' && messageSuccess) {
|
||||
message = messageSuccess;
|
||||
}
|
||||
if (status === 'failure' && messageFailure) {
|
||||
message = messageFailure;
|
||||
}
|
||||
if (status === 'cancelled' && messageCancelled) {
|
||||
message = messageCancelled;
|
||||
}
|
||||
}
|
||||
if (!message) {
|
||||
throw new Error('no message, check your message inputs');
|
||||
}
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message,
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken,
|
||||
status,
|
||||
};
|
||||
}
|
||||
const run = async () => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, repoToken, proxyUrl } = await getInputs();
|
||||
const messageIdComment = `<!-- ${messageId} -->`;
|
||||
const { payload: { pull_request: pullRequest, issue, repository }, sha: commitSha, } = github.context;
|
||||
if (!repository) {
|
||||
core.info('unable to determine repository from request type');
|
||||
|
|
@ -123,7 +147,7 @@ const run = async () => {
|
|||
}
|
||||
}
|
||||
let comment;
|
||||
const body = `${messageIdComment}\n\n${messageText}`;
|
||||
const body = `${messageIdComment}\n\n${message}`;
|
||||
if (proxyUrl) {
|
||||
comment = await createCommentProxy({
|
||||
commentId: existingCommentId,
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@
|
|||
"clean": "rm -rf node_modules dist package-lock.json __tests__/runner/**/*",
|
||||
"lint": "eslint src/**/*.ts",
|
||||
"package": "ncc build --source-map",
|
||||
"release": "npm run build && npm run package",
|
||||
"test": "vitest run",
|
||||
"watch": "vitest"
|
||||
},
|
||||
|
|
|
|||
90
src/main.ts
90
src/main.ts
|
|
@ -25,9 +25,9 @@ interface CreateCommentProxyParams {
|
|||
proxyUrl: string
|
||||
}
|
||||
|
||||
const createCommentProxy = async (
|
||||
async function createCommentProxy(
|
||||
params: CreateCommentProxyParams,
|
||||
): Promise<CreateIssueCommentResponseData | null> => {
|
||||
): Promise<CreateIssueCommentResponseData | null> {
|
||||
const { repoToken, owner, repo, issueNumber, body, commentId, proxyUrl } = params
|
||||
|
||||
const http = new HttpClient('http-client-add-pr-comment')
|
||||
|
|
@ -43,10 +43,10 @@ const createCommentProxy = async (
|
|||
return response.result
|
||||
}
|
||||
|
||||
const getExistingCommentId = (
|
||||
function getExistingCommentId(
|
||||
comments: IssuesListCommentsResponseData,
|
||||
messageId: string,
|
||||
): number | undefined => {
|
||||
): number | undefined {
|
||||
const found = comments.find(({ body }) => {
|
||||
return (body?.search(messageId) ?? -1) > -1
|
||||
})
|
||||
|
|
@ -57,29 +57,22 @@ const getExistingCommentId = (
|
|||
interface AddPrCommentInputs {
|
||||
allowRepeats: boolean
|
||||
message?: string
|
||||
messagePath?: string
|
||||
proxyUrl?: string
|
||||
repoToken?: string
|
||||
messageId: string
|
||||
messagePath?: string
|
||||
messageSuccess?: string
|
||||
messageFailure?: string
|
||||
messageCancelled?: string
|
||||
proxyUrl?: string
|
||||
repoToken: string
|
||||
status?: string
|
||||
}
|
||||
|
||||
const getInputs = (): AddPrCommentInputs => {
|
||||
async function getInputs(): Promise<AddPrCommentInputs> {
|
||||
const messageId = core.getInput('message-id')
|
||||
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message: core.getInput('message'),
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
messagePath: core.getInput('message-path'),
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken: core.getInput('repo-token') || process.env['GITHUB_TOKEN'],
|
||||
}
|
||||
}
|
||||
|
||||
const run = async (): Promise<void> => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, messagePath, repoToken, proxyUrl } = getInputs()
|
||||
const messageIdComment = `<!-- ${messageId} -->`
|
||||
const messageInput = core.getInput('message')
|
||||
const messagePath = core.getInput('message-path')
|
||||
const repoToken = core.getInput('repo-token') || process.env['GITHUB_TOKEN']
|
||||
const status = core.getInput('status')
|
||||
|
||||
if (!repoToken) {
|
||||
throw new Error(
|
||||
|
|
@ -87,20 +80,59 @@ const run = async (): Promise<void> => {
|
|||
)
|
||||
}
|
||||
|
||||
if (message && messagePath) {
|
||||
if (messageInput && messagePath) {
|
||||
throw new Error('must specify only one, message or message-path')
|
||||
}
|
||||
|
||||
let messageText = message
|
||||
let message
|
||||
|
||||
if (messagePath) {
|
||||
messageText = await fs.readFile(messagePath, { encoding: 'utf8' })
|
||||
message = await fs.readFile(messagePath, { encoding: 'utf8' })
|
||||
} else {
|
||||
message = messageInput
|
||||
}
|
||||
|
||||
if (!messageText) {
|
||||
throw new Error('could not get message text, check your message-path')
|
||||
const messageSuccess = core.getInput(`message-success`)
|
||||
const messageFailure = core.getInput(`message-failure`)
|
||||
const messageCancelled = core.getInput(`message-cancelled`)
|
||||
|
||||
if ((messageSuccess || messageFailure || messageCancelled) && !status) {
|
||||
throw new Error('to use a status message you must provide a status input')
|
||||
}
|
||||
|
||||
if (status) {
|
||||
if (status === 'success' && messageSuccess) {
|
||||
message = messageSuccess
|
||||
}
|
||||
|
||||
if (status === 'failure' && messageFailure) {
|
||||
message = messageFailure
|
||||
}
|
||||
|
||||
if (status === 'cancelled' && messageCancelled) {
|
||||
message = messageCancelled
|
||||
}
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
throw new Error('no message, check your message inputs')
|
||||
}
|
||||
|
||||
return {
|
||||
allowRepeats: Boolean(core.getInput('allow-repeats') === 'true'),
|
||||
message,
|
||||
messageId: messageId === '' ? 'add-pr-comment' : messageId,
|
||||
proxyUrl: core.getInput('proxy-url').replace(/\/$/, ''),
|
||||
repoToken,
|
||||
status,
|
||||
}
|
||||
}
|
||||
|
||||
const run = async (): Promise<void> => {
|
||||
try {
|
||||
const { allowRepeats, message, messageId, repoToken, proxyUrl } = await getInputs()
|
||||
const messageIdComment = `<!-- ${messageId} -->`
|
||||
|
||||
const {
|
||||
payload: { pull_request: pullRequest, issue, repository },
|
||||
sha: commitSha,
|
||||
|
|
@ -166,7 +198,7 @@ const run = async (): Promise<void> => {
|
|||
}
|
||||
|
||||
let comment: CreateIssueCommentResponseData | null | undefined
|
||||
const body = `${messageIdComment}\n\n${messageText}`
|
||||
const body = `${messageIdComment}\n\n${message}`
|
||||
|
||||
if (proxyUrl) {
|
||||
comment = await createCommentProxy({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue