Fix: Ignore other users' comments (#332)

Make findPreviousComment only return comments made by the current user
(usually github-actions[bot])
This commit is contained in:
Rob Cowsill 2021-06-14 19:25:28 +01:00 committed by GitHub
parent 2020b5bc34
commit 65b0a353cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 309 additions and 283 deletions

View file

@ -16,45 +16,51 @@ const repo = {
repo: "sticky-pull-request-comment"
}
it("findPreviousComment", async () => {
const authenticatedUser = {
login: "github-actions[bot]"
}
const otherUser = {
login: "some-user"
}
const comment = {
user: {
login: "github-actions[bot]"
},
user: authenticatedUser,
body: "previous message\n<!-- Sticky Pull Request Comment -->"
}
const commentWithCustomHeader = {
user: {
login: "github-actions[bot]"
},
user: authenticatedUser,
body: "previous message\n<!-- Sticky Pull Request CommentTypeA -->"
}
const headerFirstComment = {
user: {
login: "github-actions[bot]"
},
user: authenticatedUser,
body: "<!-- Sticky Pull Request CommentLegacyComment -->\nheader first message"
}
const otherUserComment = {
user: otherUser,
body: "Fake previous message\n<!-- Sticky Pull Request Comment -->"
}
const otherComments = [
{
user: {
login: "some-user"
},
user: otherUser,
body: "lgtm"
},
{
user: {
login: "github-actions[bot]"
},
user: authenticatedUser,
body: "previous message\n<!-- Sticky Pull Request CommentTypeB -->"
}
]
const octokit: any = {
graphql: jest.fn(() =>
Promise.resolve({
viewer: authenticatedUser
})
),
rest: {
issues: {
listComments: jest.fn(() =>
Promise.resolve({
data: [
commentWithCustomHeader,
otherUserComment,
comment,
headerFirstComment,
...otherComments

52
dist/index.js generated vendored
View file

@ -42,16 +42,17 @@ function headerComment(header) {
}
function findPreviousComment(octokit, repo, issue_number, header) {
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 }));
const h = headerComment(header);
return comments.find(comment => { var _a; return (_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(h); });
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)); });
});
}
exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
return core.warning('Comment body cannot be blank');
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)}` }));
@ -61,7 +62,7 @@ exports.updateComment = updateComment;
function createComment(octokit, repo, issue_number, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
return core.warning('Comment body cannot be blank');
return core.warning("Comment body cannot be blank");
yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}` }));
@ -109,33 +110,33 @@ const core = __importStar(__nccwpck_require__(186));
const github_1 = __nccwpck_require__(438);
const fs_1 = __nccwpck_require__(747);
exports.pullRequestNumber = ((_b = (_a = github_1.context === null || github_1.context === void 0 ? void 0 : github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.number) ||
+core.getInput('number', { required: false });
+core.getInput("number", { required: false });
exports.repo = buildRepo();
exports.header = core.getInput('header', { required: false });
exports.append = core.getBooleanInput('append', { required: true });
exports.recreate = core.getBooleanInput('recreate', { required: true });
exports.deleteOldComment = core.getBooleanInput('delete', { required: true });
exports.githubToken = core.getInput('GITHUB_TOKEN', { required: true });
exports.header = core.getInput("header", { required: false });
exports.append = core.getBooleanInput("append", { required: true });
exports.recreate = core.getBooleanInput("recreate", { required: true });
exports.deleteOldComment = core.getBooleanInput("delete", { required: true });
exports.githubToken = core.getInput("GITHUB_TOKEN", { required: true });
exports.body = buildBody();
function buildRepo() {
return {
owner: github_1.context.repo.owner,
repo: core.getInput('repo', { required: false }) || github_1.context.repo.repo
repo: core.getInput("repo", { required: false }) || github_1.context.repo.repo
};
}
function buildBody() {
const path = core.getInput('path', { required: false });
const path = core.getInput("path", { required: false });
if (path) {
try {
return fs_1.readFileSync(path, 'utf-8');
return fs_1.readFileSync(path, "utf-8");
}
catch (error) {
core.setFailed(error.message);
return '';
return "";
}
}
else {
return core.getInput('message', { required: false });
return core.getInput("message", { required: false });
}
}
@ -183,15 +184,15 @@ const config_1 = __nccwpck_require__(88);
function run() {
return __awaiter(this, void 0, void 0, function* () {
if (isNaN(config_1.pullRequestNumber) || config_1.pullRequestNumber < 1) {
core.info('no pull request numbers given: skip step');
core.info("no pull request numbers given: skip step");
return;
}
try {
if (!config_1.deleteOldComment && !config_1.body) {
throw new Error('Either message or path input is required');
throw new Error("Either message or path input is required");
}
if (config_1.deleteOldComment && config_1.recreate) {
throw new Error('delete and recreate cannot be both set to true');
throw new Error("delete and recreate cannot be both set to true");
}
const octokit = github.getOctokit(config_1.githubToken);
const previous = yield comment_1.findPreviousComment(octokit, config_1.repo, config_1.pullRequestNumber, config_1.header);
@ -354,7 +355,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getState = exports.saveState = exports.group = exports.endGroup = exports.startGroup = exports.info = exports.warning = exports.error = exports.debug = exports.isDebug = exports.setFailed = exports.setCommandEcho = exports.setOutput = exports.getBooleanInput = exports.getInput = exports.addPath = exports.setSecret = exports.exportVariable = exports.ExitCode = void 0;
exports.getState = exports.saveState = exports.group = exports.endGroup = exports.startGroup = exports.info = exports.warning = exports.error = exports.debug = exports.isDebug = exports.setFailed = exports.setCommandEcho = exports.setOutput = exports.getBooleanInput = exports.getMultilineInput = exports.getInput = exports.addPath = exports.setSecret = exports.exportVariable = exports.ExitCode = void 0;
const command_1 = __nccwpck_require__(351);
const file_command_1 = __nccwpck_require__(717);
const utils_1 = __nccwpck_require__(278);
@ -440,6 +441,21 @@ function getInput(name, options) {
return val.trim();
}
exports.getInput = getInput;
/**
* Gets the values of an multiline input. Each value is also trimmed.
*
* @param name name of the input to get
* @param options optional. See InputOptions.
* @returns string[]
*
*/
function getMultilineInput(name, options) {
const inputs = getInput(name, options)
.split('\n')
.filter(x => x !== '');
return inputs;
}
exports.getMultilineInput = getMultilineInput;
/**
* Gets the input value of the boolean type in the YAML 1.2 "core schema" specification.
* Support boolean input list: `true | True | TRUE | false | False | FALSE` .

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

7
lib/comment.js generated
View file

@ -35,16 +35,17 @@ function headerComment(header) {
}
function findPreviousComment(octokit, repo, issue_number, header) {
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 }));
const h = headerComment(header);
return comments.find(comment => { var _a; return (_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(h); });
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)); });
});
}
exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
return core.warning('Comment body cannot be blank');
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)}` }));
@ -54,7 +55,7 @@ exports.updateComment = updateComment;
function createComment(octokit, repo, issue_number, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
return core.warning('Comment body cannot be blank');
return core.warning("Comment body cannot be blank");
yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}` }));

View file

@ -14,12 +14,15 @@ export async function findPreviousComment(
issue_number: number,
header: string
): Promise<{body?: string; id: number} | undefined> {
const {viewer} = await octokit.graphql("query { viewer { login } }")
const {data: comments} = await octokit.rest.issues.listComments({
...repo,
issue_number
})
const h = headerComment(header)
return comments.find(comment => comment.body?.includes(h))
return comments.find(
comment => comment.user?.login === viewer.login && comment.body?.includes(h)
)
}
export async function updateComment(
octokit: InstanceType<typeof GitHub>,