diff --git a/lib/index.js b/lib/index.js index 8decf3a0..2a626919 100644 --- a/lib/index.js +++ b/lib/index.js @@ -2338,8 +2338,7 @@ function run() { } const pivotalId = utils_1.getPivotalId(headBranch); if (!pivotalId) { - const comment = Object.assign(Object.assign({}, commonPayload), { body: utils_1.getNoIdComment(headBranch) }); - yield utils_1.addComment(client, comment); + yield utils_1.addNoIdComment(client, headBranch, commonPayload); core.setFailed('Pivotal id is missing in your branch.'); process.exit(1); } @@ -2378,8 +2377,7 @@ function run() { } } else { - const comment = Object.assign(Object.assign({}, commonPayload), { body: utils_1.getNoIdComment(headBranch) }); - yield utils_1.addComment(client, comment); + yield utils_1.addNoIdComment(client, headBranch, commonPayload); core.setFailed('Invalid pivotal story id. Please create a branch with a valid pivotal story'); process.exit(1); } @@ -2900,6 +2898,27 @@ const getStoryIcon = (storyType) => { return ''; } }; +/** + * Helpful function to add a comment that we couldn't find the Pivotal ID + * of this comment. Will attempt to only add once. + */ +exports.addNoIdComment = (client, branch, params) => __awaiter(void 0, void 0, void 0, function* () { + const { data: comments } = yield client.issues.listComments(params); + const noIdComment = exports.getNoIdComment(branch); + // Find a previously created comment by our bot + const previousComments = comments.filter(comment => comment.body.includes(noIdComment)); + if (previousComments.length > 0) { + // Update existing comment + const { id } = previousComments[0]; + console.log(`Comment already exists as comment #${id}`); + } + else { + // Insert a new comment + console.log('Adding a new comment'); + const comment = Object.assign(Object.assign({}, params), { body: noIdComment }); + yield exports.addComment(client, comment); + } +}); /** * Returns true if the body contains the hidden marker. Used to avoid adding * pivotal story details to the PR multiple times. diff --git a/src/main.ts b/src/main.ts index dd0ac30c..dd605852 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,7 +18,7 @@ import { getPrTitleComment, getHugePrComment, isHumongousPR, - getNoIdComment, + addNoIdComment, shouldAddComments, } from './utils'; import { PullRequestParams, PivotalDetails } from './types'; @@ -96,11 +96,7 @@ async function run() { const pivotalId = getPivotalId(headBranch); if (!pivotalId) { - const comment: IssuesCreateCommentParams = { - ...commonPayload, - body: getNoIdComment(headBranch), - }; - await addComment(client, comment); + await addNoIdComment(client, headBranch, commonPayload); core.setFailed('Pivotal id is missing in your branch.'); process.exit(1); @@ -159,11 +155,7 @@ async function run() { } } } else { - const comment: IssuesCreateCommentParams = { - ...commonPayload, - body: getNoIdComment(headBranch), - }; - await addComment(client, comment); + await addNoIdComment(client, headBranch, commonPayload); core.setFailed('Invalid pivotal story id. Please create a branch with a valid pivotal story'); process.exit(1); diff --git a/src/utils.ts b/src/utils.ts index ea590d7f..f4f68b7e 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -2,7 +2,7 @@ import axios from 'axios'; import * as core from '@actions/core'; import * as github from '@actions/github'; import similarity from 'string-similarity'; -import { IssuesAddLabelsParams, PullsUpdateParams, IssuesCreateCommentParams } from '@octokit/rest'; +import { IssuesAddLabelsParams, PullsUpdateParams, IssuesCreateCommentParams, IssuesListCommentsParams } from '@octokit/rest'; import { MARKER_REGEX, HIDDEN_MARKER, BOT_BRANCH_PATTERNS, DEFAULT_BRANCH_PATTERNS } from './constants'; import { PivotalStory, PivotalProjectResponse, PivotalDetails, Label } from './types'; @@ -252,6 +252,39 @@ const getStoryIcon = (storyType: string): string => { } }; +/** + * Helpful function to add a comment that we couldn't find the Pivotal ID + * of this comment. Will attempt to only add once. + */ +export const addNoIdComment = async ( + client: github.GitHub, + branch: string, + params: IssuesListCommentsParams +) => { + const { data: comments } = await client.issues.listComments(params); + + const noIdComment = getNoIdComment(branch); + + // Find a previously created comment by our bot + const previousComments = comments.filter( + comment => comment.body.includes(noIdComment), + ); + + if (previousComments.length > 0) { + // Update existing comment + const { id } = previousComments[0]; + console.log(`Comment already exists as comment #${id}`); + } else { + // Insert a new comment + console.log('Adding a new comment'); + const comment: IssuesCreateCommentParams = { + ...params, + body: noIdComment + }; + await addComment(client, comment); + } +} + /** * Returns true if the body contains the hidden marker. Used to avoid adding * pivotal story details to the PR multiple times.