Public/Add-PDKGithubRelease.ps1

Function Add-PDKGithubRelease {
    <#
    .SYNOPSIS
        To create a github Release.
    .PARAMETER Name
        The name of the tag.
    .PARAMETER Message
        Text describing the contents of the tag.
    .PARAMETER Branch
        Specifies the commitish value that determines where the Git tag is created from.
    .PARAMETER Draft
        To create a draft (unpublished) release.
    .PARAMETER Prerelease
        To identify the release as a prerelease.
    .PARAMETER Owner
        Specifies the owner of the github Repository.
    .PARAMETER Repository
        Specifies the target repository name.
    .PARAMETER PassThru
        Return objects from commands and allow more manipulation.
    .EXAMPLE
        PS C:\> Add-PDKGithubRelease -Name "V6.9.0" -Owner 'dark-vador' -Repository 'Sith'
    .EXAMPLE
        PS C:\> Add-PDKGithubRelease -Name "V6.9.0" -Message 'Destroy' -Branch 'dev' -Owner 'dark-vador' -Repository 'Sith'
    .NOTES
        File Name : Add-PDKGithubRelease.ps1
        Author : Thomas ILLIET (contact@thomas-illiet.fr)
        Reference : https://developer.github.com/v3/repos/releases/
    #>

    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True, Position=0)]
        [String]$Name,
        [Parameter(Mandatory=$False, Position=1)]
        [String]$Message,
        [Parameter(Mandatory=$False, Position=2)]
        [String]$Branch='master',
        [Parameter(Mandatory=$False, Position=3)]
        [Bool]$Draft=$False,
        [Parameter(Mandatory=$False, Position=4)]
        [Bool]$Prerelease=$False,
        [Parameter(Mandatory=$True, Position=5)]
        [String]$Owner,
        [Parameter(Mandatory=$True, Position=6)]
        [String]$Repository,
        [parameter(Mandatory=$False, Position=7)]
        [Switch]$PassThru
    )

    # To use this function you need to have valid GithubAuthorization
    if(([string]::IsNullOrEmpty($Script:GithubAuthorization)) -eq $False) {

        # +++++++++++++++++++++++++++++
        # + Create Request
        $Data = @{
            tag_name         = $Name
            target_commitish = $Branch
            name             = $Name
            body             = $Message
            draft            = $Draft
            prerelease       = $Prerelease
        }
        $Params = @{
            Method      = 'Post'
            Headers     = @{Authorization=$Script:GithubAuthorization}
            Uri         = "https://api.github.com/repos/$Owner/$Repository/releases"
            Body        = ($Data | ConvertTo-Json -Compress)
            ErrorAction = "Stop"
        }
        Write-Debug "Uri - $($Params.Uri)"
        Write-Debug "Request - $($Params.Body)"

        # +++++++++++++++++++++++++++++
        # + Send Request
        try {
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            $CreateReleaseObject = Invoke-RestMethod @Params
            if ($PassThru) { return $CreateReleaseObject }
        } Catch {
            $ErrorDetails = ConvertFrom-Json -InputObject $_
            switch ($ErrorDetails.errors.code) {
                "already_exists" { Write-Error "Release branch already exists" }
                Default          { Write-Error "Unknown Error : $_" }
            }
        }
    } else {
        Write-Error "Unable to find Github Credential, please define your access with the following command 'Set-PDKGithubCredential'"
    }
}