Пример #1
0
 // GH #816: Ensure URLs with & are encoded correctly.
 public string SD_URL_encode_816(string path)
 {
     return(SpacedockMod.FromJson(string.Format(
                                      @"{{""name"":""Mod"",""id"":69420,""game"":""Kerbal Space Program"",""game_id"":3102,""short_description"":""A mod"",""description"":""A mod"",""downloads"":0,""followers"":0,""author"":""linuxgurugamer"",""default_version_id"":1,""shared_authors"":[],""background"":null,""bg_offset_y"":null,""license"":""MIT"",""website"":null,""donations"":null,""source_code"":null,""url"":""/mod/69420/Mod"",""versions"":[{{""friendly_version"":""1"",""game_version"":""1.12.2"",""id"":1,""created"":""2021-07-16T20:46:12.309009+00:00"",""download_path"":""{0}"",""changelog"":"""",""downloads"":0}}]}}",
                                      path
                                      )).versions[0].download_path.OriginalString);
 }
Пример #2
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());
        }
Пример #3
0
        private static List <string> GetAuthors(SpacedockMod mod)
        {
            var result = new List <string> {
                mod.author
            };

            if (mod.shared_authors != null)
            {
                result.AddRange(mod.shared_authors.Select(i => i.Username));
            }

            return(result);
        }
Пример #4
0
        private static SpacedockMod MakeTestMod()
        {
            var sdmod = new SpacedockMod
            {
                license           = "CC-BY",
                name              = "Dogecoin Flag",
                short_description = "Such test. Very unit. Wow.",
                author            = "pjf",
                versions          = new SDVersion[1]
            };

            sdmod.versions[0] = new SDVersion
            {
                friendly_version = new CKAN.Version("0.25"),
                download_path    = new Uri("http://example.com/")
            };

            return(sdmod);
        }
Пример #5
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));
        }
Пример #6
0
        // GH #214: Make sure we pick up the right version
        public void SD_Version_Select_214()
        {
            var mod = SpacedockMod.FromJson(TestData.KS_CustomAsteroids_string());

            Assert.AreEqual(711, mod.Latest().id, "GH #214 - Select default_version_id");
        }
Пример #7
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));
        }