Skip to content

Commit

Permalink
requested changes are done participants logic in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dgparmar14 committed Jul 22, 2024
1 parent b89aaeb commit b67e6ee
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 60 deletions.
4 changes: 2 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export default async function Home() {
Discussions
</h2>
<Link
href="/discussion"
className="flex items-center gap-1 rounded px-3 py-2 text-secondary-400 underline underline-offset-2 transition-all duration-200 ease-in-out hover:gap-2 hover:text-primary-200"
href="/discussions"
className="flex items-center gap-1 rounded p-2 text-secondary-400 underline underline-offset-2 transition-all duration-200 ease-in-out hover:gap-2 hover:text-primary-200"
>
More
<MdOutlineArrowForwardIos />
Expand Down
20 changes: 12 additions & 8 deletions components/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import remarkGfm from "remark-gfm";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import clsx from "clsx";
export default async function Markdown(props: {
import { useMemo } from "react";

export default function Markdown(props: {
children: string;
className?: string;
}) {
const result = await unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeStringify)
.process(props.children || "");
const processedMarkdown = useMemo(() => {
return unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkRehype)
.use(rehypeStringify)
.processSync(props.children || ""); // Using processSync for efficiency
}, [props.children]);

return (
<div className="prose font-inter text-sm leading-relaxed dark:prose-invert prose-h2:mt-3 sm:text-base xl:text-left">
<div
className={clsx(props.className ?? "")}
dangerouslySetInnerHTML={{ __html: result.toString() }}
dangerouslySetInnerHTML={{ __html: processedMarkdown.toString() }}
/>
</div>
);
Expand Down
6 changes: 4 additions & 2 deletions components/contributors/GithubActivity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,9 @@ let icon = (type: string) => {
/>
</svg>
);
case "discussion":
case "discussion_answered":
case "discussion_comment_created":
case "discussion_created":
return <IoIosChatboxes className="size-5 text-secondary-700" />;
default:
return (
Expand Down Expand Up @@ -373,7 +375,7 @@ export default function GithubActivity({ activityData }: Props) {
const [start, end] = parseDateRangeSearchParam(searchParams.get("between"));

const updateSearchParam = (key: string, value?: string) => {
const current = new URLSearchParams(searchParams);
const current = new URLSearchParams(searchParams.toString());
if (!value) {
current.delete(key);
} else {
Expand Down
2 changes: 1 addition & 1 deletion components/discussions/FilterDiscussions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const FilterDiscussions = ({ categories }: Props) => {
const [start, end] = parseDateRangeSearchParam(searchParams.get("between"));

const updateSearchParam = (key: string, value: string) => {
const current = new URLSearchParams(searchParams);
const current = new URLSearchParams(searchParams.toString());
if (!value) {
current.delete(key);
} else {
Expand Down
2 changes: 1 addition & 1 deletion components/discussions/GithubDiscussion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const GithubDiscussion = ({
{/* Right side */}
<div className="ml-2">
{/* Title and Time */}
<div className={`flex items-center justify-end `}>
<div className={`flex items-center justify-start `}>
<div className={`${isProfilePage && "flex"} w-3/4`}>
<Link href={discussion.link}>
<p className="w-full text-lg font-semibold text-primary-900 dark:text-primary-100">
Expand Down
3 changes: 2 additions & 1 deletion config/GraduateAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { points } from "@/lib/api";
import { Contributor } from "@/lib/types";

export let professionalSelfSkills = [
Expand Down Expand Up @@ -314,7 +315,7 @@ export let resolveGraduateAttributes = (
case "empathy":
return resolveLevel(
attribute,
contributor.highlights.discussion_answered / 5,
contributor.highlights.discussion_created / points.discussion_answered,
);
default:
return { ...attribute };
Expand Down
2 changes: 1 addition & 1 deletion lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const root = join(process.cwd(), "data-repo/contributors");
const slackRoot = join(process.cwd(), "data-repo/data/slack");
const githubRoot = join(process.cwd(), "data-repo/data/github");

const points = {
export const points = {
comment_created: 1,
issue_assigned: 1,
pr_reviewed: 4,
Expand Down
67 changes: 44 additions & 23 deletions lib/discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ interface Participant {
};
};
}
interface Dicussion {
repository: {
discussion: {
answer: {
author: {
login: string;
};
};
};
};
}

const root = join(process.cwd(), "data-repo/data/github/discussions");

Expand All @@ -31,10 +42,14 @@ export async function fetchParticipants(discussion: ParsedDiscussion) {
const org = env.NEXT_PUBLIC_GITHUB_ORG;
const number = discussion.link?.split("/").pop() ?? "";

const participants: Participant = await octokit.graphql(`query {
repository(owner: "${org}", name: "${discussion.repoName}") {
discussion (number: ${number}) {
comments(first: 100) {
const query = `query($org: String!, $repoName: String!, $discussionNumber: Int!, $cursor: String) {
repository(owner: $org, name: $repoName) {
discussion(number: $discussionNumber) {
comments(first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
author {
Expand All @@ -45,7 +60,14 @@ export async function fetchParticipants(discussion: ParsedDiscussion) {
}
}
}
}`);
}
`;
const participants: Participant = await octokit.graphql.paginate(query, {
org,
repoName: discussion.repoName,
discussionNumber: Number(number),
cursor: null,
});
return Array.from(
new Set(
participants.repository.discussion.comments.edges.map(
Expand All @@ -55,6 +77,18 @@ export async function fetchParticipants(discussion: ParsedDiscussion) {
);
}

async function appendParticipantsToDiscussions(
discussions: ParsedDiscussion[],
) {
await Promise.all(
discussions.map(async (discussion) => {
if (!discussion.isAnswered)
discussion.participants = await fetchParticipants(discussion);
}),
);
return discussions;
}

export async function fetchGithubDiscussion(
noOfDiscussion?: number | null,
user?: string,
Expand Down Expand Up @@ -86,32 +120,19 @@ export async function fetchGithubDiscussion(
}
});

// get all particpants for github discussions
discussions.forEach(async (discussion) => {
// append participants to discussion
discussion.participants = await fetchParticipants(discussion);
});
let discussionsToReturn: ParsedDiscussion[] = discussions;

if (user) {
return discussions.filter(
discussionsToReturn = discussions.filter(
(discussion) =>
(discussion.participants ?? []).includes(user) ||
discussion.author === user,
);
} else if (noOfDiscussion) {
discussionsToReturn = discussions.slice(0, noOfDiscussion);
}

return noOfDiscussion ? discussions.slice(0, noOfDiscussion) : discussions;
}
interface Dicussion {
repository: {
discussion: {
answer: {
author: {
login: string;
};
};
};
};
return await appendParticipantsToDiscussions(discussionsToReturn);
}

export async function checkAnsweredByUser(
Expand Down
37 changes: 24 additions & 13 deletions scraper/src/github-scraper/discussion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parseISO, startOfDay, subDays } from "date-fns";
import { octokit } from "./config.js";
import { Discussion, ParsedDiscussion } from "./types.js";
import { saveDiscussionData } from "./utils.js";
Expand All @@ -24,10 +23,20 @@ query($org: String!, $cursor: String) {
login
}
url
isAnswered
category{
name
emojiHTML
}
comments(first: 10) {
edges {
node {
author {
login
}
}
}
}
createdAt
}
}
Expand All @@ -39,7 +48,7 @@ query($org: String!, $cursor: String) {
}
`;

async function fetchDiscussionsForOrg(org: string, cursor = null) {
async function fetchGitHubDiscussions(org: string, cursor = null) {
const variables = { org, cursor };
const response = await octokit.graphql.paginate(query, variables);

Expand All @@ -57,12 +66,9 @@ async function fetchDiscussionsForOrg(org: string, cursor = null) {

async function parseDiscussionData(
allDiscussions: { repoName: string; discussions: Discussion[] }[],
date: string,
numDays: number,
endDate: Date,
startDate: Date,
) {
const endDate: Date = startOfDay(parseISO(date));
const startDate: Date = startOfDay(subDays(endDate, numDays));

const parsedDiscussions: ParsedDiscussion[] = allDiscussions.flatMap(
(repo) => {
const filteredDiscussions = repo.discussions.filter((d) => {
Expand All @@ -71,17 +77,22 @@ async function parseDiscussionData(
});

return filteredDiscussions.map((d) => {
const participants = Array.from(
new Set(d.node.comments.edges.map((c) => c.node.author.login)),
);
return {
source: "github",
title: d.node.title,
text: d.node.body,
author: d.node.author.login,
link: d.node.url,
isAnswered: d.node.isAnswered,
time: d.node.createdAt,
category: {
name: d.node.category.name,
emoji: d.node.category.emojiHTML.replace(/<\/?div>/g, ""),
},
participants: participants,
repoName: repo.repoName,
};
});
Expand All @@ -91,18 +102,18 @@ async function parseDiscussionData(
return parsedDiscussions;
}

export async function fetchAllDiscussionEventsByOrg(
export async function scrapeDiscussions(
organizationName: string,
dataDir: string,
date: string,
numDays: number = 1,
endDate: Date,
startDate: Date,
) {
try {
const allDiscussions = await fetchDiscussionsForOrg(organizationName);
const allDiscussions = await fetchGitHubDiscussions(organizationName);
const parsedDiscussions = await parseDiscussionData(
allDiscussions,
date,
numDays,
endDate,
startDate,
);
await saveDiscussionData(parsedDiscussions, dataDir);
} catch (error: any) {
Expand Down
16 changes: 8 additions & 8 deletions scraper/src/github-scraper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import { IGitHubEvent, ProcessData } from "./types.js";
import { fetchEvents } from "./fetchEvents.js";
import { parseEvents } from "./parseEvents.js";
import { mergedData } from "./saveData.js";
import { fetchAllDiscussionEventsByOrg } from "./discussion.js";
import { scrapeDiscussions } from "./discussion.js";

let processedData: ProcessData = {};

const scrapeGitHub = async (
org: string,
date: string,
numDays: number = 1,
orgName: string,
endDate: Date,
startDate: Date,
): Promise<void> => {
const endDate: Date = startOfDay(parseISO(date));
const startDate: Date = startOfDay(subDays(endDate, numDays));
console.log(
`Scraping GitHub data for ${org} from ${formatISO(startDate)} to ${formatISO(endDate)}`,
);
Expand Down Expand Up @@ -75,9 +72,12 @@ const main = async () => {
console.error("Invalid date value:", dateArg);
process.exit(1);
}
await scrapeGitHub(orgName, date, Number(numDays), orgName);
const endDate: Date = startOfDay(parseISO(date));
const startDate: Date = startOfDay(subDays(endDate, Number(numDays)));

await scrapeGitHub(orgName, endDate, startDate);
await mergedData(dataDir, processedData);
await fetchAllDiscussionEventsByOrg(orgName, dataDir, date, Number(numDays));
await scrapeDiscussions(orgName, dataDir, endDate, startDate);

console.log("Done");
};
Expand Down
2 changes: 2 additions & 0 deletions scraper/src/github-scraper/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export interface AuthoredIssueAndPr {

export type Discussion = {
node: {
isAnswered: Boolean;
title: string;
body: string;
author: {
Expand Down Expand Up @@ -203,6 +204,7 @@ export type ParsedDiscussion = {
text: string;
author: string;
link: string;
isAnswered: Boolean;
time: string;
category?: {
name: string;
Expand Down

0 comments on commit b67e6ee

Please sign in to comment.