public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Kref != null && metadata.Kref.Source == KrefSource)
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing MetaNetkan transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                // Make sure resources exist, save metanetkan
                if (json["resources"] == null)
                {
                    json["resources"] = new JObject();
                }
                var resourcesJson = (JObject)json["resources"];
                resourcesJson.SafeAdd("metanetkan", metadata.Kref.Id);

                var uri            = new Uri(metadata.Kref.Id);
                var targetFileText = _http.DownloadText(CKAN.Net.GetRawUri(uri));

                Log.DebugFormat("Target netkan:{0}{1}", Environment.NewLine, targetFileText);

                var targetJson     = JObject.Parse(targetFileText);
                var targetMetadata = new Metadata(targetJson);

                if (targetMetadata.Kref == null || targetMetadata.Kref.Source != "netkan")
                {
                    json["spec_version"] = ModuleVersion.Max(metadata.SpecVersion, targetMetadata.SpecVersion)
                                           .ToSpecVersionJson();

                    if (targetJson["$kref"] != null)
                    {
                        json["$kref"] = targetJson["$kref"];
                    }
                    else
                    {
                        json.Remove("$kref");
                    }

                    json.SafeMerge("resources", targetJson["resources"]);

                    foreach (var property in targetJson.Properties())
                    {
                        json.SafeAdd(property.Name, property.Value);
                    }

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

                    yield return(new Metadata(json));
                }
                else
                {
                    throw new Kraken("The target of a metanetkan may not also be a metanetkan.");
                }
            }
            else
            {
                yield return(metadata);
            }
        }
Exemple #2
0
        private string Call(int modid)
        {
            var url = CurseApiBase + modid + CurseApiEnd;

            Log.DebugFormat("Calling {0}", url);

            return(_http.DownloadText(new Uri(url)));
        }
Exemple #3
0
        private string Call(string nameOrId)
        {
            int id;
            // If it's numeric, use the old URL format,
            // otherwise use the new.
            var url = Int32.TryParse(nameOrId, out id)
                ? CurseApiBaseOld + id
                : CurseApiBase + nameOrId;

            Log.InfoFormat("Calling {0}", url);
            return(_http.DownloadText(new Uri(url)));
        }
Exemple #4
0
        private string Call(string path)
        {
            // TODO: There's got to be a better way than using regexps.
            // new Uri (spacedock_api, path) doesn't work, it only uses the *base* of the first arg,
            // and hence drops the /api path.

            // Remove leading slashes.
            path = Regex.Replace(path, "^/+", "");

            var url = SpacedockApiBase + path;

            Log.DebugFormat("Calling {0}", url);

            return(_http.DownloadText(new Uri(url)));
        }
Exemple #5
0
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == KrefSource)
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing MetaNetkan transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var uri            = new Uri(metadata.Kref.Id);
                var targetFileText = _http.DownloadText(uri);

                Log.DebugFormat("Target netkan:{0}{1}", Environment.NewLine, targetFileText);

                var targetJson     = JObject.Parse(targetFileText);
                var targetMetadata = new Metadata(targetJson);

                if (targetMetadata.Kref == null || targetMetadata.Kref.Source != "netkan")
                {
                    json["spec_version"] = ModuleVersion.Max(metadata.SpecVersion, targetMetadata.SpecVersion)
                                           .ToSpecVersionJson();

                    if (targetJson["$kref"] != null)
                    {
                        json["$kref"] = targetJson["$kref"];
                    }
                    else
                    {
                        json.Remove("$kref");
                    }

                    foreach (var property in targetJson.Properties())
                    {
                        json.SafeAdd(property.Name, property.Value);
                    }

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

                    return(new Metadata(json));
                }
                else
                {
                    throw new Kraken("The target of a metanetkan may not also be a metanetkan.");
                }
            }

            return(metadata);
        }
Exemple #6
0
        private string Call(string path)
        {
            var url = new Uri(ApiBase, path);

            Log.DebugFormat("Calling {0}", url);

            try
            {
                return(_http.DownloadText(url, _oauthToken));
            }
            catch (WebException webEx)
            {
                Log.ErrorFormat("WebException while accessing {0}: {1}", url, webEx);
                throw;
            }
        }
Exemple #7
0
        private string Call(string path)
        {
            var url = new Uri(ApiBase, path);

            Log.DebugFormat("Calling {0}", url);

            try
            {
                return(_http.DownloadText(url, _oauthToken));
            }
            catch (NativeAndCurlDownloadFailedKraken k)
            {
                if (k.responseStatus == 403 && k.responseHeader.Contains("X-RateLimit-Remaining: 0"))
                {
                    throw new Kraken("GitHub API rate limit exceeded.");
                }
                throw;
            }
        }
