Skip to content

Commit

Permalink
Updated action to accept the comment threshold, requiring a certain n…
Browse files Browse the repository at this point in the history
…umber of comments before an answer can be marked
  • Loading branch information
wesleyscholl committed Oct 16, 2023
1 parent 5fe48b4 commit 9f9ce66
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/answer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ name: Mark Discussion Comment Answer
on:
discussion_comment:
types: [created]
if: >-
${{ github.event.discussion_comment.state == 'open' }} &&
${{ github.event.discussion_comment.discussion.category.is_answerable == true }} &&
${{ github.event.discussion_comment.discussion.answer_chosen_at == null }} &&
${{ github.event.discussion_comment.locked == false }}

jobs:
mark-comment-answer:
Expand All @@ -18,6 +23,7 @@ jobs:
with:
GH_TOKEN: "${{ secrets.DISCUSS_TOKEN }}"
reaction_threshold: 3
comment_threshold: 3

- name: Show Mark Answer Output
run: |
Expand Down
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ inputs:
reaction_threshold:
description: Number of positive comment reactions required to mark as an answer.
required: false
default: 0

default: 3
comment_threshold:
description: Number of total comments required to mark as an answer.
required: false
default: 3
branding:
icon: "check-circle"
color: "green"
1 change: 1 addition & 0 deletions src/__tests__/alreadyAnswered.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"discussion": {
"answer_chosen_at": "2023-10-10T00:22:45.000-04:00",
"node_id": "D_kwDOKczwv84AV0aF",
"comments": 3,
"category": {
"is_answerable": true
}
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/event.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"discussion": {
"answer_chosen_at": null,
"node_id": "D_kwDOKczwv84AV0aF",
"comments": 5,
"category": {
"is_answerable": true
}
Expand Down
30 changes: 26 additions & 4 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ afterAll(async () => {

test("Test if github token is invalid", async () => {
await mockedGetInput.mockReturnValueOnce("{{ INVALID_TOKEN }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("3");

const mockedResponse = {
clientMutationId: "1234",
Expand All @@ -39,12 +41,14 @@ test("Test if github token is invalid", async () => {

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(2);
await expect(mockedGetInput).toHaveBeenCalledTimes(3);
await expect(mockedSetFailed).toHaveBeenCalledWith("GitHub token missing or invalid, please enter a GITHUB_TOKEN");
});

test("run function successfully runs", async () => {
await mockedGetInput.mockReturnValueOnce("{{ secrets.GITHUB_TOKEN }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("DC_kwDOKEe7W84AbmPS");

const mockedResponse = {
Expand All @@ -57,25 +61,43 @@ test("run function successfully runs", async () => {

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(2);
await expect(mockedGetInput).toHaveBeenCalledTimes(3);
});

test("Test if discussion is already answered", async () => {
await mockedGetInput.mockReturnValueOnce("{{ github.token }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("3");
process.env.GITHUB_EVENT_PATH = "src/__tests__/alreadyAnswered.json";

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(2);
await expect(mockedGetInput).toHaveBeenCalledTimes(3);
await expect(mockedSetFailed).toHaveBeenCalledWith("Discussion is already answered.");
});

test("Test if discussion is unanswerable", async () => {
await mockedGetInput.mockReturnValueOnce("{{ github.token }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("3");
process.env.GITHUB_EVENT_PATH = "src/__tests__/unanswerable.json";

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(2);
await expect(mockedGetInput).toHaveBeenCalledTimes(3);
await expect(mockedSetFailed).toHaveBeenCalledWith("Discussion category is not answerable.");
});

test("Not enough comments to mark an answer", async () => {
await mockedGetInput.mockReturnValueOnce("{{ github.token }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("1");
process.env.GITHUB_EVENT_PATH = "src/__tests__/notEnoughComments.json";

await markDiscussionCommentAnswer();

await expect(mockedGetInput).toHaveBeenCalledTimes(3);
await expect(mockedSetFailed).toHaveBeenCalledWith(
"Discussion does not have enough comments for an answer to be chosen."
);
});
8 changes: 4 additions & 4 deletions src/__tests__/mark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ test("should call run() when JEST_WORKER_ID is not defined", async () => {
// Mock process.env to simulate JEST_WORKER_ID not being defined
delete process.env.JEST_WORKER_ID;

mockedGetInput.mockReturnValueOnce("{{ secrets.GITHUB_TOKEN }}");
mockedGetInput.mockReturnValueOnce("commentId");
mockedGetInput.mockReturnValueOnce("discussion");
await mockedGetInput.mockReturnValueOnce("{{ secrets.GITHUB_TOKEN }}");
await mockedGetInput.mockReturnValueOnce("3");
await mockedGetInput.mockReturnValueOnce("3");

// Mock the GraphQL response
const mockedResponse = {
Expand All @@ -53,7 +53,7 @@ test("should call run() when JEST_WORKER_ID is not defined", async () => {
await markDiscussionCommentAnswer();

// Assertions
expect(mockedGetInput).toHaveBeenCalledTimes(2);
expect(mockedGetInput).toHaveBeenCalledTimes(3);
// expect(mockedSetOutput).toHaveBeenCalledWith("commentId", "commentId");
// expect(mockedSetOutput).toHaveBeenCalledWith("discussion", "discussionId");
});
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/notEnoughComments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"comment": {
"node_id": "DC_kwDOKEe7W84AbmPS"
},
"discussion": {
"answer_chosen_at": null,
"node_id": "D_kwDOKczwv84AV0aF",
"comments": 1,
"category": {
"is_answerable": true
}
},
"repository": {
"name": "mark-discussion-comment-answer",
"owner": {
"login": "konjoinfinity"
}
}
}
1 change: 1 addition & 0 deletions src/__tests__/unanswerable.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"discussion": {
"answer_chosen_at": null,
"node_id": "D_kwDOKczwv84AV0aF",
"comments": 3,
"category": {
"is_answerable": false
}
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface Res {
export async function markDiscussionCommentAnswer() {
const token = getInput("GH_TOKEN");
const reactionThreshold = Number(getInput("reaction_threshold"));
const commentThreshold = getInput("comment_threshold");
const eventPayload = require(String(process.env.GITHUB_EVENT_PATH));
const repoName = eventPayload.repository.name;
const repoOwner = eventPayload.repository.owner.login;
Expand All @@ -33,6 +34,11 @@ export async function markDiscussionCommentAnswer() {
return;
}

if (Number(eventPayload.discussion.comments) <= Number(commentThreshold)) {
setFailed("Discussion does not have enough comments for an answer to be chosen.");
return;
}

function countPositiveReactions(data: any) {
const comments = data.repository.discussions.edges[0].node.comments.edges;
const positiveReactions = ["+1", "LAUGH", "HEART", "HOORAY", "ROCKET"];
Expand Down

0 comments on commit 9f9ce66

Please sign in to comment.