-
Notifications
You must be signed in to change notification settings - Fork 5
/
github_api
executable file
·40 lines (34 loc) · 1.12 KB
/
github_api
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
#!/bin/bash -eu
# Similar to `gh api <endpoint>`, this utility makes API calls to GitHub.
#
# ENVIRONMENT VARIABLES:
# GITHUB_TOKEN: Required. The GitHub token to use for authentication.
# GH_HOST: Optional. The GitHub host to use. We'll figure out the host from `BUILDKITE_REPO` env var if not set.
#
# Usage:
# github_api <endpoint> [curl-options]
#
# Example:
# github_api user # Send API request and print JSON response
# github_api user --head # Make a HEAD request and print response headers
if [[ -z "${GITHUB_TOKEN:-}" ]]; then
echo "Missing environment variable GITHUB_TOKEN"
exit 1
fi
github_endpoint="$1"
shift
github_host=${GH_HOST:-}
if [[ -z "${github_host}" ]]; then
if [[ "${BUILDKITE_REPO:-}" =~ ^(https?://|git@)([^/:]+)[:/] ]]; then
github_host="${BASH_REMATCH[2]}"
else
github_host=github.com
fi
fi
if [[ "$github_host" == "github.com" ]]; then
url="https://api.github.com/$github_endpoint"
else
url="https://${github_host}/api/v3/$github_endpoint"
fi
curl -H "Authorization: Bearer ${GITHUB_TOKEN}" -H "Accept: application/vnd.github+json" -s "$@" "$url"
exit $?