Skip to content

Commit 7ea773c

Browse files
smaglio81HowardWolosky
authored andcommitted
Adding Get-GitHubRelease (#125)
Similar to #110, I needed some functionality around releases. I didn't so much need functionality to publish a release, but I needed to retrieve the list of releases that we're published. So, this is just a function for Get-GitHubRelease.
1 parent d6c3b53 commit 7ea773c

File tree

3 files changed

+299
-0
lines changed

3 files changed

+299
-0
lines changed

GitHubReleases.ps1

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
function Get-GitHubRelease
5+
{
6+
<#
7+
.SYNOPSIS
8+
Retrieves information about a release or list of releases on GitHub.
9+
10+
.DESCRIPTION
11+
Retrieves information about a release or list of releases on GitHub.
12+
13+
The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub
14+
15+
.PARAMETER OwnerName
16+
Owner of the repository.
17+
If not supplied here, the DefaultOwnerName configuration property value will be used.
18+
19+
.PARAMETER RepositoryName
20+
Name of the repository.
21+
If not supplied here, the DefaultRepositoryName configuration property value will be used.
22+
23+
.PARAMETER Uri
24+
Uri for the repository.
25+
The OwnerName and RepositoryName will be extracted from here instead of needing to provide
26+
them individually.
27+
28+
.PARAMETER ReleaseId
29+
Specific releaseId of a release.
30+
This is an optional parameter which can limit the results to a single release.
31+
32+
.PARAMETER Latest
33+
Retrieve only the latest release.
34+
This is an optional parameter which can limit the results to a single release.
35+
36+
.PARAMETER Tag
37+
Retrieves a list of releases with the associated tag.
38+
This is an optional parameter which can filter the list of releases.
39+
40+
.PARAMETER AccessToken
41+
If provided, this will be used as the AccessToken for authentication with the
42+
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
43+
44+
.PARAMETER NoStatus
45+
If this switch is specified, long-running commands will run on the main thread
46+
with no commandline status update. When not specified, those commands run in
47+
the background, enabling the command prompt to provide status information.
48+
If not supplied here, the DefaultNoStatus configuration property value will be used.
49+
50+
.EXAMPLE
51+
Get-GitHubRelease
52+
53+
Gets all releases for the default configured owner/repository.
54+
55+
.EXAMPLE
56+
Get-GitHubRelease -ReleaseId 12345
57+
58+
Get a specific release for the default configured owner/repository
59+
60+
.EXAMPLE
61+
Get-GitHubRelease -OwnerName dotnet -RepositoryName core
62+
63+
Gets all releases from the dotnet\core repository.
64+
65+
.EXAMPLE
66+
Get-GitHubRelease -Uri https://github.com/microsoft/PowerShellForGitHub
67+
68+
Gets all releases from the microsoft/PowerShellForGitHub repository.
69+
70+
.EXAMPLE
71+
Get-GitHubRelease -OwnerName dotnet -RepositoryName core -Latest
72+
73+
Gets the latest release from the dotnet\core repository.
74+
75+
.EXAMPLE
76+
Get-GitHubRelease -Uri https://github.com/microsoft/PowerShellForGitHub -Tag 0.8.0
77+
78+
Gets the release tagged with 0.8.0 from the microsoft/PowerShellForGitHub repository.
79+
80+
.NOTES
81+
Information about published releases are available to everyone. Only users with push
82+
access will receive listings for draft releases.
83+
#>
84+
[CmdletBinding(
85+
SupportsShouldProcess,
86+
DefaultParametersetName='Elements')]
87+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification="Methods called within here make use of PSShouldProcess, and the switch is passed on to them inherently.")]
88+
param(
89+
[Parameter(
90+
ParameterSetName='Elements')]
91+
[Parameter(
92+
ParameterSetName="Elements-ReleaseId")]
93+
[Parameter(
94+
ParameterSetName="Elements-Latest")]
95+
[Parameter(
96+
ParameterSetName="Elements-Tag")]
97+
[string] $OwnerName,
98+
99+
[Parameter(
100+
ParameterSetName='Elements')]
101+
[Parameter(
102+
ParameterSetName="Elements-ReleaseId")]
103+
[Parameter(
104+
ParameterSetName="Elements-Latest")]
105+
[Parameter(
106+
ParameterSetName="Elements-Tag")]
107+
[string] $RepositoryName,
108+
109+
[Parameter(
110+
Mandatory,
111+
ParameterSetName='Uri')]
112+
[Parameter(
113+
Mandatory,
114+
ParameterSetName="Uri-ReleaseId")]
115+
[Parameter(
116+
Mandatory,
117+
ParameterSetName="Uri-Latest")]
118+
[Parameter(
119+
Mandatory,
120+
ParameterSetName="Uri-Tag")]
121+
[string] $Uri,
122+
123+
[Parameter(
124+
Mandatory,
125+
ParameterSetName="Elements-ReleaseId")]
126+
[Parameter(
127+
Mandatory,
128+
ParameterSetName="Uri-ReleaseId")]
129+
[string] $ReleaseId,
130+
131+
[Parameter(
132+
Mandatory,
133+
ParameterSetName="Elements-Latest")]
134+
[Parameter(
135+
Mandatory,
136+
ParameterSetName="Uri-Latest")]
137+
[switch] $Latest,
138+
139+
[Parameter(
140+
Mandatory,
141+
ParameterSetName="Elements-Tag")]
142+
[Parameter(
143+
Mandatory,
144+
ParameterSetName="Uri-Tag")]
145+
[string] $Tag,
146+
147+
[string] $AccessToken,
148+
149+
[switch] $NoStatus
150+
)
151+
152+
Write-InvocationLog -Invocation $MyInvocation
153+
154+
$elements = Resolve-RepositoryElements -BoundParameters $PSBoundParameters -DisableValidation
155+
$OwnerName = $elements.ownerName
156+
$RepositoryName = $elements.repositoryName
157+
158+
$telemetryProperties = @{}
159+
160+
$telemetryProperties['OwnerName'] = Get-PiiSafeString -PlainText $OwnerName
161+
$telemetryProperties['RepositoryName'] = Get-PiiSafeString -PlainText $RepositoryName
162+
163+
$uriFragment = "repos/$OwnerName/$RepositoryName/releases"
164+
$description = "Getting releases for $OwnerName/$RepositoryName"
165+
166+
if(-not [String]::IsNullOrEmpty($ReleaseId))
167+
{
168+
$telemetryProperties['ProvidedReleaseId'] = $true
169+
170+
$uriFragment += "/$ReleaseId"
171+
$description = "Getting release information for $ReleaseId from $OwnerName/$RepositoryName"
172+
}
173+
174+
if($Latest)
175+
{
176+
$telemetryProperties['GetLatest'] = $true
177+
178+
$uriFragment += "/latest"
179+
$description = "Getting latest release from $OwnerName/$RepositoryName"
180+
}
181+
182+
if(-not [String]::IsNullOrEmpty($Tag))
183+
{
184+
$telemetryProperties['ProvidedTag'] = $true
185+
186+
$uriFragment += "/tags/$Tag"
187+
$description = "Getting releases tagged with $Tag from $OwnerName/$RepositoryName"
188+
}
189+
190+
$params = @{
191+
'UriFragment' = $uriFragment
192+
'Description' = $description
193+
'AccessToken' = $AccessToken
194+
'TelemetryEventName' = $MyInvocation.MyCommand.Name
195+
'TelemetryProperties' = $telemetryProperties
196+
'NoStatus' = (Resolve-ParameterWithDefaultConfigurationValue -BoundParameters $PSBoundParameters -Name NoStatus -ConfigValueName DefaultNoStatus)
197+
}
198+
199+
return Invoke-GHRestMethodMultipleResult @params
200+
}

