-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
249 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace CKAN.NetKAN.Sources.SourceForge | ||
{ | ||
internal interface ISourceForgeApi | ||
{ | ||
SourceForgeMod GetMod(SourceForgeRef sfRef); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using System.IO; | ||
using System.Xml; | ||
using System.ServiceModel.Syndication; | ||
|
||
using CKAN.NetKAN.Services; | ||
|
||
namespace CKAN.NetKAN.Sources.SourceForge | ||
{ | ||
internal sealed class SourceForgeApi : ISourceForgeApi | ||
{ | ||
public SourceForgeApi(IHttpService httpSvc) | ||
{ | ||
this.httpSvc = httpSvc; | ||
} | ||
|
||
public SourceForgeMod GetMod(SourceForgeRef sfRef) | ||
=> new SourceForgeMod(sfRef, | ||
SyndicationFeed.Load(XmlReader.Create(new StringReader( | ||
httpSvc.DownloadText(new Uri( | ||
$"https://sourceforge.net/projects/{sfRef.Name}/rss")) | ||
?? "")))); | ||
|
||
private readonly IHttpService httpSvc; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Linq; | ||
using System.ServiceModel.Syndication; | ||
|
||
namespace CKAN.NetKAN.Sources.SourceForge | ||
{ | ||
internal class SourceForgeMod | ||
{ | ||
public SourceForgeMod(SourceForgeRef sfRef, | ||
SyndicationFeed feed) | ||
{ | ||
Title = feed.Title.Text; | ||
Description = feed.Description.Text; | ||
HomepageLink = $"https://sourceforge.net/projects/{sfRef.Name}/"; | ||
RepositoryLink = $"https://sourceforge.net/p/{sfRef.Name}/code/"; | ||
BugTrackerLink = $"https://sourceforge.net/p/{sfRef.Name}/bugs/"; | ||
Versions = feed.Items.Where(item => item.Title.Text.EndsWith(".zip", StringComparison.OrdinalIgnoreCase)) | ||
.Select(item => new SourceForgeVersion(item)) | ||
.ToArray(); | ||
} | ||
|
||
public readonly string Title; | ||
public readonly string Description; | ||
public readonly string HomepageLink; | ||
public readonly string RepositoryLink; | ||
public readonly string BugTrackerLink; | ||
public readonly SourceForgeVersion[] Versions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
using CKAN.Extensions; | ||
using CKAN.NetKAN.Model; | ||
|
||
namespace CKAN.NetKAN.Sources.SourceForge | ||
{ | ||
/// <summary> | ||
/// Represents a SourceForge $kref | ||
/// </summary> | ||
internal sealed class SourceForgeRef : RemoteRef | ||
{ | ||
/// <summary> | ||
/// Initialize the SourceForge reference | ||
/// </summary> | ||
/// <param name="reference">The base $kref object from a netkan</param> | ||
public SourceForgeRef(RemoteRef reference) | ||
: base(reference) | ||
{ | ||
if (Pattern.TryMatch(reference.Id, out Match? match)) | ||
{ | ||
Name = match.Groups["name"].Value; | ||
} | ||
else | ||
{ | ||
throw new Kraken(string.Format(@"Could not parse reference: ""{0}""", | ||
reference)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The name of the project on SourceForge | ||
/// </summary> | ||
public readonly string Name; | ||
|
||
private static readonly Regex Pattern = | ||
new Regex(@"^(?<name>[^/]+)$", | ||
RegexOptions.Compiled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Linq; | ||
using System.ServiceModel.Syndication; | ||
|
||
namespace CKAN.NetKAN.Sources.SourceForge | ||
{ | ||
internal class SourceForgeVersion | ||
{ | ||
public SourceForgeVersion(SyndicationItem item) | ||
{ | ||
Title = item.Title.Text.TrimStart('/'); | ||
// Throw an exception on missing or multiple <link/>s | ||
Link = item.Links.Single().Uri; | ||
Timestamp = item.PublishDate; | ||
} | ||
|
||
public readonly string Title; | ||
public readonly Uri Link; | ||
public readonly DateTimeOffset Timestamp; | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
using Newtonsoft.Json.Linq; | ||
using log4net; | ||
|
||
using CKAN.NetKAN.Model; | ||
using CKAN.NetKAN.Extensions; | ||
using CKAN.NetKAN.Sources.SourceForge; | ||
|
||
namespace CKAN.NetKAN.Transformers | ||
{ | ||
/// <summary> | ||
/// An <see cref="ITransformer"/> that looks up data from GitLab. | ||
/// </summary> | ||
internal sealed class SourceForgeTransformer : ITransformer | ||
{ | ||
/// <summary> | ||
/// Initialize the transformer | ||
/// </summary> | ||
/// <param name="api">Object to use for accessing the SourceForge API</param> | ||
public SourceForgeTransformer(ISourceForgeApi api) | ||
{ | ||
this.api = api; | ||
} | ||
|
||
/// <summary> | ||
/// Defines the name of this transformer | ||
/// </summary> | ||
public string Name => "sourceforge"; | ||
|
||
/// <summary> | ||
/// If input metadata has a GitLab kref, inflate it with whatever info we can get, | ||
/// otherwise return it unchanged | ||
/// </summary> | ||
/// <param name="metadata">Input netkan</param> | ||
/// <param name="opts">Inflation options from command line</param> | ||
/// <returns></returns> | ||
public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions? opts) | ||
{ | ||
if (metadata.Kref?.Source == Name) | ||
{ | ||
log.InfoFormat("Executing SourceForge transformation with {0}", metadata.Kref); | ||
var reference = new SourceForgeRef(metadata.Kref); | ||
var mod = api.GetMod(reference); | ||
var releases = mod.Versions | ||
.Skip(opts?.SkipReleases ?? 0) | ||
.Take(opts?.Releases ?? 1) | ||
.ToArray(); | ||
if (releases.Length < 1) | ||
{ | ||
log.WarnFormat("No releases found for {0}", reference); | ||
return Enumerable.Repeat(metadata, 1); | ||
} | ||
return releases.Select(ver => TransformOne(metadata.Json(), mod, ver)); | ||
} | ||
else | ||
{ | ||
// Passthrough for non-GitLab mods | ||
return Enumerable.Repeat(metadata, 1); | ||
} | ||
} | ||
|
||
private static Metadata TransformOne(JObject json, | ||
SourceForgeMod mod, | ||
SourceForgeVersion version) | ||
{ | ||
json.SafeAdd("name", mod.Title); | ||
json.SafeMerge("resources", JObject.FromObject(new Dictionary<string, string?>() | ||
{ | ||
{ "homepage", mod.HomepageLink }, | ||
{ "repository", mod.RepositoryLink }, | ||
{ "bugtracker", mod.BugTrackerLink }, | ||
})); | ||
// SourceForge doesn't send redirects to user agents it considers browser-like | ||
json.SafeAdd("download", Net.ResolveRedirect(version.Link, "Wget") | ||
?.OriginalString); | ||
json.SafeAdd(Metadata.UpdatedPropertyName, version.Timestamp); | ||
|
||
json.Remove("$kref"); | ||
|
||
log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json); | ||
return new Metadata(json); | ||
} | ||
|
||
private readonly ISourceForgeApi api; | ||
private static readonly ILog log = LogManager.GetLogger(typeof(GitlabTransformer)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters