Skip to content

Commit 2d71a44

Browse files
Use gh api for GitHub release metadata lookups
1 parent e2f7da1 commit 2d71a44

2 files changed

Lines changed: 88 additions & 30 deletions

File tree

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ jobs:
5050
| ----- | -------- | ------- | ----------- |
5151
| `Version` | `false` | `latest` | Desired PowerShell Core version (e.g. `7.4.1`, `7.6.0-preview.6`). Use `latest` to install the newest stable release (or newest prerelease when `Prerelease` is `true`). |
5252
| `Prerelease` | `false` | `false` | Install a prerelease version. When `true` and `Version` is `latest`, resolves to the latest prerelease. Similar to `-Prerelease` on `Install-PSResource`. |
53+
| `Token` | `false` | `${{ github.token }}` | Token used for GitHub API requests. Set to an empty string (`''`) for anonymous API calls. |
54+
| `Host` | `false` | `github.com` | GitHub host used by the CLI/API path. Keep `github.com` for GitHub.com, or set your GHES hostname. |
5355

5456
## Secrets
5557

56-
This action does **not** require any secrets.
58+
This action does **not** require custom secrets by default because it uses `${{ github.token }}`.
59+
60+
If needed, provide `Token` with a custom PAT. To force anonymous API access, set `Token: ''`.
5761
5862
## Outputs
5963

action.yml

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ inputs:
2323
Similar to the `-Prerelease` switch on `Install-PSResource`.
2424
required: false
2525
default: 'false'
26+
Token:
27+
description: |
28+
Token used for GitHub API calls.
29+
Defaults to github.token. Set to an empty string for anonymous API access.
30+
required: false
31+
default: ${{ github.token }}
32+
Host:
33+
description: |
34+
GitHub host used by gh CLI for API calls.
35+
Use github.com for GitHub.com or your GHES hostname.
36+
required: false
37+
default: github.com
2638

2739
runs:
2840
using: composite
@@ -34,24 +46,50 @@ runs:
3446
env:
3547
REQUESTED_VERSION: ${{ inputs.Version }}
3648
PRERELEASE: ${{ inputs.Prerelease }}
37-
GITHUB_TOKEN: ${{ github.token }}
49+
GITHUB_TOKEN: ${{ inputs.Token }}
50+
GH_TOKEN: ${{ inputs.Token }}
51+
GH_HOST: ${{ inputs.Host }}
3852
run:
3953
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
4054
# Install-PowerShell
4155
set -e
4256
echo "Requested version: [$REQUESTED_VERSION]"
4357
echo "Prerelease: [$PRERELEASE]"
4458

59+
github_api_get() {
60+
local endpoint="$1"
61+
if command -v gh >/dev/null 2>&1; then
62+
local gh_args=()
63+
if [[ -n "$GH_HOST" ]]; then
64+
gh_args+=(--hostname "$GH_HOST")
65+
fi
66+
gh api "${gh_args[@]}" -H "X-GitHub-Api-Version: 2022-11-28" "$endpoint"
67+
return
68+
fi
69+
70+
local auth_header=()
71+
if [[ -n "$GITHUB_TOKEN" ]]; then
72+
auth_header=(-H "Authorization: Bearer $GITHUB_TOKEN")
73+
fi
74+
75+
local api_base="https://api.github.com"
76+
if [[ -n "$GH_HOST" && "$GH_HOST" != "github.com" ]]; then
77+
api_base="https://${GH_HOST}/api/v3"
78+
fi
79+
80+
curl -s -f \
81+
-H "Accept: application/vnd.github+json" \
82+
"${auth_header[@]}" \
83+
-H "X-GitHub-Api-Version: 2022-11-28" \
84+
"${api_base}/${endpoint}"
85+
}
86+
4587
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
4688
case "${REQUESTED_VERSION:-}" in
4789
[Ll][Aa][Tt][Ee][Ss][Tt])
4890
if [[ "$PRERELEASE" == "true" ]]; then
4991
REQUESTED_VERSION=$(
50-
curl -s -f \
51-
-H "Accept: application/vnd.github+json" \
52-
-H "Authorization: Bearer $GITHUB_TOKEN" \
53-
-H "X-GitHub-Api-Version: 2022-11-28" \
54-
'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' |
92+
github_api_get 'repos/PowerShell/PowerShell/releases?per_page=100' |
5593
jq -r '[.[] | select(.prerelease == true)] | (.[0].tag_name // empty)' | sed 's/^v//'
5694
)
5795
if [[ -z "$REQUESTED_VERSION" ]]; then
@@ -61,11 +99,7 @@ runs:
6199
echo "Latest prerelease PowerShell version detected: $REQUESTED_VERSION"
62100
else
63101
REQUESTED_VERSION=$(
64-
curl -s -f \
65-
-H "Accept: application/vnd.github+json" \
66-
-H "Authorization: Bearer $GITHUB_TOKEN" \
67-
-H "X-GitHub-Api-Version: 2022-11-28" \
68-
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
102+
github_api_get 'repos/PowerShell/PowerShell/releases/latest' |
69103
jq -r '.tag_name' | sed 's/^v//'
70104
)
71105
if [[ -z "$REQUESTED_VERSION" ]]; then
@@ -99,11 +133,7 @@ runs:
99133
# Query GitHub Releases API for the actual asset URLs to handle naming
100134
# convention differences across releases (e.g. powershell-preview_ vs powershell_).
101135
RELEASE_JSON=$(
102-
curl -s -f \
103-
-H "Accept: application/vnd.github+json" \
104-
-H "Authorization: Bearer $GITHUB_TOKEN" \
105-
-H "X-GitHub-Api-Version: 2022-11-28" \
106-
"https://api.github.com/repos/PowerShell/PowerShell/releases/tags/v${REQUESTED_VERSION}"
136+
github_api_get "repos/PowerShell/PowerShell/releases/tags/v${REQUESTED_VERSION}"
107137
)
108138
if [[ -z "$RELEASE_JSON" ]]; then
109139
echo "Error: Failed to fetch release info for v${REQUESTED_VERSION} from GitHub."
@@ -222,24 +252,50 @@ runs:
222252
env:
223253
REQUESTED_VERSION: ${{ inputs.Version }}
224254
PRERELEASE: ${{ inputs.Prerelease }}
225-
GITHUB_TOKEN: ${{ github.token }}
255+
GITHUB_TOKEN: ${{ inputs.Token }}
256+
GH_TOKEN: ${{ inputs.Token }}
257+
GH_HOST: ${{ inputs.Host }}
226258
run:
227259
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
228260
# Install-PowerShell
229261
set -e
230262
echo "Requested version: [$REQUESTED_VERSION]"
231263
echo "Prerelease: [$PRERELEASE]"
232264

