Skip to content

Commit 07ae25b

Browse files
Add auto merge workflow
1 parent d8620c7 commit 07ae25b

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: 'Generate GitHub App token'
2+
description: 'Generate an API token for a GitHub app'
3+
4+
inputs:
5+
app_id:
6+
description: 'The app id'
7+
type: string
8+
installation_id:
9+
description: 'The app installation id'
10+
type: string
11+
private_key:
12+
description: 'The app private key in PEM format'
13+
type: string
14+
outputs:
15+
token:
16+
description: "The generated GitHub App token"
17+
value: ${{ steps.token.outputs.value }}
18+
19+
runs:
20+
using: "composite"
21+
steps:
22+
- name: Create temporary directory
23+
id: tempdir
24+
shell: bash
25+
run: |
26+
tempdir=$(mktemp -d)
27+
echo "tempdir=${tempdir}" >> $GITHUB_OUTPUT
28+
29+
- name: Create JWT
30+
id: jwt
31+
shell: bash
32+
run: |
33+
DIR="${{ steps.tempdir.outputs.tempdir }}"
34+
python3 -m venv ${DIR}/.venv
35+
source ${DIR}/.venv/bin/activate
36+
pip install --quiet --upgrade pip
37+
pip install --quiet jwt
38+
39+
cat > ./create-jwt.py << EOF
40+
#!/usr/bin/env python3
41+
from time import time
42+
from os import environ
43+
from sys import argv
44+
45+
from jwt import JWT, jwk_from_pem
46+
47+
private_key, app_id, now = argv[1], argv[2], int(time())
48+
signing_key = jwk_from_pem(private_key.encode("utf-8"))
49+
print(JWT().encode(dict(iat=now, exp=now + 600, iss=app_id), signing_key, alg="RS256"))
50+
EOF
51+
52+
chmod 755 ./create-jwt.py
53+
54+
VALUE=$(./create-jwt.py "${{ inputs.private_key }}" "${{ inputs.app_id }}")
55+
echo "::add-mask::${VALUE}"
56+
echo "value=${VALUE}" >> "$GITHUB_OUTPUT"
57+
58+
- name: Create token
59+
id: token
60+
shell: bash
61+
run: |
62+
TOKEN=$(curl --silent --request POST \
63+
--url "https://api.github.com/app/installations/${{ inputs.installation_id }}/access_tokens" \
64+
--header "Accept: application/vnd.github+json" \
65+
--header "Authorization: Bearer ${{ steps.jwt.outputs.value }}" \
66+
--header "X-GitHub-Api-Version: 2022-11-28" \
67+
| jq .token -r)
68+
echo "::add-mask::${TOKEN}"
69+
echo "value=${TOKEN}" >> "$GITHUB_OUTPUT"
70+
71+
- name: Delete temporary directory
72+
if: always()
73+
shell: bash
74+
run: |
75+
rm -rf ${{ steps.tempdir.outputs.tempdir }}

.github/workflows/auto-merge.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Auto Merge Bot PRs
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
validate:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout source
13+
uses: actions/checkout@v4
14+
15+
- name: Create app token
16+
id: create-app-token
17+
uses: ./.github/actions/github/app-token
18+
with:
19+
app_id: ${{ vars.LETTUCE_BOT_APP_ID }}
20+
installation_id: ${{ vars.LETTUCE_BOT_INSTALLATION_ID }}
21+
private_key: ${{ secrets.LETTUCE_BOT_PRIVATE_KEY }}
22+
23+
- name: Auto Merge Lettuce Bot
24+
if: ${{ github.actor == 'lettuce-bot[bot]' || github.actor == 'renovate[bot]' }}
25+
run: |
26+
gh pr merge --repo ${{ github.repository }} --auto --merge ${{ github.event.number }}
27+
env:
28+
GH_TOKEN: ${{ steps.create-app-token.outputs.token }}

0 commit comments

Comments
 (0)