Skip to content

Commit

Permalink
Added Azure Client
Browse files Browse the repository at this point in the history
Doesn't really have much logic, mainly needed for another project to be kickstarted :P
  • Loading branch information
Azyyyyyy committed Jan 16, 2024
1 parent 861cf84 commit 31393cf
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<Authors>Azyyyyyy</Authors>
<RepositoryUrl>https://github.com/Azyyyyyy/TinyUpdate</RepositoryUrl>
<Version>2024.01.15-alpha</Version>
<AssemblyVersion>2024.01.15</AssemblyVersion>
<FileVersion>2024.01.15</FileVersion>
<Version>2024.01.16-alpha</Version>
<AssemblyVersion>2024.01.16</AssemblyVersion>
<FileVersion>2024.01.16</FileVersion>
<Description>Cross platform updater that's small on space but big in function!</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>

Expand Down
7 changes: 7 additions & 0 deletions TinyUpdate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.Core.Tests", "te
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.MicrosoftStore", "src\Clients\TinyUpdate.MicrosoftStore\TinyUpdate.MicrosoftStore.csproj", "{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyUpdate.Azure", "src\Clients\TinyUpdate.Azure\TinyUpdate.Azure.csproj", "{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -78,6 +80,10 @@ Global
{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B}.Release|Any CPU.Build.0 = Release|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{4B3D4412-9AF3-4D5A-B013-3462B46E815F} = {63F2556D-A89A-4552-84C7-D2BE30718485}
Expand All @@ -94,5 +100,6 @@ Global
{B5BC1A21-5A4C-403E-918B-CE48675E6F89} = {0BD356C4-B4F2-45D6-A94F-77ED1ECEEE11}
{81B0A72E-04FA-4A4F-856F-68C22FF2D945} = {0BD356C4-B4F2-45D6-A94F-77ED1ECEEE11}
{F364F894-0AF5-4A42-A6D3-9B6A8C10E45B} = {EE289D98-EE1C-4D17-9BB0-B4981E26A49D}
{35DBC016-62BB-41B4-B2F8-1153E1ACB5AA} = {EE289D98-EE1C-4D17-9BB0-B4981E26A49D}
EndGlobalSection
EndGlobal
151 changes: 151 additions & 0 deletions src/Clients/TinyUpdate.Azure/AzureClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Azure.Pipelines.WebApi;
using Microsoft.Extensions.Logging;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.WebApi;
using SemVersion;
using TinyUpdate.Core.Abstract;
using Artifact = Microsoft.Azure.Pipelines.WebApi.Artifact;

namespace TinyUpdate.Azure;

public class AzureClient : IPackageClient
{
const string c_collectionUri = "";
const string project = "";
const int pipelineId = 1;
private const string personalAccessToken = "";

private readonly Uri _orgUrl = new Uri(c_collectionUri);

private readonly HttpClient _downloadClient;
private readonly IUpdateApplier _updateApplier;
private readonly ILogger _logger;
private readonly IHasher _hasher;
public AzureClient(IUpdateApplier updateApplier, ILogger logger, HttpClient downloadClient, IHasher hasher)
{
_updateApplier = updateApplier;
_logger = logger;
_downloadClient = downloadClient;
_hasher = hasher;
}

//TODO: REDO
public async IAsyncEnumerable<ReleaseEntry> GetUpdates()
{
using var connection = MakeConnection();
using var pipelineConnection = new PipelinesHttpClient(connection.Uri, connection.Credentials);

//TODO: Find the first release which allow
var runs = await pipelineConnection.ListRunsAsync(project, pipelineId);
foreach (var run in runs)
{
if (run == null || run.State != RunState.Completed
|| run.Result.GetValueOrDefault(RunResult.Failed) != RunResult.Succeeded)
{
continue;
}

Artifact artifact;
try
{
artifact = await pipelineConnection.GetArtifactAsync(project, pipelineId, run.Id, "RELEASE");
}
catch (Exception e)
{
_logger.LogError(e, "Failed to get RELEASE artifact");
continue;
}

if (artifact.SignedContent.SignatureExpires >= DateTime.Now)
{
_logger.LogWarning("Unable to download RELEASE file");
yield break;
}

await using var releaseFileStream = await _downloadClient.GetStreamAsync(artifact.SignedContent.Url);
IAsyncEnumerable<AzureReleaseEntry?> releases;

try
{
releases = JsonSerializer.DeserializeAsyncEnumerable<AzureReleaseEntry>(releaseFileStream);
}
catch (Exception e)
{
_logger.LogError(e, "Failed to download RELEASE artifact");
continue;
}

await foreach (var release in releases)
{
if (release != null && _hasher.IsValidHash(release.Hash))
{
yield return release;
}
}
}
}

public Task<bool> DownloadUpdate(ReleaseEntry releaseEntry, IProgress<double>? progress)
{
throw new NotImplementedException();
}

public Task<bool> ApplyUpdate(ReleaseEntry releaseEntry, IProgress<double>? progress)
{
throw new NotImplementedException();
}

private VssConnection MakeConnection() => new VssConnection(_orgUrl, new VssBasicCredential(string.Empty, personalAccessToken));
}

public class AzureReleaseEntry : ReleaseEntry
{
public AzureReleaseEntry(string hash, long filesize, SemanticVersion? previousVersion, SemanticVersion newVersion, string fileName, bool isDelta, int runId, string artifactName)
{
Hash = hash;
Filesize = filesize;
PreviousVersion = previousVersion;
NewVersion = newVersion;
FileName = fileName;
IsDelta = isDelta;
RunId = runId;
ArtifactName = artifactName;
}

[JsonIgnore]
public override bool HasUpdate => true;

public string Hash { get; }

public long Filesize { get; }

[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault),
JsonConverter(typeof(SemanticVersionConverter))]
public SemanticVersion? PreviousVersion { get; }

[JsonConverter(typeof(SemanticVersionConverter))]
public SemanticVersion NewVersion { get; }

public string FileName { get; }

public bool IsDelta { get; }

public int RunId { get; }

public string ArtifactName { get; }
}

public class SemanticVersionConverter : JsonConverter<SemanticVersion>
{
public override SemanticVersion? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return SemanticVersion.Parse(reader.GetString());
}

public override void Write(Utf8JsonWriter writer, SemanticVersion value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
}
17 changes: 17 additions & 0 deletions src/Clients/TinyUpdate.Azure/TinyUpdate.Azure.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="16.205.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\TinyUpdate.Core\TinyUpdate.Core.csproj" />
</ItemGroup>

</Project>

0 comments on commit 31393cf

Please sign in to comment.