-
Notifications
You must be signed in to change notification settings - Fork 0
141 lines (125 loc) · 5.15 KB
/
generate-python-client.yaml
File metadata and controls
141 lines (125 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: Generate Python Client
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *" # Runs daily at midnight UTC
jobs:
generate-client:
runs-on: ubuntu-latest
env:
OPENAPI_SPEC_URL: https://app.fieldmanager.io/api/location/openapi.json
steps:
# Step 1: Initialize Log File
- name: 📝 Initialize Log File
run: |
mkdir -p logs
echo "" > logs/log
# Step 2: Checkout the code
- name: 📥 Checkout repository
uses: actions/checkout@v4
with:
ref: trunk
# Step 3: Set up Python environment
- name: 🐍 Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
# Step 4: Set up Node.js
- name: 🟢 Set up Node.js
uses: actions/setup-node@v4
with:
node-version: "22"
# Step 5: Install dependencies
- name: 📦 Install Dependencies
run: |
python -m pip install --upgrade pip
pip install openapi-python-client yq
sudo apt-get update && sudo apt-get install -y jq
echo "Recording openapi-python-client version..." >> logs/log
openapi-python-client --version >> logs/log 2>&1
# Step 6: Download OpenAPI Specification
- name: ⬇️ Download OpenAPI Specification
run: |
echo "Downloading OpenAPI specification..."
mkdir -p openapi_specification
curl -s ${{ env.OPENAPI_SPEC_URL }} > ./openapi_specification/openapi.json
# Step 7: Format OpenAPI Specification
- name: 🔧 Format OpenAPI Specification
run: |
echo "Formatting OpenAPI specification..."
npx prettier --write ./openapi_specification/openapi.json
# Step 8: Get API Version
- name: 🔢 Get API Version
run: |
if yq -e '.package_version_override' config.yaml > /dev/null; then
VERSION=$(yq -r '.package_version_override' config.yaml)
else
VERSION=$(jq -r '.info.version' ./openapi_specification/openapi.json)
fi
if [ -f openapi_specification/version ]; then
OLD_VERSION=$(cat openapi_specification/version)
if [ "$VERSION" = "$OLD_VERSION" ]; then
echo "API version has not changed. Skipping update."
echo "version_changed=false" >> $GITHUB_ENV
exit 0
else
echo "API version has changed from $OLD_VERSION to $VERSION."
echo "version_changed=true" >> $GITHUB_ENV
fi
fi
echo "$VERSION" > openapi_specification/version
echo "VERSION=$VERSION" >> $GITHUB_ENV
# Step 9: Check for existing PR
- name: 🔍 Check for existing PR
if: env.version_changed == 'true'
run: |
PR_EXISTS=$(gh pr list --repo ${{ github.repository }} --base trunk --search "update-python-client-${{ env.VERSION }} in:title" --json number --jq 'length')
if [ "$PR_EXISTS" -gt 0 ]; then
echo "A pull request for version ${{ env.VERSION }} already exists. Skipping update."
echo "pr_exists=true" >> $GITHUB_ENV
else
echo "No pull request found for version ${{ env.VERSION }}. Proceeding with update."
echo "pr_exists=false" >> $GITHUB_ENV
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Step 10: Generate Python Client
- name: ⚙️ Generate Python Client
if: env.version_changed == 'true' && env.pr_exists == 'false'
run: |
echo "Generating API client..." >> logs/log
openapi-python-client generate \
--path ./openapi_specification/openapi.json \
--overwrite \
--custom-template-path=templates \
--config config.yaml >> logs/log 2>&1
cat logs/log
# Step 11: Check for Changes
- name: 👀 Check for Changes
if: env.version_changed == 'true' && env.pr_exists == 'false'
id: git_diff
run: |
git add .
if git diff --cached --quiet; then
echo "changes_detected=false" >> $GITHUB_OUTPUT
else
echo "changes_detected=true" >> $GITHUB_OUTPUT
fi
# Step 12: Configure Git
- name: 🔨 Configure Git
if: env.version_changed == 'true' && env.pr_exists == 'false' && steps.git_diff.outputs.changes_detected == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
# Step 13: Create Pull Request
- name: 🚀 Create Pull Request
if: env.version_changed == 'true' && env.pr_exists == 'false' && steps.git_diff.outputs.changes_detected == 'true'
uses: peter-evans/create-pull-request@v7
with:
token: ${{ secrets.PULL_REQUEST_TOKEN }}
branch: update-python-client-${{ env.VERSION }}
base: trunk
title: "Update Python client to API version ${{ env.VERSION }}"
body: |
This PR updates the generated Python client to match API version ${{ env.VERSION }}.
branch-suffix: timestamp