fetchUserContributions
함수js
복사편집
async function fetchUserContributions(owner, repo, username) {
try {
const response = await axios.get(
`https://api.github.com/repos/${owner}/${repo}/commits`,
{
headers: getGitHubHeaders(),
params: {
author: username,
per_page: 100 // 좀 더 많은 커밋 가져오기
}
}
);
return response.data;
} catch (error) {
console.error('사용자 기여 가져오기 오류:', error);
return []; // 기여가 없을 수 있음
}
}
owner
, repo
, username
의 커밋 목록을 최대 100개까지 받아옵니다.author
파라미터로 특정 사용자의 커밋만 필터링.[]
)을 반환.fetchCommitDetails
함수js
복사편집
async function fetchCommitDetails(owner, repo, sha) {
try {
const response = await axios.get(`https://api.github.com/repos/${owner}/${repo}/commits/${sha}`, {
headers: getGitHubHeaders()
});
return response.data;
} catch (error) {
throw handleApiError(error, '커밋 상세정보 가져오기');
}
}
sha
해시) 추가 상세 정보(파일 변경 사항, additions/deletions 등)를 받아오는 함수입니다.analyze-contributions
엔드포인트 내부js
복사편집
app.post('/api/analyze-contributions', async (req, res) => {
try {
const { owner, repo, username } = req.body;
// (1) 사용자 커밋 목록 가져오기
const userCommits = await fetchUserContributions(owner, repo, username);
// (2) 각 커밋별로 SHA 기반 상세 정보 가져오기 (최대 20개)
const contributionDetails = [];
for (const commit of userCommits.slice(0, 20)) {
const details = await fetchCommitDetails(owner, repo, commit.sha);
// ... 파일 변경/추가/삭제 등 세부 필드를 추출
contributionDetails.push(...);
}
// (3) 커밋 패턴 분석 및 시각화 데이터 준비
const commitPatterns = analyzeUserCommitPatterns(contributionDetails);
// (4) 파일 타입별 변경량, 코멘트, 기여 내용 등 구조화
// (5) 최종 결과 JSON으로 응답
res.json({
username,
contributionDetails,
contributionAnalysis,
visualizationData
});
} catch (error) {
// ...
}
});
POST /api/analyze-contributions
)는 사용자의 모든 커밋 내역을 바탕으로 파일별 수정량 및 커밋 패턴을 분석한 뒤, 이를 JSON 형태로 반환합니다.사용자 커밋이나 레포지토리 정보 등을 분석해 차트로 표시할 때, 다양한 통계가 필요합니다. 아래 코드는 통계를 뽑아낼 함수들과 그 결과를 visualizationData
에 담는 로직을 보여줍니다.
analyzeCommitPatterns
함수js
복사편집
function analyzeCommitPatterns(commits) {
// 요일별 커밋 수 (일=0, 토=6)
const dayCommits = [0, 0, 0, 0, 0, 0, 0];
// 시간대별(0~23시)
const hourCommits = Array(24).fill(0);
// 월별(1월=0 ~ 12월=11)
const monthCommits = Array(12).fill(0);
commits.forEach(commit => {
if (commit && commit.commit && commit.commit.author && commit.commit.author.date) {
const date = new Date(commit.commit.author.date);
dayCommits[date.getDay()]++;
hourCommits[date.getHours()]++;
monthCommits[date.getMonth()]++;
}
});
return {
dayCommits,
hourCommits,
monthCommits
};
}
commit.author.date
필드를 Date
객체로 변환 후, .getDay()
, .getHours()
, .getMonth()
등을 통해 빈도 배열을 업데이트합니다.