Exemple #8
0
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Kref?.Source == "ksp-avc")
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing KSP-AVC $kref transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var        url       = new Uri(metadata.Kref.Id);
                AvcVersion remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(
                    githubSrc?.DownloadText(url)
                    ?? httpSvc.DownloadText(CKAN.Net.GetRawUri(url))
                    );

                json.SafeAdd("name", remoteAvc.Name);
                json.Remove("$kref");
                json.SafeAdd("download", remoteAvc.Download);

                // Set .resources.repository based on GITHUB properties
                if (remoteAvc.Github?.Username != null && remoteAvc.Github?.Repository != null)
                {
                    // Make sure resources exist.
                    if (json["resources"] == null)
                    {
                        json["resources"] = new JObject();
                    }
                    var resourcesJson = (JObject)json["resources"];
                    resourcesJson.SafeAdd("repository", $"https://github.com/{remoteAvc.Github.Username}/{remoteAvc.Github.Repository}");
                }

                // Use standard KSP-AVC logic to set version and the ksp_version_* properties
                AvcTransformer.ApplyVersions(json, remoteAvc);

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

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }
        private string Call(string path)
        {
            // TODO: There's got to be a better way than using regexps.
            // new Uri (kerbalstuff_api, path) doesn't work, it only uses the *base* of the first arg,
            // and hence drops the /api path.

            // Remove leading slashes.
            path = Regex.Replace(path, "^/+", "");

            var url = KerbalstuffApiBase + path;

            Log.DebugFormat("Calling {0}", url);
            try
            {
                return(_http.DownloadText(new Uri(url)));
            }
            catch (DllNotFoundException)
            {
                //Curl is not installed. Curl is a workaround for a mono issue.
                //TODO Richard - Once repos are merged go and check all Platform calls to see if they are mono checks
                if (!Platform.IsWindows)
                {
                    throw;
                }
                //On mircrosft.net so try native code.
                using (var web = new WebClient())
                {
                    try
                    {
                        return(web.DownloadString(url));
                    }
                    catch (WebException webEx)
                    {
                        Log.ErrorFormat("WebException while accessing {0}: {1}", url, webEx);
                        throw;
                    }
                }
            }
        }
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            _vrefValidator.Validate(metadata);

            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing internal AVC transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }

                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadModule(metadata);
                var avc  = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    var resourcesJson = (JObject)json["resources"];
                    var remoteUri     = resourcesJson?["remote-avc"] != null
                        ? new Uri((string)resourcesJson["remote-avc"])
                        : GetRemoteAvcUri(avc);

                    if (remoteUri != null)
                    {
                        if (resourcesJson == null)
                        {
                            json["resources"] = resourcesJson = new JObject();
                        }
                        resourcesJson.SafeAdd("remote-avc", remoteUri.OriginalString);

                        try
                        {
                            var remoteJson = _github?.DownloadText(remoteUri)
                                             ?? _http.DownloadText(remoteUri);
                            var remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(remoteJson);

                            if (avc.version.CompareTo(remoteAvc.version) == 0)
                            {
                                // Local AVC and Remote AVC describe the same version, prefer
                                Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferentially.");
                                avc = remoteAvc;
                            }
                        }
                        catch (JsonReaderException e)
                        {
                            Log.WarnFormat("Error parsing remote version file {0}: {1}",
                                           remoteUri, e.Message);
                            Log.Debug(e);
                        }
                        catch (Exception e)
                        {
                            Log.WarnFormat("Error fetching remote version file {0}: {1}", remoteUri, e.Message);
                            Log.Debug(e);
                        }
                    }

                    ApplyVersions(json, avc);

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

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

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }
        public Metadata Transform(Metadata metadata)
        {
            if (metadata.Kref != null && metadata.Kref.Source == "jenkins")
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing Jenkins transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var buildType          = "stable";
                var useFilenameVersion = false;
                var assetMatchPattern  = Constants.DefaultAssetMatchPattern;

                var jenkinsMetadata = (JObject)json["x_netkan_jenkins"];
                if (jenkinsMetadata != null)
                {
                    var jenkinsBuildMetadata = (string)jenkinsMetadata["build"];

                    if (jenkinsBuildMetadata != null)
                    {
                        buildType = jenkinsBuildMetadata;
                    }

                    var jenkinsUseFilenameVersionMetadata = (bool?)jenkinsMetadata["use_filename_version"];

                    if (jenkinsUseFilenameVersionMetadata != null)
                    {
                        useFilenameVersion = jenkinsUseFilenameVersionMetadata.Value;
                    }

                    var jenkinsAssetMatchMetadata = (string)jenkinsMetadata["asset_match"];

                    if (jenkinsAssetMatchMetadata != null)
                    {
                        assetMatchPattern = new Regex(jenkinsAssetMatchMetadata);
                    }
                }

                Log.InfoFormat("Attempting to retrieve the last {0} build", buildType);

                // Get the job metadata
                var job = JsonConvert.DeserializeObject <JObject>(
                    _http.DownloadText(new Uri(metadata.Kref.Id + "api/json"))
                    );

                // Get the build reference metadata
                var buildRef = (JObject)job[BuildTypeToProperty[buildType]];

                // Get the build number and url
                var buildNumber = (int)buildRef["number"];
                var buildUrl    = (string)buildRef["url"];

                Log.InfoFormat("The last {0} build is #{1}", buildType, buildNumber);

                // Get the build metadata
                var build = JsonConvert.DeserializeObject <JObject>(
                    _http.DownloadText(new Uri(buildUrl + "api/json"))
                    );

                // Get the artifact metadata
                var artifacts = ((JArray)build["artifacts"])
                                .Select(i => (JObject)i)
                                .Where(i => assetMatchPattern.IsMatch((string)i["fileName"]))
                                .ToList();

                switch (artifacts.Count)
                {
                case 1:
                    var artifact = artifacts.Single();

                    var artifactFileName     = artifact["fileName"];
                    var artifactRelativePath = artifact["relativePath"];

                    // I'm not sure if 'relativePath' is the right property to use here
                    var download = Uri.EscapeUriString(buildUrl + "artifact/" + artifactRelativePath);
                    var version  = artifactFileName;

                    Log.DebugFormat("Using download URL: {0}", download);
                    json.SafeAdd("download", download);

                    if (useFilenameVersion)
                    {
                        Log.DebugFormat("Using filename as version: {0}", version);
                        json.SafeAdd("version", version);
                    }

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

                    var resourcesJson = (JObject)json["resources"];
                    resourcesJson.SafeAdd("ci", Uri.EscapeUriString(metadata.Kref.Id));

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

                    return(new Metadata(json));

                case 0:
                    throw new Exception("Could not find any matching artifacts");

                default:
                    throw new Exception("Found too many matching artifacts");
                }
            }

            return(metadata);
        }
