Skip to content

Commit 7fde564

Browse files
authored
Merge pull request #106 from cognifloyd/pat-regeneration
Simplify Github PAT regeneration
2 parents 1213a35 + ee81860 commit 7fde564

2 files changed

Lines changed: 57 additions & 32 deletions

File tree

.circle/index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import hashlib
66
import json
77
import os
8+
import sys
89
import subprocess
910
import time
1011
from collections import OrderedDict
@@ -21,6 +22,7 @@
2122

2223
GITHUB_USERNAME = os.environ.get('MACHINE_USER')
2324
GITHUB_PASSWORD = os.environ.get("MACHINE_PASSWORD", os.environ.get("GH_TOKEN"))
25+
ACTIVE_PACK_NAME = os.environ.get('PACK_NAME', "unknown")
2426

2527
SESSION = requests.Session()
2628
SESSION.auth = (GITHUB_USERNAME, GITHUB_PASSWORD)
@@ -149,6 +151,13 @@ def get_available_versions():
149151
proc.kill()
150152
outs, _ = proc.communicate()
151153
result = outs.decode().strip()
154+
if proc.returncode != 0:
155+
sys.exit(
156+
"Error retrieving data with github graphql API.\n"
157+
"The Github PAT might need to be regenerated:\n"
158+
"https://github.com/settings/tokens/new?scopes=public_repo"
159+
"&description=CircleCI%3A%20stackstorm-" + ACTIVE_PACK_NAME
160+
)
152161

153162
# https://stackoverflow.com/a/43807246/1134951
154163
decoder = json.JSONDecoder()

tools/reset_github_user_token_and_update_circleci.sh

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,30 @@
1111
# * CIRCLECI_TOKEN: a CircleCI token for the Exchange organization.
1212
#
1313

14-
if [[ ! $# -eq 1 ]];
14+
if [[ ! $# -gt 0 ]];
1515
then
16-
echo "Usage: $0 <pack>"
16+
echo "Usage: $0 <pack> [<pack> <pack> ...]"
1717
exit 1
1818
fi
1919

20-
PACK="$1"
20+
BROWSER_NAME=firefox
21+
BROWSER="/Applications/Firefox.app/Contents/MacOS/firefox -private-window"
22+
2123
EXCHANGE_ORG="${EXCHANGE_ORG:-StackStorm-Exchange}"
2224
EXCHANGE_PREFIX="${EXCHANGE_PREFIX:-stackstorm}"
23-
# Add the stackstorm- prefix to the repo name if it doesn't exist already
24-
if [[ "$PACK" = ${EXCHANGE_PREFIX}-* ]];
25-
then
26-
REPO_NAME="$PACK"
27-
else
28-
REPO_NAME="${EXCHANGE_PREFIX}-${PACK}"
29-
fi
25+
26+
function repo_name() {
27+
local pack="$1"
28+
local repo_name=""
29+
# Add the stackstorm- prefix to the repo name if it doesn't exist already
30+
if [[ "$pack" = ${EXCHANGE_PREFIX}-* ]];
31+
then
32+
repo_name="$pack"
33+
else
34+
repo_name="${EXCHANGE_PREFIX}-${pack}"
35+
fi
36+
echo ${repo_name}
37+
}
3038

3139
DEFAULT_USERNAME="stackstorm-neptr"
3240
if [[ -z "$USERNAME" ]];
@@ -55,28 +63,36 @@ fi
5563

5664
# GitHub: create a user-scope token
5765
# TODO: Delete any existing token for that repo
58-
echo "Github: Creating a Github user-scoped token"
59-
GITHUB_USER_TOKEN=$(curl -sS --fail -u "${USERNAME}:${PASSWORD}" -X POST \
60-
--header "Content-Type: application/json" \
61-
-d '{"scopes": ["public_repo"], "note": "CircleCI: '"${REPO_NAME}"'"}' \
62-
"https://api.github.com/authorizations" | jq --raw-output ".token")
66+
echo "Github: Creating a Github user-scoped token for each pack"
67+
echo
6368

64-
if [[ -z "$GITHUB_USER_TOKEN" ]];
65-
then
66-
echo "Could not create a GitHub Personal Access Token."
67-
exit 1
68-
fi
69+
for PACK in $@; do
70+
REPO_NAME=$(repo_name ${PACK})
71+
echo "REPO_NAME=${REPO_NAME}"
72+
echo
6973

70-
echo "GitHub Personal Access Token for ${USERNAME}:"
71-
echo
72-
echo " ${GITHUB_USER_TOKEN}"
73-
echo
74+
echo "Please click 'Generate Token' when ${BROWSER_NAME} opens."
75+
echo "Then copy the Github PAT token and paste it here:"
76+
${BROWSER} "https://github.com/settings/tokens/new?scopes=public_repo&description=CircleCI%3A%20${REPO_NAME}"
77+
read -s GITHUB_USER_TOKEN
78+
79+
if [[ -z "$GITHUB_USER_TOKEN" ]];
80+
then
81+
echo "Could not create a GitHub Personal Access Token."
82+
exit 1
83+
fi
84+
85+
echo "GitHub Personal Access Token for ${USERNAME}:"
86+
echo
87+
echo " ${GITHUB_USER_TOKEN}"
88+
echo
7489

75-
# CircleCI: specify the credentials (the machine login and the new user-scope token)
76-
echo "CircleCI: Setting credentials (machine login and user-scoped token)"
77-
curl -sS --fail -X POST --header "Content-Type: application/json" \
78-
-d '{"name":"MACHINE_USER", "value":"'"${USERNAME}"'"}' \
79-
"https://circleci.com/api/v1.1/project/github/${EXCHANGE_ORG}/${REPO_NAME}/envvar?circle-token=${CIRCLECI_TOKEN}"
80-
curl -sS --fail -X POST --header "Content-Type: application/json" \
81-
-d '{"name":"MACHINE_PASSWORD", "value": "'"${GITHUB_USER_TOKEN}"'"}' \
82-
"https://circleci.com/api/v1.1/project/github/${EXCHANGE_ORG}/${REPO_NAME}/envvar?circle-token=${CIRCLECI_TOKEN}"
90+
# CircleCI: specify the credentials (the machine login and the new user-scope token)
91+
echo "CircleCI: Setting credentials (machine login and user-scoped token)"
92+
curl -sS --fail -X POST --header "Content-Type: application/json" \
93+
-d '{"name":"MACHINE_USER", "value":"'"${USERNAME}"'"}' \
94+
"https://circleci.com/api/v1.1/project/github/${EXCHANGE_ORG}/${REPO_NAME}/envvar?circle-token=${CIRCLECI_TOKEN}"
95+
curl -sS --fail -X POST --header "Content-Type: application/json" \
96+
-d '{"name":"MACHINE_PASSWORD", "value": "'"${GITHUB_USER_TOKEN}"'"}' \
97+
"https://circleci.com/api/v1.1/project/github/${EXCHANGE_ORG}/${REPO_NAME}/envvar?circle-token=${CIRCLECI_TOKEN}"
98+
done

0 commit comments

Comments
 (0)