示例#1
0
func importPulls(repo *c.Repository, org_login string, client *github.Client, page int) error {
	// Import pulls for a given repo
	opt := &github.PullRequestListOptions{}
	opt.State = "all"
	opt.ListOptions = github.ListOptions{Page: page, PerPage: 100}

	pulls, response, err := client.PullRequests.List(org_login, repo.Name, opt)
	fmt.Printf("Getting pulls for repo %s at page %d\n", repo.Name, page)
	fmt.Println(response)
	if err != nil {
		return err
	}

	// There are more pages, lets fetch the next one
	if response.NextPage > 0 {
		importPulls(repo, org_login, client, response.NextPage)
	}

	for _, gh_pull := range pulls {
		var pull c.Pull

		// Does the commit exist?
		c.DB.Where("number = ? and repository_id = ?", *gh_pull.Number, repo.Id).First(&pull)

		if pull.Id == 0 {
			pull.RepositoryId = repo.Id
			pull.Title = *gh_pull.Title
			pull.Body = getStr(gh_pull.Body)
			if gh_pull.User != nil {
				pull.Admin = *gh_pull.User.SiteAdmin
				var user c.User
				user.FromGhUser(gh_pull.User)

				pull.UserId = findOrCreateUser(&user)
			}
			pull.Number = int64(*gh_pull.Number)
			if gh_pull.CreatedAt != nil {
				pull.GhCreatedAt.Time = *gh_pull.CreatedAt
				pull.GhCreatedAt.Valid = true
			}

		}

		// If the pull has not been updated go to the next one
		if pull.GhUpdatedAt.Time == *gh_pull.UpdatedAt {
			continue
		}

		pull.GhUpdatedAt.Time = *gh_pull.UpdatedAt
		pull.GhUpdatedAt.Valid = true

		if gh_pull.MergedAt != nil {
			pull.MergedAt.Time = *gh_pull.MergedAt
			pull.MergedAt.Valid = true
		}

		c.DB.Save(&pull)
	}
	return nil
}
示例#2
0
func generateCommitCountPerUser() {
	fmt.Println("Generating commit count per user")

	rows, err := c.DB.Table("repo_stats").Select(`
	   user_id, array_agg(distinct o.login) as organization_list, sum(commits) as commit_count
	 `).Joins(`
	 	inner join repositories r
	 	on r.id = repo_stats.repository_id
	   inner join organizations o
	   on o.id = r.organization_id
	 `).Group("user_id").Rows()

	c.PanicOn(err)

	for rows.Next() {
		var user c.User
		var user_id, commit_count int64
		var org_list string
		rows.Scan(&user_id, &org_list, &commit_count)

		if user_id == 0 {
			continue
		}

		c.DB.Where("id = ?", user_id).First(&user)

		if commit_count != user.CommitCount {
			user.CommitCount = commit_count
			user.OrgList = org_list
			c.DB.Save(&user)
		}
	}

	rows.Close()
}
示例#3
0
func findOrCreateUser(user *c.User) int64 {

	var new_user c.User
	c.DB.Where("gh_id = ?", user.GhId).First(&new_user)

	if new_user.Id == 0 {
		new_user.Login = user.Login
		new_user.AvatarUrl = user.AvatarUrl
		new_user.GhId = user.GhId
		new_user.CommitCount = 0
		c.DB.Save(&new_user)
	}

	return new_user.Id
}
示例#4
0
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
}