Skip to content

Commit

Permalink
chore: manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsley-einstein committed Nov 27, 2024
1 parent 7368a20 commit 1aae776
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 53 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ script.ts
.wrangler
test-dashboard.md
.platform*
*Dockerfile*
*Dockerfile*
.dockerignore
12 changes: 3 additions & 9 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
{
"name": "scan-contributions",
"description": "UbiquityOS plugin for scanning contributions based on issues/pulls.",
"ubiquity:listeners": [
"issue_comment.created",
"issue_comment.edited",
"issue_comment"
],
"ubiquity:listeners": ["issue_comment.created", "issue_comment.edited", "issue_comment"],
"commands": {
"scanContributions": {
"ubiquity:example": "/scanContributions",
Expand All @@ -25,8 +21,6 @@
"type": "string"
}
},
"required": [
"configurableResponse"
]
"required": ["configurableResponse"]
}
}
}
103 changes: 63 additions & 40 deletions src/handlers/scan-contributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,8 @@ export async function scanContributions(context: Context) {

const issueTimelineEvents = await octokit.paginate(octokit.issues.listEventsForTimeline, { owner, repo, issue_number: issueNumber });
const issueEvents = await octokit.paginate(octokit.issues.listEvents, { owner, repo, issue_number: issueNumber });
const issueReviewsEvents = payload.issue.pull_request
? await octokit.paginate(octokit.pulls.listReviews, { owner, repo, pull_number: issueNumber })
: await octokit.paginate(octokit.issues.listComments, { owner, repo, issue_number: issueNumber });
const issueReactionEvents = await octokit.paginate(octokit.reactions.listForIssue, { owner, repo, issue_number: issueNumber });
const issueCommentsReactionEvents = await Promise.all(
(await octokit.paginate(octokit.issues.listComments, { owner, repo, issue_number: issueNumber })).map((comment) =>
octokit.paginate(octokit.reactions.listForIssueComment, { owner, repo, comment_id: comment.id })
)
);
const issueReviewCommentsReactionEvents = payload.issue.pull_request
? await Promise.all(
(await octokit.paginate(octokit.pulls.listReviews, { owner, repo, pull_number: issueNumber })).map((comment) =>
octokit.paginate(octokit.reactions.listForIssueComment, { owner, repo, comment_id: comment.id })
)
)
: await Promise.all(
(await octokit.paginate(octokit.issues.listComments, { owner, repo, issue_number: issueNumber })).map((comment) =>
octokit.paginate(octokit.reactions.listForIssueComment, { owner, repo, comment_id: comment.id })
)
);
const issueCommentEvents = await octokit.paginate(octokit.issues.listComments, { owner, repo, issue_number: issueNumber });

issueTimelineEvents.forEach((ev) => {
if ("actor" in ev && ev.actor && store[ev.actor.login]) {
Expand All @@ -65,37 +47,62 @@ export async function scanContributions(context: Context) {
}
});

issueReviewsEvents.forEach((ev) => {
const identifier = "state" in ev ? ev.state : "commented";
if (ev.user && store[ev.user.login]) {
if (!store[ev.user.login][identifier]) store[ev.user.login][identifier] = 1;
else store[ev.user.login][identifier] += 1;
}
});

issueReactionEvents.forEach((ev) => {
if (ev.user && store[ev.user.login]) {
if (!store[ev.user.login][ev.content]) store[ev.user.login][ev.content] = 1;
else store[ev.user.login][ev.content] += 1;
}
});

issueCommentsReactionEvents.forEach((event) => {
event.forEach((ev) => {
if (ev.user && store[ev.user.login]) {
if (!store[ev.user.login][ev.content]) store[ev.user.login][ev.content] = 1;
else store[ev.user.login][ev.content] += 1;
for (const issueCommentEvent of issueCommentEvents) {
const reactions = await octokit.paginate(octokit.reactions.listForIssueComment, { owner, repo, comment_id: issueCommentEvent.id });

reactions.forEach((reaction) => {
if (reaction.user && store[reaction.user.login]) {
if (!store[reaction.user.login][reaction.content]) store[reaction.user.login][reaction.content] = 1;
else store[reaction.user.login][reaction.content] += 1;
}
});
});
issueReviewCommentsReactionEvents.forEach((event) => {
event.forEach((ev) => {
if (ev.user && store[ev.user.login]) {
if (!store[ev.user.login][ev.content]) store[ev.user.login][ev.content] = 1;
else store[ev.user.login][ev.content] += 1;
}

if (payload.issue.pull_request) {
const pullReviews = await octokit.paginate(octokit.pulls.listReviews, { owner, repo, pull_number: issueNumber });
const pullReviewComments = await octokit.paginate(octokit.pulls.listReviewComments, { owner, repo, pull_number: issueNumber });

for (const pullReview of pullReviews) {
if (pullReview.user && store[pullReview.user.login]) {
if (!store[pullReview.user.login][pullReview.state]) store[pullReview.user.login][pullReview.state] = 1;
else store[pullReview.user.login][pullReview.state] += 1;
}
});
});

const reactions = await octokit.paginate(octokit.reactions.listForPullRequestReviewComment, { owner, repo, comment_id: pullReview.id });

reactions.forEach((reaction) => {
if (reaction.user && store[reaction.user.login]) {
if (!store[reaction.user.login][reaction.content]) store[reaction.user.login][reaction.content] = 1;
else store[reaction.user.login][reaction.content] += 1;
}
});
}

for (const pullReviewComment of pullReviewComments) {
const identifier = "response_to_review_" + pullReviewComment.pull_request_review_id;

if (pullReviewComment.user && store[pullReviewComment.user.login]) {
if (!store[pullReviewComment.user.login][identifier]) store[pullReviewComment.user.login][identifier] = 1;
else store[pullReviewComment.user.login][identifier] += 1;
}

const reactions = await octokit.paginate(octokit.reactions.listForPullRequestReviewComment, { owner, repo, comment_id: pullReviewComment.id });

reactions.forEach((reaction) => {
if (reaction.user && store[reaction.user.login]) {
if (!store[reaction.user.login][reaction.content]) store[reaction.user.login][reaction.content] = 1;
else store[reaction.user.login][reaction.content] += 1;
}
});
}
}

logger.info("Contributions stats: ", store);
const octokitCommentBody = "```json\n" + JSON.stringify(store, undefined, 2) + "\n```";
Expand All @@ -108,9 +115,25 @@ export async function scanContributions(context: Context) {
});
} catch (error) {
if (error instanceof Error) {
const octokitCommentBody =
"An error occured while scanning this repository\n ```json\n" + JSON.stringify({ error: error, stack: error.stack }, undefined, 2) + "\n```";
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: octokitCommentBody,
});
logger.error(`Error creating comment:`, { error: error, stack: error.stack });
throw error;
} else {
const octokitCommentBody =
"An error occured while scanning this repository\n ```json\n" + JSON.stringify({ err: error, error: new Error() }, undefined, 2) + "\n```";
await octokit.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: octokitCommentBody,
});
logger.error(`Error creating comment:`, { err: error, error: new Error() });
throw error;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Plugin tests", () => {
expect(content).toEqual(manifest);
});

