Skip to content

Commit

Permalink
Merge pull request ubiquity-os-marketplace#119 from gentlementlegen/f…
Browse files Browse the repository at this point in the history
…ix/ignore-hidden-comments
  • Loading branch information
0x4007 authored Sep 17, 2024
2 parents e46ac5c + fccde9f commit 8227710
Show file tree
Hide file tree
Showing 16 changed files with 1,555 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/github-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { RestEndpointMethodTypes } from "@octokit/rest";

export type GitHubIssue = RestEndpointMethodTypes["issues"]["get"]["response"]["data"];
export type GitHubPullRequest = RestEndpointMethodTypes["pulls"]["get"]["response"]["data"];
export type GitHubIssueComment = RestEndpointMethodTypes["issues"]["listComments"]["response"]["data"][0];
export type GitHubIssueComment = RestEndpointMethodTypes["issues"]["listComments"]["response"]["data"][0] & {
isMinimized?: boolean;
};
export type GitHubIssueEvent = RestEndpointMethodTypes["issues"]["listEvents"]["response"]["data"][0];
export type GitHubTimelineEvent = RestEndpointMethodTypes["issues"]["listEventsForTimeline"]["response"]["data"][0];
export type GitHubRepository = RestEndpointMethodTypes["repos"]["get"]["response"]["data"];
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/get-comment-details.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { GitHubIssueComment } from "../github-types";
import { getOctokitInstance } from "../octokit";
import { IssueComment } from "@octokit/graphql-schema";
import { QUERY_COMMENT_DETAILS } from "../types/requests";

export async function getMinimizedCommentStatus(comments: GitHubIssueComment[]) {
const octokit = getOctokitInstance();
const commentsData = await octokit.graphql<{ nodes?: IssueComment[] }>(QUERY_COMMENT_DETAILS, {
node_ids: comments.map((o) => o.node_id),
});

if (commentsData.nodes?.length) {
for (const commentNode of commentsData.nodes) {
const comment = comments.find((o) => o.node_id === commentNode.id);
// For each comment we add the 'isMinimized' info, which corresponds to a collapsed comment
if (comment) {
comment.isMinimized = commentNode.isMinimized;
}
}
}
}
6 changes: 6 additions & 0 deletions src/parser/data-purge-module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Value } from "@sinclair/typebox/value";
import configuration from "../configuration/config-reader";
import { DataPurgeConfiguration, dataPurgeConfigurationType } from "../configuration/data-purge-config";
import logger from "../helpers/logger";
import { IssueActivity } from "../issue-activity";
import { Module, Result } from "./processor";

Expand All @@ -20,6 +21,11 @@ export class DataPurgeModule implements Module {

async transform(data: Readonly<IssueActivity>, result: Result) {
for (const comment of data.allComments) {
// Skips comments if they are minimized
if ("isMinimized" in comment && comment.isMinimized) {
logger.debug("Skipping hidden comment", { comment });
continue;
}
if (comment.body && comment.user?.login && result[comment.user.login]) {
const newContent = comment.body
// Remove quoted text
Expand Down
7 changes: 6 additions & 1 deletion src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
GitHubTimelineEvent,
GitHubUser,
} from "./github-types";
import { getMinimizedCommentStatus } from "./helpers/get-comment-details";

// async function main(gitHubIssueUrl: GitHubIssue["html_url"]) {
// const issueParams = parseGitHubUrl(gitHubIssueUrl);
Expand Down Expand Up @@ -80,7 +81,11 @@ export async function getIssueEvents(issueParams: IssueParams): Promise<GitHubIs

export async function getIssueComments(issueParams: IssueParams): Promise<GitHubIssueComment[]> {
const octokit = getOctokitInstance();
return await octokit.paginate(octokit.issues.listComments.endpoint.merge(issueParams));
const comments: GitHubIssueComment[] = await octokit.paginate(
octokit.issues.listComments.endpoint.merge(issueParams)
);
await getMinimizedCommentStatus(comments);
return comments;
}
export async function getPullRequestReviews(pullParams: PullParams): Promise<GitHubPullRequestReviewState[]> {
const octokit = getOctokitInstance();
Expand Down
11 changes: 11 additions & 0 deletions src/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ export const LINKED_PULL_REQUESTS = /* GraphQL */ `
}
}
`;

export const QUERY_COMMENT_DETAILS = /* GraphQL */ `
query commentDetails($node_ids: [ID!]!) {
nodes(ids: $node_ids) {
... on IssueComment {
id
isMinimized
}
}
}
`;
12 changes: 12 additions & 0 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { http, HttpResponse } from "msw";
import { db } from "./db";
import issue22CommentsGet from "./routes/issue-22-comments-get.json";
import issue22Get from "./routes/issue-22-get.json";
import issue13Get from "./routes/issue-13-get.json";
import issue25CommentsGet from "./routes/issue-25-comments-get.json";
import issue69EventsGet from "./routes/issue-69-events-get.json";
import issue13EventsGet from "./routes/issue-13-events-get.json";
import issue69CommentsGet from "./routes/issue-69-comments-get.json";
import issue13CommentsGet from "./routes/issue-13-comments-get.json";
import issue69Get from "./routes/issue-69-get.json";
import issueEvents2Get from "./routes/issue-events-2-get.json";
import issueEventsGet from "./routes/issue-events-get.json";
Expand All @@ -20,6 +23,9 @@ import pullsReviewsGet from "./routes/pulls-reviews-get.json";
* Intercepts the routes and returns a custom payload
*/
export const handlers = [
http.get("https://api.github.com/repos/Meniole/conversation-rewards/issues/13", () => {
return HttpResponse.json(issue13Get);
}),
http.get("https://api.github.com/repos/ubiquibot/comment-incentives/issues/22", () => {
return HttpResponse.json(issue22Get);
}),
Expand All @@ -38,12 +44,18 @@ export const handlers = [
http.get("https://api.github.com/repos/ubiquibot/comment-incentives/issues/22/comments", () => {
return HttpResponse.json(issue22CommentsGet);
}),
http.get("https://api.github.com/repos/Meniole/conversation-rewards/issues/13/events", () => {
return HttpResponse.json(issue13EventsGet);
}),
http.get("https://api.github.com/repos/ubiquity/work.ubq.fi/issues/69/comments", () => {
return HttpResponse.json(issue69CommentsGet);
}),
http.get("https://api.github.com/repos/ubiquibot/comment-incentives/issues/25/comments", () => {
return HttpResponse.json(issue25CommentsGet);
}),
http.get("https://api.github.com/repos/Meniole/conversation-rewards/issues/13/comments", () => {
return HttpResponse.json(issue13CommentsGet);
}),
http.get("https://api.github.com/repos/ubiquibot/comment-incentives/issues/22/timeline", () => {
return HttpResponse.json(issueTimelineGet);
}),
Expand Down
26 changes: 26 additions & 0 deletions tests/__mocks__/results/hidden-comment-purged.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"gentlementlegen": {
"comments": [
{
"content": "Issue",
"id": 2524890317,
"type": "ISSUE_SPECIFICATION",
"url": "https://github.com/Meniole/conversation-rewards/issues/13"
}
],
"total": 0,
"userId": 9807008
},
"ubiquity-ubiquibot": {
"comments": [
{
"content": "Other comment.",
"id": 2349004935,
"type": "ISSUE_CONTRIBUTOR",
"url": "https://github.com/Meniole/conversation-rewards/issues/13#issuecomment-2349004935"
}
],
"total": 0,
"userId": 163369652
}
}
Loading

0 comments on commit 8227710

Please sign in to comment.