Functions/Public/New-GitHubRelease.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function New-GitHubRelease {
<#
    .SYNOPSIS
    Create a new release for a repository
 
    .DESCRIPTION
    Create a new release for a repository
 
    .PARAMETER Repository
    The name of the repository
 
    .PARAMETER Name
    The name of the release
 
    .PARAMETER Description
    Text describing the contents of the tag.
 
    .PARAMETER Target
    Specifies the commitish value that determines where the Git tag is created from.
    Can be any branch or commit SHA. Unused if the Git tag already exists.
    Default: the repository's default branch (usually master).
 
    .PARAMETER Tag
    The name of the tag
 
    .PARAMETER Asset
    An array of Assets to upload with the release.
 
    An asset should be in the form of a hashtable and must contain the following keys:
 
    Path - The Path of the asset that will be uploaded
    Content-Type - The Content-Type type of the asset that will be uploaded
 
    For example, the hashtable would be formed like this:
 
    @{
 
        "Path" = "C:\Assets\Asset-v1.0.zip"
        "Content-Type" = "application/zip"
 
    }
 
    For a list of supplorted content types see the following url -> https://www.iana.org/assignments/media-types/media-types.xhtml
 
    .PARAMETER Draft
    Make the release a draft
 
    .PARAMETER Prerelease
    Make the release a prerelease
 
    .INPUTS
    System.String
    Switch
    Hashtable
 
    .OUTPUTS
    System.Management.Automation.PSObject
 
    .EXAMPLE
    New-GitHubRelease -Repository MyRepository -Name TestRelease -Description "Test v1.0 release" -Target master -Tag v1.0
 
    .EXAMPLE
    New-GitHubRelease -Repository MyRepository -Name TestRelease -Description "Test v1.0 release" -Target master -Tag v1.0 -Draft
 
    .EXAMPLE
    New-GitHubRelease -Repository MyRepository -Name TestRelease -Description "Test v1.0 release" -Target master -Tag v1.0 -Prerelease
 
    .EXAMPLE
    $Asset = @{
        "Path" = ".\Release\TestRelease-0.1.0.zip"
        "Content-Type" = "application/zip"
    }
    New-GitHubRelease -Repository MyRepository -Name TestRelease -Description "Test v1.0 release" -Target master -Tag v1.0 -Asset $Asset
 
 
#>

[CmdletBinding(SupportsShouldProcess, ConfirmImpact="High")][OutputType('System.Management.Automation.PSObject')]

    Param (

        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [String]$Repository,

        [Parameter(Mandatory=$true, Position=1)]
        [ValidateNotNullOrEmpty()]
        [String]$Name,

        [Parameter(Mandatory=$false, Position=2)]
        [ValidateNotNullOrEmpty()]
        [String]$Description,

        [Parameter(Mandatory=$false, Position=3)]
        [ValidateNotNullOrEmpty()]
        [String]$Target,

        [Parameter(Mandatory=$true, Position=4)]
        [ValidateNotNullOrEmpty()]
        [String]$Tag,

        [Parameter(Mandatory=$false, Position=5)]
        [ValidateNotNullOrEmpty()]
        [Hashtable[]]$Asset,

        [Parameter(Mandatory=$false, Position=6)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.SwitchParameter]$Draft,

        [Parameter(Mandatory=$false, Position=7)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.SwitchParameter]$Prerelease

    )

    try {

        # --- Grab the sessionstate variable & test throw if it is null
        $SessionInfo = Get-GitHubSessionInformation -Verbose:$VerbosePreference

        $Body = [PSCustomObject] @{

            tag_name = $Tag
            target_commitish = $Target
            name = $Name
            body = $Description
            draft = $Draft.IsPresent
            prerelease = $Prerelease.IsPresent

        }

        $URI = "/repos/$($SessionInfo.Username)/$($Repository)/releases"

        if ($PSCmdlet.ShouldProcess($Repository)) {

            # --- Create the release
            $Response = Invoke-GitHubRestMethod -Method POST -URI $URI -Body ($Body | ConvertTo-JSON) -Verbose:$VerbosePreference

            # --- If the Assets parameter is passed upload each file
            if ($PSBoundParameters.ContainsKey("Asset")) {

                foreach ($Item in $Asset) {

                    if (!($Item.ContainsKey("Path") -and $Item.ContainsKey("Content-Type"))) {

                        throw "The Assets parameter is not correct. See function help for more information."

                    }

                    # --- Retrieve parameters from hashtable
                    $Path = $Item.Get_Item("Path")
                    $ContentType = $Item.Get_Item("Content-Type")

                    # --- Execute Request
                    $ResolvedAsset = Get-Item -LiteralPath $Path
                    $UploadURI = "/repos/$($SessionInfo.Username)/$($Repository)/releases/$($Response.id)/assets?name=$($ResolvedAsset.Name)"
                    Write-Verbose -Message "Uploading asset $($ResolvedAsset.FullName)"
                    Invoke-GitHubRestMethod -Method POST -URI $UploadURI -InFile $ResolvedAsset.FullName -ContentType $ContentType -Verbose:$VerbosePreference | Out-Null

                }

            }

        }

    }
    catch [Exception]{

        throw $_.Exception

    }

}