Exemple #12
0
        public IEnumerable <Metadata> Transform(Metadata metadata, TransformOptions opts)
        {
            if (metadata.Vref != null && metadata.Vref.Source == "ksp-avc")
            {
                var json = metadata.Json();

                Log.InfoFormat("Executing internal AVC transformation with {0}", metadata.Kref);
                Log.DebugFormat("Input metadata:{0}{1}", Environment.NewLine, json);

                var noVersion = metadata.Version == null;

                if (noVersion)
                {
                    json["version"] = "0"; // TODO: DBB: Dummy version necessary to the next statement doesn't throw
                }

                var mod = CkanModule.FromJson(json.ToString());

                if (noVersion)
                {
                    json.Remove("version");
                }

                var file = _http.DownloadPackage(metadata.Download, metadata.Identifier, metadata.RemoteTimestamp);
                var avc  = _moduleService.GetInternalAvc(mod, file, metadata.Vref.Id);

                if (avc != null)
                {
                    Log.Info("Found internal AVC version file");

                    var remoteUri = GetRemoteAvcUri(avc);

                    if (remoteUri != null)
                    {
                        try
                        {
                            var remoteJson = _github?.DownloadText(remoteUri)
                                             ?? _http.DownloadText(remoteUri);
                            var remoteAvc = JsonConvert.DeserializeObject <AvcVersion>(remoteJson);

                            if (avc.version.CompareTo(remoteAvc.version) == 0)
                            {
                                // Local AVC and Remote AVC describe the same version, prefer
                                Log.Info("Remote AVC version file describes same version as local AVC version file, using it preferrentially.");
                                avc = remoteAvc;
                            }
                        }
                        catch (Exception e)
                        {
                            Log.InfoFormat("An error occured fetching the remote AVC version file, ignoring: {0}", e.Message);
                            Log.Debug(e);
                        }
                    }

                    ApplyVersions(json, avc);

                    // It's cool if we don't have version info at all, it's optional in the AVC spec.

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

                yield return(new Metadata(json));
            }
            else
            {
                yield return(metadata);
            }
        }