Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use github token when getting latest release #21

Merged
merged 15 commits into from
Sep 24, 2024
5 changes: 5 additions & 0 deletions install-cmdstan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ This action installs and caches [CmdStan](https://mc-stan.org/users/interfaces/c

**Optional** Number of cores to use for building. Default `1`.

### `github-token`

**Optional** The GitHub token used to create an authenticated client. By default
will use one automatically created by the github action.

## Example usage

```yaml
Expand Down
8 changes: 8 additions & 0 deletions install-cmdstan/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,27 @@ inputs:
description: 'Number of cores to use for building CmdStan'
required: false
default: '1'
github-token:
description: The GitHub token used to create an authenticated client
default: ${{ github.token }}
required: false

runs:
using: 'composite'
steps:
- name: Determine CmdStan Version (Unix)
if: runner.os != 'Windows' && inputs.cmdstan-version == 'latest'
env:
GH_TOKEN: ${{ inputs.github-token }}
run: |
chmod +x ${{ github.action_path }}/scripts/get-latest-release.sh
${{ github.action_path }}/scripts/get-latest-release.sh
shell: bash

- name: Determine CmdStan Version (Windows)
if: runner.os == 'Windows' && inputs.cmdstan-version == 'latest'
env:
GH_TOKEN: ${{ inputs.github-token }}
run: ${{ github.action_path }}\scripts\get-latest-release.ps1
shell: pwsh

Expand Down
11 changes: 10 additions & 1 deletion install-cmdstan/scripts/get-latest-release.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
$max_attempts = 10
$wait_time = 5 # seconds
$version = $null
$token = $env:GH_TOKEN
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
if (-not [string]::IsNullOrWhiteSpace($token)) {
$headers.Add('Accept','application/vnd.github+json')
$headers.Add('Authorization', -join('Bearer: ', $token))
$headers.Add('X-GitHub-Api-Version','2022-11-28')
} else {
Write-Host "No authentication token found in the environment."
}

for ($attempt = 1; $attempt -le $max_attempts; $attempt++) {
try {
# Using Invoke-RestMethod to fetch the latest release data
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/stan-dev/cmdstan/releases/latest" -Method Get -ErrorAction Stop
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/stan-dev/cmdstan/releases/latest" -Method Get -ErrorAction Stop -Headers $headers
$version = $response.tag_name -replace '^v', '' # Remove 'v' from version if present

# Check if the version is successfully retrieved
Expand Down
12 changes: 11 additions & 1 deletion install-cmdstan/scripts/get-latest-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ version=""

for ((i=0; i<retries; i++)); do
echo "Attempt $((i+1)) of $retries"
auth_str=""
if [ -n "${GH_TOKEN}" ]; then
## we have a github token in the enviroment; set autentication string
auth_str+=" -H \"Accept: application/vnd.github+json\""
auth_str+=" -H \"Authorization: Bearer $GH_TOKEN\""
auth_str+=" -H \"X-GitHub-Api-Version: 2022-11-28\""
else
echo "No authentication token found in the environment."
fi
# Save the response body to a temporary file and capture HTTP status code separately
response=$(curl -s -w "%{http_code}" -o temp.json https://api.github.com/repos/stan-dev/cmdstan/releases/latest)
response=$(eval curl -s -w "%{http_code}" -L -o temp.json $auth_str \
https://api.github.com/repos/stan-dev/cmdstan/releases/latest)
http_code=$(echo $response | tail -n1) # Extract the HTTP status code
version=$(jq -r '.tag_name' temp.json | tr -d 'v')
rm temp.json
Expand Down
Loading