Skip to content

Commit

Permalink
Merge pull request #38 from mattpannella/develop
Browse files Browse the repository at this point in the history
merge into master
  • Loading branch information
mattpannella authored Oct 14, 2022
2 parents 06f8674 + 136eb3a commit a88b3e2
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 15 deletions.
35 changes: 29 additions & 6 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,24 @@ public async Task RunUpdates()
Repo? repo = core.repository;
_writeMessage("Checking Core: " + name);
bool allowPrerelease = _settingsManager.GetCoreSettings(core.identifier).allowPrerelease;
List<Github.Release>? releases = await _fetchReleases(repo.owner, repo.name, _githubApiKey);
if(releases == null) {
continue;

var mostRecentRelease = core.release;
var prerelease = core.prerelease;
if(allowPrerelease && mostRecentRelease == null) {
mostRecentRelease = prerelease;
}
else if(allowPrerelease && prerelease != null) {
string semver1 = SemverUtil.FindSemver(mostRecentRelease.tag_name);
string semver2 = SemverUtil.FindSemver(prerelease.tag_name);
if(SemverUtil.SemverCompare(semver2, semver1)) {
mostRecentRelease = prerelease;
}
}
var mostRecentRelease = _getMostRecentRelease(releases, allowPrerelease);
if(mostRecentRelease == null) {
_writeMessage("No releases found. Skipping");
continue;
}
string tag_name = mostRecentRelease.tag_name;
List<Github.Asset> assets = mostRecentRelease.assets;

string releaseSemver = SemverUtil.FindSemver(tag_name);

Expand Down Expand Up @@ -205,6 +212,8 @@ public async Task RunUpdates()

// might need to search for the right zip here if there's more than one
//iterate through assets to find the zip release
var release = await _fetchRelease(repo.owner, repo.name, tag_name, _githubApiKey);
List<Github.Asset> assets = release.assets;
foreach(Github.Asset asset in assets) {
if(!ZIP_TYPES.Contains(asset.content_type)) {
//not a zip file. move on
Expand Down Expand Up @@ -305,7 +314,9 @@ private void Divide()
private string BuildAssetUrl(DependencyFile asset)
{
string archive = _settingsManager.GetConfig().archive_name;
if(archive != null && asset.archive_zip != null) {
if(asset.file_name != null && asset.archive_zip == null && asset.archive_file == null && !asset.zip) {
return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.file_name;
} else if(archive != null && asset.archive_zip != null) {
return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.archive_zip + ".zip/" + asset.file_name;
} else if(archive != null && asset.archive_file != null) {
return ARCHIVE_BASE_URL + "/" + archive + "/" + asset.archive_file;
Expand Down Expand Up @@ -339,6 +350,18 @@ private Github.Release _getMostRecentRelease(List<Github.Release> releases, bool
}
}

private async Task<Github.Release>? _fetchRelease(string user, string repository, string tag_name, string token = "")
{
try {
var release = await GithubApi.GetRelease(user, repository, tag_name, token);
return release;
} catch (HttpRequestException e) {
_writeMessage("Error communicating with Github API.");
_writeMessage(e.Message);
return null;
}
}

private async Task<bool> _getAsset(string downloadLink, string coreName)
{
bool updated = false;
Expand Down
9 changes: 9 additions & 0 deletions models/Asset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace pannella.analoguepocket;

public class Asset
{
public string platform { get; set; }
public string filename { get; set; }
public bool core_specific { get; set; }
public List<string> extensions { get; set; }
}
2 changes: 2 additions & 0 deletions models/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public class Core
public string? identifier { get; set; }
public Repo? repository { get; set; }
public string? platform { get; set; }
public Release release { get; set; }
public Release? prerelease { get; set; }

public override string ToString()
{
Expand Down
8 changes: 8 additions & 0 deletions models/Release.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace pannella.analoguepocket;

public class Release
{
public string tag_name { get; set; }
public string release_date { get; set; }
public List<Asset> assets { get; set; }
}
2 changes: 1 addition & 1 deletion models/Settings/CoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class CoreSettings
public CoreSettings()
{
skip = false;
allowPrerelease = false;
allowPrerelease = true;
}
}
2 changes: 1 addition & 1 deletion pocket_updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.4.0</Version>
<Version>2.5.0</Version>
<Description>Keep your Analogue Pocket up to date</Description>
<Copyright>2022 Matt Pannella</Copyright>
<Authors>Matt Pannella</Authors>
Expand Down
2 changes: 1 addition & 1 deletion services/CoresService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace pannella.analoguepocket;

public static class CoresService
{
private const string END_POINT = "https://joshcampbell191.github.io/openfpga-cores-inventory/api/v0/analogue-pocket/cores.json";
private const string END_POINT = "https://joshcampbell191.github.io/openfpga-cores-inventory/api/v1/analogue-pocket/cores.json";

public static async Task<List<Core>> GetCores()
{
Expand Down
30 changes: 24 additions & 6 deletions services/GithubApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ public static class GithubApi
public static async Task<List<Github.Release>> GetReleases(string user, string repository, string? token = "")
{
string url = String.Format(END_POINT, user, repository);
var responseBody = await CallAPI(url, token);

List<Github.Release>? releases = JsonSerializer.Deserialize<List<Github.Release>>(responseBody);

if(releases == null) {
releases = new List<Github.Release>();
}

return releases;
}

public static async Task<Github.Release?> GetRelease(string user, string repository, string tag_name, string? token = "")
{
string url = String.Format(END_POINT, user, repository) + "/tags/" + tag_name;

var responseBody = await CallAPI(url, token);
Github.Release? release = JsonSerializer.Deserialize<Github.Release>(responseBody);

return release;
}

private static async Task<string> CallAPI(string url, string? token = "")
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var request = new HttpRequestMessage
Expand All @@ -26,12 +49,7 @@ public static class GithubApi
response.EnsureSuccessStatusCode();

var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
List<Github.Release>? releases = JsonSerializer.Deserialize<List<Github.Release>>(responseBody);

if(releases == null) {
releases = new List<Github.Release>();
}

return releases;
return responseBody;
}
}

0 comments on commit a88b3e2

Please sign in to comment.