winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

GitHubClient.cs (2836B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="GitHubClient.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGet.Client.Engine.Helpers
      8 {
      9     using System.Threading.Tasks;
     10     using Octokit;
     11 
     12     /// <summary>
     13     /// Handles GitHub interactions.
     14     /// </summary>
     15     internal class GitHubClient
     16     {
     17         private readonly string owner;
     18         private readonly string repo;
     19         private readonly IGitHubClient gitHubClient;
     20 
     21         /// <summary>
     22         /// Initializes a new instance of the <see cref="GitHubClient"/> class.
     23         /// </summary>
     24         /// <param name="owner">Owner.</param>
     25         /// <param name="repo">Repository.</param>
     26         public GitHubClient(string owner, string repo)
     27         {
     28             this.gitHubClient = new Octokit.GitHubClient(new ProductHeaderValue(HttpClientHelper.UserAgent));
     29             this.owner = owner;
     30             this.repo = repo;
     31         }
     32 
     33         /// <summary>
     34         /// Gets a release.
     35         /// </summary>
     36         /// <param name="releaseTag">Release tag.</param>
     37         /// <returns>The Release.</returns>
     38         public async Task<Release> GetReleaseAsync(string releaseTag)
     39         {
     40             return await this.gitHubClient.Repository.Release.Get(this.owner, this.repo, releaseTag);
     41         }
     42 
     43         /// <summary>
     44         /// Gets the latest released and waits.
     45         /// </summary>
     46         /// <param name="includePrerelease">Include prerelease.</param>
     47         /// <returns>Latest version.</returns>
     48         public async Task<string> GetLatestReleaseTagNameAsync(bool includePrerelease)
     49         {
     50             return (await this.GetLatestReleaseAsync(includePrerelease)).TagName;
     51         }
     52 
     53         /// <summary>
     54         /// Gets the latest released version.
     55         /// </summary>
     56         /// <param name="includePrerelease">Include prerelease.</param>
     57         /// <returns>Latest version.</returns>
     58         public async Task<Release> GetLatestReleaseAsync(bool includePrerelease)
     59         {
     60             Release release;
     61 
     62             // GetLatest doesn't respect prerelease or gives an option to get it.
     63             if (includePrerelease)
     64             {
     65                 // GetAll orders by newest and includes pre releases.
     66                 release = (await this.gitHubClient.Repository.Release.GetAll(this.owner, this.repo))[0];
     67             }
     68             else
     69             {
     70                 release = await this.gitHubClient.Repository.Release.GetLatest(this.owner, this.repo);
     71             }
     72 
     73             return release;
     74         }
     75     }
     76 }