Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fast-forward update to default branch of forked repo prior to submission #235

Merged
merged 6 commits into from
Mar 16, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/WingetCreateCore/Common/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ private async Task<PullRequest> SubmitPRAsync(string packageId, string version,
repo = await this.github.Repository.Forks.Create(this.wingetRepoOwner, this.wingetRepo, new NewRepositoryFork());
createdRepo = true;
}

await this.UpdateForkedRepoWithUpstreamCommits(repo);
}
else
{
Expand All @@ -306,6 +308,7 @@ private async Task<PullRequest> SubmitPRAsync(string packageId, string version,
var upstreamMasterSha = upstreamMaster.Object.Sha;

Reference newBranch = null;

try
{
var retryPolicy = Policy.Handle<ApiException>().WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(i));
Expand Down Expand Up @@ -366,6 +369,24 @@ await retryPolicy.ExecuteAsync(async () =>
}
}

/// <summary>
/// Checks if the provided forked repository is behind on upstream commits and updates the default branch with the fetched commits. Update can only be a fast-forward update.
/// </summary>
/// <param name="forkedRepo"><see cref="Repository"/>Forked repository to be updated.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
private async Task UpdateForkedRepoWithUpstreamCommits(Repository forkedRepo)
{
var upstream = forkedRepo.Parent;
var compareResult = await this.github.Repository.Commit.Compare(upstream.Id, upstream.DefaultBranch, $"{forkedRepo.Owner.Login}:{forkedRepo.DefaultBranch}");

// Check to ensure that the update is only a fast-forward update.
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
if (compareResult.BehindBy > 0 && compareResult.AheadBy == 0)
{
var upstreamBranchReference = await this.github.Git.Reference.Get(upstream.Id, $"heads/{upstream.DefaultBranch}");
await this.github.Git.Reference.Update(forkedRepo.Id, $"heads/{forkedRepo.DefaultBranch}", new ReferenceUpdate(upstreamBranchReference.Object.Sha));
}
}

private async Task DeletePullRequestBranch(int pullRequestId)
{
// Delete branch if it's not on a forked repo.
Expand Down