コード例 #1
0
ファイル: import.go プロジェクト: jakeporter/govcode.org
func importStats(repo *c.Repository, org_login string, client *github.Client) error {
	stats, res, err := client.Repositories.ListContributorsStats(org_login, repo.Name)
	fmt.Println("Getting stats", repo.Name)
	fmt.Println(res)
	if err != nil {
		if res != nil && res.StatusCode == 202 {
			fmt.Println("Sleeping")
			time.Sleep(4 * time.Second)
			importStats(repo, org_login, client)
			return nil
		}
	}

	for _, s := range stats {
		for _, w := range s.Weeks {
			if *w.Commits == 0 {
				continue
			}

			var stat c.RepoStat
			var user c.User
			user.FromGhContrib(s.Author)

			user_id := findOrCreateUser(&user)
			// Does it exist?
			timeStr := fmt.Sprintf("%4d%02d%02d", w.Week.Time.Year(), w.Week.Time.Month(), w.Week.Time.Day())
			c.DB.Where("user_id = ? and repository_id = ? and to_char(week, 'YYYYMMDD') = ?",
				user_id,
				repo.Id,
				timeStr).First(&stat)

			if stat.Id == 0 || stat.Commits != int64(*w.Commits) {
				stat.UserId = user_id
				stat.RepositoryId = repo.Id
				stat.Week = w.Week.Time
				stat.Add = int64(*w.Additions)
				stat.Del = int64(*w.Deletions)
				stat.Commits = int64(*w.Commits)
				c.DB.Save(&stat)
			}
		}
	}

	return nil
}