Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append resource links to staging PRs #3454

Merged
merged 1 commit into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}