Skip to content

Commit

Permalink
Merge pull request #222 from wizeline/feat/210/GITHUB-weekly-particip…
Browse files Browse the repository at this point in the history
…ation-by-project

Feat/210/GitHub weekly participation by project
  • Loading branch information
martinrobled0 authored Oct 11, 2023
2 parents 818a22e + 2fc2d02 commit e9a7484
Show file tree
Hide file tree
Showing 12 changed files with 8,000 additions and 24,673 deletions.
51 changes: 51 additions & 0 deletions app/core/components/GitHub/GitHubActivity/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Card, CardContent, TableContainer, Table, TableHead, TableRow, TableCell, TableBody } from "@mui/material";

interface gitHubActivitySchema {
id: string,
typeEvent: string,
created_at: Date,
author: string,
avatar_url:string,
projectId:string | null,
}

export default function GitHubActivity({ repoName, projectId, activityData }: { repoName: string, projectId: string, activityData: gitHubActivitySchema[] }) {
return (
<Card sx={{ width: 1, overflowY: 'scroll', height: 600 }}>
<CardContent>
<TableContainer >
<Table>
<TableHead>
<TableRow>
<TableCell align="left">Event Type</TableCell>
<TableCell align="right">Author</TableCell>
<TableCell align="right">Created At</TableCell>
</TableRow>
</TableHead>
<TableBody>

{
activityData && activityData.map(event => (
<TableRow
key={event.id}
sx={{ '&:last-child td, &:last-child th': { border: 0 } }}
>
<TableCell component="th" scope="row">
{event.typeEvent}
</TableCell>
<TableCell align="right">{event.author}</TableCell>
<TableCell align="right">{ new Intl.DateTimeFormat([], {
year: "numeric",
month: "long",
day: "2-digit",
}).format(new Date(event.created_at))}</TableCell>
</TableRow>
))
}
</TableBody>
</Table>
</TableContainer>
</CardContent>
</Card>
);
}
31 changes: 31 additions & 0 deletions app/githubUpdates.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getActivity } from "./routes/api/github/get-proyectActivity";
import { PrismaClient } from "@prisma/client";

const db = new PrismaClient();


// export async function searchLastUpdateProjects() {

// const lastUpdate = await prisma.$queryRaw<any[]>`SELECT DISTINCT p."id", p."name", p."projectBoard", MAX(ga.created_at) from "Projects" p
// RIGHT JOIN "GitHubActivity" ga on p."id" = ga."projectId"
// WHERE p."isArchived" = FALSE
// GROUP BY p."id", p."name", p."projectBoard"
// `;


// } I will use it later jeje

export async function getGitHubActivity() {
const projectsBoards = await db.$queryRaw<any[]>`
SELECT p."id", p."name", r.url from "Projects" p
RIGHT JOIN "Repos" r on p."id" = r."projectId"
WHERE p."isArchived" = FALSE and r."url" is not null
`;

try{
projectsBoards.map(async board => await getActivity(board.url, board.id).catch((e) => {throw(e)}) )
}catch(e){
throw (e);
}

}
47 changes: 47 additions & 0 deletions app/models/githubactivity.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { prisma } from "../db.server";
import type { PrismaClient } from "@prisma/client";

interface gitHubActivityChartType {
count: number,
typeEvent: string,
}

export async function saveActivity(
id: string,
typeEvent: string,
created_at: string,
author: string,
avatar_url: string,
projectId: string,
db?: PrismaClient
){

const dbConnection = db ? db : prisma;

const activityRegister = await dbConnection.gitHubActivity.findFirst({ where: { id } });

if(!activityRegister) {

return await dbConnection.gitHubActivity.create({
data: {
id,
typeEvent,
created_at: new Date(created_at),
author,
avatar_url,
projectId
}
})
}
}


export async function getGitActivityData(projectId: string) {
return await prisma.gitHubActivity.findMany({ where: { projectId }, orderBy: { id: "desc" }});
}

export const getActivityStadistic = async (week: number) => {
return await prisma.$queryRaw<gitHubActivityChartType[]>`SELECT Count(*)::int, "typeEvent" FROM "GitHubActivity" where date_part('week', "created_at")=${week} GROUP BY "typeEvent"`;

}

48 changes: 48 additions & 0 deletions app/routes/api/github/get-proyectActivity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Octokit } from "@octokit/core";
import { env } from "process";
import { saveActivity } from "../../../models/githubactivity.server";
import { PrismaClient } from "@prisma/client";
const octokit = new Octokit({ auth: env.GITHUB_KEY });

const db = new PrismaClient();

function cleanUrlRepo(repoInfo: string) {
if (repoInfo) {
return repoInfo.substring(repoInfo.lastIndexOf("/") + 1);
} else {
return "";
}
}

export const getActivity = async (repo: string, projectId: string) => {

const owner = "wizeline";
const repoUrlClean = cleanUrlRepo(repo);
if(repo != ''){
try{
const repoActivity = await octokit.request(`GET /repos/${owner}/${repoUrlClean}/events`, {
owner,
repo,
}).catch((e) => { throw(e)});


if(repoActivity.data.length){
repoActivity.data?.forEach( (activity: { id: string; type: string; created_at: string; actor: { display_login: string; avatar_url: string; }; }) => {
saveActivity(activity.id ,
activity.type?.replace(/([a-z0-9])([A-Z])/g, '$1 $2') as string, //this is for separe the string with camel case into pieces
activity.created_at as string, activity.actor.display_login as string,
activity.actor.avatar_url as string, projectId, db );
return;
});
}


}catch(e){
console.error(e);
}

}else{
return;
}
};

Loading

0 comments on commit e9a7484

Please sign in to comment.