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 13, 2024
1 parent 0b23fcf commit aa07666
Show file tree
Hide file tree
Showing 21 changed files with 395 additions and 396 deletions.
5 changes: 4 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ export default async (app, { getRouter }) => {
app.on("issues.opened", issue_opened);

// Comment is created in an issue by a user
app.on("issue_comment.created", issue_comment_created);
app.on(
["issue_comment.created", "issue_comment.edited"],
issue_comment_created
);
};
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
27 changes: 22 additions & 5 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 @@ -17,8 +25,17 @@ export default async (context) => {
issue_opener_username
);
if (issue_opener === OpenerIsMaintainer.SKIP) return;
const issueComment = context.issue({
body: `@${issue_opener} ` + config[issue_opener],
});
return await context.octokit.issues.createComment(issueComment);
if (issue_opener === OpenerIsMaintainer.YES) {
return await context.octokit.issues.createComment(
context.issue({
body: config[issue_opener],
})
);
} else {
return await context.octokit.issues.createComment(
context.issue({
body: `@${issue_opener_username} ` + config[issue_opener],
})
);
}
};
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);
258 changes: 0 additions & 258 deletions test/fixtures/context.txt

This file was deleted.

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
}
}
Loading

0 comments on commit aa07666

Please sign in to comment.