πŸ›β™»οΈ paging support & use graphql (#471)

* πŸ›β™»οΈ paging support & use graphql

* πŸš€ build

* 🚨 ignore eslint
This commit is contained in:
marocchino 2021-10-20 22:19:58 +09:00 β€’ committed by GitHub
parent 1bffac7054
commit 1a8e0448ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 490 additions and 114 deletions

82
lib/comment.js generated
View file

@ -33,22 +33,78 @@ const core = __importStar(require("@actions/core"));
function headerComment(header) {
return `<!-- Sticky Pull Request Comment${header} -->`;
}
function findPreviousComment(octokit, repo, issue_number, header) {
function findPreviousComment(octokit, repo, number, header) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
return __awaiter(this, void 0, void 0, function* () {
const { viewer } = yield octokit.graphql("query { viewer { login } }");
const { data: comments } = yield octokit.rest.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number }));
let after = null;
let hasNextPage = true;
const h = headerComment(header);
return comments.find(comment => { var _a, _b; return ((_a = comment.user) === null || _a === void 0 ? void 0 : _a.login) === viewer.login && ((_b = comment.body) === null || _b === void 0 ? void 0 : _b.includes(h)); });
while (hasNextPage) {
const data = yield octokit.graphql(`
query($repo: String! $owner: String! $number: Int! $after: String) {
viewer { login }
repository(name: $repo owner: $owner) {
pullRequest(number: $number) {
comments(first: 100 after: $after) {
nodes {
id
author {
login
}
isMinimized
body
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}
`, Object.assign(Object.assign({}, repo), { after, number }));
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const viewer = data.viewer;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const repository = data.repository;
const target = (_c = (_b = (_a = repository.pullRequest) === null || _a === void 0 ? void 0 : _a.comments) === null || _b === void 0 ? void 0 : _b.nodes) === null || _c === void 0 ? void 0 : _c.find((node) => {
var _a, _b;
return ((_a = node === null || node === void 0 ? void 0 : node.author) === null || _a === void 0 ? void 0 : _a.login) === viewer.login &&
!(node === null || node === void 0 ? void 0 : node.isMinimized) &&
((_b = node === null || node === void 0 ? void 0 : node.body) === null || _b === void 0 ? void 0 : _b.includes(h));
});
if (target) {
return target;
}
after = (_f = (_e = (_d = repository.pullRequest) === null || _d === void 0 ? void 0 : _d.comments) === null || _e === void 0 ? void 0 : _e.pageInfo) === null || _f === void 0 ? void 0 : _f.endCursor;
hasNextPage =
(_k = (_j = (_h = (_g = repository.pullRequest) === null || _g === void 0 ? void 0 : _g.comments) === null || _h === void 0 ? void 0 : _h.pageInfo) === null || _j === void 0 ? void 0 : _j.hasNextPage) !== null && _k !== void 0 ? _k : false;
}
return undefined;
});
}
exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body, header, previousBody) {
function updateComment(octokit, id, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
return core.warning("Comment body cannot be blank");
yield octokit.rest.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}` }));
yield octokit.graphql(`
mutation($input: UpdateIssueCommentInput!) {
updateIssueComment(input: $input) {
issueComment {
id
body
}
}
}
`, {
input: {
id,
body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}`
}
});
});
}
exports.updateComment = updateComment;
@ -62,9 +118,15 @@ function createComment(octokit, repo, issue_number, body, header, previousBody)
});
}
exports.createComment = createComment;
function deleteComment(octokit, repo, comment_id) {
function deleteComment(octokit, id) {
return __awaiter(this, void 0, void 0, function* () {
yield octokit.rest.issues.deleteComment(Object.assign(Object.assign({}, repo), { comment_id }));
yield octokit.graphql(`
mutation($id: ID!) {
deleteIssueComment(input: { id: $id }) {
clientMutationId
}
}
`, { id });
});
}
exports.deleteComment = deleteComment;

6
lib/main.js generated
View file

@ -52,16 +52,16 @@ function run() {
return;
}
if (config_1.deleteOldComment) {
yield (0, comment_1.deleteComment)(octokit, config_1.repo, previous.id);
yield (0, comment_1.deleteComment)(octokit, previous.id);
return;
}
const previousBody = (0, comment_1.getBodyOf)(previous, config_1.append, config_1.hideDetails);
if (config_1.recreate) {
yield (0, comment_1.deleteComment)(octokit, config_1.repo, previous.id);
yield (0, comment_1.deleteComment)(octokit, previous.id);
yield (0, comment_1.createComment)(octokit, config_1.repo, config_1.pullRequestNumber, config_1.body, config_1.header, previousBody);
return;
}
yield (0, comment_1.updateComment)(octokit, config_1.repo, previous.id, config_1.body, config_1.header, previousBody);
yield (0, comment_1.updateComment)(octokit, previous.id, config_1.body, config_1.header, previousBody);
}
catch (error) {
if (error instanceof Error) {