Skip to content

Commit d994230

Browse files
committed
Add update and delete for references
1 parent 40e874c commit d994230

File tree

3 files changed

+264
-1
lines changed

3 files changed

+264
-1
lines changed

GitHubReferences.ps1

Lines changed: 214 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function New-GitHubReference
139139
them individually.
140140
141141
.PARAMETER Reference
142-
The name of the fully qualified reference to be created (eg: heads/master)
142+
The name of the reference to be created (eg: heads/master or tags/myTag)
143143
144144
.PARAMETER Sha
145145
The SHA1 value for the reference to be created
@@ -227,3 +227,216 @@ function New-GitHubReference
227227

228228
return Invoke-GHRestMethod @params
229229
}
230+
231+
function Update-GitHubReference
232+
{
233+
<#
234+
.SYNOPSIS
235+
Update a reference in a given GitHub repository.
236+
237+
.DESCRIPTION
238+
Update a reference in a given GitHub repository.
239+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
240+
241+
.PARAMETER OwnerName
242+
Owner of the repository.
243+
If not supplied here, the DefaultOwnerName configuration property value will be used.
244+
245+
.PARAMETER RepositoryName
246+
Name of the repository.
247+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
248+
249+
.PARAMETER Uri
250+
Uri for the repository.
251+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
252+
them individually.
253+
254+
.PARAMETER Reference
255+
The name of the reference to be created (eg: heads/master or tags/myTag)
256+
257+
.PARAMETER Sha
258+
The updated SHA1 value to be set for this reference
259+
260+
.PARAMETER Force
261+
Indicates whether to force the update. If not set it will ensure that the update is a fast-forward update.
262+
263+
.PARAMETER AccessToken
264+
If provided, this will be used as the AccessToken for authentication with the
265+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
266+
267+
.PARAMETER NoStatus
268+
If this switch is specified, long-running commands will run on the main thread
269+
with no commandline status update. When not specified, those commands run in
270+
the background, enabling the command prompt to provide status information.
271+
If not supplied here, the DefaultNoStatus configuration property value will be used.
272+
273+
.EXAMPLE
274+
Update-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference heads/master -Sha aa218f56b14c9653891f9e74264a383fa43fefbd
275+
#>
276+
[CmdletBinding(
277+
SupportsShouldProcess,
278+
DefaultParametersetName='Elements')]
279+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
280+
param(
281+
[Parameter(ParameterSetName='Elements')]
282+
[string] $OwnerName,
283+
284+
[Parameter(ParameterSetName='Elements')]
285+
[string] $RepositoryName,
286+
287+
[Parameter(
288+
Mandatory,
289+
ParameterSetName='Uri')]
290+
[string] $Uri,
291+
292+
[Parameter(Mandatory)]
293+
[string] $Reference,
294+
295+
[Parameter(Mandatory)]
296+
[string] $Sha,
297+
298+
[switch] $Force,
299+
300+
[string] $AccessToken,
301+
302+
[switch] $NoStatus
303+
)
304+
305+
Write-InvocationLog -Invocation $MyInvocation
306+
307+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation
308+
$OwnerName = $elements.ownerName
309+
$RepositoryName = $elements.repositoryName
310+
311+
$telemetryProperties = @{
312+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
313+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
314+
}
315+
316+
if ($OwnerName -xor $RepositoryName)
317+
{
318+
$message = 'You must specify both Owner Name and Repository Name.'
319+
Write-Log -Message $message -Level Error
320+
throw $message
321+
}
322+
323+
$uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference"
324+
$description = "Updating SHA for Reference $Reference in $RepositoryName to $Sha"
325+
326+
$hashBody = @{
327+
'force' = $Force.IsPresent
328+
'sha' = $Sha
329+
}
330+
331+
$params = @{
332+
'UriFragment' = $uriFragment
333+
'Method' = 'Patch'
334+
'Body' = (ConvertTo-Json -InputObject $hashBody)
335+
'Description' = $description
336+
'AccessToken' = $AccessToken
337+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
338+
'TelemetryProperties' = $telemetryProperties
339+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
340+
}
341+
342+
return Invoke-GHRestMethod @params
343+
}
344+
345+
function Remove-GitHubReference
346+
{
347+
<#
348+
.SYNOPSIS
349+
Delete a reference in a given GitHub repository.
350+
351+
.DESCRIPTION
352+
Delete a reference in a given GitHub repository.
353+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
354+
355+
.PARAMETER OwnerName
356+
Owner of the repository.
357+
If not supplied here, the DefaultOwnerName configuration property value will be used.
358+
359+
.PARAMETER RepositoryName
360+
Name of the repository.
361+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
362+
363+
.PARAMETER Uri
364+
Uri for the repository.
365+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
366+
them individually.
367+
368+
.PARAMETER Reference
369+
The name of the reference to be deleted (eg: heads/master)
370+
371+
.PARAMETER AccessToken
372+
If provided, this will be used as the AccessToken for authentication with the
373+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
374+
375+
.PARAMETER NoStatus
376+
If this switch is specified, long-running commands will run on the main thread
377+
with no commandline status update. When not specified, those commands run in
378+
the background, enabling the command prompt to provide status information.
379+
If not supplied here, the DefaultNoStatus configuration property value will be used.
380+
381+
.EXAMPLE
382+
Remove-GitHubReference -OwnerName Powershell -RepositoryName PowerShellForGitHub -Reference heads/master
383+
#>
384+
[CmdletBinding(
385+
SupportsShouldProcess,
386+
DefaultParametersetName='Elements')]
387+
[Alias('Delete-GitHubReference')]
388+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
389+
param(
390+
[Parameter(ParameterSetName='Elements')]
391+
[string] $OwnerName,
392+
393+
[Parameter(ParameterSetName='Elements')]
394+
[string] $RepositoryName,
395+
396+
[Parameter(
397+
Mandatory,
398+
ParameterSetName='Uri')]
399+
[string] $Uri,
400+
401+
[Parameter(Mandatory)]
402+
[string] $Reference,
403+
404+
[string] $AccessToken,
405+
406+
[switch] $NoStatus
407+
)
408+
409+
Write-InvocationLog -Invocation $MyInvocation
410+
411+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation
412+
$OwnerName = $elements.ownerName
413+
$RepositoryName = $elements.repositoryName
414+
415+
$telemetryProperties = @{
416+
'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName)
417+
'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName)
418+
}
419+
420+
if ($OwnerName -xor $RepositoryName)
421+
{
422+
$message = 'You must specify both Owner Name and Repository Name.'
423+
Write-Log -Message $message -Level Error
424+
throw $message
425+
}
426+
427+
$uriFragment = "repos/$OwnerName/$RepositoryName/git/refs/$Reference"
428+
$description = "Deleting Reference $Reference from repository $RepositoryName"
429+
430+
$params = @{
431+
'UriFragment' = $uriFragment
432+
'Method' = 'Delete'
433+
'Body' = (ConvertTo-Json -InputObject $hashBody)
434+
'Description' = $description
435+
'AccessToken' = $AccessToken
436+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
437+
'TelemetryProperties' = $telemetryProperties
438+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -Name NoStatus -ConfigValueName DefaultNoStatus)
439+
}
440+
441+
return Invoke-GHRestMethod @params
442+
}

