Skip to content

Commit

Permalink
Create skip-releases option for NetKAN
Browse files Browse the repository at this point in the history
With the --skip-releases <int> option set, NetKAN skips the given number of releases for inflation.
This happens in the transformers for all the kref sources, just before the --releases option is applied to take multiple releases.

This feature is useful for backfilling old releases of a mod, where you don't want to re-inflate the newer ones just to get to the older ones.
  • Loading branch information
DasSkelett committed Feb 10, 2020
1 parent 33a0fbc commit 58c84a1
Show file tree
Hide file tree
Showing 23 changed files with 47 additions and 19 deletions.
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
2 changes: 1 addition & 1 deletion Tests/NetKAN/Transformers/GithubTransformerTests.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 GithubTransformerTests
{
private TransformOptions opts = new TransformOptions(1, null);
private TransformOptions opts = new TransformOptions(1, null, null);

[Test]
public void CalculatesRepositoryUrlCorrectly()
Expand Down
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 58c84a1

Please sign in to comment.