Skip to content

dispatch-internal-pr-in-public #34

dispatch-internal-pr-in-public

dispatch-internal-pr-in-public #34

name: dispatch-internal-pr-in-public
on:
pull_request_review:
types: [submitted]
permissions: write-all
jobs:
dispatch:
runs-on: ubuntu-latest
if: ${{ github.repository != 'ruflab/shimmer' && github.event.review.state == 'approved' && github.event.pull_request.base.ref == 'public-clone' }}
steps:
- name: Get token
id: get-token
run: |
# Client ID as first argument
client_id=${{ secrets.APP_CLIENT_ID }}
app_id=${{ secrets.APP_ID }}
pem=$( cat <<'EOF'
${{ secrets.APP_KEY }}
EOF
) # file path of the private key as second argument
now=$(date +%s)
iat=$((${now} - 60)) # Issues 60 seconds in the past
exp=$((${now} + 600)) # Expires 10 minutes in the future
b64enc() { openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n'; }
header_json='{
"typ":"JWT",
"alg":"RS256"
}'
# Header encode
header=$( echo -n "${header_json}" | b64enc )
payload_json="{
\"iat\":${iat},
\"exp\":${exp},
\"iss\":\"${client_id}\"
}"
# Payload encode
payload=$( echo -n "${payload_json}" | b64enc )
# Signature
header_payload="${header}"."${payload}"
signature=$(
openssl dgst -sha256 -sign <(echo -n "${pem}") \
<(echo -n "${header_payload}") | b64enc
)
#echo $header_payload
#echo $signature
# Create JWT
JWT=$(printf '%s\n' "${header_payload}"."${signature}")
echo "::add-mask::$JWT"
resp=$(curl --request POST \
--url "https://api.github.com/app/installations/$app_id/access_tokens" \
--header "Accept: application/vnd.github+json" \
--header "Authorization: Bearer $JWT" \
--header "X-GitHub-Api-Version: 2022-11-28"
)
token=$(jq -r '.token' <<< $resp)
echo "::add-mask::$token"
if [[ -z "$token" ]]; then
echo "Could not fetch access token"
exit 1
fi
echo "TOKEN=$token" >> "$GITHUB_OUTPUT"
- name: Dispatch
run: |
payload='{
"event_type": "on-priv-updated",
"client_payload": {
"base": "${{ github.event.pull_request.base.sha }}",
"branch": "${{ github.event.pull_request.head.ref }}",
"pr_id": "${{ github.event.pull_request.number }}"
}
}'
curl -L \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ steps.get-token.outputs.TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ secrets.PUBLIC_REPO }}/dispatches \
-d "$payload"
- uses: actions/checkout@v4
with:
token: ${{ steps.get-token.outputs.TOKEN }}
- name: Link to pr
run: |
title=$(jq -sRr @uri <<'EOF'
${{ github.event.pull_request.title }}
EOF
)
body=$(jq -sRr @uri <<'EOF'
${{ github.event.pull_request.body }}
EOF
)
publicBranch="$featureBranch-$prId"
publicUrl="https://github.com/$pubRepo/compare/$publicBranch?expand=1&title=$title&body=$body"
git config user.name "github-actions"
git config user.email "[email protected]"
gh repo set-default ${{ github.repository }}
gh pr comment $prId -b "Open a PR on the public repo by clicking [HERE]($publicUrl) (please wait a few seconds for the branch to be automatically created)."
gh pr close $prId
env:
featureBranch: ${{ github.event.pull_request.head.ref }}
prId: ${{ github.event.pull_request.number }}
pubRepo: ${{ secrets.PUBLIC_REPO }}
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}