Skip to content

Commit

Permalink
Merge pull request #7 from danmooozi/develop-commit
Browse files Browse the repository at this point in the history
commit 날짜, commit Lang 완료
  • Loading branch information
gitdog01 authored Jan 28, 2024
2 parents de76cf4 + 979ac81 commit 7fcc4bf
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 26 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/axios": "^0.14.0",
"axios": "^1.6.2",
"express": "^4.18.2",
"octokit": "^3.1.2",
"prettier": "^3.1.1",
"query-string": "^8.1.0",
"swagger-jsdoc": "^6.2.8",
Expand Down
25 changes: 19 additions & 6 deletions src/routes/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import {
getUserRepoStars,
getFriendsCount,
} from "@/services/index";
import {
getMost3UsedLanguageInCommit,
getMostCommitDay,
getMyRepoCommits,
} from "@/services/commit";

const router = express.Router();

Expand Down Expand Up @@ -55,21 +60,29 @@ router.get("/data", async (req, res) => {
const user = await getUser(accessToken);
const userName = user.login;

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

console.log(commits);
// const commits = Object.values(commitsByRepo).flat(Infinity);

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 commitCount = 1234; // number
const commitDate = "월요일"; // 월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
const mostUsedLanguage = getMost3UsedLanguageInCommit(commits);
let moreThan = "high";
const commitCount = commits.length; // number
if (commitCount > 100) moreThan = "high";
else if (commitCount > 50) moreThan = "middle";
else moreThan = "low";

const mostCommitDay = getMostCommitDay(commits);
const commitDate = mostCommitDay; // 월요일, 화요일, 수요일, 목요일, 금요일, 토요일, 일요일
const mostCommunication = {
// 여기 줄때 github profile 주소도 주면 좋지 않을까 ?
name: "bsy1141", // string
Expand Down
208 changes: 192 additions & 16 deletions src/services/commit/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getGithubAxiosInstance } from '@/utils/github';
import qs from 'query-string';
import { Octokit } from "octokit";

type TQuery = {
[key: string]: any;
Expand All @@ -11,18 +10,195 @@ export const getRepoCommits = async (
repoName: string,
query: TQuery = {},
) => {
const instance = getGithubAxiosInstance(accessToken);
const url = qs.stringifyUrl(
{
url: `/repos/${ownerName}/${repoName}/commits`,
query,
},
{
skipNull: true,
skipEmptyString: true,
},
);
const { data } = await instance.get(url);

return data;
const octokit = new Octokit({
auth: accessToken,
});

/*
const { data } = await octokit.request("GET /repos/{owner}/{repo}/commits", {
owner: ownerName,
repo: repoName,
...query,
});
*/
const { data } = await octokit.request("GET /search/commits", {
q: `author:${ownerName} author-date:2023-01-01..2023-12-31`,
});

const myCommits = data.items;

console.log(myCommits);

const details = [];
for (const commit of myCommits) {
const { sha } = commit;
const { data } = await octokit.request(
"GET /repos/{owner}/{repo}/commits/{commit_sha}",
{
owner: ownerName,
repo: repoName,
commit_sha: sha,
},
);
details.push({
files: data.files,
date: data.commit.author.date,
message: data.commit.message,
parents: data.parents,
});
}

return details;
};

export const getMyRepoCommits = async (
accessToken: string,
userName: string,
) => {
let pagesRemaining = true;
let data: any[] = [];
let count = 1;
const octokit = new Octokit({
auth: accessToken,
});

while (pagesRemaining) {
const response = await octokit.request("GET /search/commits", {
q: `author:${userName} author-date:2023-01-01..2023-12-31`,
per_page: 100,
page: count,
});

data = [...data, ...response.data.items];

const linkHeader = response.headers.link;

pagesRemaining = !!(linkHeader && linkHeader.includes(`rel=\"next\"`));

if (pagesRemaining) {
count++;
}
}
console.log(data.length);
const details = [];
let remaining = data.length;
for (const commit of data) {
console.log(remaining--);
const { sha, repository } = commit;
const { data } = await octokit.request(
"GET /repos/{owner}/{repo}/commits/{commit_sha}",
{
owner: repository.full_name.split("/")[0],
repo: repository.full_name.split("/")[1],
commit_sha: sha,
},
);
details.push({
files: data.files,
date: data.commit.author.date,
message: data.commit.message,
parents: data.parents,
});
}

return details;
};

const extensionToLanguage = (extension: string) => {
const map = {
ts: "typescript",
js: "javascript",
py: "python",
go: "go",
java: "java",
rb: "ruby",
php: "php",
html: "html",
css: "css",
scss: "scss",
sass: "sass",
less: "less",
tsx: "typescript",
jsx: "javascript",
json: "json",
yml: "yaml",
yaml: "yaml",
md: "markdown",
sh: "shell",
sql: "sql",
kt: "kotlin",
cs: "csharp",
swift: "swift",
dart: "dart",
rs: "rust",
fs: "fsharp",
fsx: "fsharp",
fsi: "fsharp",
hs: "haskell",
ex: "elixir",
exs: "elixir",
erl: "erlang",
clj: "clojure",
cljs: "clojure",
cljc: "clojure",
edn: "clojure",
lua: "lua",
scala: "scala",
sc: "scala",
vue: "vue",
cpp: "cpp",
c: "c",
h: "c",
m: "objectivec",
mm: "objectivec",
};
return (map as { [key: string]: string })[extension];
};

export const getMost3UsedLanguageInCommit = (commits: any[]) => {
const languageMap = new Map();
for (const commit of commits) {
const { files } = commit;
for (const file of files) {
const { filename } = file;
const extension = filename.split(".").slice(-1)[0];
const language = extensionToLanguage(extension);
if (!language) continue;
if (languageMap.has(language)) {
languageMap.set(language, languageMap.get(language) + 1);
} else {
languageMap.set(language, 1);
}
}
}

const sorted = [...languageMap.entries()].sort((a, b) => b[1] - a[1]);
const mostUsedLanguage = sorted.slice(0, 3).map((item) => item[0]);
return mostUsedLanguage;
};

export const getMostCommitDay = (commits: any[]) => {
const dayMap = new Map();
for (const commit of commits) {
const { date } = commit;
const day = new Date(date).getDay();
if (dayMap.has(day)) {
dayMap.set(day, dayMap.get(day) + 1);
} else {
dayMap.set(day, 1);
}
}

const sorted = [...dayMap.entries()].sort((a, b) => b[1] - a[1]);
const mostCommitDay = sorted[0][0];
const dayMap2 = new Map([
[0, "일요일"],
[1, "월요일"],
[2, "화요일"],
[3, "수요일"],
[4, "목요일"],
[5, "금요일"],
[6, "토요일"],
]);
return dayMap2.get(mostCommitDay);
};
7 changes: 3 additions & 4 deletions src/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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,
Expand Down Expand Up @@ -91,7 +91,6 @@ export const getUserRepoStars = async (

const jobs = Promise.all(queue);
return (await jobs).reduce((acc, cur) => {
console.log(acc, cur);
return acc + cur;
}, 0);
};
Expand Down

0 comments on commit 7fcc4bf

Please sign in to comment.