265+
github_api_get() {
266+
local endpoint="$1"
267+
if command -v gh >/dev/null 2>&1; then
268+
local gh_args=()
269+
if [[ -n "$GH_HOST" ]]; then
270+
gh_args+=(--hostname "$GH_HOST")
271+
fi
272+
gh api "${gh_args[@]}" -H "X-GitHub-Api-Version: 2022-11-28" "$endpoint"
273+
return
274+
fi
275+
276+
local auth_header=()
277+
if [[ -n "$GITHUB_TOKEN" ]]; then
278+
auth_header=(-H "Authorization: Bearer $GITHUB_TOKEN")
279+
fi
280+
281+
local api_base="https://api.github.com"
282+
if [[ -n "$GH_HOST" && "$GH_HOST" != "github.com" ]]; then
283+
api_base="https://${GH_HOST}/api/v3"
284+
fi
285+
286+
curl -s -f \
287+
-H "Accept: application/vnd.github+json" \
288+
"${auth_header[@]}" \
289+
-H "X-GitHub-Api-Version: 2022-11-28" \
290+
"${api_base}/${endpoint}"
291+
}
292+
233293
# Only resolve to latest version if explicitly set to 'latest' (case-insensitive)
234294
case "${REQUESTED_VERSION:-}" in
235295
[Ll][Aa][Tt][Ee][Ss][Tt])
236296
if [[ "$PRERELEASE" == "true" ]]; then
237297
REQUESTED_VERSION=$(
238-
curl -s -f \
239-
-H "Accept: application/vnd.github+json" \
240-
-H "Authorization: Bearer $GITHUB_TOKEN" \
241-
-H "X-GitHub-Api-Version: 2022-11-28" \
242-
'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' |
298+
github_api_get 'repos/PowerShell/PowerShell/releases?per_page=100' |
243299
jq -r '[.[] | select(.prerelease == true)] | (.[0].tag_name // empty)' | sed 's/^v//'
244300
)
245301
if [[ -z "$REQUESTED_VERSION" ]]; then
@@ -249,11 +305,7 @@ runs:
249305
echo "Latest prerelease PowerShell version detected: $REQUESTED_VERSION"
250306
else
251307
REQUESTED_VERSION=$(
252-
curl -s -f \
253-
-H "Accept: application/vnd.github+json" \
254-
-H "Authorization: Bearer $GITHUB_TOKEN" \
255-
-H "X-GitHub-Api-Version: 2022-11-28" \
256-
https://api.github.com/repos/PowerShell/PowerShell/releases/latest |
308+
github_api_get 'repos/PowerShell/PowerShell/releases/latest' |
257309
jq -r '.tag_name' | sed 's/^v//'
258310
)
259311
if [[ -z "$REQUESTED_VERSION" ]]; then
@@ -321,7 +373,7 @@ runs:
321373
env:
322374
REQUESTED_VERSION: ${{ inputs.Version }}
323375
PRERELEASE: ${{ inputs.Prerelease }}
324-
GITHUB_TOKEN: ${{ github.token }}
376+
GITHUB_TOKEN: ${{ inputs.Token }}
325377
run:
326378
| # zizmor: ignore[github-env] GITHUB_PATH writes use hardcoded install dirs, not user input
327379
# Install-PowerShell
@@ -333,9 +385,11 @@ runs:
333385
if ($req -and $req.Trim().ToLower() -eq 'latest') {
334386
$headers = @{
335387
'Accept' = 'application/vnd.github+json'
336-
'Authorization' = "Bearer $($env:GITHUB_TOKEN)"
337388
'X-GitHub-Api-Version' = '2022-11-28'
338389
}
390+
if ($env:GITHUB_TOKEN) {
391+
$headers['Authorization'] = "Bearer $($env:GITHUB_TOKEN)"
392+
}
339393
if ($env:PRERELEASE -eq 'true') {
340394
$releases = Invoke-RestMethod -Uri 'https://api.github.com/repos/PowerShell/PowerShell/releases?per_page=100' -Headers $headers
341395
$latestRelease = $releases | Where-Object { $_.prerelease -eq $true } | Select-Object -First 1

0 commit comments

Comments
 (0)