PowerShellForGitHub.psd1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
'Remove-GitHubIssueLabel',
105105
'Remove-GitHubLabel',
106106
'Remove-GitHubMilestone',
107+
'Remove-GitHubReference',
107108
'Remove-GitHubRepository',
108109
'Rename-GitHubRepository',
109110
'Reset-GitHubConfiguration',
@@ -123,13 +124,15 @@
123124
'Update-GitHubCurrentUser',
124125
'Update-GitHubIssue',
125126
'Update-GitHubLabel',
127+
'Update-GithubReference',
126128
'Update-GitHubRepository'
127129
)
128130

129131
AliasesToExport = @(
130132
'Delete-GitHubComment',
131133
'Delete-GitHubLabel',
132134
'Delete-GitHubMilestone',
135+
'Delete-GitHubReference',
133136
'Delete-GitHubRepository',
134137
'Get-GitHubBranch',
135138
'Transfer-GitHubRepositoryOwnership'

Tests/GitHubReferences.Tests.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,53 @@ try
123123

124124
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
125125
}
126+
127+
Describe 'Delete a reference(branch) from repository' {
128+
$repositoryName = [Guid]::NewGuid()
129+
New-GitHubRepository -RepositoryName $repositoryName -AutoInit
130+
131+
Context 'On deleting a newly created reference in a new repository' {
132+
$refname = "heads/myRef"
133+
$existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master"
134+
$sha = $existingref.object.sha
135+
New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha
136+
Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname
137+
138+
It 'Should not return any details when the reference is queried' {
139+
Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname |
140+
Should be $null
141+
}
142+
143+
It 'Should throw an exception when the same reference is deleted again' {
144+
{ Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname } |
145+
Should Throw
146+
}
147+
}
148+
149+
Context 'On deleting an invalid reference from a new repository' {
150+
It 'Should throw an exception' {
151+
{ Remove-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/$([Guid]::NewGuid())" } |
152+
Should Throw
153+
}
154+
}
155+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
156+
}
157+
158+
Describe 'Update a reference(branch) in repository' {
159+
$repositoryName = [Guid]::NewGuid()
160+
New-GitHubRepository -RepositoryName $repositoryName -AutoInit
161+
$refname = "heads/myRef"
162+
$existingref = Get-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference "heads/master"
163+
$sha = $existingref.object.sha
164+
New-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha $sha
165+
Context 'On updating a newly created reference to a different SHA' {
166+
It 'Should throw an exception if the SHA is invalid' {
167+
{ Update-GitHubReference -OwnerName $ownerName -RepositoryName $repositoryName -Reference $refname -Sha "1234" } |
168+
Should Throw
169+
}
170+
}
171+
$null = Remove-GitHubRepository -OwnerName $ownerName -RepositoryName $repositoryName
172+
}
126173
}
127174
}
128175
catch

0 commit comments

Comments
 (0)