Files
setup-cli/src/utils.ts
Bobbie Soedirgo b5dccf414b fix: authenticate action requests to github api (#78)
* fix: only test on latest

Should help with GitHub API rate limiting

* Update .github/workflows/test.yml

* fix: add github token to workflow

* fix: authenticate with github api

* chore: remove token env

* chore: update dist files

* Revert "chore: remove token env"

This reverts commit 913c7a8e6f.

* chore: use gh token env var

* chore: update user agent string

* chore: organize imports

Co-authored-by: Han Qiao <qiao@supabase.io>
2022-10-19 16:36:59 +08:00

56 lines
1.7 KiB
TypeScript

import os from 'os'
import {HttpClient} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/lib/auth'
interface GitHubTag {
tag_name: string
}
// arch in [arm, arm64, x64...] (https://nodejs.org/docs/latest-v16.x/api/os.html#osarch)
// return value in [amd64, arm64, arm]
const mapArch = (arch: string): string => {
const mappings: Record<string, string> = {
x64: 'amd64'
}
return mappings[arch] || arch
}
// os in [darwin, linux, win32...] (https://nodejs.org/docs/latest-v16.x/api/os.html#osplatform)
// return value in [darwin, linux, windows]
const mapOS = (platform: string): string => {
const mappings: Record<string, string> = {
win32: 'windows'
}
return mappings[platform] || platform
}
export const getDownloadUrl = async (version: string): Promise<string> => {
const platform = mapOS(os.platform())
const arch = mapArch(os.arch())
const resolvedVersion = await resolveVersion(version)
const filename = `supabase_${resolvedVersion}_${platform}_${arch}`
return `https://github.com/supabase/cli/releases/download/v${resolvedVersion}/${filename}.tar.gz`
}
// Authenticate with GH_TOKEN to avoid GitHub API rate limits
const token = process.env['GH_TOKEN']
const http = new HttpClient(
'supabase/setup-cli',
token ? [new BearerCredentialHandler(token)] : undefined
)
const resolveVersion = async (version: string): Promise<string> => {
if (version !== 'latest') {
return version
}
const url = 'https://api.github.com/repos/supabase/cli/releases/latest'
const tag = (await http.getJson<GitHubTag>(url)).result?.tag_name
if (!tag) {
throw new Error('Cannot fetch tag info')
}
return tag.substring(1)
}