|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +function Get-GitHubReference |
| 5 | +{ |
| 6 | +<# |
| 7 | + .SYNOPSIS |
| 8 | + Retrieve a reference of a given GitHub repository. |
| 9 | +
|
| 10 | + .DESCRIPTION |
| 11 | + Retrieve a reference of a given GitHub repository. |
| 12 | + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub |
| 13 | +
|
| 14 | + .PARAMETER OwnerName |
| 15 | + Owner of the repository. |
| 16 | + If not supplied here, the DefaultOwnerName configuration property value will be used. |
| 17 | +
|
| 18 | + .PARAMETER RepositoryName |
| 19 | + Name of the repository. |
| 20 | + If not supplied here, the DefaultRepositoryName configuration property value will be used. |
| 21 | +
|
| 22 | + .PARAMETER Uri |
| 23 | + Uri for the repository. |
| 24 | + The OwnerName and RepositoryName will be extracted from here instead of needing to provide |
| 25 | + them individually. |
| 26 | +
|
| 27 | + .PARAMETER Reference |
| 28 | + Name of the reference, for example: "heads/<branch name>" for branches and "tags/<tag name>" for tags |
| 29 | +
|
| 30 | + .PARAMETER AccessToken |
| 31 | + If provided, this will be used as the AccessToken for authentication with the |
| 32 | + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. |
| 33 | +
|
| 34 | + .PARAMETER NoStatus |
| 35 | + If this switch is specified, long-running commands will run on the main thread |
| 36 | + with no commandline status update. When not specified, those commands run in |
| 37 | + the background, enabling the command prompt to provide status information. |
| 38 | + If not supplied here, the DefaultNoStatus configuration property value will be used. |
| 39 | +
|
| 40 | + .EXAMPLE |
| 41 | + Get-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference heads/master |
| 42 | +#> |
| 43 | + [CmdletBinding( |
| 44 | + SupportsShouldProcess, |
| 45 | + DefaultParametersetName='Elements')] |
| 46 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] |
| 47 | + param( |
| 48 | + [Parameter(ParameterSetName='Elements')] |
| 49 | + [string] $OwnerName, |
| 50 | + |
| 51 | + [Parameter(ParameterSetName='Elements')] |
| 52 | + [string] $RepositoryName, |
| 53 | + |
| 54 | + [Parameter( |
| 55 | + Mandatory, |
| 56 | + ParameterSetName='Uri')] |
| 57 | + [string] $Uri, |
| 58 | + |
| 59 | + [Parameter(Mandatory)] |
| 60 | + [string] $Reference, |
| 61 | + |
| 62 | + [string] $AccessToken, |
| 63 | + |
| 64 | + [switch] $NoStatus |
| 65 | + ) |
| 66 | + |
| 67 | + Write-InvocationLog -Invocation $MyInvocation |
| 68 | + |
| 69 | + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation |
| 70 | + $OwnerName = $elements.ownerName |
| 71 | + $RepositoryName = $elements.repositoryName |
| 72 | + |
| 73 | + $telemetryProperties = @{ |
| 74 | + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) |
| 75 | + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) |
| 76 | + } |
| 77 | + |
| 78 | + if ($OwnerName -xor $RepositoryName) |
| 79 | + { |
| 80 | + $message = 'You must specify both Owner Name and Repository Name.' |
| 81 | + Write-Log -Message $message -Level Error |
| 82 | + throw $message |
| 83 | + } |
| 84 | + $uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs/$Reference" |
| 85 | + $description = "Getting Reference $Reference for $RepositoryName" |
| 86 | + |
| 87 | + $params = @{ |
| 88 | + 'UriFragment' = $uriFragment |
| 89 | + 'Method' = 'Get' |
| 90 | + 'Description' = $description |
| 91 | + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' |
| 92 | + 'AccessToken' = $AccessToken |
| 93 | + 'TelemetryEventName' = $MyInvocation.MyCommand.Name |
| 94 | + 'TelemetryProperties' = $telemetryProperties |
| 95 | + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) |
| 96 | + } |
| 97 | + |
| 98 | + try |
| 99 | + { |
| 100 | + return Invoke-GHRestMethod @params |
| 101 | + } |
| 102 | + catch |
| 103 | + { |
| 104 | + Write-InteractiveHost "No reference named $Reference exists in repository $RepositoryName" -NoNewline -f Red |
| 105 | + Write-Log -Level Error "No reference named $Reference exists in repository $RepositoryName" |
| 106 | + return $null |
| 107 | + } |
| 108 | + |
| 109 | +} |
| 110 | + |
| 111 | +function New-GitHubReference |
| 112 | +{ |
| 113 | + <# |
| 114 | + .SYNOPSIS |
| 115 | + Create a reference in a given GitHub repository. |
| 116 | +
|
| 117 | + .DESCRIPTION |
| 118 | + Create a reference in a given GitHub repository. |
| 119 | + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub |
| 120 | +
|
| 121 | + .PARAMETER OwnerName |
| 122 | + Owner of the repository. |
| 123 | + If not supplied here, the DefaultOwnerName configuration property value will be used. |
| 124 | +
|
| 125 | + .PARAMETER RepositoryName |
| 126 | + Name of the repository. |
| 127 | + If not supplied here, the DefaultRepositoryName configuration property value will be used. |
| 128 | +
|
| 129 | + .PARAMETER Uri |
| 130 | + Uri for the repository. |
| 131 | + The OwnerName and RepositoryName will be extracted from here instead of needing to provide |
| 132 | + them individually. |
| 133 | +
|
| 134 | + .PARAMETER Reference |
| 135 | + The name of the fully qualified reference to be created (eg: refs/heads/master) |
| 136 | +
|
| 137 | + .PARAMETER Sha |
| 138 | + The SHA1 value for the reference to be created |
| 139 | +
|
| 140 | + .PARAMETER AccessToken |
| 141 | + If provided, this will be used as the AccessToken for authentication with the |
| 142 | + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. |
| 143 | +
|
| 144 | + .PARAMETER NoStatus |
| 145 | + If this switch is specified, long-running commands will run on the main thread |
| 146 | + with no commandline status update. When not specified, those commands run in |
| 147 | + the background, enabling the command prompt to provide status information. |
| 148 | + If not supplied here, the DefaultNoStatus configuration property value will be used. |
| 149 | +
|
| 150 | + .EXAMPLE |
| 151 | + -GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference refs/heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd |
| 152 | + #> |
| 153 | + [CmdletBinding( |
| 154 | + SupportsShouldProcess, |
| 155 | + DefaultParametersetName='Elements')] |
| 156 | + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")] |
| 157 | + param( |
| 158 | + [Parameter(ParameterSetName='Elements')] |
| 159 | + [string] $OwnerName, |
| 160 | + |
| 161 | + [Parameter(ParameterSetName='Elements')] |
| 162 | + [string] $RepositoryName, |
| 163 | + |
| 164 | + [Parameter( |
| 165 | + Mandatory, |
| 166 | + ParameterSetName='Uri')] |
| 167 | + [string] $Uri, |
| 168 | + |
| 169 | + [Parameter(Mandatory)] |
| 170 | + [string] $Reference, |
| 171 | + |
| 172 | + [Parameter(Mandatory)] |
| 173 | + [string] $Sha, |
| 174 | + |
| 175 | + [string] $AccessToken, |
| 176 | + |
| 177 | + [switch] $NoStatus |
| 178 | + ) |
| 179 | + |
| 180 | + Write-InvocationLog -Invocation $MyInvocation |
| 181 | + |
| 182 | + $elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation |
| 183 | + $OwnerName = $elements.ownerName |
| 184 | + $RepositoryName = $elements.repositoryName |
| 185 | + |
| 186 | + $telemetryProperties = @{ |
| 187 | + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) |
| 188 | + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) |
| 189 | + } |
| 190 | + |
| 191 | + if ($OwnerName -xor $RepositoryName) |
| 192 | + { |
| 193 | + $message = 'You must specify both Owner Name and Repository Name.' |
| 194 | + Write-Log -Message $message -Level Error |
| 195 | + throw $message |
| 196 | + } |
| 197 | + |
| 198 | + $uriFragment = "/repos/$OwnerName/$RepositoryName/git/refs" |
| 199 | + $description = "Creating Reference $Reference for $RepositoryName from SHA $Sha" |
| 200 | + |
| 201 | + $hashBody = @{ |
| 202 | + 'ref' = $Reference |
| 203 | + 'sha' = $Sha |
| 204 | + } |
| 205 | + |
| 206 | + $params = @{ |
| 207 | + 'UriFragment' = $uriFragment |
| 208 | + 'Method' = 'Post' |
| 209 | + 'Body' = (ConvertTo-Json -InputObject $hashBody) |
| 210 | + 'Description' = $description |
| 211 | + 'AcceptHeader' = 'application/vnd.github.symmetra-preview+json' |
| 212 | + 'AccessToken' = $AccessToken |
| 213 | + 'TelemetryEventName' = $MyInvocation.MyCommand.Name |
| 214 | + 'TelemetryProperties' = $telemetryProperties |
| 215 | + 'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus) |
| 216 | + } |
| 217 | + |
| 218 | + return Invoke-GHRestMethod @params |
| 219 | +} |
0 commit comments