Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,24 @@ protected async Task<bool> GitHubSubmitManifests(Manifests manifests, string prT
Logger.ErrorLocalized(nameof(Resources.SyncForkWithUpstream_Message));
return false;
}
else if (e is BranchMergeConflictException)
{
// While attempting to sync fork through the GitHub API, a branch merge conflict was detected.
// The user will need to manually resolve the conflict.
Logger.ErrorLocalized(nameof(Resources.BranchMergeConflict_Message));
return false;
}
else if (e is GenericSyncFailureException)
{
// While attempting to sync fork through the GitHub API, a generic sync failure occurred.
Logger.ErrorLocalized(nameof(Resources.SyncForkFailed_Message));
return false;
}
else if (e is HttpRequestException)
{
Logger.ErrorLocalized(nameof(Resources.NetworkConnectionFailure_Message));
return false;
}
else
{
throw;
Expand Down
18 changes: 18 additions & 0 deletions src/WingetCreateCLI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/WingetCreateCLI/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1400,4 +1400,10 @@ Warning: Using this argument may result in the token being logged. Consider an a
<data name="ConfirmZippedBinary_Message" xml:space="preserve">
<value>Does this executable depend on DLLs or any other files present in the zip archive?</value>
</data>
<data name="SyncForkFailed_Message" xml:space="preserve">
<value>The forked repository could not be synced with the upstream commits. Sync your fork manually and try again.</value>
</data>
<data name="BranchMergeConflict_Message" xml:space="preserve">
<value>The forked repository could not be synced with the upstream commits due to a merge conflict. Resolve conflicts manually and try again.</value>
</data>
</root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.

namespace Microsoft.WingetCreateCore.Common.Exceptions
{
using System;

/// <summary>
/// The exception that is thrown when the forked repo's branch has a merge conflict with the upstream branch.
/// </summary>
public class BranchMergeConflictException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="BranchMergeConflictException"/> class.
/// </summary>
public BranchMergeConflictException()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.

namespace Microsoft.WingetCreateCore.Common.Exceptions
{
using System;

/// <summary>
/// The exception that is thrown when a generic failure occurs while syncing the forked repo with upstream commits.
/// </summary>
public class GenericSyncFailureException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="GenericSyncFailureException"/> class.
/// </summary>
public GenericSyncFailureException()
{
}
}
}
19 changes: 17 additions & 2 deletions src/WingetCreateCore/Common/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,11 @@ private async Task UpdateForkedRepoWithUpstreamCommits(Repository forkedRepo)

// Octokit .NET doesn't support sync fork endpoint, so we make a direct call to the GitHub API.
// Tracking issue for the request: https://github.com/octokit/octokit.net/issues/2989
// API reference: https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#sync-a-fork-branch-with-the-upstream-repository
HttpClient httpClient = new HttpClient();

// API reference: https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#sync-a-fork-branch-with-the-upstream-repository
var url = $"https://api.github.com/repos/{forkedRepo.Owner.Login}/{forkedRepo.Name}/merge-upstream";

// Headers
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", this.github.Credentials.Password);
httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json");
Expand All @@ -501,8 +503,21 @@ private async Task UpdateForkedRepoWithUpstreamCommits(Repository forkedRepo)
JsonObject jsonObject = new JsonObject { { "branch", forkedRepo.DefaultBranch } };
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");

var url = $"https://api.github.com/repos/{forkedRepo.Owner.Login}/{forkedRepo.Name}/merge-upstream";
var response = await httpClient.PostAsync(url, content);

// 409 status code
if (response.StatusCode == System.Net.HttpStatusCode.Conflict)
{
throw new BranchMergeConflictException();
}

// 422 status code
if (response.StatusCode == System.Net.HttpStatusCode.UnprocessableEntity)
{
throw new GenericSyncFailureException();
}

// The API doesn't document another error code. If this fails, a generic HttpRequestException is thrown.
response.EnsureSuccessStatusCode();
}
}
Expand Down