it("Should handle an issue comment /scan-contributions", async () => {
it("Should handle an issue comment /scanContributions", async () => {
const issues = db.issue.getAll();
const issueNumber = issues[issues.length - 1].number;

Expand All @@ -49,10 +49,10 @@ describe("Plugin tests", () => {
createContext(STRINGS.CONFIGURABLE_RESPONSE, "Hello world, again!", 1, 2, 3, issueNumber);
createContext(STRINGS.CONFIGURABLE_RESPONSE, "Hello world, for the third time.", 1, 1, 4, issueNumber);

const { context, infoSpy } = createContext(STRINGS.CONFIGURABLE_RESPONSE, "/scan-contributions", 1, 1, 1, issueNumber);
const { context, infoSpy } = createContext(STRINGS.CONFIGURABLE_RESPONSE, "/scanContributions", 1, 1, 1, issueNumber);

expect(context.eventName).toBe(ISSUE_COMMENT_CREATED);
expect(context.payload.comment.body).toBe("/scan-contributions");
expect(context.payload.comment.body).toBe("/scanContributions");

await runPlugin(context);
expect(infoSpy).toHaveBeenNthCalledWith(1, STRINGS.SCANNING_EVENTS);
Expand Down

0 comments on commit 1aae776

Please sign in to comment.