forked from Azure/ALZ-PowerShell-Module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ALZGithubRelease.ps1
More file actions
147 lines (116 loc) · 6.96 KB
/
Get-ALZGithubRelease.ps1
File metadata and controls
147 lines (116 loc) · 6.96 KB
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
####################################
# Get-ALZGithubRelease.ps1 #
####################################
# Version: 0.1.0
# Based on Invoke-GitHubReleaseFetcher by Jack Tracey:
# Source: https://github.com/jtracey93/PublicScripts/blob/master/GitHub/PowerShell/Invoke-GitHubReleaseFetcher.ps1
<#
.SYNOPSIS
Checks for the releases of a GitHub repository and downloads the latest release or all releases and pulls it into a specified directory, one for each version.
.DESCRIPTION
Checks for the releases of a GitHub repository and downloads the latest release or all releases and pulls it into a specified directory, one for each version.
.EXAMPLE
.NOTES
# Release notes 16/03/2023 - V0.1.0:
- Initial release.
#>
function Get-ALZGithubRelease {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 1, HelpMessage = "Please the provide the full URL of the GitHub repository you wish to check for the latest release.")]
[string]
$githubRepoUrl,
[Parameter(Mandatory = $false, Position = 2, HelpMessage = "The releases to download. Specify 'all' to download all releases or 'latest' to download the latest release. Defaults to the latest release.")]
[array]
$releases = @("latest"),
[Parameter(Mandatory = $false, Position = 3, HelpMessage = "The directory to download the releases to. Defaults to the current directory.")]
[string]
$directoryForReleases = "$PWD/releases",
[Parameter(Mandatory = $false, Position = 4, HelpMessage = "An array of strings contianing the paths to the directories or files that you wish to keep when downloading and extracting from the releases.")]
[array]
$directoryAndFilesToKeep = $null,
[Parameter(Mandatory = $false, Position = 4, HelpMessage = "Whether to just query the API and return the release versions.")]
[switch]
$queryOnly
)
# Split Repo URL into parts
$repoOrgPlusRepo = $githubRepoUrl.Split("/")[-2..-1] -join "/"
Write-Verbose "=====> Checking for releases on GitHub Repo: $repoOrgPlusRepo"
# Get releases on repo
$repoReleasesUrl = "https://api.github.com/repos/$repoOrgPlusRepo/releases"
$perPage = 30
$page = 1
$allRepoReleases = @()
do {
Write-Verbose "=====> Retrieving page $page of releases on GitHub Repo: $repoOrgPlusRepo"
$results = Invoke-RestMethod ($repoReleasesUrl + "?per_page=$perPage&page=$page") -RetryIntervalSec 3 -MaximumRetryCount 100
$allRepoReleases += $results
$page++
}
while ($results.count -eq $perPage)
Write-Verbose "=====> All available releases on GitHub Repo: $repoOrgPlusRepo"
$allRepoReleases | Select-Object name, tag_name, published_at, prerelease, draft, html_url | Format-Table -AutoSize | Out-String | Write-Verbose
# Get latest release on repo
$latestRepoRelease = $allRepoReleases | Where-Object { $_.prerelease -eq $false } | Where-Object { $_.draft -eq $false } | Sort-Object -Descending published_at | Select-Object -First 1
# replace latest with the tag of the latest release
if ($releases -contains "latest") {
$releases += $latestRepoRelease.tag_name
$releases = $releases | Where-Object { $_ -ne "latest" }
}
Write-Verbose "=====> Latest available release on GitHub Repo: $repoOrgPlusRepo"
$latestRepoRelease | Select-Object name, tag_name, published_at, prerelease, draft, html_url | Format-Table -AutoSize | Out-String | Write-Verbose
# Check if directory exists
Write-Verbose "=====> Checking if directory for releases exists: $directoryForReleases"
if (!(Test-Path $directoryForReleases)) {
Write-Verbose "Directory does not exist for releases, will now create: $directoryForReleases"
New-Item -ItemType Directory -Path $directoryForReleases | Out-String | Write-Verbose
}
# if all is specified add all the releases to the array and remove all
if ($releases -contains "all") {
$releases = $allRepoReleases | Select-Object -ExpandProperty tag_name
$releases = $releases | Where-Object { $_ -ne "all" }
}
# Remove all the releases that were not found
foreach ($release in $releases) {
if (($allRepoReleases | Where-Object { $_.tag_name -eq $release } | Measure-Object).Count -eq 0) {
Write-Warning "Release $release was not found on GitHub Repo: $repoOrgPlusRepo"
$releases = $releases | Where-Object { $_ -ne $release }
}
}
$selectedReleases = $allRepoReleases | Where-Object { $releases -contains $_.tag_name }
if($queryOnly) {
return $selectedReleases
}
foreach ($release in $selectedReleases) {
# Check the firectory for this release
$releaseDirectory = "$directoryForReleases/$($release.tag_name)"
Write-Verbose "===> Checking if directory for release version exists: $releaseDirectory"
if (!(Test-Path $releaseDirectory)) {
Write-Verbose "Directory does not exist for release $($release.tag_name), will now create: $releaseDirectory"
New-Item -ItemType Directory -Path $releaseDirectory | Out-String | Write-Verbose
}
Write-Verbose "===> Checking if any content exists inside of $releaseDirectory"
$contentInReleaseDirectory = Get-ChildItem -Path $releaseDirectory -Recurse -ErrorAction SilentlyContinue
if ($null -eq $contentInReleaseDirectory) {
Write-Verbose "===> Pulling and extracting release $($release.tag_name) into $releaseDirectory"
New-Item -ItemType Directory -Path "$releaseDirectory/tmp" | Out-String | Write-Verbose
Invoke-WebRequest -Uri "https://github.com/$repoOrgPlusRepo/archive/refs/tags/$($release.tag_name).zip" -OutFile "$releaseDirectory/tmp/$($release.tag_name).zip" -RetryIntervalSec 3 -MaximumRetryCount 100 | Out-String | Write-Verbose
Expand-Archive -Path "$releaseDirectory/tmp/$($release.tag_name).zip" -DestinationPath "$releaseDirectory/tmp/extracted" | Out-String | Write-Verbose
$extractedSubFolder = Get-ChildItem -Path "$releaseDirectory/tmp/extracted" -Directory
if ($null -ne $directoryAndFilesToKeep) {
foreach ($path in $directoryAndFilesToKeep) {
Write-Verbose "===> Moving $path into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/$($path)" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}
}
if ($null -eq $directoryAndFilesToKeep) {
Write-Verbose "===> Moving all extracted contents into $releaseDirectory."
Move-Item -Path "$($extractedSubFolder.FullName)/*" -Destination "$releaseDirectory" -ErrorAction SilentlyContinue | Out-String | Write-Verbose
}
Remove-Item -Path "$releaseDirectory/tmp" -Force -Recurse
} else {
Write-Verbose "===> Content already exists in $releaseDirectory. Skipping"
}
}
return $selectedReleases
}