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

Add owners of parent GitHub repos to authors #2922

Merged
merged 2 commits into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions Netkan/Extensions/JObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ public static void SafeAdd(this JObject jobject, string propertyName, JToken tok
}
}

/// <summary>
/// Write a property to a <see cref="JObject"/> only if it does not already exist and the value is not null.
/// The value is generated on-demand by calling tokenCallback, only when we need it.
/// </summary>
/// <param name="jobject">The <see cref="JObject"/> to write to</param>
/// <param name="propertyName">The name of the property to write</param>
/// <param name="tokenCallback">Function to generate value of the property to write if it does not exist</param>
public static void SafeAdd(this JObject jobject, string propertyName, Func<JToken> tokenCallback)
{
if (String.IsNullOrWhiteSpace(propertyName))
{
Log.Warn("Asked to set a property named null on a JSON object!");
return;
}
if (jobject[propertyName] == null)
{
jobject.SafeAdd(propertyName, tokenCallback());
}
}

/// <summary>
/// Merge an object's properties into one of our child objects
/// E.g., the "resources" object should accumulate values from all levels
Expand Down
15 changes: 15 additions & 0 deletions Netkan/Sources/Github/GithubRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@ public sealed class GithubRepo

[JsonProperty("license")]
public GithubLicense License { get; set; }

[JsonProperty("parent")]
public GithubRepo ParentRepo { get; set; }

[JsonProperty("source")]
public GithubRepo SourceRepo { get; set; }

[JsonProperty("owner")]
public GithubUser Owner { get; set; }
}

public class GithubLicense
{
[JsonProperty("spdx_id")]
public string Id;
}

public class GithubUser
{
[JsonProperty("login")]
public string Login { get; set; }
}
}
2 changes: 1 addition & 1 deletion Netkan/Transformers/CurseTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private Metadata TransformOne(JObject json, CurseMod curseMod, CurseFile latestV
else if (useCurseIdVersion) json.SafeAdd("version", latestVersion.GetCurseIdVersion());
else json.SafeAdd("version", latestVersion.GetFileVersion());

json.SafeAdd("author", JToken.FromObject(curseMod.authors));
json.SafeAdd("author", () => JToken.FromObject(curseMod.authors));
json.SafeAdd("download", Regex.Replace(latestVersion.GetDownloadUrl(), " ", "%20"));

// Curse provides users with the following default selection of licenses. Let's convert them to CKAN
Expand Down
31 changes: 24 additions & 7 deletions Netkan/Transformers/GithubTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,6 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)

private Metadata TransformOne(Metadata metadata, JObject json, GithubRef ghRef, GithubRepo ghRepo, GithubRelease ghRelease)
{
// Make sure resources exist.
if (json["resources"] == null)
json["resources"] = new JObject();

var resourcesJson = (JObject)json["resources"];

if (!string.IsNullOrWhiteSpace(ghRepo.Description))
json.SafeAdd("abstract", ghRepo.Description);

Expand All @@ -96,6 +90,12 @@ private Metadata TransformOne(Metadata metadata, JObject json, GithubRef ghRef,
&& ghRepo.License.Id != "NOASSERTION")
json.SafeAdd("license", ghRepo.License.Id);

// Make sure resources exist.
if (json["resources"] == null)
json["resources"] = new JObject();

var resourcesJson = (JObject)json["resources"];

if (!string.IsNullOrWhiteSpace(ghRepo.Homepage))
resourcesJson.SafeAdd("homepage", ghRepo.Homepage);

Expand All @@ -104,7 +104,7 @@ private Metadata TransformOne(Metadata metadata, JObject json, GithubRef ghRef,
if (ghRelease != null)
{
json.SafeAdd("version", ghRelease.Version.ToString());
json.SafeAdd("author", ghRelease.Author);
json.SafeAdd("author", () => getAuthors(ghRepo, ghRelease));
json.SafeAdd("download", ghRelease.Download.ToString());
json.SafeAdd(Model.Metadata.UpdatedPropertyName, ghRelease.AssetUpdated);

Expand Down Expand Up @@ -145,5 +145,22 @@ private Metadata TransformOne(Metadata metadata, JObject json, GithubRef ghRef,
}
}

private JToken getAuthors(GithubRepo repo, GithubRelease release)
{
// Start with the user that published the release
var authors = new HashSet<string>() { release.Author };
for (GithubRepo r = repo; r != null;)
{
// Add repo owner
authors.Add(r.Owner.Login);
// Check parent repos
r = r.ParentRepo == null
? null
: _api.GetRepo(new GithubRef($"#/ckan/github/{r.ParentRepo.FullName}", false, _matchPreleases));
}
// Return a string if just one author, else an array
return authors.Count == 1 ? (JToken)authors.First() : new JArray(authors);
}

}
}
2 changes: 1 addition & 1 deletion Tests/NetKAN/Extensions/JObjectExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void SafeAddDoesNotAddPropertyWhenValueIsNull()
sut["foo"] = "bar";

// Act
sut.SafeAdd("foo", null);
sut.SafeAdd("foo", null as JToken);

// Assert
Assert.That((string)sut["foo"], Is.EqualTo("bar"),
Expand Down