-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetHub.ps1
78 lines (64 loc) · 2.79 KB
/
GetHub.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
# GetHub -Owner 'username' -Repo 'repository' -FileName 'file name'
# GetHub -Owner 'username' -Repo 'repository' -RegexPattern 'regex pattern'
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$Owner,
[Parameter(Mandatory=$true)]
[string]$Repo,
#[Parameter(Mandatory=$true)]
#[string]$Token,
[Parameter(Mandatory=$true, ParameterSetName="FileName")]
[string]$FileName,
[Parameter(Mandatory=$true, ParameterSetName="RegexPattern")]
[string]$RegexPattern
)
If ($FileName)
{
# Construct the GitHub API URL to fetch release information
$apiUrl = "https://github.com/$Owner/$Repo/releases/latest/download/$FileName"
# Download file
Invoke-WebRequest -Uri $apiUrl -OutFile $FileName -Resume
$assetName = $FileName
$currentDate = Get-Date
Write-Host "Downloaded: $assetName"
Write-Host "Assets downloaded from the latest release as of $currentDate."
}
Else # ($RegexPattern)
{
# Construct the GitHub API URL to fetch release information
$apiUrl = "https://api.github.com/repos/$Owner/$Repo/releases/latest"
# Set a user agent header for GitHub API requests
$headers = @{
'User-Agent' = 'GetHub'
#"Authorization" = "token $Token"
}
# Invoke the GitHub API to get the latest release details
$response = Invoke-RestMethod -Uri $apiUrl -Headers $headers
# Check if the request was successful
if ($response) {
# if name is empty use tag_name
$latestReleaseName = if ($response.name) { $response.name } else { $response.tag_name }
$assets = $response.assets
# Filter assets based on the regex pattern
$filteredAssets = $assets | Where-Object { $_.name -match $RegexPattern }
if ($filteredAssets.Count -eq 0) {
Write-Host "No assets match the regex pattern '$RegexPattern' in the latest release '$latestReleaseName'."
#} elseif ($filteredAssets.Count -gt 1) {
# Write-Host "Multiple assets match the regex pattern '$RegexPattern' in the latest release '$latestReleaseName'. Please refine the pattern."
} else {
# Download the filtered assets
foreach ($asset in $filteredAssets) {
$downloadUrl = $asset.browser_download_url
$assetName = $asset.name
$stats = Invoke-WebRequest -Uri $downloadUrl -OutFile $assetName -Resume
Write-Host "Downloaded: $assetName"
}
Write-Host "Assets downloaded from the latest release '$latestReleaseName'."
}
} else {
Write-Host 'Failed to fetch release information from GitHub. Please check repository details.'
#Write-Host 'Failed to fetch release information from GitHub. Please check your credentials and repository details.'
}
}
Return $assetName