Skip to content

Commit

Permalink
Merge #2996 Create skip-releases option for NetKAN
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 10, 2020
2 parents 62babf6 + 053c487 commit e9bf033
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.

### Features

- [Netkan] Create skip-releases option for NetKAN (#2996 by: DasSkelett; reviewed: HebaruSan)

### Bugfixes

### Internal
Expand Down
3 changes: 3 additions & 0 deletions Netkan/CmdLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal class CmdLineOptions
[Option("releases", DefaultValue = "1", HelpText = "Number of releases to inflate, or 'all'")]
public string Releases { get; set; }

[Option("skip-releases", DefaultValue = "0", HelpText = "Number of releases to skip / index of release to inflate.")]
public string SkipReleases { get; set; }

[Option("prerelease", HelpText = "Index GitHub prereleases")]
public bool PreRelease { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions Netkan/Processors/QueueHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private IEnumerable<SendMessageBatchRequestEntry> Inflate(Message msg)
IEnumerable<Metadata> ckans = null;
bool caught = false;
string caughtMessage = null;
var opts = new TransformOptions(releases, highVer);
var opts = new TransformOptions(releases, null, highVer);
try
{
ckans = inflator.Inflate($"{netkan.Identifier}.netkan", netkan, opts);
Expand Down Expand Up @@ -147,7 +147,7 @@ private IEnumerable<SendMessageBatchRequestEntry> Inflate(Message msg)
private SendMessageBatchRequestEntry inflationMessage(Metadata ckan, Metadata netkan, TransformOptions opts, bool success, string err = null)
{
bool staged = netkan.Staged || opts.Staged;
string stagingReason =
string stagingReason =
!string.IsNullOrEmpty(netkan.StagingReason) ? netkan.StagingReason
: !string.IsNullOrEmpty(opts.StagingReason) ? opts.StagingReason
: null;
Expand Down
6 changes: 6 additions & 0 deletions Netkan/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public static int Main(string[] args)
netkan,
new TransformOptions(
ParseReleases(Options.Releases),
ParseSkipReleases(Options.SkipReleases),
ParseHighestVersion(Options.HighestVersion)
)
);
Expand Down Expand Up @@ -131,6 +132,11 @@ public static int Main(string[] args)
return val == "all" ? (int?)null : int.Parse(val);
}

private static int? ParseSkipReleases(string val)
{
return string.IsNullOrWhiteSpace(val) ? (int?)null : int.Parse(val);
}

private static ModuleVersion ParseHighestVersion(string val)
{
return val == null ? null : new ModuleVersion(val);
Expand Down
4 changes: 4 additions & 0 deletions Netkan/Transformers/CurseTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
// Look up our mod on Curse by its Id.
var curseMod = _api.GetMod(metadata.Kref.Id);
var versions = curseMod.All();
if (opts.SkipReleases.HasValue)
{
versions = versions.Skip(opts.SkipReleases.Value);
}
if (opts.Releases.HasValue)
{
versions = versions.Take(opts.Releases.Value);
Expand Down
6 changes: 5 additions & 1 deletion Netkan/Transformers/GithubTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
// Get the GitHub repository
var ghRepo = _api.GetRepo(ghRef);
var versions = _api.GetAllReleases(ghRef);
if (opts.SkipReleases.HasValue)
{
versions = versions.Skip(opts.SkipReleases.Value);
}
if (opts.Releases.HasValue)
{
versions = versions.Take(opts.Releases.Value);
Expand Down Expand Up @@ -168,7 +172,7 @@ private JToken getAuthors(GithubRepo repo, GithubRelease release)
}
// Check parent repos
r = r.ParentRepo == null
? null
? null
: _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}", false, _matchPreleases));
}
// Return a string if just one author, else an array
Expand Down
4 changes: 3 additions & 1 deletion Netkan/Transformers/ITransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ namespace CKAN.NetKAN.Transformers
{
internal class TransformOptions
{
public TransformOptions(int? releases, ModuleVersion highVer)
public TransformOptions(int? releases, int? skipReleases, ModuleVersion highVer)
{
Releases = releases;
SkipReleases = skipReleases;
HighestVersion = highVer;
}

public readonly int? Releases;
public readonly int? SkipReleases;
public readonly ModuleVersion HighestVersion;
public bool Staged;
public string StagingReason;
Expand Down
4 changes: 4 additions & 0 deletions Netkan/Transformers/JenkinsTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
JenkinsRef jRef = new JenkinsRef(metadata.Kref);

var versions = _api.GetAllBuilds(jRef, options);
if (opts.SkipReleases.HasValue)
{
versions = versions.Skip(opts.SkipReleases.Value);
}
if (opts.Releases.HasValue)
{
versions = versions.Take(opts.Releases.Value);
Expand Down
4 changes: 4 additions & 0 deletions Netkan/Transformers/SpacedockTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
// Look up our mod on SD by its Id.
var sdMod = _api.GetMod(Convert.ToInt32(metadata.Kref.Id));
var versions = sdMod.All();
if (opts.SkipReleases.HasValue)
{
versions = versions.Skip(opts.SkipReleases.Value);
}
if (opts.Releases.HasValue)
{
versions = versions.Take(opts.Releases.Value);
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/MainClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tests.NetKAN
[TestFixture]
public class MainClassTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void FixVersionStringsUnharmed()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/NetkanOverride.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Tests.NetKAN
public class NetkanOverride
{
JObject such_metadata;
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[SetUp]
public void Setup()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/AvcKrefTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class AvcKrefTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test,
TestCase(
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/AvcTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class AvcTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void AddsMissingVersionInfo()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/CurseTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class CurseTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

// GH #199: Don't pre-fill KSP version fields if we see a ksp_min/max
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class DownloadAttributeTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void AddsDownloadAttributes()
Expand Down
1 change: 1 addition & 0 deletions Tests/NetKAN/Transformers/EpochTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void Transform_WithHighVersionParam_MatchesExpected(string version, strin
ITransformer sut = new EpochTransformer();
TransformOptions opts = new TransformOptions(
1,
null,
string.IsNullOrEmpty(highVer)
? null
: new ModuleVersion(highVer)
Expand Down
4 changes: 2 additions & 2 deletions Tests/NetKAN/Transformers/GeneratedByTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class GeneratedByTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void AddsGeneratedByProperty()
{
Expand Down
105 changes: 89 additions & 16 deletions Tests/NetKAN/Transformers/GithubTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Moq;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using CKAN;
using CKAN.Versioning;
using CKAN.NetKAN.Model;
using CKAN.NetKAN.Sources.Github;
Expand All @@ -14,16 +13,12 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class GithubTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private readonly TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void CalculatesRepositoryUrlCorrectly()
private Mock<IGithubApi> apiMockUp;
[OneTimeSetUp]
public void setupApiMockup()
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["$kref"] = "#/ckan/github/ExampleAccount/ExampleProject";

var mApi = new Mock<IGithubApi>();
mApi.Setup(i => i.GetRepo(It.IsAny<GithubRef>()))
.Returns(new GithubRepo
Expand All @@ -40,14 +35,37 @@ public void CalculatesRepositoryUrlCorrectly()
));

mApi.Setup(i => i.GetAllReleases(It.IsAny<GithubRef>()))
.Returns(new GithubRelease[] { new GithubRelease(
"ExampleProject",
new ModuleVersion("1.0"),
new Uri("http://github.example/download"),
null
)});
.Returns(new GithubRelease[] {
new GithubRelease(
"ExampleProject",
new ModuleVersion("1.0"),
new Uri("http://github.example/download/1.0"),
null
),
new GithubRelease("ExampleProject",
new ModuleVersion("1.1"),
new Uri("http://github.example/download/1.1"),
null
),
new GithubRelease("ExampleProject",
new ModuleVersion("1.2"),
new Uri("http://github.example/download/1.2"),
null
),
});

var sut = new GithubTransformer(mApi.Object, false);
apiMockUp = mApi;
}

[Test]
public void CalculatesRepositoryUrlCorrectly()
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["$kref"] = "#/ckan/github/ExampleAccount/ExampleProject";

var sut = new GithubTransformer(apiMockUp.Object, false);

// Act
var result = sut.Transform(new Metadata(json), opts).First();
Expand Down Expand Up @@ -104,5 +122,60 @@ public void Transform_DownloadURLWithEncodedCharacter_DontDoubleEncode()
);
}

[Test]
public void Transform_MultipleReleases_TransformsAll()
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["$kref"] = "#/ckan/github/ExampleAccount/ExampleProject";

var sut = new GithubTransformer(apiMockUp.Object, false);

// Act
var results = sut.Transform(
new Metadata(json),
new TransformOptions(2, null, null)
);
var transformedJsons = results.Select(result => result.Json()).ToArray();

// Assert
Assert.AreEqual(
"http://github.example/download/1.0",
(string)transformedJsons[0]["download"]
);
Assert.AreEqual(
"http://github.example/download/1.1",
(string)transformedJsons[1]["download"]
);
}

[Test]
public void Transform_SkipReleases_SkipsCorrectly()
{
// Arrange
var json = new JObject();
json["spec_version"] = 1;
json["$kref"] = "#/ckan/github/ExampleAccount/ExampleProject";

var sut = new GithubTransformer(apiMockUp.Object, false);

// Act
var results = sut.Transform(
new Metadata(json),
new TransformOptions(3, 1, null)
).ToArray();
// var transformedJsons = results.Select(result => result.Json()).ToArray();

// Assert
Assert.AreEqual(
"1.1",
results[0].Version.ToString()
);
Assert.AreEqual(
"1.2",
results[1].Version.ToString()
);
}
}
}
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/HttpTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class HttpTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[TestCase("#/ckan/github/foo/bar")]
[TestCase("#/ckan/netkan/http://awesomemod.example/awesomemod.netkan")]
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/InternalCkanTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class InternalCkanTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void AddsMiddingProperties()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/MetaNetkanTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class MetaNetkanTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void DoesNothingWhenNoMatch()
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/SpacedockTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class SpacedockTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

// GH #199: Don't pre-fill KSP version fields if we see a ksp_min/max
[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class StripNetkanMetadataTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[TestCaseSource("StripNetkanMetadataTestCaseSource")]
public void StripNetkanMetadata(string json, string expected)
Expand Down
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/VersionEditTransformerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Tests.NetKAN.Transformers
[TestFixture]
public sealed class VersionEditTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void DoesNothingWhenNoMatch()
Expand Down

0 comments on commit e9bf033

Please sign in to comment.