public static int PublishRelease(this ICakeContext context, string tag, string releaseTitle, FilePath releaseNotesFilePath,
                                         bool draftRelease, bool preRelease, OctoDeploySettings octoDeploySettings)
        {
            var releaseNotes = File.ReadAllText(releaseNotesFilePath.FullPath);

            return(context.PublishRelease(tag, releaseTitle, releaseNotes, draftRelease, preRelease, octoDeploySettings));
        }
        public static void PublishReleaseWithArtifacts(this ICakeContext context, string tag, string releaseTitle,
                                                       FilePath releaseNotesFilePath, bool draftRelease, bool preRelease, FilePath[] artifactPaths, string[] artifactNames,
                                                       string[] artifactMimeTypes, OctoDeploySettings octoDeploySettings)
        {
            var releaseNotes = File.ReadAllText(releaseNotesFilePath.FullPath);

            context.PublishReleaseWithArtifacts(tag, releaseTitle, releaseNotes, draftRelease, preRelease, artifactPaths, artifactNames, artifactMimeTypes, octoDeploySettings);
        }
        public static void PublishReleaseWithArtifact(this ICakeContext context, string tag, string releaseTitle,
                                                      string releaseNotes, bool draftRelease, bool preRelease, FilePath artifactPath, string artifactName,
                                                      string artifactMimeType, OctoDeploySettings octoDeploySettings)
        {
            var id = context.PublishRelease(tag, releaseTitle, releaseNotes, draftRelease, preRelease,
                                            octoDeploySettings);

            context.UploadArtifact(id, artifactPath, artifactName, artifactMimeType, octoDeploySettings);
        }
        public static void PublishReleaseWithArtifacts(this ICakeContext context, string tag, string releaseTitle,
                                                       string releaseNotes, bool draftRelease, bool preRelease, FilePath[] artifactPaths, string[] artifactNames,
                                                       string[] artifactMimeTypes, OctoDeploySettings octoDeploySettings)
        {
            if (artifactPaths.Length != artifactMimeTypes.Length || artifactPaths.Length != artifactNames.Length)
            {
                throw new CakeException("ArtifactPaths, ArtifactNames and ArtifactMimeTypes all need to be the same length");
            }

            var id = context.PublishRelease(tag, releaseTitle, releaseNotes, draftRelease, preRelease,
                                            octoDeploySettings);

            for (var r = 0; r < artifactPaths.Length; r++)
            {
                context.UploadArtifact(id, artifactPaths[r], artifactNames[r], artifactMimeTypes[r], octoDeploySettings);
            }
        }
        public static void UploadArtifact(this ICakeContext context, int releaseId, FilePath artifactPath, string artifactName,
                                          string artifactMimeType, OctoDeploySettings octoDeploySettings)
        {
            var client = new GitHubClient(new ProductHeaderValue("Cake.OctoDeploy"), new Uri(GitHubApiBaseUrl))
            {
                Credentials = new Credentials(octoDeploySettings.AccessToken)
            };

            context.Log.Information("Uploading Artifacts...");
            using (var archiveContents = File.OpenRead(artifactPath.FullPath))
            {
                var assetUpload = new ReleaseAssetUpload
                {
                    FileName    = artifactName,
                    ContentType = artifactMimeType,
                    RawData     = archiveContents
                };

                try
                {
                    var release = client.Repository
                                  .Release
                                  .Get(octoDeploySettings.Owner, octoDeploySettings.Repository, releaseId)
                                  .Result;

                    var asset = client.Repository
                                .Release
                                .UploadAsset(release, assetUpload)
                                .Result;

                    context.Log.Information($"Uploaded artifact {artifactPath.FullPath} to GitHub. Id {asset.Id}");
                }
                catch (Exception exception)
                {
                    var innerException = exception.InnerException;
                    throw new CakeException(string.IsNullOrEmpty(innerException?.Message) ? "Unknown error occured while creating release" : innerException.Message);
                }
            }
        }
        public static int PublishRelease(this ICakeContext context, string tag, string releaseTitle, string releaseNotes, bool draftRelease, bool preRelease, OctoDeploySettings octoDeploySettings)
        {
            var client = new GitHubClient(new ProductHeaderValue("Cake.OctoDeploy"), new Uri(GitHubApiBaseUrl))
            {
                Credentials = new Credentials(octoDeploySettings.AccessToken)
            };

            var newRelease = new NewRelease(tag)
            {
                Name       = releaseTitle,
                Body       = releaseNotes,
                Draft      = draftRelease,
                Prerelease = preRelease
            };

            try
            {
                var result = client.Repository
                             .Release
                             .Create(octoDeploySettings.Owner, octoDeploySettings.Repository, newRelease)
                             .Result;

                context.Log.Information($"Created GitHub release with Id {result.Id}");

                return(result.Id);
            }
            catch (AggregateException exception)
            {
                var innerException = exception.InnerException;
                throw new CakeException(string.IsNullOrEmpty(innerException?.Message) ? "Unknown error occured while creating release" : innerException.Message);
            }
        }