forked from nebula-it/winget-pkgs-automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateManifest.ps1
79 lines (71 loc) · 4.35 KB
/
createManifest.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
# Prepare headers for auth
# Only create header if env var exists, this helps with local run where this does not exist
# And it is not usually required, we are using it here to avoid any API rate limits
if ($env:GITHUB_TOKEN) {
$headers = @{
Authorization = "Bearer $($env:GITHUB_TOKEN)"
}
}
# Initialize Git
git config user.name 'github-actions[bot]'
git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
$packages = Get-ChildItem .\packages\*.json -Exclude schema.json
foreach ($package in $packages) {
$packageInfo = Get-Content -Raw $package | ConvertFrom-Json
Write-Host "`n======== Working on Package $($packageInfo.Name) ========" -ForegroundColor Green
# Get Latest version of Package releases
$req = Invoke-RestMethod "https://api.github.com/repos/$($packageInfo.repoUrl)/releases/latest" -Headers $headers
# Get Latest version number, remove `v` from string
$latestVersion = $req.tag_name.Replace('v', '')
# Once winget has support for tar.gz , add `|tar\.gz` to extension filter
$downloadURLFilter = $package.downloadURLFilter ? $package.downloadURLFilter : '.*windows.*\.(zip|exe)$'
# Get download url
$latestVersionDownloadURL = $req.assets | Where-Object name -Match $downloadURLFilter | Select-Object -ExpandProperty browser_download_url
# If the local manifest contains 'submittedVersion' we use that otherwise we get the latest version from winget
# This is to solve the issue where we have submitted the PR for a new version but its not merged into winget yet
if($packageInfo.submittedVersion){
$wingetLatestVersion = $packageInfo.submittedVersion
Write-Host "Retrived latest version $($wingetLatestVersion) from local manifest."
}
else {
# Get contents of winget manifest e.g 'https://github.com/microsoft/winget-pkgs/tree/master/manifests/s/Sidero/talosctl'
$wingetPackageList = Invoke-RestMethod "https://api.github.com/repos/microsoft/winget-pkgs/contents/$($packageInfo.wingetManifestPath)" -Headers $headers
$wingetLatestVersion = $wingetPackageList | Select-Object -Last 1 | Select-Object -ExpandProperty name
Write-Host "submittedVersion not found in local manifest. Retreived $($wingetLatestVersion) from wingt."
}
if ($latestVersion -ne $wingetLatestVersion) {
Write-Host "Updating $($packageInfo.name) from '$wingetLatestVersion' to '$latestVersion'"
$packageIdentifier = $packageInfo.packageIdentifier
if (-not $packageIdentifier) {
# If no $packageIdentifier is defined in packages manifest, we construct one using the wingetManifestPath. Where the second last
# subpath is org name and last bit of path is package name
$packageIdentifier = "$(($packageInfo.wingetManifestPath).split('/')[-2]).$(($packageInfo.wingetManifestPath).split('/')[-1])"
}
$wingetCmd = ".\wingetcreate.exe update $packageIdentifier -s -v $latestVersion -u $($latestVersionDownloadURL -join (' '))" + ' -t $($env:WINGET_PAT)'
Write-Host "Using cmd: $($wingetCmd)"
# Download wingetcreate
if (-not (Test-Path .\wingetcreate.exe)) {
Invoke-WebRequest https://aka.ms/wingetcreate/latest -OutFile wingetcreate.exe
}
# Update the existing manifest
# A bug with Invoke-Expression is not adding the output to wingetOutput var properly
# using Tee-Object as workaround: https://stackoverflow.com/questions/37330115/invoke-expression-output-on-screen-and-in-a-variable
Invoke-Expression $wingetCmd | Tee-Object -Variable wingetOutput
# First we get the lines that says 'Pull request can be found here:'
# Then split it at Spaces and get the last string, which is URL
$wingetPrURL = (($wingetOutput | Select-String -Pattern 'Pull request can be found here:\s+https:\/\/[\w\-\.\/]+').Matches[0] -split ' ')[-1]
Write-Debug "PR URL: $wingetPrURL"
# If a PR is successfully submitted then update the local manifest
if ($wingetPrURL) {
$packageInfo | Add-Member -MemberType NoteProperty -Name submittedVersion -Value $latestVersion -Force
$packageInfo | Add-Member -MemberType NoteProperty -Name submittedPrURL -Value $wingetPrURL -Force
$packageInfo | ConvertTo-Json | Set-Content -Path $package
git add $package
git commit -m "Updated $($packageInfo.name) to version $($latestVersion)"
git push
}
}
else {
Write-Host "Latest version of $($packageInfo.name) ($($latestVersion)) is already present in Winget."
}
}