Exemple #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
}