Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add the comment for no id if the comment does not already exist #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
Expand Down
14 changes: 3 additions & 11 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getPrTitleComment,
getHugePrComment,
isHumongousPR,
getNoIdComment,
addNoIdComment,
shouldAddComments,
} from './utils';
import { PullRequestParams, PivotalDetails } from './types';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
35 changes: 34 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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.
Expand Down