Beispiel #1
0
        /// <summary>
        /// Creates a new instance of <see cref="T:github_release_poster.NewRelease"/> from the data
        /// provided in this method's parameters.
        /// </summary>
        /// <param name="body">String containing the body content for this release.</param>
        /// <param name="isDraft">True if this release is to be narked as a Draft.</param>
        /// <param name="name">String containing the name of this release.</param>
        /// <param name="isPreRelease">True if this release is to be marked as Pre-Release.</param>
        /// <param name="tagName">String containing the name of the Git tag to be created and associated with the commit for this release.</param>
        /// <param name="targetBranchName">String containing the name of the target Git branch.</param>
        /// <returns>Reference to an instance of <see cref="T:github_release_poster.NewRelease"/> whose
        /// properties are initialized to the values passed to this method.  No validation is done.</returns>
        /// <remarks>Applications should use a validator object on the output of this method before using this object
        /// in any API calls.</remarks>
        public static NewRelease CreateNewRelease(string body,
                                                  bool isDraft, string name, bool isPreRelease, string tagName,
                                                  string targetBranchName)
        {
            Console.WriteLine("Initializing new release metadata...");

            var result = new NewRelease()
            {
                body             = body,
                draft            = isDraft,
                name             = name,
                prerelease       = isPreRelease,
                tag_name         = tagName,
                target_commitish = targetBranchName
            };

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Posts the new release to GitHub.
        /// </summary>
        /// <param name="repoName">(Required.) Name of the GitHub repository to which the release is to be posted.</param>
        /// <param name="repoOwner">(Required.) GitHub username of a user with push access to the repository to which the release is to be posted.</param>
        /// <param name="userAccessToken">(Required.) GitHub user access token which belongs to the repository owner and that has push access to the repository.</param>
        /// <param name="releaseAssetDir">(Required.) Full path to the directory where the assets for the current release are located.  Must be a path of a folder that currently exists.</param>
        /// <param name="release">(Required.) Reference to an instance of <see cref="T:github_releaser.NewRelease"/></param>
        /// <param name="shouldNotZip">(Optional, default is false). Set to true to upload release assets individually rather than ZIPping the contents of the release asset dir.</param>
        /// that contains the required release information in its properties.
        /// <param name="assetsZipName">Name to give to the zip file of the assets.  Ignored if <see cref="shouldNotZip"/> is set to true.</param>
        /// <returns>True if the post succeeded and all assets got uploaded; false otherwise.</returns>
        /// <remarks>The <see cref="T:github_releaser.NewRelease"/> object is serialized to JSON and then posted to the GitHub Server.  This object is assumed to have valid information.
        /// To validate the information, call the <see cref="M:github_releaser.GitHubReleaeValidator.IsReleaseValid"/> method.</remarks>
        public static bool PostNewRelease(string repoName,
                                          string repoOwner, string userAccessToken, string releaseAssetDir,
                                          NewRelease release, bool shouldNotZip = false, string assetsZipName = "assets.zip")
        {
            Console.WriteLine(Resources.PostingReleaseToWhichRepo, release.name, repoName);

            // Parameter validation
            if (string.IsNullOrWhiteSpace(repoName))
            {
                throw new ArgumentNullException(nameof(repoName));
            }

            if (string.IsNullOrWhiteSpace(repoOwner))
            {
                throw new ArgumentNullException(nameof(repoOwner));
            }

            if (string.IsNullOrWhiteSpace(userAccessToken))
            {
                throw new ArgumentNullException(nameof(userAccessToken));
            }

            if (string.IsNullOrWhiteSpace(releaseAssetDir))
            {
                throw new ArgumentNullException(nameof(releaseAssetDir));
            }

            if (!Directory.Exists(releaseAssetDir))
            {
                throw new DirectoryNotFoundException(string.Format(
                                                         Resources.ReleaseAssetDirNotFound, releaseAssetDir));
            }

            if (release == null)
            {
                throw new ArgumentNullException(nameof(release));
            }
            try
            {
                // Form the request body
                var json = release.ToJson();

                if (string.IsNullOrWhiteSpace(json))
                {
                    Console.WriteLine(Resources.FailedToFormatReleaseJson);
                    return(false);
                }

                Console.WriteLine(Resources.SendingPostRequestToCreateRelease, release.name);

                var createReleaseUrl = string.Format(Resources.CreateReleaseApiPostURL, repoOwner, repoName);

                var client  = new RestClient(createReleaseUrl);
                var request = GitHubRequestFactory.PrepareGitHubRequest(Method.POST, userAccessToken);

                request.AddHeader(Resources.AcceptHeaderName, Resources.GitHubApiV3Accept);
                request.AddParameter(Resources.UndefinedParameterName, json, ParameterType.RequestBody);

                var response = client.Execute(request);

                if (response == null || response.StatusCode != HttpStatusCode.Created)
                {
                    Console.WriteLine("ERROR: Failed to post release.");
                    return(false);
                }

                var newRelease = response.Content.FromJson();

                // get the ID of the new release
                Console.WriteLine($"Posted release {newRelease.id} to {newRelease.target_commitish}.");

                Console.WriteLine(Resources.ProcessingReleaseAssets);

                request = GitHubRequestFactory.PrepareGitHubRequest(Method.POST, userAccessToken);

                // process the assets
                // If the user has set shouldNotZip to false, zip up the release assets.
                if (shouldNotZip)
                {
                    AssetUploader.UploadAssetsIndividually(releaseAssetDir, newRelease, userAccessToken);
                }
                else
                {
                    AssetUploader.UploadAssetsZipped(releaseAssetDir, newRelease, userAccessToken, assetsZipName);
                }

                Console.WriteLine(Resources.ReleasePostedToGitHub, newRelease.name);
                return(true);
            }
            catch
            {
                Console.WriteLine(Resources.FailedToPackageReleaseForPosting);
                return(false);
            }
        }
        /// <summary>
        /// Validates the release specified.
        /// </summary>
        /// <param name="release">Reference to an instance of <see cref="github_release_poster.NewRelease"/>
        /// that contains the metadata for the release.</param>
        /// <param name="repoName">String containing the name of the repository to which this release is to be published.</param>
        /// <param name="repoOwner">String containing the GitHub username of the owner of the repository.</param>
        /// <param name="userAcessToken">String containing the user access token of a GitHub user with push access to the repository.</param>
        /// <param name="releaseAssetDir">String containing the path to the directory where release assets are stored.</param>
        /// <returns>True if the release is valid; false otherwise.  Terminates the program with a nonzero exit code if the
        /// release does not pass validation.</returns>
        /// <exception cref="T:System.ArgumentNullException">Thrown if the <see cref="release"/> argument is a null
        /// reference of if any of the string arguments are blank.</exception>
        public static bool IsReleaseValid(NewRelease release,
                                          string repoName, string repoOwner, string userAcessToken,
                                          string releaseAssetDir)
        {
            Console.WriteLine(Resources.ValidatingReleaseMetadata);

            // reject a null reference to the release object itself
            if (release == null)
            {
                throw new ArgumentNullException(nameof(release));
            }

            if (string.IsNullOrWhiteSpace(release.tag_name))
            {
                throw new ArgumentNullException(nameof(release.tag_name));
            }

            if (string.IsNullOrWhiteSpace(release.name))
            {
                throw new ArgumentNullException(nameof(release.name));
            }

            if (string.IsNullOrWhiteSpace(release.target_commitish))
            {
                throw new ArgumentNullException(nameof(release.target_commitish));
            }

            if (string.IsNullOrWhiteSpace(repoName))
            {
                throw new ArgumentNullException(nameof(repoName));
            }

            if (string.IsNullOrWhiteSpace(repoOwner))
            {
                throw new ArgumentNullException(nameof(repoOwner));
            }

            if (string.IsNullOrWhiteSpace(userAcessToken))
            {
                throw new ArgumentNullException(nameof(userAcessToken));
            }

            if (string.IsNullOrWhiteSpace(releaseAssetDir))
            {
                throw new ArgumentNullException(nameof(releaseAssetDir));
            }

            /* If the release asset directory does not yet exist, then the build must have failed! LOL.
             * However, let's tell the user just to be sure. */
            if (!Directory.Exists(releaseAssetDir))
            {
                Console.WriteLine(Resources.ReleaseAssetDirNotFound, releaseAssetDir);
                Environment.Exit(exitCode: Resources.ERROR_RELEASE_ASSET_DIR_NOT_EXISTS);
            }

            /* The GitHub release API allows an unlimited number of assets to be posted to a release.  However,
             * it limits the size of any single file to be no more than 2 GB in size.  Scan the release asset dir.
             * If it, or any of its subdirectories (that the current user can access, anyway) contains a file that is
             * 2 GB or more in size, stop this program after displaying an error.*/
            if (FileSearcher.GetAllFilesInFolder(releaseAssetDir)
                .Where(fsi => (fsi.Attributes & FileAttributes.Directory) != FileAttributes.Directory)
                .Any(fsi => Convert.ToUInt64(new FileInfo(fsi.FullName).Length) >= Resources.TwoGigaBytes))
            {
                Console.WriteLine(Resources.ReleaseAssetDirContainsTooBigFile);
                Environment.Exit(Resources.ERROR_RELEASE_ASSET_IS_TOO_BIG);
            }

            Console.WriteLine(Resources.ReleaseAssetsAndMetadataValid);
            return(true);
        }