mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2026-04-07 06:59:25 +00:00
fix: add context: globalThis to rollup config to suppress this-is-undefined warning
Agent-Logs-Url: https://github.com/marocchino/sticky-pull-request-comment/sessions/d06505e0-ed2a-4dff-841c-baf27a74d54e Co-authored-by: marocchino <128431+marocchino@users.noreply.github.com>
This commit is contained in:
parent
c4fbe28317
commit
6e06a5b4b9
11 changed files with 49 additions and 583 deletions
117
dist/comment.js
generated
vendored
117
dist/comment.js
generated
vendored
|
|
@ -1,117 +0,0 @@
|
|||
import * as core from "@actions/core";
|
||||
function headerComment(header) {
|
||||
return `<!-- Sticky Pull Request Comment${header} -->`;
|
||||
}
|
||||
function bodyWithHeader(body, header) {
|
||||
return `${body}\n${headerComment(header)}`;
|
||||
}
|
||||
function bodyWithoutHeader(body, header) {
|
||||
return body.replace(`\n${headerComment(header)}`, "");
|
||||
}
|
||||
export async function findPreviousComment(octokit, repo, number, header) {
|
||||
let after = null;
|
||||
let hasNextPage = true;
|
||||
const h = headerComment(header);
|
||||
while (hasNextPage) {
|
||||
const data = await 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`, { ...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 = repository.pullRequest?.comments?.nodes?.find((node) => node?.author?.login === viewer.login.replace("[bot]", "") &&
|
||||
!node?.isMinimized &&
|
||||
node?.body?.includes(h));
|
||||
if (target) {
|
||||
return target;
|
||||
}
|
||||
after = repository.pullRequest?.comments?.pageInfo?.endCursor;
|
||||
hasNextPage = repository.pullRequest?.comments?.pageInfo?.hasNextPage ?? false;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
export async function updateComment(octokit, id, body, header, previousBody) {
|
||||
if (!body && !previousBody)
|
||||
return core.warning("Comment body cannot be blank");
|
||||
const rawPreviousBody = previousBody ? bodyWithoutHeader(previousBody, header) : "";
|
||||
await octokit.graphql(`
|
||||
mutation($input: UpdateIssueCommentInput!) {
|
||||
updateIssueComment(input: $input) {
|
||||
issueComment {
|
||||
id
|
||||
body
|
||||
}
|
||||
}
|
||||
}
|
||||
`, {
|
||||
input: {
|
||||
id,
|
||||
body: previousBody
|
||||
? bodyWithHeader(`${rawPreviousBody}\n${body}`, header)
|
||||
: bodyWithHeader(body, header),
|
||||
},
|
||||
});
|
||||
}
|
||||
export async function createComment(octokit, repo, issue_number, body, header, previousBody) {
|
||||
if (!body && !previousBody) {
|
||||
core.warning("Comment body cannot be blank");
|
||||
return;
|
||||
}
|
||||
return await octokit.rest.issues.createComment({
|
||||
...repo,
|
||||
issue_number,
|
||||
body: previousBody ? `${previousBody}\n${body}` : bodyWithHeader(body, header),
|
||||
});
|
||||
}
|
||||
export async function deleteComment(octokit, id) {
|
||||
await octokit.graphql(`
|
||||
mutation($id: ID!) {
|
||||
deleteIssueComment(input: { id: $id }) {
|
||||
clientMutationId
|
||||
}
|
||||
}
|
||||
`, { id });
|
||||
}
|
||||
export async function minimizeComment(octokit, subjectId, classifier) {
|
||||
await octokit.graphql(`
|
||||
mutation($input: MinimizeCommentInput!) {
|
||||
minimizeComment(input: $input) {
|
||||
clientMutationId
|
||||
}
|
||||
}
|
||||
`, { input: { subjectId, classifier } });
|
||||
}
|
||||
export function getBodyOf(previous, append, hideDetails) {
|
||||
if (!append) {
|
||||
return undefined;
|
||||
}
|
||||
if (!hideDetails || !previous.body) {
|
||||
return previous.body;
|
||||
}
|
||||
return previous.body.replace(/(<details.*?)\s*\bopen\b(.*>)/g, "$1$2");
|
||||
}
|
||||
export function commentsEqual(body, previous, header) {
|
||||
const newBody = bodyWithHeader(body, header);
|
||||
return newBody === previous;
|
||||
}
|
||||
65
dist/config.js
generated
vendored
65
dist/config.js
generated
vendored
|
|
@ -1,65 +0,0 @@
|
|||
import { readFileSync } from "node:fs";
|
||||
import * as core from "@actions/core";
|
||||
import { context } from "@actions/github";
|
||||
import { create } from "@actions/glob";
|
||||
export const pullRequestNumber = +core.getInput("number_force", { required: false }) ||
|
||||
context?.payload?.pull_request?.number ||
|
||||
+core.getInput("number", { required: false });
|
||||
export const repo = buildRepo();
|
||||
export const header = core.getInput("header", { required: false });
|
||||
export const append = core.getBooleanInput("append", { required: true });
|
||||
export const hideDetails = core.getBooleanInput("hide_details", {
|
||||
required: true,
|
||||
});
|
||||
export const recreate = core.getBooleanInput("recreate", { required: true });
|
||||
export const hideAndRecreate = core.getBooleanInput("hide_and_recreate", {
|
||||
required: true,
|
||||
});
|
||||
export const hideClassify = core.getInput("hide_classify", {
|
||||
required: true,
|
||||
});
|
||||
export const deleteOldComment = core.getBooleanInput("delete", { required: true });
|
||||
export const onlyCreateComment = core.getBooleanInput("only_create", {
|
||||
required: true,
|
||||
});
|
||||
export const onlyUpdateComment = core.getBooleanInput("only_update", {
|
||||
required: true,
|
||||
});
|
||||
export const skipUnchanged = core.getBooleanInput("skip_unchanged", {
|
||||
required: true,
|
||||
});
|
||||
export const hideOldComment = core.getBooleanInput("hide", { required: true });
|
||||
export const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
||||
export const ignoreEmpty = core.getBooleanInput("ignore_empty", {
|
||||
required: true,
|
||||
});
|
||||
function buildRepo() {
|
||||
return {
|
||||
owner: core.getInput("owner", { required: false }) || context.repo.owner,
|
||||
repo: core.getInput("repo", { required: false }) || context.repo.repo,
|
||||
};
|
||||
}
|
||||
export async function getBody() {
|
||||
const pathInput = core.getMultilineInput("path", { required: false });
|
||||
const followSymbolicLinks = core.getBooleanInput("follow_symbolic_links", {
|
||||
required: true,
|
||||
});
|
||||
if (pathInput && pathInput.length > 0) {
|
||||
try {
|
||||
const globber = await create(pathInput.join("\n"), {
|
||||
followSymbolicLinks,
|
||||
matchDirectories: false,
|
||||
});
|
||||
return (await globber.glob()).map(path => readFileSync(path, "utf-8")).join("\n");
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return core.getInput("message", { required: false });
|
||||
}
|
||||
}
|
||||
85
dist/index.js
generated
vendored
85
dist/index.js
generated
vendored
|
|
@ -27925,7 +27925,7 @@ function requireUndici () {
|
|||
var undiciExports = requireUndici();
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -27986,7 +27986,7 @@ var MediaTypes;
|
|||
HttpCodes.GatewayTimeout
|
||||
];
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -27996,7 +27996,7 @@ var MediaTypes;
|
|||
});
|
||||
};
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28006,7 +28006,7 @@ var MediaTypes;
|
|||
});
|
||||
};
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28017,7 +28017,7 @@ var MediaTypes;
|
|||
};
|
||||
const { access, appendFile, writeFile } = fs.promises;
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28031,7 +28031,7 @@ const { chmod, copyFile, lstat, mkdir, open, readdir, rename, rm, rmdir, stat, s
|
|||
process.platform === 'win32';
|
||||
fs__namespace.constants.O_RDONLY;
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28041,7 +28041,7 @@ fs__namespace.constants.O_RDONLY;
|
|||
});
|
||||
};
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28053,7 +28053,7 @@ fs__namespace.constants.O_RDONLY;
|
|||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
process.platform === 'win32';
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28063,7 +28063,7 @@ process.platform === 'win32';
|
|||
});
|
||||
};
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -28075,7 +28075,7 @@ process.platform === 'win32';
|
|||
os.platform();
|
||||
os.arch();
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -29114,7 +29114,7 @@ function requireLib () {
|
|||
|
||||
var libExports = requireLib();
|
||||
|
||||
var __awaiter$2 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
var __awaiter$2 = (globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -33728,7 +33728,7 @@ function requireBraceExpansion () {
|
|||
var y = numeric(n[1]);
|
||||
var width = Math.max(n[0].length, n[1].length);
|
||||
var incr = n.length == 3
|
||||
? Math.abs(numeric(n[2]))
|
||||
? Math.max(Math.abs(numeric(n[2])), 1)
|
||||
: 1;
|
||||
var test = lte;
|
||||
var reverse = y < x;
|
||||
|
|
@ -35108,7 +35108,7 @@ class SearchState {
|
|||
}
|
||||
}
|
||||
|
||||
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
var __awaiter$1 = (globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -35117,15 +35117,15 @@ var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _argu
|
|||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
var __asyncValues = (undefined && undefined.__asyncValues) || function (o) {
|
||||
var __asyncValues = (globalThis && globalThis.__asyncValues) || function (o) {
|
||||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
||||
var m = o[Symbol.asyncIterator], i;
|
||||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
||||
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
||||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
||||
};
|
||||
var __await = (undefined && undefined.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); };
|
||||
var __asyncGenerator = (undefined && undefined.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
||||
var __await = (globalThis && globalThis.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); };
|
||||
var __asyncGenerator = (globalThis && globalThis.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
||||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
||||
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
||||
return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
|
||||
|
|
@ -35319,7 +35319,7 @@ class DefaultGlobber {
|
|||
}
|
||||
}
|
||||
|
||||
(undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
(globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -35328,7 +35328,7 @@ class DefaultGlobber {
|
|||
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
||||
});
|
||||
};
|
||||
(undefined && undefined.__asyncValues) || function (o) {
|
||||
(globalThis && globalThis.__asyncValues) || function (o) {
|
||||
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
||||
var m = o[Symbol.asyncIterator], i;
|
||||
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
||||
|
|
@ -35336,7 +35336,7 @@ class DefaultGlobber {
|
|||
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
||||
};
|
||||
|
||||
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
var __awaiter = (globalThis && globalThis.__awaiter) || function (thisArg, _arguments, P, generator) {
|
||||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
||||
return new (P || (P = Promise))(function (resolve, reject) {
|
||||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
||||
|
|
@ -35357,7 +35357,9 @@ function create(patterns, options) {
|
|||
});
|
||||
}
|
||||
|
||||
const pullRequestNumber = context?.payload?.pull_request?.number || +getInput("number", { required: false });
|
||||
const pullRequestNumber = +getInput("number_force", { required: false }) ||
|
||||
context?.payload?.pull_request?.number ||
|
||||
+getInput("number", { required: false });
|
||||
const repo = buildRepo();
|
||||
const header = getInput("header", { required: false });
|
||||
const append = getBooleanInput("append", { required: true });
|
||||
|
|
@ -35417,35 +35419,42 @@ async function getBody() {
|
|||
}
|
||||
}
|
||||
|
||||
function validateBody(body, deleteOldComment, hideOldComment) {
|
||||
if (!deleteOldComment && !hideOldComment && !body) {
|
||||
throw new Error("Either message or path input is required");
|
||||
}
|
||||
}
|
||||
function validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate) {
|
||||
const exclusiveModes = [
|
||||
["delete", deleteOldComment],
|
||||
["recreate", recreate],
|
||||
["only_create", onlyCreateComment],
|
||||
["only_update", onlyUpdateComment],
|
||||
["hide", hideOldComment],
|
||||
["hide_and_recreate", hideAndRecreate],
|
||||
];
|
||||
const enabledModes = exclusiveModes.filter(([, flag]) => flag).map(([name]) => name);
|
||||
if (enabledModes.length > 1) {
|
||||
const last = enabledModes[enabledModes.length - 1];
|
||||
const rest = enabledModes.slice(0, -1);
|
||||
const joined = enabledModes.length === 2 ? `${rest[0]} and ${last}` : `${rest.join(", ")}, and ${last}`;
|
||||
throw new Error(`${joined} cannot be set to true simultaneously`);
|
||||
}
|
||||
}
|
||||
|
||||
async function run() {
|
||||
if (Number.isNaN(pullRequestNumber) || pullRequestNumber < 1) {
|
||||
info("no pull request numbers given: skip step");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate);
|
||||
const body = await getBody();
|
||||
if (!body && ignoreEmpty) {
|
||||
info("no body given: skip step by ignoreEmpty");
|
||||
return;
|
||||
}
|
||||
if (!deleteOldComment && !hideOldComment && !body) {
|
||||
throw new Error("Either message or path input is required");
|
||||
}
|
||||
if (deleteOldComment && recreate) {
|
||||
throw new Error("delete and recreate cannot be both set to true");
|
||||
}
|
||||
if (deleteOldComment && onlyCreateComment) {
|
||||
throw new Error("delete and only_create cannot be both set to true");
|
||||
}
|
||||
if (deleteOldComment && hideOldComment) {
|
||||
throw new Error("delete and hide cannot be both set to true");
|
||||
}
|
||||
if (onlyCreateComment && onlyUpdateComment) {
|
||||
throw new Error("only_create and only_update cannot be both set to true");
|
||||
}
|
||||
if (hideOldComment && hideAndRecreate) {
|
||||
throw new Error("hide and hide_and_recreate cannot be both set to true");
|
||||
}
|
||||
validateBody(body, deleteOldComment, hideOldComment);
|
||||
const octokit = getOctokit(githubToken);
|
||||
const previous = await findPreviousComment(octokit, repo, pullRequestNumber, header);
|
||||
setOutput("previous_comment_id", previous?.id);
|
||||
|
|
|
|||
2
dist/index.js.map
generated
vendored
2
dist/index.js.map
generated
vendored
File diff suppressed because one or more lines are too long
68
dist/main.js
generated
vendored
68
dist/main.js
generated
vendored
|
|
@ -1,68 +0,0 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as github from "@actions/github";
|
||||
import { commentsEqual, createComment, deleteComment, findPreviousComment, getBodyOf, minimizeComment, updateComment, } from "./comment";
|
||||
import { append, deleteOldComment, getBody, githubToken, header, hideAndRecreate, hideClassify, hideDetails, hideOldComment, ignoreEmpty, onlyCreateComment, onlyUpdateComment, pullRequestNumber, recreate, repo, skipUnchanged, } from "./config";
|
||||
import { validateBody, validateExclusiveModes } from "./validate";
|
||||
async function run() {
|
||||
if (Number.isNaN(pullRequestNumber) || pullRequestNumber < 1) {
|
||||
core.info("no pull request numbers given: skip step");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate);
|
||||
const body = await getBody();
|
||||
if (!body && ignoreEmpty) {
|
||||
core.info("no body given: skip step by ignoreEmpty");
|
||||
return;
|
||||
}
|
||||
validateBody(body, deleteOldComment, hideOldComment);
|
||||
const octokit = github.getOctokit(githubToken);
|
||||
const previous = await findPreviousComment(octokit, repo, pullRequestNumber, header);
|
||||
core.setOutput("previous_comment_id", previous?.id);
|
||||
if (!previous) {
|
||||
if (onlyUpdateComment || hideOldComment || deleteOldComment) {
|
||||
return;
|
||||
}
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
if (onlyCreateComment) {
|
||||
// don't comment anything, user specified only_create and there is an
|
||||
// existing comment, so this is probably a placeholder / introduction one.
|
||||
return;
|
||||
}
|
||||
if (hideOldComment) {
|
||||
await minimizeComment(octokit, previous.id, hideClassify);
|
||||
return;
|
||||
}
|
||||
if (deleteOldComment) {
|
||||
await deleteComment(octokit, previous.id);
|
||||
return;
|
||||
}
|
||||
if (skipUnchanged && commentsEqual(body, previous.body || "", header)) {
|
||||
// don't recreate or update if the message is unchanged
|
||||
return;
|
||||
}
|
||||
const previousBody = getBodyOf({ body: previous.body || "" }, append, hideDetails);
|
||||
if (recreate) {
|
||||
await deleteComment(octokit, previous.id);
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header, previousBody);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
if (hideAndRecreate) {
|
||||
await minimizeComment(octokit, previous.id, hideClassify);
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
await updateComment(octokit, previous.id, body, header, previousBody);
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
run();
|
||||
117
dist/src/comment.js
generated
vendored
117
dist/src/comment.js
generated
vendored
|
|
@ -1,117 +0,0 @@
|
|||
import * as core from "@actions/core";
|
||||
function headerComment(header) {
|
||||
return `<!-- Sticky Pull Request Comment${header} -->`;
|
||||
}
|
||||
function bodyWithHeader(body, header) {
|
||||
return `${body}\n${headerComment(header)}`;
|
||||
}
|
||||
function bodyWithoutHeader(body, header) {
|
||||
return body.replace(`\n${headerComment(header)}`, "");
|
||||
}
|
||||
export async function findPreviousComment(octokit, repo, number, header) {
|
||||
let after = null;
|
||||
let hasNextPage = true;
|
||||
const h = headerComment(header);
|
||||
while (hasNextPage) {
|
||||
const data = await 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`, { ...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 = repository.pullRequest?.comments?.nodes?.find((node) => node?.author?.login === viewer.login.replace("[bot]", "") &&
|
||||
!node?.isMinimized &&
|
||||
node?.body?.includes(h));
|
||||
if (target) {
|
||||
return target;
|
||||
}
|
||||
after = repository.pullRequest?.comments?.pageInfo?.endCursor;
|
||||
hasNextPage = repository.pullRequest?.comments?.pageInfo?.hasNextPage ?? false;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
export async function updateComment(octokit, id, body, header, previousBody) {
|
||||
if (!body && !previousBody)
|
||||
return core.warning("Comment body cannot be blank");
|
||||
const rawPreviousBody = previousBody ? bodyWithoutHeader(previousBody, header) : "";
|
||||
await octokit.graphql(`
|
||||
mutation($input: UpdateIssueCommentInput!) {
|
||||
updateIssueComment(input: $input) {
|
||||
issueComment {
|
||||
id
|
||||
body
|
||||
}
|
||||
}
|
||||
}
|
||||
`, {
|
||||
input: {
|
||||
id,
|
||||
body: previousBody
|
||||
? bodyWithHeader(`${rawPreviousBody}\n${body}`, header)
|
||||
: bodyWithHeader(body, header),
|
||||
},
|
||||
});
|
||||
}
|
||||
export async function createComment(octokit, repo, issue_number, body, header, previousBody) {
|
||||
if (!body && !previousBody) {
|
||||
core.warning("Comment body cannot be blank");
|
||||
return;
|
||||
}
|
||||
return await octokit.rest.issues.createComment({
|
||||
...repo,
|
||||
issue_number,
|
||||
body: previousBody ? `${previousBody}\n${body}` : bodyWithHeader(body, header),
|
||||
});
|
||||
}
|
||||
export async function deleteComment(octokit, id) {
|
||||
await octokit.graphql(`
|
||||
mutation($id: ID!) {
|
||||
deleteIssueComment(input: { id: $id }) {
|
||||
clientMutationId
|
||||
}
|
||||
}
|
||||
`, { id });
|
||||
}
|
||||
export async function minimizeComment(octokit, subjectId, classifier) {
|
||||
await octokit.graphql(`
|
||||
mutation($input: MinimizeCommentInput!) {
|
||||
minimizeComment(input: $input) {
|
||||
clientMutationId
|
||||
}
|
||||
}
|
||||
`, { input: { subjectId, classifier } });
|
||||
}
|
||||
export function getBodyOf(previous, append, hideDetails) {
|
||||
if (!append) {
|
||||
return undefined;
|
||||
}
|
||||
if (!hideDetails || !previous.body) {
|
||||
return previous.body;
|
||||
}
|
||||
return previous.body.replace(/(<details.*?)\s*\bopen\b(.*>)/g, "$1$2");
|
||||
}
|
||||
export function commentsEqual(body, previous, header) {
|
||||
const newBody = bodyWithHeader(body, header);
|
||||
return newBody === previous;
|
||||
}
|
||||
65
dist/src/config.js
generated
vendored
65
dist/src/config.js
generated
vendored
|
|
@ -1,65 +0,0 @@
|
|||
import { readFileSync } from "node:fs";
|
||||
import * as core from "@actions/core";
|
||||
import { context } from "@actions/github";
|
||||
import { create } from "@actions/glob";
|
||||
export const pullRequestNumber = +core.getInput("number_force", { required: false }) ||
|
||||
context?.payload?.pull_request?.number ||
|
||||
+core.getInput("number", { required: false });
|
||||
export const repo = buildRepo();
|
||||
export const header = core.getInput("header", { required: false });
|
||||
export const append = core.getBooleanInput("append", { required: true });
|
||||
export const hideDetails = core.getBooleanInput("hide_details", {
|
||||
required: true,
|
||||
});
|
||||
export const recreate = core.getBooleanInput("recreate", { required: true });
|
||||
export const hideAndRecreate = core.getBooleanInput("hide_and_recreate", {
|
||||
required: true,
|
||||
});
|
||||
export const hideClassify = core.getInput("hide_classify", {
|
||||
required: true,
|
||||
});
|
||||
export const deleteOldComment = core.getBooleanInput("delete", { required: true });
|
||||
export const onlyCreateComment = core.getBooleanInput("only_create", {
|
||||
required: true,
|
||||
});
|
||||
export const onlyUpdateComment = core.getBooleanInput("only_update", {
|
||||
required: true,
|
||||
});
|
||||
export const skipUnchanged = core.getBooleanInput("skip_unchanged", {
|
||||
required: true,
|
||||
});
|
||||
export const hideOldComment = core.getBooleanInput("hide", { required: true });
|
||||
export const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
|
||||
export const ignoreEmpty = core.getBooleanInput("ignore_empty", {
|
||||
required: true,
|
||||
});
|
||||
function buildRepo() {
|
||||
return {
|
||||
owner: core.getInput("owner", { required: false }) || context.repo.owner,
|
||||
repo: core.getInput("repo", { required: false }) || context.repo.repo,
|
||||
};
|
||||
}
|
||||
export async function getBody() {
|
||||
const pathInput = core.getMultilineInput("path", { required: false });
|
||||
const followSymbolicLinks = core.getBooleanInput("follow_symbolic_links", {
|
||||
required: true,
|
||||
});
|
||||
if (pathInput && pathInput.length > 0) {
|
||||
try {
|
||||
const globber = await create(pathInput.join("\n"), {
|
||||
followSymbolicLinks,
|
||||
matchDirectories: false,
|
||||
});
|
||||
return (await globber.glob()).map(path => readFileSync(path, "utf-8")).join("\n");
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else {
|
||||
return core.getInput("message", { required: false });
|
||||
}
|
||||
}
|
||||
68
dist/src/main.js
generated
vendored
68
dist/src/main.js
generated
vendored
|
|
@ -1,68 +0,0 @@
|
|||
import * as core from "@actions/core";
|
||||
import * as github from "@actions/github";
|
||||
import { commentsEqual, createComment, deleteComment, findPreviousComment, getBodyOf, minimizeComment, updateComment, } from "./comment";
|
||||
import { append, deleteOldComment, getBody, githubToken, header, hideAndRecreate, hideClassify, hideDetails, hideOldComment, ignoreEmpty, onlyCreateComment, onlyUpdateComment, pullRequestNumber, recreate, repo, skipUnchanged, } from "./config";
|
||||
import { validateBody, validateExclusiveModes } from "./validate";
|
||||
async function run() {
|
||||
if (Number.isNaN(pullRequestNumber) || pullRequestNumber < 1) {
|
||||
core.info("no pull request numbers given: skip step");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate);
|
||||
const body = await getBody();
|
||||
if (!body && ignoreEmpty) {
|
||||
core.info("no body given: skip step by ignoreEmpty");
|
||||
return;
|
||||
}
|
||||
validateBody(body, deleteOldComment, hideOldComment);
|
||||
const octokit = github.getOctokit(githubToken);
|
||||
const previous = await findPreviousComment(octokit, repo, pullRequestNumber, header);
|
||||
core.setOutput("previous_comment_id", previous?.id);
|
||||
if (!previous) {
|
||||
if (onlyUpdateComment || hideOldComment || deleteOldComment) {
|
||||
return;
|
||||
}
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
if (onlyCreateComment) {
|
||||
// don't comment anything, user specified only_create and there is an
|
||||
// existing comment, so this is probably a placeholder / introduction one.
|
||||
return;
|
||||
}
|
||||
if (hideOldComment) {
|
||||
await minimizeComment(octokit, previous.id, hideClassify);
|
||||
return;
|
||||
}
|
||||
if (deleteOldComment) {
|
||||
await deleteComment(octokit, previous.id);
|
||||
return;
|
||||
}
|
||||
if (skipUnchanged && commentsEqual(body, previous.body || "", header)) {
|
||||
// don't recreate or update if the message is unchanged
|
||||
return;
|
||||
}
|
||||
const previousBody = getBodyOf({ body: previous.body || "" }, append, hideDetails);
|
||||
if (recreate) {
|
||||
await deleteComment(octokit, previous.id);
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header, previousBody);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
if (hideAndRecreate) {
|
||||
await minimizeComment(octokit, previous.id, hideClassify);
|
||||
const created = await createComment(octokit, repo, pullRequestNumber, body, header);
|
||||
core.setOutput("created_comment_id", created?.data.id);
|
||||
return;
|
||||
}
|
||||
await updateComment(octokit, previous.id, body, header, previousBody);
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof Error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
run();
|
||||
22
dist/src/validate.js
generated
vendored
22
dist/src/validate.js
generated
vendored
|
|
@ -1,22 +0,0 @@
|
|||
export function validateBody(body, deleteOldComment, hideOldComment) {
|
||||
if (!deleteOldComment && !hideOldComment && !body) {
|
||||
throw new Error("Either message or path input is required");
|
||||
}
|
||||
}
|
||||
export function validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate) {
|
||||
const exclusiveModes = [
|
||||
["delete", deleteOldComment],
|
||||
["recreate", recreate],
|
||||
["only_create", onlyCreateComment],
|
||||
["only_update", onlyUpdateComment],
|
||||
["hide", hideOldComment],
|
||||
["hide_and_recreate", hideAndRecreate],
|
||||
];
|
||||
const enabledModes = exclusiveModes.filter(([, flag]) => flag).map(([name]) => name);
|
||||
if (enabledModes.length > 1) {
|
||||
const last = enabledModes[enabledModes.length - 1];
|
||||
const rest = enabledModes.slice(0, -1);
|
||||
const joined = enabledModes.length === 2 ? `${rest[0]} and ${last}` : `${rest.join(", ")}, and ${last}`;
|
||||
throw new Error(`${joined} cannot be set to true simultaneously`);
|
||||
}
|
||||
}
|
||||
22
dist/validate.js
generated
vendored
22
dist/validate.js
generated
vendored
|
|
@ -1,22 +0,0 @@
|
|||
export function validateBody(body, deleteOldComment, hideOldComment) {
|
||||
if (!deleteOldComment && !hideOldComment && !body) {
|
||||
throw new Error("Either message or path input is required");
|
||||
}
|
||||
}
|
||||
export function validateExclusiveModes(deleteOldComment, recreate, onlyCreateComment, onlyUpdateComment, hideOldComment, hideAndRecreate) {
|
||||
const exclusiveModes = [
|
||||
["delete", deleteOldComment],
|
||||
["recreate", recreate],
|
||||
["only_create", onlyCreateComment],
|
||||
["only_update", onlyUpdateComment],
|
||||
["hide", hideOldComment],
|
||||
["hide_and_recreate", hideAndRecreate],
|
||||
];
|
||||
const enabledModes = exclusiveModes.filter(([, flag]) => flag).map(([name]) => name);
|
||||
if (enabledModes.length > 1) {
|
||||
const last = enabledModes[enabledModes.length - 1];
|
||||
const rest = enabledModes.slice(0, -1);
|
||||
const joined = enabledModes.length === 2 ? `${rest[0]} and ${last}` : `${rest.join(", ")}, and ${last}`;
|
||||
throw new Error(`${joined} cannot be set to true simultaneously`);
|
||||
}
|
||||
}
|
||||
|
|
@ -5,6 +5,7 @@ import nodeResolve from "@rollup/plugin-node-resolve"
|
|||
import typescript from "@rollup/plugin-typescript"
|
||||
|
||||
const config = {
|
||||
context: "globalThis",
|
||||
input: "src/main.ts",
|
||||
output: {
|
||||
exports: "auto",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue