Skip to content

Commit

Permalink
test: add integration tests and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Varun-Kolanu committed Jun 12, 2024
1 parent 4758756 commit c351c71
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 134 deletions.
13 changes: 9 additions & 4 deletions src/handlers/issue_comment_created.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ export default async (context) => {
const collaboratorsJson = await context.octokit.repos.listCollaborators(
context.repo({})
);
const collaborators = collaboratorsJson.data.map((coll) => coll.login);
const collaborators = collaboratorsJson.data
.filter((coll) => {
return (
coll.permissions.admin ||
coll.permissions.maintain ||
coll.permissions.triage
);
})
.map((coll) => coll.login);

if (skipCommenters(commenter, collaborators)) return;

const config = await getConfig(context);
const assignPromptKey = "assign-prompt";
const unassignPromptKey = "unassign-prompt";

const assignees = issue.assignees.map((assigneeJson) => assigneeJson.login);
const assigneeCount = assignees.length;

const request = checkRequest(comment.body, config);
if (request === Request.SKIP) return;
Expand Down
12 changes: 10 additions & 2 deletions src/handlers/issue_opened.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ export default async (context) => {
const collaboratorsJson = await context.octokit.repos.listCollaborators(
context.repo({})
);
const collaborators = collaboratorsJson.data.map((coll) => coll.login);
const collaborators = collaboratorsJson.data
.filter((coll) => {
return (
coll.permissions.admin ||
coll.permissions.maintain ||
coll.permissions.triage
);
})
.map((coll) => coll.login);
const issue_opener_username = issue.user.login;

const config = await getConfig(context);
Expand All @@ -18,7 +26,7 @@ export default async (context) => {
);
if (issue_opener === OpenerIsMaintainer.SKIP) return;
const issueComment = context.issue({
body: `@${issue_opener} ` + config[issue_opener],
body: `@${issue_opener_username} ` + config[issue_opener],
});
return await context.octokit.issues.createComment(issueComment);
};
14 changes: 7 additions & 7 deletions src/helpers/check_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export function checkRequest(comment, config) {
.replace(/\s+/g, " ") // trim whitespace
.toLowerCase(); // Case insensitive

if (config[Request.ASSIGN]) {
if (comment.includes(config[Request.ASSIGN])) return Request.ASSIGN;
else return Request.SKIP;
} else if (config[Request.UNASSIGN]) {
if (comment.includes(config[Request.UNASSIGN])) return Request.UNASSIGN;
else return Request.SKIP;
} else return Request.SKIP;
if (config[Request.ASSIGN] && comment.includes(config[Request.ASSIGN])) {
return Request.ASSIGN;
}
if (config[Request.UNASSIGN] && comment.includes(config[Request.UNASSIGN])) {
return Request.UNASSIGN;
}
return Request.SKIP;
}
2 changes: 1 addition & 1 deletion src/helpers/get_assigned_issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default async function getAssignedIssues(context, username) {
try {
const response = await context.octokit.issues.listForRepo(
context.issue({
context.repo({
assignee: username,
state: "open",
})
Expand Down
1 change: 1 addition & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { run } from "probot";
import app from "./app.js";

run(app);
20 changes: 20 additions & 0 deletions test/fixtures/issue_comment.created.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"action": "created",
"issue": {
"number": 1
},
"repository": {
"name": "test-repo",
"owner": {
"login": "test-owner"
}
},
"comment": {
"user": {
"login": "test"
}
},
"installation": {
"id": 2
}
}
5 changes: 1 addition & 4 deletions test/fixtures/issues.opened.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"action": "opened",
"issue": {
"number": 1,
"user": {
"login": "test-opener"
}
"number": 1
},
"repository": {
"name": "test-repo",
Expand Down
62 changes: 0 additions & 62 deletions test/index.test.js

This file was deleted.

174 changes: 174 additions & 0 deletions test/integration/issue_comment_created.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import nock from "nock";
import { describe, beforeEach, afterEach, test } from "node:test";
import assert from "node:assert";

import getProbotConfig from "../utils/get-probot.js";
import { setupNock, setupNockSkip } from "../utils/setupNocks.js";
import { createPayloadIssueComment } from "../utils/createPayload.js";

describe("Issue Comment Created handler", () => {
let probot;
let server;
let mock;

beforeEach(async () => {
nock.disableNetConnect();
server = await getProbotConfig();
probot = server.probotApp;
});

test("only fetches until collaborators and returns if commenter is a bot", async () => {
mock = setupNockSkip();

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment("issue-assigner[bot]"),
});
});

test("only fetches until collaborators and returns if commenter is a maintainer", async () => {
mock = setupNockSkip();

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment("test-maintainer"),
});
});

test("assigns an issue if requested to assign and all OK!", async () => {
mock = setupNock(
"@test-commenter This issue has been successfully assigned to you! 🚀"
)
// Fetch already assigned issues
.get(
"/repos/test-owner/test-repo/issues?assignee=test-commenter&state=open"
)
.reply(200, [])

// Assign issue
.post("/repos/test-owner/test-repo/issues/1/assignees", (body) => {
assert.deepStrictEqual(body, {
assignees: ["test-commenter"],
});
return true;
})
.reply(200);

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"test-commenter",
"@issue-assigner claim"
),
});
});

describe("Doesn't assign the issue if requested to assign and if ", () => {
test("that issue is already assigned to the commeneter", async () => {
mock = setupNock(
"@assignee1 You have already been assigned to this issue."
)
// Fetch already assigned issues
.get("/repos/test-owner/test-repo/issues?assignee=assignee1&state=open")
.reply(200, [
{
number: 1,
html_url: "sampleurl",
},
]);

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"assignee1",
"@issue-assigner claim",
true
),
});
});

test("max assignees reached in issue", async () => {
mock = setupNock(
"@test-commenter Sorry, maximum limit for assignees in this issue has reached. Please check other issues or contact a maintainer."
)
// Fetch already assigned issues
.get(
"/repos/test-owner/test-repo/issues?assignee=test-commenter&state=open"
)
.reply(200, []);

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"test-commenter",
"@issue-assigner claim",
true
),
});
});

test("max issues for user reached in repo", async () => {
mock = setupNock(
"@test-commenter You already have this issue assigned: [ issue#2 ](sample-url). Abandon your existing issue or contact a maintainer if you want to get this issue assigned."
)
// Fetch already assigned issues
.get(
"/repos/test-owner/test-repo/issues?assignee=test-commenter&state=open"
)
.reply(200, [
{
number: 2,
html_url: "sample-url",
},
]);

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"test-commenter",
"@issue-assigner claim"
),
});
});
});

test("unassigns an issue if requested to unassign and all OK!", async () => {
mock = setupNock(
"@assignee1 You have been unassigned to this issue successfully."
)
// Unassign the issue
.delete("/repos/test-owner/test-repo/issues/1/assignees", (body) => {
assert.deepStrictEqual(body, { assignees: ["assignee1"] });
return true;
})
.reply(201);

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"assignee1",
"@issue-assigner abandon",
true
),
});
});

test("doesn't unassign an issue if requested to unassign and already not assigned", async () => {
mock = setupNock("@test-commenter You were not assigned to this issue.");

await probot.receive({
name: "issue_comment",
payload: createPayloadIssueComment(
"test-commenter",
"@issue-assigner abandon",
true
),
});
});

afterEach(() => {
assert.deepStrictEqual(mock.activeMocks(), []);
nock.cleanAll();
nock.enableNetConnect();
});
});
Loading

0 comments on commit c351c71

Please sign in to comment.