@google-automations/issue-utils
Version:
Utilities for github issues
84 lines • 3.15 kB
JavaScript
;
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.addOrUpdateIssueComment = exports.getCommentMark = void 0;
/**
* It creates a comment string used for `addOrUpdateissuecomment`.
*/
const getCommentMark = (installationId) => {
return `<!-- probot comment [${installationId}]-->`;
};
exports.getCommentMark = getCommentMark;
/**
* It creates a comment, or if the bot already created a comment, it
* updates the same comment.
*
* @param {Octokit} octokit - The Octokit instance.
* @param {string} owner - The owner of the issue.
* @param {string} repo - The name of the repository.
* @param {number} issueNumber - The number of the issue.
* @param {number} installationId - A unique number for identifying the issue
* comment.
* @param {string} commentBody - The body of the comment.
* @param {AddOrUpdateIssueCommentOptions} options
* @param {boolean} options.onlyUpdate - If set to true, it will only update an
* existing issue comment.
*/
const addOrUpdateIssueComment = async (octokit, owner, repo, issueNumber, installationId, commentBody, options = {}) => {
var _a;
const commentMark = (0, exports.getCommentMark)(installationId);
const listCommentsResponse = await octokit.issues.listComments({
owner: owner,
repo: repo,
per_page: 50,
issue_number: issueNumber,
});
for (const comment of listCommentsResponse.data) {
if ((_a = comment.body) === null || _a === void 0 ? void 0 : _a.includes(commentMark)) {
// We found the existing comment, so updating it
const { data: updatedComment } = await octokit.issues.updateComment({
owner,
repo,
comment_id: comment.id,
body: `${commentMark}\n${commentBody}`,
});
return {
owner,
repo,
issueNumber,
body: updatedComment.body || '',
htmlUrl: updatedComment.html_url,
};
}
}
if (options.onlyUpdate) {
return null;
}
const { data: newComment } = await octokit.issues.createComment({
owner: owner,
repo: repo,
issue_number: issueNumber,
body: `${commentMark}\n${commentBody}`,
});
return {
owner,
repo,
issueNumber,
body: newComment.body || '',
htmlUrl: newComment.html_url,
};
};
exports.addOrUpdateIssueComment = addOrUpdateIssueComment;
//# sourceMappingURL=issue-comments.js.map