-
Notifications
You must be signed in to change notification settings - Fork 2
257 lines (223 loc) · 10.9 KB
/
commit.yml
File metadata and controls
257 lines (223 loc) · 10.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
name: Add versioning tag to commit
concurrency: commit
on:
push:
branches:
- main
paths:
- web/**
- pingpong/**
- alembic/**
- saml/**
- uv.lock
- pyproject.toml
jobs:
tag:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 11 # Fetch the last 10 commits and the current one
fetch-tags: true
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@22103cc46bda19c2b464ffe86db46df6922fd323
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.x'
- name: Add versioning tag to commit
env:
CHANGED_FILES_STRING: ${{ steps.changed-files.outputs.all_changed_files }}
GH_TOKEN: ${{ github.token }}
run: |
set -e
python << 'EOF'
import json
import os
import re
import subprocess
import sys
LEGACY_TAG_PATTERN = re.compile(r'^(\d+)-srv(\d+)-web(\d+)$')
NEW_TAG_PATTERN = re.compile(r'^v(\d+)\+srv(\d+)\.web(\d+)$')
def is_versioning_tag(tag):
return bool(LEGACY_TAG_PATTERN.match(tag) or NEW_TAG_PATTERN.match(tag))
def get_last_tag(n=1):
if n > 10:
raise ValueError('No versioning tags found on the last 10 commits')
try:
# Get all tags pointing to the HEAD~n commit
tags = []
try:
# Get the shorthand Git SHA of HEAD~{n}
sha_output = subprocess.check_output(
['git', 'rev-parse', '--short', f'HEAD~{n}'],
stderr=subprocess.DEVNULL
)
sha = sha_output.decode().strip()
print(f"Shorthand Git SHA of HEAD~{n}: {sha}")
output = subprocess.check_output(
['git', 'tag', '--points-at', f'HEAD~{n}'],
stderr=subprocess.DEVNULL
)
decoded_output = output.decode().strip()
print("Raw Output:", decoded_output) # Print the raw decoded output
tags = decoded_output.split('\n') # Split the output into a list of tags
print("Tags List:", tags) # Print the list of tags
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e}")
# Filter tags that match our versioning format
matching_tags = [tag for tag in tags if is_versioning_tag(tag)]
print(f'Matching versioning tags on commit HEAD~{n}: {matching_tags}')
if not matching_tags:
print(f'No versioning tags found on commit HEAD~{n}')
return get_last_tag(n + 1)
print(f'Found versioning tags on commit HEAD~{n}: {matching_tags}')
# If multiple matching tags, return the one with the highest build number
return max(matching_tags, key=lambda t: parse_tag(t)['build'])
except subprocess.CalledProcessError as e:
raise ValueError("Failed to retrieve versioning tags from the previous commits", e)
def parse_tag(tag):
print(f'Parsing tag: {tag}')
match = LEGACY_TAG_PATTERN.match(tag)
if not match:
match = NEW_TAG_PATTERN.match(tag)
if not match:
raise ValueError(f"Invalid versioning tag format: {tag}. Expected NNNN-srvNNN-webNNN or vNNNN+srvNNN.webNNN")
try:
return {
'build': int(match.group(1)),
'srv': int(match.group(2)),
'web': int(match.group(3)),
}
except Exception as e:
raise ValueError(f"Failed to parse versioning tag: {tag}", e)
def create_new_tag(last_tag, web_update, server_update):
if last_tag:
last_tag_info = parse_tag(last_tag)
new_build = last_tag_info['build'] + 1
new_srv = last_tag_info['srv'] + (1 if server_update else 0)
new_web = last_tag_info['web'] + (1 if web_update else 0)
else:
new_build = 1
new_srv = 1
new_web = 1
return f"v{new_build}+srv{new_srv}.web{new_web}", new_srv, new_web
def get_compare_range():
default_range = 'HEAD~1..HEAD'
event_path = os.environ.get('GITHUB_EVENT_PATH')
if not event_path:
return default_range
try:
with open(event_path, encoding='utf-8') as f:
event = json.load(f)
except Exception as e:
print(f'Failed to parse GitHub event payload ({event_path}): {e}. Falling back to {default_range}')
return default_range
before = event.get('before')
after = event.get('after') or os.environ.get('GITHUB_SHA')
if not before or not after or before == '0' * 40:
return default_range
return f'{before}..{after}'
def get_changed_diff_lines(file_path, compare_range):
try:
patch = subprocess.check_output(
['git', 'diff', '--unified=0', compare_range, '--', file_path],
stderr=subprocess.DEVNULL,
).decode()
except subprocess.CalledProcessError:
return []
changed_lines = []
for line in patch.splitlines():
if line.startswith(('diff --git ', 'index ', '@@', '--- ', '+++ ')):
continue
if line.startswith(('+', '-')):
changed_lines.append(line[1:].strip())
return changed_lines
def is_version_only_change(file_path, compare_range):
patterns = {
'pyproject.toml': re.compile(r'^version\s*=\s*".*"$'),
'uv.lock': re.compile(r'^version\s*=\s*".*"$'),
'web/pingpong/package.json': re.compile(r'^"version"\s*:\s*".*"\s*,?$'),
}
pattern = patterns.get(file_path)
if not pattern:
return False
def uv_lock_has_pingpong_context():
package_name_pattern = re.compile(r'^name\s*=\s*"pingpong"\s*$')
try:
patch = subprocess.check_output(
['git', 'diff', '--unified=1', compare_range, '--', 'uv.lock'],
stderr=subprocess.DEVNULL,
).decode()
except subprocess.CalledProcessError:
return False
hunk_has_pingpong_name = False
hunk_has_version_change = False
for line in patch.splitlines():
if line.startswith(('diff --git ', 'index ', '--- ', '+++ ')):
continue
if line.startswith('@@'):
if hunk_has_pingpong_name and hunk_has_version_change:
return True
hunk_has_pingpong_name = False
hunk_has_version_change = False
continue
if not line or line.startswith('\\'):
continue
if line[0] not in {' ', '+', '-'}:
continue
content = line[1:].strip()
if package_name_pattern.match(content):
hunk_has_pingpong_name = True
if line[0] in {'+', '-'} and pattern.match(content):
hunk_has_version_change = True
return hunk_has_pingpong_name and hunk_has_version_change
changed_lines = get_changed_diff_lines(file_path, compare_range)
if not changed_lines:
return False
if file_path == 'uv.lock':
only_version = (
len(changed_lines) == 2
and all(pattern.match(line) for line in changed_lines)
and uv_lock_has_pingpong_context()
)
else:
only_version = all(pattern.match(line) for line in changed_lines)
print(f'Checking version-only change for {file_path} in {compare_range}: {only_version}')
return only_version
def detect_component_updates(changed_files, compare_range):
web_files = [file for file in changed_files if file.startswith('web/')]
server_files = [
file for file in changed_files
if file.startswith(('pingpong/', 'saml/', 'alembic/')) or file in ['uv.lock', 'pyproject.toml']
]
web_update = any(file != 'web/pingpong/package.json' for file in web_files)
if not web_update and 'web/pingpong/package.json' in web_files:
web_update = not is_version_only_change('web/pingpong/package.json', compare_range)
server_update = any(file not in {'pyproject.toml', 'uv.lock'} for file in server_files)
if not server_update and 'pyproject.toml' in server_files:
server_update = not is_version_only_change('pyproject.toml', compare_range)
if not server_update and 'uv.lock' in server_files:
server_update = not is_version_only_change('uv.lock', compare_range)
return web_update, server_update
try:
subprocess.run(['git', 'fetch', 'origin', '--tags', '--depth=11', 'HEAD'], check=True)
changed_files = [file for file in os.environ['CHANGED_FILES_STRING'].split() if file]
compare_range = get_compare_range()
web_update, server_update = detect_component_updates(changed_files, compare_range)
print(f'Changed files: {changed_files}')
print(f'Component updates -> server: {server_update}, web: {web_update}')
last_tag = get_last_tag()
new_tag, new_srv, new_web = create_new_tag(last_tag, web_update, server_update)
subprocess.run(['git', 'tag', new_tag], check=True)
subprocess.run(['git', 'push', 'origin', new_tag], check=True)
print(f"Successfully pushed new versioning tag: {new_tag}")
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)
EOF
shell: bash