-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-gh.sh
More file actions
43 lines (34 loc) · 1.27 KB
/
setup-gh.sh
File metadata and controls
43 lines (34 loc) · 1.27 KB
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
#! /usr/bin/env bash
# This script sets up the authentication parameters for the GitHub CLI
# that are needed to run the version2.py script.
# It is assumed that the GitHub CLI is already installed and configured
# on the system.
# The script will update the user's github token to have the needed scopes,
# set the token in the environment and pull in the gh users username as
# another environment variable.
# Exit if not sourced
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
echo "ERROR: This script must be sourced, not executed."
echo "Usage: source ${BASH_SOURCE[0]}"
exit 1
fi
# Check if the GITHUB_TOKEN environment variable is already set
set_gh_token() {
# If not set, set the scopes for the token and set the environment variable
gh auth refresh --scopes read:project
# Set the GITHUB_TOKEN environment variable
export GITHUB_TOKEN=$(gh auth token)
}
configure_gh_env() {
if [ -n "${GITHUB_TOKEN}" ]; then
echo "GITHUB_TOKEN needs to be unset to update the token scopes."
unset GITHUB_TOKEN
fi
# set the gh_token
set_gh_token
# set the gh_user
export GITHUB_UNAME=$(gh api user --jq .login)
echo "GITHUB_TOKEN is set. Length is: ${#GITHUB_TOKEN}"
echo "GITHUB_UNAME is set to: ${GITHUB_UNAME}"
}
configure_gh_env