Esempio n. 1
0
        public static async Task <int> RunAndReturn(PushOptions options, PackageHelper packageHelper)
        {
            // --package=MyFile.zip
            // --package=./MyFile.zip
            // --package=../MyParentFolder.zip
            var filePath = options.Package;
            var apiKey   = options.ApiKey;

            var keyParts = packageHelper.SplitKey(apiKey);

            // Check we can find the file
            packageHelper.EnsurePackageExists(filePath);

            // Check File is a ZIP
            packageHelper.EnsureIsZip(filePath);

            // Check zip contains valid package.xml
            packageHelper.EnsureContainsPackageXml(filePath);

            // gets a package list from our.umbraco
            // if the api key is invalid we will also find out here.
            var packages = await packageHelper.GetPackageList(keyParts);

            if (packages != null)
            {
                packageHelper.EnsurePackageDoesntAlreadyExists(packages, filePath);
            }

            // Parse package.xml before upload to print out info
            // and to use for comparison on what is already uploaded
            var packageInfo = Parse.PackageXml(filePath);

            // OK all checks passed - time to upload it
            await UploadPackage(options, packageHelper);

            // Got this far then it got uploaded to our.umb all OK
            Console.WriteLine(Resources.Push_Complete, filePath);

            return(0);
        }
Esempio n. 2
0
        public static async Task <int> RunAndReturn(PushOptions options, PackageHelper packageHelper)
        {
            // --package=MyFile.zip
            // --package=./MyFile.zip
            // --package=../MyParentFolder.zip
            var filePath = options.Package;
            var apiKey   = options.ApiKey;

            var keyParts = packageHelper.SplitKey(apiKey);

            // Check we can find the file
            packageHelper.EnsurePackageExists(filePath);

            // Check File is a ZIP
            packageHelper.EnsureIsZip(filePath);

            // Check zip contains valid package.xml
            packageHelper.EnsureContainsPackageXml(filePath);

            // gets a package list from our.umbraco
            // if the api key is invalid we will also find out here.
            var packages = await packageHelper.GetPackageList(keyParts);

            var currentPackageId = await packageHelper.GetCurrentPackageFileId(keyParts);

            if (packages != null)
            {
                packageHelper.EnsurePackageDoesntAlreadyExists(packages, filePath);
            }

            // Archive packages
            var archivePatterns   = new List <string>();
            var packagesToArchive = new List <int>();

            if (options.Archive != null)
            {
                archivePatterns.AddRange(options.Archive);
            }

            if (archivePatterns.Count > 0)
            {
                foreach (var archivePattern in archivePatterns)
                {
                    if (archivePattern == "current")
                    {
                        // If the archive option is "current", then archive the current package
                        if (currentPackageId != "0")
                        {
                            packagesToArchive.Add(int.Parse(currentPackageId));
                        }
                    }
                    else
                    {
                        // Convert the archive option to a regex
                        var archiveRegex = new Regex("^" + archivePattern.Replace(".", "\\.").Replace("*", "(.*)") + "$", RegexOptions.IgnoreCase);

                        // Find packages that match the regex and extract their IDs
                        var archiveIds = packages.Where(x => archiveRegex.IsMatch(x.Value <string>("Name"))).Select(x => x.Value <int>("Id")).ToArray();

                        packagesToArchive.AddRange(archiveIds);
                    }
                }
            }

            if (packagesToArchive.Count > 0)
            {
                await packageHelper.ArchivePackages(keyParts, packagesToArchive.Distinct());

                Console.WriteLine($"Archived {packagesToArchive.Count} packages matching the archive pattern.");
            }

            // Parse package.xml before upload to print out info
            // and to use for comparison on what is already uploaded
            var packageInfo = Parse.PackageXml(filePath);

            // OK all checks passed - time to upload it
            await UploadPackage(options, packageHelper, packageInfo);

            return(0);
        }
Esempio n. 3
0
        private static async Task UploadPackage(PushOptions options, PackageHelper packageHelper, PackageInfo packageInfo)
        {
            try
            {
                // HttpClient will use this event handler to give us
                // Reporting on how its progress the file upload
                var processMsgHandler = new ProgressMessageHandler(new HttpClientHandler());
                processMsgHandler.HttpSendProgress += (sender, e) =>
                {
                    // Could try to reimplement progressbar - but that library did not work in GH Actions :(
                    var percent = e.ProgressPercentage;
                };

                var keyParts        = packageHelper.SplitKey(options.ApiKey);
                var packageFileName = Path.GetFileName(options.Package);

                Console.WriteLine(Resources.Push_Uploading, packageFileName);

                var url = "/Umbraco/Api/ProjectUpload/UpdatePackage";

                using (var client = packageHelper.GetClientBase(url, keyParts.Token, keyParts.MemberId, keyParts.ProjectId))
                {
                    MultipartFormDataContent form = new MultipartFormDataContent();
                    var fileInfo = new FileInfo(options.Package);
                    var content  = new StreamContent(fileInfo.OpenRead());
                    content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                    {
                        Name     = "file",
                        FileName = fileInfo.Name
                    };
                    form.Add(content);
                    form.Add(new StringContent(ParseCurrentFlag(options.Current)), "isCurrent");
                    form.Add(new StringContent(options.DotNetVersion), "dotNetVersion");
                    form.Add(new StringContent("package"), "fileType");
                    form.Add(GetVersionCompatibility(options.WorksWith), "umbracoVersions");
                    form.Add(new StringContent(packageInfo.VersionString), "packageVersion");

                    var httpResponse = await client.PostAsync(url, form);

                    if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        packageHelper.WriteError(Resources.Push_ApiKeyInvalid);
                        Environment.Exit(5); // ERROR_ACCESS_DENIED
                    }
                    else if (httpResponse.IsSuccessStatusCode)
                    {
                        Console.WriteLine(Resources.Push_Complete, packageFileName);

                        // Response is not reported (at the moment)
                        // var apiReponse = await httpResponse.Content.ReadAsStringAsync();
                        // Console.WriteLine(apiReponse);
                    }
                }
            }
            catch (HttpRequestException ex)
            {
                // Could get network error or our.umb down
                Console.WriteLine(Resources.Error, ex);
                throw;
            }
        }