1. 사용자 커밋 관련 GitHub API 활용

(1) 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 []; // 기여가 없을 수 있음
  }
}

(2) 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, '커밋 상세정보 가져오기');
  }
}

(3) 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) {
    // ...
  }
});


2. 시각화를 위한 분석 로직

사용자 커밋이나 레포지토리 정보 등을 분석해 차트로 표시할 때, 다양한 통계가 필요합니다. 아래 코드는 통계를 뽑아낼 함수들과 그 결과를 visualizationData에 담는 로직을 보여줍니다.

(1) 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
  };
}