Skip to content

Commit

Permalink
Append resource links to staging PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Sep 26, 2021
1 parent 61514f1 commit 4a2076b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions Netkan/CKAN-netkan.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<Compile Include="Transformers\PropertySortTransformer.cs" />
<Compile Include="Transformers\SpacedockTransformer.cs" />
<Compile Include="Transformers\StagingTransformer.cs" />
<Compile Include="Transformers\StagingLinksTransformer.cs" />
<Compile Include="Transformers\StripNetkanMetadataTransformer.cs" />
<Compile Include="Transformers\VersionEditTransformer.cs" />
<Compile Include="Transformers\VersionedOverrideTransformer.cs" />
Expand Down
1 change: 1 addition & 0 deletions Netkan/Transformers/NetkanTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ IValidator validator
new AvcKrefTransformer(http, ghApi),
new InternalCkanTransformer(http, moduleService),
new AvcTransformer(http, moduleService, ghApi),
new StagingLinksTransformer(),
new LocalizationsTransformer(http, moduleService),
new VersionEditTransformer(),
new ForcedVTransformer(),
Expand Down
54 changes: 54 additions & 0 deletions Netkan/Transformers/StagingLinksTransformer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Text;
using System.Linq;
using log4net;
using Newtonsoft.Json.Linq;

using CKAN.NetKAN.Model;

namespace CKAN.NetKAN.Transformers
{
internal sealed class StagingLinksTransformer : ITransformer
{
public string Name { get { return "staging_links"; } }

public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
{
if (opts.Staged && !string.IsNullOrEmpty(opts.StagingReason))
{
var table = LinkTable(metadata);
if (!string.IsNullOrEmpty(table))
{
log.DebugFormat("Adding links to staging reason: {0}",
table);
opts.StagingReason += table;
}
}
// This transformer never changes the metadata
yield return metadata;
}

private string LinkTable(Metadata metadata)
{
var resourcesJson = (JObject)metadata?.Json()?["resources"];
if (resourcesJson == null)
{
// No resources, no links to append
return "";
}
StringBuilder table = new StringBuilder();
// Blank lines to separate the table from the description
table.AppendLine("");
table.AppendLine("");
table.AppendLine("Resource | URL");
table.AppendLine(":-- | :--");
foreach (var prop in resourcesJson.Properties().OrderBy(prop => prop.Name))
{
table.AppendLine($"{prop.Name} | <{prop.Value}>");
}
return table.ToString();
}

private static readonly ILog log = LogManager.GetLogger(typeof(StagingLinksTransformer));
}
}

0 comments on commit 4a2076b

Please sign in to comment.