mirror of
https://github.com/mshick/add-pr-comment.git
synced 2026-01-02 23:00:02 +11:00
Issue number (#64)
* Major code reorg * Add issue arg * Paginate existing comment list
This commit is contained in:
parent
e8076c64f7
commit
8645f3f0ea
17 changed files with 2187 additions and 1968 deletions
136
lib/main.js
136
lib/main.js
|
|
@ -22,134 +22,45 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const core = __importStar(require("@actions/core"));
|
||||
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);
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
async function getInputs() {
|
||||
const messageId = core.getInput('message-id');
|
||||
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 (messageInput && messagePath) {
|
||||
throw new Error('must specify only one, message or message-path');
|
||||
}
|
||||
let message;
|
||||
if (messagePath) {
|
||||
message = await promises_1.default.readFile(messagePath, { encoding: 'utf8' });
|
||||
}
|
||||
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 comments_1 = require("./comments");
|
||||
const config_1 = require("./config");
|
||||
const issues_1 = require("./issues");
|
||||
const proxy_1 = require("./proxy");
|
||||
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');
|
||||
core.setOutput('comment-created', 'false');
|
||||
return;
|
||||
}
|
||||
const { full_name: repoFullName } = repository;
|
||||
if (!repoFullName) {
|
||||
core.info('repository is missing a full_name property... weird');
|
||||
core.setOutput('comment-created', 'false');
|
||||
return;
|
||||
}
|
||||
const [owner, repo] = repoFullName.split('/');
|
||||
const { allowRepeats, message, messageId, repoToken, proxyUrl, issue, pullRequestNumber, commitSha, repo, owner, } = await (0, config_1.getInputs)();
|
||||
const octokit = github.getOctokit(repoToken);
|
||||
let issueNumber;
|
||||
if (issue && issue.number) {
|
||||
issueNumber = issue.number;
|
||||
if (issue) {
|
||||
issueNumber = issue;
|
||||
}
|
||||
else if (pullRequest && pullRequest.number) {
|
||||
issueNumber = pullRequest.number;
|
||||
else if (pullRequestNumber) {
|
||||
issueNumber = pullRequestNumber;
|
||||
}
|
||||
else {
|
||||
// If this is not a pull request, attempt to find a PR matching the sha
|
||||
const commitPullsList = await octokit.rest.repos.listPullRequestsAssociatedWithCommit({
|
||||
owner,
|
||||
repo,
|
||||
commit_sha: commitSha,
|
||||
});
|
||||
issueNumber = commitPullsList.data && getIssueNumberFromCommitPullsList(commitPullsList.data);
|
||||
issueNumber = await (0, issues_1.getIssueNumberFromCommitPullsList)(octokit, owner, repo, commitSha);
|
||||
}
|
||||
if (!issueNumber) {
|
||||
core.info('this action only works on issues and pull_request events or other commits associated with a pull');
|
||||
core.info('no issue number found, use a pull_request event, a pull event, or provide an issue input');
|
||||
core.setOutput('comment-created', 'false');
|
||||
return;
|
||||
}
|
||||
let existingCommentId;
|
||||
if (!allowRepeats) {
|
||||
core.debug('repeat comments are disallowed, checking for existing');
|
||||
const { data: comments } = await octokit.rest.issues.listComments({
|
||||
owner,
|
||||
repo,
|
||||
issue_number: issueNumber,
|
||||
});
|
||||
existingCommentId = getExistingCommentId(comments, messageIdComment);
|
||||
existingCommentId = await (0, comments_1.getExistingCommentId)(octokit, owner, repo, issueNumber, messageId);
|
||||
if (existingCommentId) {
|
||||
core.debug(`existing comment found with id: ${existingCommentId}`);
|
||||
}
|
||||
}
|
||||
let comment;
|
||||
const body = `${messageIdComment}\n\n${message}`;
|
||||
const body = `${messageId}\n\n${message}`;
|
||||
if (proxyUrl) {
|
||||
comment = await createCommentProxy({
|
||||
comment = await (0, proxy_1.createCommentProxy)({
|
||||
commentId: existingCommentId,
|
||||
owner,
|
||||
repo,
|
||||
|
|
@ -161,23 +72,11 @@ const run = async () => {
|
|||
core.setOutput(existingCommentId ? 'comment-updated' : 'comment-created', 'true');
|
||||
}
|
||||
else if (existingCommentId) {
|
||||
const updatedComment = await octokit.rest.issues.updateComment({
|
||||
comment_id: existingCommentId,
|
||||
owner,
|
||||
repo,
|
||||
body,
|
||||
});
|
||||
comment = updatedComment.data;
|
||||
comment = await (0, comments_1.updateComment)(octokit, owner, repo, existingCommentId, body);
|
||||
core.setOutput('comment-updated', 'true');
|
||||
}
|
||||
else {
|
||||
const createdComment = await octokit.rest.issues.createComment({
|
||||
issue_number: issueNumber,
|
||||
owner,
|
||||
repo,
|
||||
body,
|
||||
});
|
||||
comment = createdComment.data;
|
||||
comment = await (0, comments_1.createComment)(octokit, owner, repo, issueNumber, body);
|
||||
core.setOutput('comment-created', 'true');
|
||||
}
|
||||
if (comment) {
|
||||
|
|
@ -192,9 +91,6 @@ const run = async () => {
|
|||
if (err instanceof Error) {
|
||||
core.setFailed(err.message);
|
||||
}
|
||||
else {
|
||||
core.setFailed('unknown failure');
|
||||
}
|
||||
}
|
||||
};
|
||||
// Don't auto-execute in the test environment
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue