-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBuildService.cs
More file actions
69 lines (63 loc) · 2.61 KB
/
BuildService.cs
File metadata and controls
69 lines (63 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Octokit;
namespace IntelOrca.OpenLauncher.Core
{
public class BuildService
{
private readonly GitHubClient _gitHubClient = new GitHubClient(new ProductHeaderValue("OpenLauncher"));
public async Task<ImmutableArray<Build>> GetBuildsAsync(Game game, bool includeDevelop)
{
var releaseBuilds = await GetBuildsAsync(game.ReleaseRepository, true);
if (includeDevelop && game.DevelopRepository is RepositoryName repo)
{
var developBuilds = await GetBuildsAsync(repo, false);
return releaseBuilds.AddRange(developBuilds).Sort();
}
else
{
return releaseBuilds.Sort();
}
}
public async Task<ImmutableArray<Build>> GetBuildsAsync(RepositoryName repo, bool isRelease)
{
var apiOptions = new ApiOptions()
{
StartPage = 1,
PageCount = 1,
PageSize = 75
};
var releases = await _gitHubClient.Repository.Release.GetAll(repo.Owner, repo.Name, apiOptions)
.ConfigureAwait(false);
var builds = ImmutableArray.CreateBuilder<Build>(initialCapacity: releases.Count);
foreach (var release in releases)
{
builds.Add(GetBuild(release, isRelease));
}
var latestBuild = await GetLatestBuildAsync(repo, isRelease);
if (latestBuild != null && !builds.Any(x => x.Version == latestBuild.Version))
{
builds.Capacity++;
builds.Add(latestBuild);
}
return builds.MoveToImmutable();
}
public async Task<Build> GetLatestBuildAsync(RepositoryName repo, bool isRelease)
{
var release = await _gitHubClient.Repository.Release.GetLatest(repo.Owner, repo.Name)
.ConfigureAwait(false);
return GetBuild(release, isRelease);
}
private static Build GetBuild(Release release, bool isRelease)
{
var date = DateTime.SpecifyKind(release.PublishedAt?.DateTime ?? DateTime.MinValue, DateTimeKind.Utc);
return new Build(isRelease, date, release.TagName, GetAssets(release));
}
private static ImmutableArray<BuildAsset> GetAssets(Release release) =>
release.Assets
.Select(x => new BuildAsset(x.Name, new Uri(x.BrowserDownloadUrl), x.ContentType, x.Size))
.ToImmutableArray();
}
}