Skip to content

Commit dd3e77d

Browse files
authored
feat: add workflow to check and update MTProto API schema (#63)
1 parent 81a8597 commit dd3e77d

File tree

2 files changed

+405
-0
lines changed

2 files changed

+405
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
name: Check MTProto API Schema Updates
2+
3+
on:
4+
schedule:
5+
- cron: '0 */12 * * *'
6+
workflow_dispatch:
7+
inputs:
8+
force_update:
9+
description: 'Force update even if no changes detected'
10+
required: false
11+
default: false
12+
type: boolean
13+
14+
jobs:
15+
check-schema-updates:
16+
runs-on: ubuntu-latest
17+
permissions:
18+
contents: write
19+
pull-requests: write
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v5
27+
with:
28+
python-version: '3.11'
29+
cache: 'pip'
30+
31+
- name: Install dependencies
32+
run: |
33+
pip install requests
34+
35+
- name: Check for API schema updates
36+
id: check-updates
37+
run: |
38+
FORCE_UPDATE=""
39+
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.force_update }}" == "true" ]]; then
40+
FORCE_UPDATE="--force-update"
41+
fi
42+
43+
python dev_tools/check_api_schema_updates.py $FORCE_UPDATE
44+
EXIT_CODE=$?
45+
46+
if [ $EXIT_CODE -eq 0 ]; then
47+
echo "updates_detected=true" >> $GITHUB_OUTPUT
48+
CURRENT_LAYER=$(grep -o "// LAYER [0-9]*" compiler/api/source/main_api.tl | awk '{print $3}')
49+
echo "layer_version=$CURRENT_LAYER" >> $GITHUB_OUTPUT
50+
elif [ $EXIT_CODE -eq 2 ]; then
51+
echo "updates_detected=false" >> $GITHUB_OUTPUT
52+
else
53+
echo "Error checking for updates"
54+
exit 1
55+
fi
56+
57+
- name: Set up Git config
58+
if: steps.check-updates.outputs.updates_detected == 'true'
59+
run: |
60+
git config --global user.name "github-actions[bot]"
61+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
62+
63+
- name: Check existing PR
64+
if: steps.check-updates.outputs.updates_detected == 'true'
65+
id: check-pr
66+
env:
67+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68+
run: |
69+
BRANCH_NAME="update/mtproto-api-updates"
70+
71+
# Check if there's an open PR for this branch
72+
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --state open --json number -q '.[0].number')
73+
74+
if [ -n "$PR_NUMBER" ]; then
75+
echo "existing_pr=true" >> $GITHUB_OUTPUT
76+
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
77+
else
78+
echo "existing_pr=false" >> $GITHUB_OUTPUT
79+
fi
80+
81+
- name: Create API update news entry
82+
if: steps.check-updates.outputs.updates_detected == 'true'
83+
run: |
84+
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
85+
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
86+
NEWS_FILE="news/${TIMESTAMP}.misc.rst"
87+
88+
mkdir -p news
89+
90+
echo "API Layer Update" > $NEWS_FILE
91+
echo "================" >> $NEWS_FILE
92+
echo "" >> $NEWS_FILE
93+
echo "Updated MTProto API schema to Layer $LAYER_VERSION." >> $NEWS_FILE
94+
95+
- name: Compile API
96+
if: steps.check-updates.outputs.updates_detected == 'true'
97+
run: |
98+
pip install .
99+
python -m compiler.api.compiler
100+
101+
- name: Compile errors
102+
if: steps.check-updates.outputs.updates_detected == 'true'
103+
run: |
104+
python -m compiler.errors.compiler
105+
106+
- name: Create branch and commit changes
107+
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'false'
108+
run: |
109+
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
110+
BRANCH_NAME="update/mtproto-api-updates"
111+
112+
git checkout -b $BRANCH_NAME
113+
git add compiler/api/source/main_api.tl hydrogram/raw/ hydrogram/errors/ news/
114+
git commit -m "Update MTProto API schema to Layer ${LAYER_VERSION}"
115+
git push origin $BRANCH_NAME
116+
117+
- name: Update existing branch
118+
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'true'
119+
run: |
120+
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
121+
BRANCH_NAME="update/mtproto-api-updates"
122+
123+
# Create a new branch based on the current main branch
124+
git checkout -b $BRANCH_NAME-temp origin/main
125+
126+
# Add the changes and commit
127+
git add compiler/api/source/main_api.tl hydrogram/raw/ hydrogram/errors/ news/
128+
git commit -m "Update MTProto API schema to Layer ${LAYER_VERSION}"
129+
130+
# Force push to update the branch
131+
git push --force origin $BRANCH_NAME-temp:$BRANCH_NAME
132+
133+
- name: Create Pull Request
134+
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'false'
135+
env:
136+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
137+
run: |
138+
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
139+
BRANCH_NAME="update/mtproto-api-updates"
140+
PR_TITLE="Update MTProto API schema"
141+
PR_BODY="This PR automatically updates the MTProto API schema to Layer ${LAYER_VERSION}.\n\n- Updated main schema file\n- Recompiled raw and errors modules\n- Created news entry for changelog"
142+
143+
gh pr create \
144+
--title "$PR_TITLE" \
145+
--body "$PR_BODY" \
146+
--base main \
147+
--head "$BRANCH_NAME" \
148+
--label "enhancement" \
149+
--label "automated"
150+
151+
- name: Update Pull Request Title and Description
152+
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'true'
153+
env:
154+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
155+
run: |
156+
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
157+
PR_NUMBER="${{ steps.check-pr.outputs.pr_number }}"
158+
PR_TITLE="Update MTProto API schema"
159+
PR_BODY="This PR automatically updates the MTProto API schema to Layer ${LAYER_VERSION}.\n\n- Updated main schema file\n- Recompiled raw and errors modules\n- Created news entry for changelog"
160+
161+
gh pr edit $PR_NUMBER --title "$PR_TITLE" --body "$PR_BODY"
162+
163+
PR_COMMENT="Updated the MTProto API schema to Layer ${LAYER_VERSION}.\n\n- Updated main schema file\n- Recompiled raw and errors modules\n- Added new news entry for changelog"
164+
gh pr comment $PR_NUMBER --body "$PR_COMMENT"

0 commit comments

Comments
 (0)