Skip to content

Commit

Permalink
minor refactor and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans committed Apr 25, 2023
1 parent e3ce6a8 commit 73e4085
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 39 deletions.
130 changes: 128 additions & 2 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {findCommentPredicate} from '../src/find'
import {findCommentPredicate, findMatchingComment} from '../src/find'

describe('find comment tests', () => {
describe('findCommentPredicate tests', () => {
test('find by bodyIncludes', async () => {
expect(
findCommentPredicate(
Expand Down Expand Up @@ -307,3 +307,129 @@ describe('find comment tests', () => {
).toEqual(true)
})
})

describe('findMatchingComment tests', () => {
// Note: Use `testComments.slice()` to avoid mutating the original array.
const testComments = [
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 2,
body: `You've always had the power, my dear. You just had to learn it for yourself.`,
user: {login: 'glinda'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 3,
body: `I'll get you, my pretty, and your little dog too!`,
user: {login: 'wicked-witch'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 4,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 5,
body: `I'll get you, my pretty, and your little dog too!`,
user: {login: 'wicked-witch'},
created_at: '2020-01-01T00:00:00Z'
}
]

test('no comments', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 0
},
[]
)
).toEqual(undefined)
})

test('find with search direction first', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 0
},
testComments.slice()
)?.id
).toEqual(1)
})

test('find with search direction last', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'last',
nth: 0
},
testComments.slice()
)?.id
).toEqual(4)
})

test('find nth with search direction first', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 1
},
testComments.slice()
)?.id
).toEqual(4)
})

test('find nth with search direction last', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'last',
nth: 1
},
testComments.slice()
)?.id
).toEqual(1)
})
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
direction:
description: 'Search direction, specified as `first` or `last`'
default: first
nth:
description: '0-indexed number, specifying which comment to return if multiple are found'
default: 0
outputs:
comment-id:
description: 'The id of the matching comment found.'
Expand Down
45 changes: 28 additions & 17 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.findComment = exports.findCommentPredicate = void 0;
exports.findComment = exports.findMatchingComment = exports.findCommentPredicate = void 0;
const github = __importStar(__nccwpck_require__(5438));
function stringToRegex(s) {
const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/);
Expand All @@ -48,6 +48,17 @@ function stringToRegex(s) {
else
return new RegExp(s);
}
function fetchComments(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(inputs.token);
const [owner, repo] = inputs.repository.split('/');
return yield octokit.paginate(octokit.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber
});
});
}
function findCommentPredicate(inputs, comment) {
return ((inputs.commentAuthor && comment.user
? comment.user.login === inputs.commentAuthor
Expand All @@ -60,23 +71,22 @@ function findCommentPredicate(inputs, comment) {
: true));
}
exports.findCommentPredicate = findCommentPredicate;
function findMatchingComment(inputs, comments) {
if (inputs.direction == 'last') {
comments.reverse();
}
const matchingComments = comments.filter(comment => findCommentPredicate(inputs, comment));
const comment = matchingComments[inputs.nth];
if (comment) {
return comment;
}
return undefined;
}
exports.findMatchingComment = findMatchingComment;
function findComment(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = github.getOctokit(inputs.token);
const [owner, repo] = inputs.repository.split('/');
const parameters = {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber
};
const comments = yield octokit.paginate(octokit.rest.issues.listComments, parameters);
if (inputs.direction == 'last') {
comments.reverse();
}
const comment = comments.find(comment => findCommentPredicate(inputs, comment));
if (comment)
return comment;
return undefined;
const comments = yield fetchComments(inputs);
return findMatchingComment(inputs, comments);
});
}
exports.findComment = findComment;
Expand Down Expand Up @@ -140,7 +150,8 @@ function run() {
commentAuthor: core.getInput('comment-author'),
bodyIncludes: core.getInput('body-includes'),
bodyRegex: core.getInput('body-regex'),
direction: core.getInput('direction')
direction: core.getInput('direction'),
nth: Number(core.getInput('nth'))
};
core.debug(`Inputs: ${(0, util_1.inspect)(inputs)}`);
const comment = yield (0, find_1.findComment)(inputs);
Expand Down
43 changes: 23 additions & 20 deletions src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function stringToRegex(s: string): RegExp {
else return new RegExp(s)
}

async function fetchComments(inputs: Inputs): Promise<Comment[]> {
const octokit = github.getOctokit(inputs.token)
const [owner, repo] = inputs.repository.split('/')
return await octokit.paginate(octokit.rest.issues.listComments, {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber
})
}

export function findCommentPredicate(
inputs: Inputs,
comment: Comment
Expand All @@ -43,33 +53,26 @@ export function findCommentPredicate(
)
}

export async function findComment(
inputs: Inputs
): Promise<Comment | undefined> {
const octokit = github.getOctokit(inputs.token)
const [owner, repo] = inputs.repository.split('/')

const parameters = {
owner: owner,
repo: repo,
issue_number: inputs.issueNumber
}

const allComments = await octokit.paginate(
octokit.rest.issues.listComments,
parameters
)
export function findMatchingComment(
inputs: Inputs,
comments: Comment[]
): Comment | undefined {
if (inputs.direction == 'last') {
allComments.reverse()
comments.reverse()
}
const matchingComments = allComments.filter(comment =>
const matchingComments = comments.filter(comment =>
findCommentPredicate(inputs, comment)
)

const comment = matchingComments[inputs.nth]
if (comment) {
return comment
}

return undefined
}

export async function findComment(
inputs: Inputs
): Promise<Comment | undefined> {
const comments = await fetchComments(inputs)
return findMatchingComment(inputs, comments)
}

0 comments on commit 73e4085

Please sign in to comment.