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

Handle exception when creating a reference immediately after creating fork #143

Merged
merged 5 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Click on the individual commands to learn more.

You can use WingetCreate to update your existing app manifest as part of your CI/CD pipeline. For reference, see the final task in this repo's [release Azure pipeline](https://github.com/microsoft/winget-create/blob/main/pipelines/azure-pipelines.release.yml).

Unfortunately we can't use the msixbundle in a standard Azure Pipeline pool. Instead, use the standalone exe, the latest version of which can be found at https://aka.ms/wingetcreate/latest, and the latest preview version can be found at https://aka.ms/wingetcreate/preview, both of these require .NET to be installed on the build machine.
Unfortunately, we can't use the msixbundle in a standard Azure Pipeline pool. Instead, use the standalone exe, the latest version of which can be found at https://aka.ms/wingetcreate/latest, and the latest preview version can be found at https://aka.ms/wingetcreate/preview, both of these require .NET to be installed on the build machine.

If .NET isn't already installed, you can use https://aka.ms/wingetcreate/latest/self-contained or https://aka.ms/wingetcreate/preview/self-contained, which has .NET built-in, but is a larger download.

Expand All @@ -52,6 +52,17 @@ Then simply add a new powershell task to download the exe, and run it to update

The CLI also supports creating or updating manifests with multiple installer URLs. You can either create new manifests with multiple installer nodes using the [New Command](doc/new.md) or update existing manifests with multiple installer URLs using the [Update Command](doc/update.md).

## Using your GitHub Personal Access Token

When [creating your own GitHub Personal Access Token (PAT)](https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token) to be used with WingetCreate, make sure you verify the following steps are completed:

- [Authorizing your personal access token for use with SAML single sign-on](https://docs.github.com/en/github/authenticating-to-github/authenticating-with-saml-single-sign-on/authorizing-a-personal-access-token-for-use-with-saml-single-sign-on)
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
- Selecting the **public_repo** scope to allow access to public repositories

![public_repo scope](./doc/images/tokenscope-publicrepo.png)

- (Optional) Selecting the **delete_repo** scope permission if you want WingetCreate to automatically delete the forked repo that it created if the PR submission fails.

## Building the client

### Prerequisites
Expand Down
Binary file added doc/images/tokenscope-publicrepo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions src/WingetCreateCore/Common/GitHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Microsoft.WingetCreateCore.Common
using Jose;
using Microsoft.WingetCreateCore.Models;
using Octokit;
using Polly;

/// <summary>
/// Provides functionality for interacting a user's GitHub account.
Expand Down Expand Up @@ -307,8 +308,11 @@ private async Task<PullRequest> SubmitPRAsync(string packageId, string version,
Reference newBranch = null;
try
{
// Create new branch synced to upstream master
await this.github.Git.Reference.Create(repo.Id, new NewReference($"refs/{newBranchNameHeads}", upstreamMasterSha));
var retryPolicy = Policy.Handle<ApiException>().WaitAndRetryAsync(3, i => TimeSpan.FromSeconds(2));
ryfu-msft marked this conversation as resolved.
Show resolved Hide resolved
await retryPolicy.ExecuteAsync(async () =>
{
await this.github.Git.Reference.Create(repo.Id, new NewReference($"refs/{newBranchNameHeads}", upstreamMasterSha));
});

// Update from upstream branch master
newBranch = await this.github.Git.Reference.Update(repo.Id, newBranchNameHeads, new ReferenceUpdate(upstreamMasterSha));
Expand Down Expand Up @@ -344,7 +348,14 @@ private async Task<PullRequest> SubmitPRAsync(string packageId, string version,
// On error, cleanup created branch/repo before re-throwing
if (createdRepo)
{
await this.github.Repository.Delete(repo.Id);
try
{
await this.github.Repository.Delete(repo.Id);
}
catch (ForbiddenException)
{
// If we fail to delete the fork, the user did not provide a token with the "delete_repo" permission. Do nothing.
}
}
else if (newBranch != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/WingetCreateCore/WingetCreateCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Polly" Version="7.2.2" />
<PackageReference Include="RestSharp" Version="106.11.8-alpha.0.13" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.261">
<PrivateAssets>all</PrivateAssets>
Expand Down