Repository navigation
187 lines (156 loc) · 6.97 KB
/
Copy pathcheck_api_updates.yml
File metadata and controls
187 lines (156 loc) · 6.97 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
name: Check MTProto API Schema Updates
on:
schedule:
- cron: '0 */12 * * *'
workflow_dispatch:
inputs:
force_update:
description: 'Force update even if no changes detected'
required: false
default: false
type: boolean
jobs:
check-schema-updates:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
- name: Install dependencies
run: |
pip install requests
- name: Check for API schema updates
id: check-updates
run: |
# Allow the check script to exit with non-zero codes without failing the whole step
# (the script returns 0 when updates found, 2 when no updates, other codes indicate errors)
set +e
FORCE_UPDATE=""
if [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.force_update }}" == "true" ]]; then
FORCE_UPDATE="--force-update"
fi
python dev_tools/check_api_schema_updates.py $FORCE_UPDATE
EXIT_CODE=$?
# Re-enable "exit on error" for the remainder of the step
set -e
if [ $EXIT_CODE -eq 0 ]; then
echo "updates_detected=true" >> $GITHUB_OUTPUT
CURRENT_LAYER=$(grep -oP "// LAYER \K\d+" compiler/api/source/main_api.tl || echo "Unknown")
echo "layer_version=$CURRENT_LAYER" >> $GITHUB_OUTPUT
elif [ $EXIT_CODE -eq 2 ]; then
# No updates detected — set output and continue (do NOT fail the job)
echo "updates_detected=false" >> $GITHUB_OUTPUT
else
echo "Error checking for updates"
exit 1
fi
- name: Set up Git config
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
- name: Check existing PR and branch
if: steps.check-updates.outputs.updates_detected == 'true'
id: check-pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH_NAME="update/mtproto-api-updates"
# Fetch to check remote branches
git fetch origin
# Check if remote branch exists
if git ls-remote --heads origin $BRANCH_NAME | grep -q $BRANCH_NAME; then
echo "branch_exists=true" >> $GITHUB_OUTPUT
else
echo "branch_exists=false" >> $GITHUB_OUTPUT
fi
# Check if there's an open PR for this branch
PR_NUMBER=$(gh pr list --head "$BRANCH_NAME" --state open --json number -q '.[0].number' || echo "")
if [ -n "$PR_NUMBER" ]; then
echo "existing_pr=true" >> $GITHUB_OUTPUT
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
else
echo "existing_pr=false" >> $GITHUB_OUTPUT
fi
- name: Create API update news entry
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
NEWS_FILE="news/${TIMESTAMP}.misc.rst"
mkdir -p news
echo "API Layer Update" > $NEWS_FILE
echo "================" >> $NEWS_FILE
echo "" >> $NEWS_FILE
echo "Updated MTProto API schema to Layer $LAYER_VERSION." >> $NEWS_FILE
- name: Install package dependencies
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
pip install -e .
- name: Compile API
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
python -m compiler.api.compiler
- name: Compile errors
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
python -m compiler.errors.compiler
- name: Create or update branch and commit changes
if: steps.check-updates.outputs.updates_detected == 'true'
run: |
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
BRANCH_NAME="update/mtproto-api-updates"
BASE_BRANCH="${{ github.event.repository.default_branch }}"
# Create or reset branch based on default branch
git checkout -B $BRANCH_NAME origin/$BASE_BRANCH
# Add all changed and new files
git add -A compiler/api/source/main_api.tl
git add -A hydrogram/raw/
git add -A hydrogram/errors/
git add -A news/
# Check if there are changes to commit
if git diff --staged --quiet; then
echo "No changes to commit"
exit 1
fi
git commit -m "Update MTProto API schema to Layer ${LAYER_VERSION}"
# Force push to create or update the branch
git push --force --set-upstream origin $BRANCH_NAME
- name: Create Pull Request
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'false'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
BRANCH_NAME="update/mtproto-api-updates"
BASE_BRANCH="${{ github.event.repository.default_branch }}"
PR_TITLE="Update MTProto API schema"
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"
gh pr create \
--title "$PR_TITLE" \
--body "$PR_BODY" \
--base "$BASE_BRANCH" \
--head "$BRANCH_NAME" \
--label "enhancement" \
--label "automated"
- name: Update Pull Request Title and Description
if: steps.check-updates.outputs.updates_detected == 'true' && steps.check-pr.outputs.existing_pr == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
LAYER_VERSION="${{ steps.check-updates.outputs.layer_version }}"
PR_NUMBER="${{ steps.check-pr.outputs.pr_number }}"
PR_TITLE="Update MTProto API schema"
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"
gh pr edit $PR_NUMBER --title "$PR_TITLE" --body "$PR_BODY"
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"
gh pr comment $PR_NUMBER --body "$PR_COMMENT"