Skip to content

Commit

Permalink
Merge pull request #6 from danmooozi/main-commit
Browse files Browse the repository at this point in the history
사용자 커밋리스트 페이지네이션 추가
  • Loading branch information
gitdog01 authored Jan 1, 2024
2 parents 8479ae8 + 580a396 commit 3d788ad
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 50 deletions.
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

bunx lint-staged
4 changes: 4 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.ts": ["prettier --write"],
"*.js": ["prettier --write"]
}
Binary file modified bun.lockb
Binary file not shown.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"type": "module",
"scripts": {
"start": "bun run src/app.ts",
"dev": "bun --hot run src/app.ts"
"dev": "bun --watch run src/app.ts",
"prepare": "husky install"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/swagger-jsdoc": "^6.0.3",
"@types/swagger-ui-express": "^4.1.6",
"bun-types": "latest"
"bun-types": "latest",
"husky": "^8.0.0",
"lint-staged": "^15.2.0"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
55 changes: 18 additions & 37 deletions src/routes/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import express from "express";
import axios from "axios";
import { getUser } from "@/services/user";
import express from 'express';
import axios from 'axios';
import { getUser } from '@/services/user';
import {
getUserCommits,
getUserRepoStars,
getFriendsCount,
} from "@/services/index";
import { getRepoCommits } from "@/services/commit";
} from '@/services/index';
import { getRepoCommits } from '@/services/commit';

const router = express.Router();

router.get("/callback", async (req, res) => {
router.get('/callback', async (req, res) => {
const { code } = req.query;

const result = await axios({
method: "post",
method: 'post',
url: `https://github.com/login/oauth/access_token`,
headers: {
accept: "application/json",
accept: 'application/json',
},
data: {
client_id: Bun.env.CLIENT_ID,
Expand All @@ -34,53 +34,34 @@ router.get("/callback", async (req, res) => {
res.json(true);
});

router.get("/test", async (req, res) => {
const accessToken = "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";
router.get('/data', async (req, res) => {
const { accessToken } = req.query;

const user = await getUser(accessToken);
const userName = user.login;

/* const data = await getUserCommits(accessToken, userName, {
const commits = await getUserCommits(accessToken, userName, {
since: '2023-01-01T00:00:00Z',
until: '2024-01-01T00:00:00Z',
per_page: 100,
}); */
const data = await getRepoCommits(
accessToken,
"rolled-potatoes",
"utterance"
);

res.json(data);
});

router.get("/data", async (req, res) => {
const { accessToken } = req.body;

const user = await getUser(accessToken);
const userName = user.login;

const commits = await getUserCommits(accessToken, userName, {
since: "2023-01-01T00:00:00Z",
until: "2024-01-01T00:00:00Z",
per_page: 100,
});

const starCount = await getUserRepoStars(accessToken, userName);
const { followerCount, followingCount } = await getFriendsCount(accessToken);

const mostUsedLanguage = ["JavaScript", "TypeScript", "Python"]; // string[]
const moreThan = "high"; // high, middle, low
const mostUsedLanguage = ['JavaScript', 'TypeScript', 'Python']; // string[]
const moreThan = 'high'; // high, middle, low
const commitCount = 1234; // number
const commitDate = "월요일"; // 월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
const commitDate = '월요일'; // 월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
const mostCommunication = {
// 여기 줄때 github profile 주소도 주면 좋지 않을까 ?
name: "bsy1141", // string
image: "https://avatars.githubusercontent.com/u/60652298?v=4", // string
name: 'bsy1141', // string
image: 'https://avatars.githubusercontent.com/u/60652298?v=4', // string
};
const mbti = "INFP"; // string
const mbti = 'INFP'; // string

res.json({
commits,
starCount,
followerCount,
followingCount,
Expand Down
78 changes: 67 additions & 11 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,87 @@
import { getRepos, getRepoStars } from "./repo";
import { getRepoCommits } from "./commit/";
import { getUser } from "./user";
import { getRepos, getRepoStars } from './repo';
import { getRepoCommits } from './commit/';
import { getUser } from './user';

async function getTotalRepoCommits({
ownerName,
repoName,
accessToken,
query,
}: {
[key: string]: any;
}) {
const mergedQuery = {
...query,
page: 1,
per_page: 100,
};
let res: any = [];

try {
while (true) {
const data = await getRepoCommits(
accessToken,
ownerName,
repoName,
mergedQuery,
);
if (!data) return [];
res = res.concat(data);
const lastCommitItem = data.slice(-1);
const hasMorePage = lastCommitItem[0]?.parents
? lastCommitItem[0].parents.length > 0
: false;
if (hasMorePage) {
mergedQuery.page += 1;
} else {
break;
}
}
} catch (e) {
//TODO: github api error 처리
}

return res;
}

//parents 가 빈 오브젝으면 페이지 네이션끝
export const getUserCommits = async (
accessToken: string,
ownerName: string,
query?: any
query?: any,
) => {
const repositoryObject: Record<string, any> = {};

const repos = await getRepos(accessToken, ownerName);
const queue = repos.map((repo: any) => {

const jobs = repos.map(async (repo: any) => {
const ownerName = repo.owner.login;
const repoName = repo.name;
const repositoryName = repo.name;
const repositoryPath = `${ownerName}/${repositoryName}`;

if (!repositoryObject[repositoryPath]) {
repositoryObject[repositoryPath] = [];
}

return () => getRepoCommits(accessToken, ownerName, repoName, query);
repositoryObject[repositoryPath] = await getTotalRepoCommits({
ownerName,
accessToken,
query,
repoName: repositoryName,
});
return repositoryObject[repositoryPath];
});

const jobs = Promise.all(queue.slice(10, 15).map((q: any) => q()));
await Promise.all(jobs);

return jobs;
return repositoryObject;
};

export const getUserRepoStars = async (
accessToken: string,
ownerName: string
ownerName: string,
) => {
const repos = await getRepos(accessToken, ownerName);

const queue = repos.map((repo: any) => {
const ownerName = repo.owner.login;
const repoName = repo.name;
Expand Down

0 comments on commit 3d788ad

Please sign in to comment.