PowerShellForGitHub.psd1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'GitHubMiscellaneous.ps1',
3333
'GitHubOrganizations.ps1',
3434
'GitHubPullRequests.ps1',
35+
'GitHubReleases.ps1',
3536
'GitHubRepositories.ps1',
3637
'GitHubRepositoryForks.ps1',
3738
'GitHubRepositoryTraffic.ps1',
@@ -67,6 +68,7 @@
6768
'Get-GitHubPullRequest',
6869
'Get-GitHubRateLimit',
6970
'Get-GitHubReferrerTraffic',
71+
'Get-GitHubRelease',
7072
'Get-GitHubRepository',
7173
'Get-GitHubRepositoryBranch',
7274
'Get-GitHubRepositoryCollaborator',

Tests/GitHubReleases.tests.ps1

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
<#
5+
.Synopsis
6+
Tests for GitHubReleases.ps1 module
7+
#>
8+
9+
# This is common test code setup logic for all Pester test files
10+
$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent
11+
. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1')
12+
13+
try
14+
{
15+
if ($accessTokenConfigured)
16+
{
17+
Describe 'Getting releases from repository' {
18+
$ownerName = "dotnet"
19+
$repositoryName = "core"
20+
$releases = Get-GitHubRelease -OwnerName $ownerName -RepositoryName $repositoryName
21+
22+
Context 'When getting all releases' {
23+
It 'Should return multiple releases' {
24+
$releases.Count | Should BeGreaterThan 1
25+
}
26+
}
27+
28+
Context 'When getting the latest releases' {
29+
$latest = Get-GitHubRelease -OwnerName $ownerName -RepositoryName $repositoryName -Latest
30+
31+
It 'Should return one value' {
32+
@($latest).Count | Should Be 1
33+
}
34+
35+
It 'Should return the first release from the full releases list' {
36+
$releases[0].url | Should Be $releases[0].url
37+
$releases[0].name | Should Be $releases[0].name
38+
}
39+
}
40+
41+
Context 'When getting a specific release' {
42+
$specificIndex = 5
43+
$specific = Get-GitHubRelease -OwnerName $ownerName -RepositoryName $repositoryName -ReleaseId $releases[$specificIndex].id
44+
45+
It 'Should return one value' {
46+
@($specific).Count | Should Be 1
47+
}
48+
49+
It 'Should return the correct release' {
50+
$specific.name | Should Be $releases[$specificIndex].name
51+
}
52+
}
53+
54+
Context 'When getting a tagged release' {
55+
$taggedIndex = 8
56+
$tagged = Get-GitHubRelease -OwnerName $ownerName -RepositoryName $repositoryName -Tag $releases[$taggedIndex].tag_name
57+
58+
It 'Should return one value' {
59+
@($tagged).Count | Should Be 1
60+
}
61+
62+
It 'Should return the correct release' {
63+
$tagged.name | Should Be $releases[$taggedIndex].name
64+
}
65+
}
66+
}
67+
68+
Describe 'Getting releases from default owner/repository' {
69+
$originalOwnerName = Get-GitHubConfiguration -Name DefaultOwnerName
70+
$originalRepositoryName = Get-GitHubConfiguration -Name DefaultRepositoryName
71+
72+
try {
73+
Set-GitHubConfiguration -DefaultOwnerName "dotnet"
74+
Set-GitHubConfiguration -DefaultRepositoryName "core"
75+
$releases = Get-GitHubRelease
76+
77+
Context 'When getting all releases' {
78+
It 'Should return multiple releases' {
79+
$releases.Count | Should BeGreaterThan 1
80+
}
81+
}
82+
} finally {
83+
Set-GitHubConfiguration -DefaultOwnerName $originalOwnerName
84+
Set-GitHubConfiguration -DefaultRepositoryName $originalRepositoryName
85+
}
86+
}
87+
}
88+
}
89+
finally
90+
{
91+
if (Test-Path -Path $script:originalConfigFile -PathType Leaf)
92+
{
93+
# Restore the user's configuration to its pre-test state
94+
Restore-GitHubConfiguration -Path $script:originalConfigFile
95+
$script:originalConfigFile = $null
96+
}
97+
}

0 commit comments

Comments
 (0)