Skip to content

Commit

Permalink
feat(create-pr-comment): support comment overwriting (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
danadajian authored Jan 6, 2022
1 parent 67a8aee commit 466f972
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 14 deletions.
22 changes: 21 additions & 1 deletion dist/461.index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/461.index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions src/helpers/create-pr-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,27 @@ import { octokit } from '../octokit';

interface CreatePrComment {
body: string;
login?: string;
}

export const createPrComment = ({ body }: CreatePrComment) =>
octokit.issues.createComment({
export const createPrComment = async ({ body, login }: CreatePrComment) => {
if (login) {
const commentsResponse = await octokit.issues.listComments({
issue_number: context.issue.number,
...context.repo
});
const comment_id = commentsResponse.data.find(comment => comment?.user?.login === login)?.id;
if (comment_id) {
return octokit.issues.updateComment({
comment_id,
body,
...context.repo
});
}
}
return octokit.issues.createComment({
body,
issue_number: context.issue.number,
...context.repo
});
};
76 changes: 66 additions & 10 deletions test/helpers/create-pr-comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,86 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { Mocktokit } from '../types';
import { context } from '@actions/github';
import { createPrComment } from '../../src/helpers/create-pr-comment';
import { octokit } from '../../src/octokit';

jest.mock('@actions/core');
jest.mock('@actions/github', () => ({
context: { repo: { repo: 'repo', owner: 'owner' }, issue: { number: 123 } },
getOctokit: jest.fn(() => ({ rest: { issues: { createComment: jest.fn() } } }))
getOctokit: jest.fn(() => ({
rest: {
issues: {
createComment: jest.fn(),
listComments: jest.fn(),
updateComment: jest.fn()
}
}
}))
}));

(octokit.issues.listComments as unknown as Mocktokit).mockImplementation(async () => ({
data: [
{
id: 12345,
body: 'body',
user: {
login: 'login'
}
},
{
id: 456,
body: 'some other body',
user: {
login: 'some other login'
}
}
]
}));

describe('createPrComment', () => {
const body = 'body';
describe('create comment case', function () {
const body = 'body';

beforeEach(() => {
createPrComment({ body });
});

beforeEach(() => {
createPrComment({
body
it('should call createComment with correct params', () => {
expect(octokit.issues.createComment).toHaveBeenCalledWith({
body,
issue_number: 123,
...context.repo
});
});
});

it('should call createComment with correct params', () => {
expect(octokit.issues.createComment).toHaveBeenCalledWith({
body,
issue_number: 123,
...context.repo
describe('update comment case', function () {
const body = 'body';
const login = 'login';

beforeEach(() => {
createPrComment({ body, login });
});

it('should not call createComment with correct params', () => {
expect(octokit.issues.createComment).not.toHaveBeenCalled();
});

it('should call listComments with correct params', () => {
expect(octokit.issues.listComments).toHaveBeenCalledWith({
issue_number: 123,
...context.repo
});
});

it('should call updateComment with correct params', () => {
expect(octokit.issues.updateComment).toHaveBeenCalledWith({
comment_id: 12345,
body,
...context.repo
});
});
});
});

0 comments on commit 466f972

Please sign in to comment.