Skip to content

Commit 3666a69

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

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

.github/scripts/dispatch_internal_repo_workflow.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,60 @@ while [[ $# -gt 0 ]]; do
8686
;;
8787
esac
8888
done
89+
# Validate required parameters
90+
if [[ -z "$PEM_FILE" ]]; then
91+
echo "[ERROR] PEM_FILE environment variable is not set or is empty."
92+
exit 1
93+
fi
94+
95+
if [[ -z "$CLIENT_ID" ]]; then
96+
echo "[ERROR] CLIENT_ID environment variable is not set or is empty."
97+
exit 1
98+
fi
99+
100+
now=$(date +%s)
101+
iat=$((${now} - 60)) # Issues 60 seconds in the past
102+
exp=$((${now} + 600)) # Expires 10 minutes in the future
103+
104+
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
105+
106+
header_json='{
107+
"typ":"JWT",
108+
"alg":"RS256"
109+
}'
110+
# Header encode
111+
header=$( echo -n "${header_json}" | b64enc )
112+
113+
payload_json="{
114+
\"iat\":${iat},
115+
\"exp\":${exp},
116+
\"iss\":\"${$CLIENT_ID}\"
117+
}"
118+
# Payload encode
119+
payload=$( echo -n "${payload_json}" | b64enc )
120+
121+
# Signature
122+
header_payload="${header}"."${payload}"
123+
signature=$(
124+
openssl dgst -sha256 -sign <(echo -n "${$PEM_FILE}") \
125+
<(echo -n "${header_payload}") | b64enc
126+
)
127+
128+
# Create JWT
129+
JWT="${header_payload}"."${signature}"
130+
131+
INSTALLATION_ID=$(curl -X GET \
132+
-H "Accept: application/vnd.github+json" \
133+
-H "Authorization: Bearer ${JWT}" \
134+
-H "X-GitHub-Api-Version: 2022-11-28" \
135+
--url "https://api.github.com/app/installations" | jq -r '.[0].id')
136+
137+
PR_TRIGGER_PAT=$(curl --request POST \
138+
--url "https://api.github.com/app/installations/${INSTALLATION_ID}/access_tokens" \
139+
-H "Accept: application/vnd.github+json" \
140+
-H "Authorization: Bearer ${JWT}" \
141+
-H "X-GitHub-Api-Version: 2022-11-28" | jq -r '.token')
142+
89143

90144
# Set default values if not provided
91145
if [[ -z "$PR_TRIGGER_PAT" ]]; then

0 commit comments

Comments
 (0)