Exemplo n.º 1
0
// GetRecentRevisions fetches the most recent 'numRevisions'
func (gRepoPoller *GithubRepositoryPoller) GetRecentRevisions(maxRevisions int) (
	revisions []model.Revision, err error) {
	commitURL := getCommitURL(gRepoPoller.ProjectRef)

	for {
		githubCommits, header, err := thirdparty.GetGithubCommits(
			gRepoPoller.OauthToken, commitURL)
		if err != nil {
			return nil, err
		}

		for _, commit := range githubCommits {
			if len(revisions) == maxRevisions {
				break
			}
			revisions = append(revisions, githubCommitToRevision(
				&commit))
		}

		// stop querying for commits if we've reached our target or got back no
		// commits
		if len(revisions) == maxRevisions || len(revisions) == 0 {
			break
		}

		// stop quering for commits if there's no next page
		if commitURL = thirdparty.NextGithubPageLink(header); commitURL == "" {
			break
		}
	}
	return
}
Exemplo n.º 2
0
// GetRevisionsSince fetches the all commits from the corresponding Github
// ProjectRef that were made after 'revision'
func (gRepoPoller *GithubRepositoryPoller) GetRevisionsSince(revision string,
	maxRevisionsToSearch int) (revisions []model.Revision, err error) {
	commitURL := getCommitURL(gRepoPoller.ProjectRef)
	var foundLatest bool

	for len(revisions) < maxRevisionsToSearch {
		githubCommits, header, err := thirdparty.GetGithubCommits(
			gRepoPoller.OauthToken, commitURL)
		if err != nil {
			return nil, err
		}

		for _, commit := range githubCommits {
			if isLastRevision(revision, &commit) {
				foundLatest = true
				break
			}
			revisions = append(revisions, githubCommitToRevision(
				&commit))
		}

		// stop querying for commits if we've found the latest commit or got
		// back no commits
		if foundLatest || len(revisions) == 0 {
			break
		}

		// stop quering for commits if there's no next page
		if commitURL = thirdparty.NextGithubPageLink(header); commitURL == "" {
			break
		}
	}

	if !foundLatest {
		return []model.Revision{}, fmt.Errorf("Unable to locate "+
			"requested revision “%v” within “%v” revisions", revision,
			maxRevisionsToSearch)
	}

	return
}
Exemplo n.º 3
0
// GetRevisionsSince fetches the all commits from the corresponding Github
// ProjectRef that were made after 'revision'
func (gRepoPoller *GithubRepositoryPoller) GetRevisionsSince(
	revision string, maxRevisionsToSearch int) ([]model.Revision, error) {

	var foundLatest bool
	var commits []thirdparty.GithubCommit
	var header http.Header
	commitURL := getCommitURL(gRepoPoller.ProjectRef)
	revisions := []model.Revision{}

	for len(revisions) < maxRevisionsToSearch {
		var err error
		commits, header, err = thirdparty.GetGithubCommits(gRepoPoller.OauthToken, commitURL)
		if err != nil {
			return nil, err
		}

		for _, commit := range commits {
			if isLastRevision(revision, &commit) {
				foundLatest = true
				break
			}
			revisions = append(revisions, githubCommitToRevision(&commit))
		}

		// stop querying for commits if we've found the latest commit or got back no commits
		if foundLatest || len(revisions) == 0 {
			break
		}

		// stop quering for commits if there's no next page
		if commitURL = thirdparty.NextGithubPageLink(header); commitURL == "" {
			break
		}
	}

	if !foundLatest {
		var revisionDetails *model.RepositoryErrorDetails
		var revisionError error
		// attempt to get the merge base commit
		baseRevision, err := thirdparty.GetGitHubMergeBaseRevision(
			gRepoPoller.OauthToken,
			gRepoPoller.ProjectRef.Owner,
			gRepoPoller.ProjectRef.Repo,
			revision,
			&commits[0],
		)
		if err != nil {
			// unable to get merge base commit so set projectRef revision details with a blank base revision
			revisionDetails = &model.RepositoryErrorDetails{
				Exists:            true,
				InvalidRevision:   revision[:10],
				MergeBaseRevision: "",
			}
			revisionError = fmt.Errorf("unable to find a suggested merge base commit for revision %v, must fix on projects settings page: %v",
				revision, err)
		} else {
			// update project ref to have an inconsistent status
			revisionDetails = &model.RepositoryErrorDetails{
				Exists:            true,
				InvalidRevision:   revision[:10],
				MergeBaseRevision: baseRevision,
			}
			revisionError = fmt.Errorf("base revision, %v not found, suggested base revision, %v found, must confirm on project settings page",
				revision, baseRevision)
		}

		gRepoPoller.ProjectRef.RepotrackerError = revisionDetails
		if err = gRepoPoller.ProjectRef.Upsert(); err != nil {
			return []model.Revision{}, fmt.Errorf("unable to update projectRef revision details: %v", err)
		}

		return []model.Revision{}, revisionError
	}

	return revisions, nil
}