示例#1
0
        public void SDHome()
        {
            var sd = new SpacedockMod {
                name = "foo bar", id = 123
            };

            // SDHome no longer escapes URLs.
            Assert.AreEqual("https://spacedock.info/mod/123/foo bar", sd.GetPageUrl().ToString());
        }
示例#2
0
        private Metadata TransformOne(Metadata metadata, JObject json, SpacedockMod sdMod, SDVersion latestVersion)
        {
            Log.InfoFormat("Found SpaceDock mod: {0} {1}", sdMod.name, latestVersion.friendly_version);

            // Only pre-fill version info if there's none already. GH #199
            if (json["ksp_version_min"] == null && json["ksp_version_max"] == null && json["ksp_version"] == null)
            {
                Log.DebugFormat("Writing ksp_version from SpaceDock: {0}", latestVersion.KSP_version);
                json["ksp_version"] = latestVersion.KSP_version.ToString();
            }

            json.SafeAdd("name", sdMod.name);
            json.SafeAdd("abstract", sdMod.short_description);
            json.SafeAdd("version", latestVersion.friendly_version.ToString());
            json.Remove("$kref");
            json.SafeAdd("download", latestVersion.download_path.OriginalString);
            json.SafeAdd(Model.Metadata.UpdatedPropertyName, latestVersion.created);

            var authors = GetAuthors(sdMod);

            if (authors.Count == 1)
            {
                json.SafeAdd("author", sdMod.author);
            }
            else if (authors.Count > 1)
            {
                json.SafeAdd("author", new JArray(authors));
            }

            // SD provides users with the following default selection of licenses. Let's convert them to CKAN
            // compatible license strings if possible.
            //
            // "MIT" - OK
            // "BSD" - Specific version is indeterminate
            // "GPLv2" - Becomes "GPL-2.0"
            // "GPLv3" - Becomes "GPL-3.0"
            // "LGPL" - Specific version is indeterminate

            var sdLicense = sdMod.license.Trim();

            switch (sdLicense)
            {
            case "GPLv2":
                json.SafeAdd("license", "GPL-2.0");
                break;

            case "GPLv3":
                json.SafeAdd("license", "GPL-3.0");
                break;

            default:
                json.SafeAdd("license", sdLicense);
                break;
            }

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

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

            TryAddResourceURL(metadata.Identifier, resourcesJson, "homepage", sdMod.website);
            TryAddResourceURL(metadata.Identifier, resourcesJson, "repository", sdMod.source_code);
            resourcesJson.SafeAdd("spacedock", sdMod.GetPageUrl().OriginalString);

            if (sdMod.background != null)
            {
                TryAddResourceURL(metadata.Identifier, resourcesJson, "x_screenshot", sdMod.background.ToString());
            }

            Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

            return(new Metadata(json));
        }
示例#3
0
        private Metadata TransformOne(Metadata metadata, JObject json, SpacedockMod sdMod, SDVersion latestVersion)
        {
            Log.InfoFormat("Found SpaceDock mod: {0} {1}", sdMod.name, latestVersion.friendly_version);

            // Only pre-fill version info if there's none already. GH #199
            if (json["ksp_version_min"] == null && json["ksp_version_max"] == null && json["ksp_version"] == null)
            {
                Log.DebugFormat("Writing ksp_version from SpaceDock: {0}", latestVersion.KSP_version);
                json["ksp_version"] = latestVersion.KSP_version.ToString();
            }

            json.SafeAdd("name", sdMod.name);
            json.SafeAdd("abstract", sdMod.short_description);
            json.SafeAdd("version", latestVersion.friendly_version.ToString());
            json.Remove("$kref");
            json.SafeAdd("download", latestVersion.download_path.OriginalString);
            json.SafeAdd(Model.Metadata.UpdatedPropertyName, latestVersion.created);

            var authors = GetAuthors(sdMod);

            if (authors.Count == 1)
            {
                json.SafeAdd("author", sdMod.author);
            }
            else if (authors.Count > 1)
            {
                json.SafeAdd("author", new JArray(authors));
            }

            // SD provides users with the following default selection of licenses. Let's convert them to CKAN
            // compatible license strings if possible.
            //
            // "MIT" - OK
            // "BSD" - Specific version is indeterminate
            // "GPLv2" - Becomes "GPL-2.0"
            // "GPLv3" - Becomes "GPL-3.0"
            // "LGPL" - Specific version is indeterminate

            var sdLicense = sdMod.license.Trim();

            switch (sdLicense)
            {
            case "GPLv2":
                json.SafeAdd("license", "GPL-2.0");
                break;

            case "GPLv3":
                json.SafeAdd("license", "GPL-3.0");
                break;

            default:
                json.SafeAdd("license", sdLicense);
                break;
            }

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

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

            resourcesJson.SafeAdd("spacedock", sdMod.GetPageUrl().OriginalString);
            TryAddResourceURL(metadata.Identifier, resourcesJson, "homepage", sdMod.website);

            if (sdMod.background != null)
            {
                TryAddResourceURL(metadata.Identifier, resourcesJson, "x_screenshot", sdMod.background.ToString());
            }

            if (!string.IsNullOrEmpty(sdMod.source_code))
            {
                try
                {
                    var uri = new Uri(sdMod.source_code);
                    if (uri.Host == "github.com")
                    {
                        var match = githubUrlPathPattern.Match(uri.AbsolutePath);
                        if (match.Success)
                        {
                            var owner    = match.Groups["owner"].Value;
                            var repo     = match.Groups["repo"].Value;
                            var repoInfo = _githubApi.GetRepo(new GithubRef(
                                                                  $"#/ckan/github/{owner}/{repo}", false, false
                                                                  ));

                            if (sdMod.source_code != repoInfo.HtmlUrl)
                            {
                                TryAddResourceURL(metadata.Identifier, resourcesJson, "repository", repoInfo.HtmlUrl);
                            }
                            // Fall back to homepage from GitHub
                            TryAddResourceURL(metadata.Identifier, resourcesJson, "homepage", repoInfo.Homepage);
                            if (repoInfo.HasIssues)
                            {
                                // Set bugtracker if repo has issues list
                                TryAddResourceURL(metadata.Identifier, resourcesJson, "bugtracker", $"{repoInfo.HtmlUrl}/issues");
                            }
                            if (repoInfo.Archived)
                            {
                                Log.Warn("Repo is archived, consider freezing");
                            }
                        }
                    }
                }
                catch
                {
                    // Just give up, it's fine
                }
            }
            TryAddResourceURL(metadata.Identifier, resourcesJson, "repository", sdMod.source_code);

            Log.DebugFormat("Transformed metadata:{0}{1}", Environment.NewLine, json);

            return(new Metadata(json));
        }