Skip to content

Commit 6924003

Browse files
committed
CCM-10193 change to use GitHub app token
Signed-off-by: Tim Ireland <[email protected]>
1 parent c552ef2 commit 6924003

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,59 @@ while [[ $# -gt 0 ]]; do
111111
esac
112112
done
113113

114+
if [[ -z "$PEM_FILE" ]]; then
115+
echo "[ERROR] PEM_FILE environment variable is not set or is empty."
116+
exit 1
117+
fi
118+
119+
if [[ -z "$CLIENT_ID" ]]; then
120+
echo "[ERROR] CLIENT_ID environment variable is not set or is empty."
121+
exit 1
122+
fi
123+
124+
now=$(date +%s)
125+
iat=$((${now} - 60)) # Issues 60 seconds in the past
126+
exp=$((${now} + 600)) # Expires 10 minutes in the future
127+
128+
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
129+
130+
header_json='{
131+
"typ":"JWT",
132+
"alg":"RS256"
133+
}'
134+
# Header encode
135+
header=$( echo -n "${header_json}" | b64enc )
136+
137+
payload_json="{
138+
\"iat\":${iat},
139+
\"exp\":${exp},
140+
\"iss\":\"${$CLIENT_ID}\"
141+
}"
142+
# Payload encode
143+
payload=$( echo -n "${payload_json}" | b64enc )
144+
145+
# Signature
146+
header_payload="${header}"."${payload}"
147+
signature=$(
148+
openssl dgst -sha256 -sign <(echo -n "${$PEM_FILE}") \
149+
<(echo -n "${header_payload}") | b64enc
150+
)
151+
152+
# Create JWT
153+
JWT="${header_payload}"."${signature}"
154+
155+
INSTALLATION_ID=$(curl -X GET \
156+
-H "Accept: application/vnd.github+json" \
157+
-H "Authorization: Bearer ${JWT}" \
158+
-H "X-GitHub-Api-Version: 2022-11-28" \
159+
--url "https://api.github.com/app/installations" | jq -r '.[0].id')
160+
161+
PR_TRIGGER_PAT=$(curl --request POST \
162+
--url "https://api.github.com/app/installations/${INSTALLATION_ID}/access_tokens" \
163+
-H "Accept: application/vnd.github+json" \
164+
-H "Authorization: Bearer ${JWT}" \
165+
-H "X-GitHub-Api-Version: 2022-11-28" | jq -r '.token')
166+
114167
# Set default values if not provided
115168
if [[ -z "$PR_TRIGGER_PAT" ]]; then
116169
echo "[ERROR] PR_TRIGGER_PAT environment variable is not set or is empty."

0 commit comments

Comments
 (0)