itw-python-builder 0.2.14__py3-none-any.whl → 0.2.16__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- itw_python_builder/cli.py +30 -2
- itw_python_builder/task_utils.py +103 -16
- itw_python_builder/tasks.py +261 -57
- itw_python_builder/templates/task_template.md +0 -17
- itw_python_builder/timesheet.py +134 -0
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/METADATA +38 -24
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/RECORD +11 -10
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/WHEEL +1 -1
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/entry_points.txt +0 -0
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/licenses/LICENSE +0 -0
- {itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/top_level.txt +0 -0
itw_python_builder/cli.py
CHANGED
|
@@ -1,10 +1,28 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import fcntl
|
|
3
|
+
import struct
|
|
4
|
+
import termios
|
|
1
5
|
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
|
2
6
|
|
|
3
|
-
from invoke import Argument, Collection, Program
|
|
7
|
+
from invoke import Argument, Collection, Program, runners, terminals
|
|
4
8
|
|
|
5
9
|
from itw_python_builder import tasks
|
|
6
10
|
|
|
7
11
|
|
|
12
|
+
# Fix for Python 3.14+: termios.FIONREAD expects a 4-byte (int) buffer.
|
|
13
|
+
# Invoke historically passed a 2-byte buffer (b" ") and unpacked with "h",
|
|
14
|
+
# which causes SystemError: buffer overflow on Python 3.14.
|
|
15
|
+
def _fixed_bytes_to_read(input_):
|
|
16
|
+
if not terminals.WINDOWS and terminals.isatty(input_) and terminals.has_fileno(input_):
|
|
17
|
+
fionread = fcntl.ioctl(input_, termios.FIONREAD, b" ")
|
|
18
|
+
return int(struct.unpack("i", fionread)[0])
|
|
19
|
+
return 1
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
terminals.bytes_to_read = _fixed_bytes_to_read
|
|
23
|
+
runners.bytes_to_read = _fixed_bytes_to_read
|
|
24
|
+
|
|
25
|
+
|
|
8
26
|
def _get_version() -> str:
|
|
9
27
|
try:
|
|
10
28
|
return _pkg_version('itw_python_builder')
|
|
@@ -29,7 +47,17 @@ class ItwProgram(Program):
|
|
|
29
47
|
return result
|
|
30
48
|
|
|
31
49
|
|
|
50
|
+
def normalize_cli_args(argv: list) -> list:
|
|
51
|
+
arguments = []
|
|
52
|
+
for index, argument in enumerate(argv):
|
|
53
|
+
if argument == '--':
|
|
54
|
+
arguments.extend(argv[index:])
|
|
55
|
+
break
|
|
56
|
+
arguments.append('--tm' if argument == '-tm' else argument)
|
|
57
|
+
return arguments
|
|
58
|
+
|
|
59
|
+
|
|
32
60
|
def main():
|
|
33
61
|
namespace = Collection.from_module(tasks)
|
|
34
62
|
program = ItwProgram(namespace=namespace, name='itw', version=_get_version())
|
|
35
|
-
program.run()
|
|
63
|
+
program.run(argv=normalize_cli_args(sys.argv))
|
itw_python_builder/task_utils.py
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import re
|
|
2
|
+
import unicodedata
|
|
2
3
|
from difflib import SequenceMatcher
|
|
4
|
+
from urllib.parse import quote
|
|
3
5
|
|
|
4
6
|
from itw_python_builder.utils import _gitlab_api_call
|
|
5
7
|
|
|
6
8
|
SIMILARITY_THRESHOLD = 0.95
|
|
9
|
+
GITLAB_PAGE_SIZE = 100
|
|
10
|
+
_RE_DIGITS = re.compile(r'\d+')
|
|
7
11
|
|
|
8
12
|
DATE_PLACEHOLDER = 'YYYY-MM-DD'
|
|
9
13
|
|
|
@@ -48,7 +52,12 @@ def log_info(msg: str) -> None:
|
|
|
48
52
|
|
|
49
53
|
|
|
50
54
|
def _normalize_title(value: str) -> str:
|
|
51
|
-
|
|
55
|
+
collapsed = ' '.join((value or '').lower().split())
|
|
56
|
+
return unicodedata.normalize('NFC', collapsed)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _digit_tokens(value: str) -> list:
|
|
60
|
+
return _RE_DIGITS.findall(value or '')
|
|
52
61
|
|
|
53
62
|
|
|
54
63
|
def is_similar(left: str, right: str, threshold: float = SIMILARITY_THRESHOLD) -> bool:
|
|
@@ -59,19 +68,73 @@ def is_similar(left: str, right: str, threshold: float = SIMILARITY_THRESHOLD) -
|
|
|
59
68
|
return False
|
|
60
69
|
if a == b:
|
|
61
70
|
return True
|
|
71
|
+
if _digit_tokens(a) != _digit_tokens(b):
|
|
72
|
+
return False
|
|
62
73
|
return SequenceMatcher(None, a, b).ratio() >= threshold
|
|
63
74
|
|
|
64
75
|
|
|
65
76
|
def find_similar(value: str, items: list, key: str = 'title', threshold: float = SIMILARITY_THRESHOLD):
|
|
66
|
-
"""Return
|
|
77
|
+
"""Return an exact normalized match if one exists, else the first ~threshold match."""
|
|
78
|
+
target = _normalize_title(value)
|
|
79
|
+
fuzzy = None
|
|
67
80
|
for item in items:
|
|
68
|
-
|
|
81
|
+
other = item.get(key, '')
|
|
82
|
+
if target and target == _normalize_title(other):
|
|
69
83
|
return item
|
|
70
|
-
|
|
84
|
+
if fuzzy is None and is_similar(value, other, threshold):
|
|
85
|
+
fuzzy = item
|
|
86
|
+
return fuzzy
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _gitlab_api_list(api_host: str, endpoint: str, token: str) -> list:
|
|
90
|
+
"""GET a GitLab collection, following pages until a short page is returned."""
|
|
91
|
+
joiner = '&' if '?' in endpoint else '?'
|
|
92
|
+
page = 1
|
|
93
|
+
collected = []
|
|
94
|
+
while True:
|
|
95
|
+
chunk = _gitlab_api_call(
|
|
96
|
+
api_host,
|
|
97
|
+
f'{endpoint}{joiner}per_page={GITLAB_PAGE_SIZE}&page={page}',
|
|
98
|
+
token,
|
|
99
|
+
)
|
|
100
|
+
if not isinstance(chunk, list):
|
|
101
|
+
raise RuntimeError(
|
|
102
|
+
f'GitLab API GET {endpoint} expected a list, got {type(chunk).__name__}'
|
|
103
|
+
)
|
|
104
|
+
collected.extend(chunk)
|
|
105
|
+
if len(chunk) < GITLAB_PAGE_SIZE:
|
|
106
|
+
break
|
|
107
|
+
page += 1
|
|
108
|
+
return collected
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _issue_milestone_title(issue: dict) -> str:
|
|
112
|
+
milestone = issue.get('milestone') or {}
|
|
113
|
+
if isinstance(milestone, dict):
|
|
114
|
+
return (milestone.get('title') or '').strip()
|
|
115
|
+
return ''
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def is_duplicate_issue(issue: dict, title: str, milestone: str = None, assignee: str = None) -> bool:
|
|
119
|
+
"""True when similar titles have the same milestone and assignees."""
|
|
120
|
+
if not is_similar(title, issue.get('title', '')):
|
|
121
|
+
return False
|
|
122
|
+
existing = _issue_milestone_title(issue)
|
|
123
|
+
if milestone and not is_similar(milestone, existing):
|
|
124
|
+
return False
|
|
125
|
+
if not milestone and existing:
|
|
126
|
+
return False
|
|
127
|
+
expected_assignees = {
|
|
128
|
+
username.lstrip('@').lower()
|
|
129
|
+
for username in re.split(r'[\s,]+', (assignee or '').strip()) if username
|
|
130
|
+
}
|
|
131
|
+
existing_assignees = {
|
|
132
|
+
assigned['username'].lower() for assigned in (issue.get('assignees') or [])
|
|
133
|
+
}
|
|
134
|
+
return expected_assignees == existing_assignees
|
|
71
135
|
|
|
72
136
|
|
|
73
137
|
def _list_group_projects(api_host: str, group_path: str, token: str) -> list:
|
|
74
|
-
from urllib.parse import quote
|
|
75
138
|
encoded = quote(group_path, safe='')
|
|
76
139
|
return _gitlab_api_call(
|
|
77
140
|
api_host,
|
|
@@ -113,27 +176,51 @@ def create_gitlab_issue(api_host: str, project_id: int, payload: dict, token: st
|
|
|
113
176
|
|
|
114
177
|
|
|
115
178
|
def get_project_by_path(api_host: str, project_path: str, token: str) -> dict:
|
|
116
|
-
from urllib.parse import quote
|
|
117
179
|
encoded = quote(project_path, safe='')
|
|
118
180
|
return _gitlab_api_call(api_host, f'/projects/{encoded}', token)
|
|
119
181
|
|
|
120
182
|
|
|
183
|
+
def list_project_milestones(api_host: str, project_id: int, token: str) -> list:
|
|
184
|
+
return _gitlab_api_list(api_host, f'/projects/{project_id}/milestones?include_ancestors=true&state=active', token)
|
|
185
|
+
|
|
186
|
+
|
|
121
187
|
def find_project_milestone(api_host: str, project_id: int, title: str, token: str):
|
|
122
|
-
|
|
188
|
+
"""Find a project or ancestor-group milestone by title."""
|
|
189
|
+
encoded = quote(title, safe='')
|
|
190
|
+
base = f'/projects/{project_id}/milestones?include_ancestors=true'
|
|
191
|
+
exact = _gitlab_api_list(api_host, f'{base}&title={encoded}', token)
|
|
192
|
+
match = find_similar(title, exact)
|
|
193
|
+
if match:
|
|
194
|
+
return match
|
|
195
|
+
searched = _gitlab_api_list(api_host, f'{base}&search={encoded}', token)
|
|
196
|
+
return find_similar(title, searched)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _list_issues_for_milestone(api_host: str, project_id: int, milestone: str, token: str) -> list:
|
|
200
|
+
encoded = quote(milestone, safe='') if milestone else 'None'
|
|
201
|
+
return _gitlab_api_list(
|
|
123
202
|
api_host,
|
|
124
|
-
f'/projects/{project_id}/
|
|
203
|
+
f'/projects/{project_id}/issues?milestone={encoded}',
|
|
125
204
|
token,
|
|
126
205
|
)
|
|
127
|
-
return find_similar(title, results)
|
|
128
206
|
|
|
129
207
|
|
|
130
|
-
def find_project_issue(api_host: str, project_id: int, title: str, token: str, milestone: str = None):
|
|
131
|
-
"""Look for a
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
208
|
+
def find_project_issue(api_host: str, project_id: int, title: str, token: str, milestone: str = None, assignee: str = None):
|
|
209
|
+
"""Look for a similar title with the same milestone and assignees."""
|
|
210
|
+
encoded = quote(title, safe='')
|
|
211
|
+
searched = _gitlab_api_list(
|
|
212
|
+
api_host,
|
|
213
|
+
f'/projects/{project_id}/issues?search={encoded}&in=title',
|
|
214
|
+
token,
|
|
215
|
+
)
|
|
216
|
+
for item in searched:
|
|
217
|
+
if is_duplicate_issue(item, title, milestone, assignee):
|
|
218
|
+
return item
|
|
219
|
+
|
|
220
|
+
for item in _list_issues_for_milestone(api_host, project_id, milestone, token):
|
|
221
|
+
if is_duplicate_issue(item, title, milestone, assignee):
|
|
222
|
+
return item
|
|
223
|
+
return None
|
|
137
224
|
|
|
138
225
|
|
|
139
226
|
def create_project_milestone(
|
itw_python_builder/tasks.py
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import getpass
|
|
2
2
|
import io
|
|
3
|
+
import os
|
|
4
|
+
import re
|
|
5
|
+
import shlex
|
|
3
6
|
import shutil
|
|
4
7
|
import sys
|
|
5
8
|
import tempfile
|
|
6
9
|
from datetime import datetime
|
|
7
10
|
|
|
8
11
|
from itw_python_builder.ssr_tasks import * # noqa: F401,F403 — exposes ssr-init
|
|
12
|
+
from itw_python_builder.ssr_tasks import _read_angular_json
|
|
9
13
|
from itw_python_builder.utils import (
|
|
10
14
|
detect_and_activate_venv,
|
|
11
15
|
terminate_stale_test_db_sessions,
|
|
@@ -39,6 +43,7 @@ from itw_python_builder.task_utils import (
|
|
|
39
43
|
extract_glab_credentials,
|
|
40
44
|
build_issue_description,
|
|
41
45
|
get_project_by_path,
|
|
46
|
+
list_project_milestones,
|
|
42
47
|
find_project_milestone,
|
|
43
48
|
find_project_issue,
|
|
44
49
|
create_project_milestone,
|
|
@@ -52,6 +57,7 @@ from itw_python_builder.task_utils import (
|
|
|
52
57
|
find_similar,
|
|
53
58
|
)
|
|
54
59
|
from itw_python_builder.version import Version
|
|
60
|
+
from itw_python_builder.timesheet import validate_timesheet_option, resolve_timesheet_merge_request, report_timesheet_release
|
|
55
61
|
|
|
56
62
|
PYLINTRC = Path(__file__).parent / ".pylintrc"
|
|
57
63
|
TASK_TEMPLATE_PATH = Path(__file__).parent / "templates" / "task_template.md"
|
|
@@ -133,12 +139,27 @@ def logout(ctx: Context):
|
|
|
133
139
|
|
|
134
140
|
@task(name='task-init')
|
|
135
141
|
def task_init(ctx: Context) -> None:
|
|
136
|
-
"""Create a TASK.md template
|
|
142
|
+
"""Create a TASK.md template, selecting the _pm repo and milestone when inside git."""
|
|
137
143
|
dest = os.path.join(os.getcwd(), 'TASK.md')
|
|
138
144
|
if os.path.exists(dest):
|
|
139
145
|
log_red('TASK.md already exists in this directory — refusing to overwrite.')
|
|
140
146
|
sys.exit(1)
|
|
141
|
-
|
|
147
|
+
content = TASK_TEMPLATE_PATH.read_text(encoding='utf-8')
|
|
148
|
+
if os.path.exists(os.path.join(os.getcwd(), '.git')):
|
|
149
|
+
host, project_path = _parse_gitlab_remote(ctx)
|
|
150
|
+
api_host = _GITLAB_API_HOST_MAP.get(host, host)
|
|
151
|
+
token = ensure_gitlab_token(ctx)
|
|
152
|
+
project = _resolve_default_project(api_host, project_path, token)
|
|
153
|
+
content = content.replace('/repo ', f'/repo {project["path_with_namespace"]}')
|
|
154
|
+
milestones = list_project_milestones(api_host, project['id'], token)
|
|
155
|
+
milestone = select_task_option(milestones, 'milestone', 'title')
|
|
156
|
+
if milestone:
|
|
157
|
+
content = content.replace('/milestone ', f'/milestone {milestone["title"]}')
|
|
158
|
+
if milestone.get('start_date'):
|
|
159
|
+
content = content.replace('/milestone-start YYYY-MM-DD', f'/milestone-start {milestone["start_date"]}')
|
|
160
|
+
if milestone.get('due_date'):
|
|
161
|
+
content = content.replace('/milestone-end YYYY-MM-DD', f'/milestone-end {milestone["due_date"]}')
|
|
162
|
+
Path(dest).write_text(content, encoding='utf-8')
|
|
142
163
|
log_green(f'Created TASK.md in {os.getcwd()}')
|
|
143
164
|
|
|
144
165
|
|
|
@@ -182,11 +203,20 @@ def create_task(ctx: Context, file=None):
|
|
|
182
203
|
log_red(str(exc))
|
|
183
204
|
sys.exit(1)
|
|
184
205
|
print(f'[itw] Parsed {len(issues)} issue(s) from {file}.')
|
|
206
|
+
_apply_file_repo(issues)
|
|
207
|
+
|
|
208
|
+
default_project = None
|
|
209
|
+
if any(not issue['repo'] for issue in issues):
|
|
210
|
+
try:
|
|
211
|
+
default_project = _resolve_default_project(api_host, project_path, token)
|
|
212
|
+
except Exception as exc:
|
|
213
|
+
log_red(f'Failed to resolve the file repo: {exc}')
|
|
214
|
+
sys.exit(1)
|
|
185
215
|
|
|
186
216
|
for idx, issue in enumerate(issues, start=1):
|
|
187
217
|
print(f'\n[itw] Issue {idx}/{len(issues)}: {issue["title"]}')
|
|
188
218
|
try:
|
|
189
|
-
project = _resolve_target_project(api_host,
|
|
219
|
+
project = _resolve_target_project(api_host, issue, token, default_project)
|
|
190
220
|
|
|
191
221
|
if issue['has_acceptance_criteria']:
|
|
192
222
|
log_info('Using inputed acceptance criteria')
|
|
@@ -211,13 +241,13 @@ def create_task(ctx: Context, file=None):
|
|
|
211
241
|
milestone_id, milestone_title = _resolve_milestone(api_host, project['id'], issue, token)
|
|
212
242
|
|
|
213
243
|
duplicate = find_project_issue(
|
|
214
|
-
api_host, project['id'], issue['title'], token, milestone_title
|
|
244
|
+
api_host, project['id'], issue['title'], token, milestone_title, issue['assignee']
|
|
215
245
|
)
|
|
216
246
|
if duplicate:
|
|
217
247
|
scope = f'milestone {milestone_title}' if milestone_title else 'issues without a milestone'
|
|
218
248
|
log_white(
|
|
219
249
|
f'Issue "{issue["title"]}" was not created because it already exists '
|
|
220
|
-
f'in this project for {scope}: {duplicate["web_url"]}'
|
|
250
|
+
f'in this project for {scope} with the same assignee: {duplicate["web_url"]}'
|
|
221
251
|
)
|
|
222
252
|
continue
|
|
223
253
|
|
|
@@ -247,11 +277,20 @@ def create_task(ctx: Context, file=None):
|
|
|
247
277
|
sys.exit(1)
|
|
248
278
|
|
|
249
279
|
|
|
250
|
-
def
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
280
|
+
def _apply_file_repo(issues: list) -> None:
|
|
281
|
+
"""Fill empty /repo values from the first /repo in the file."""
|
|
282
|
+
file_repo = None
|
|
283
|
+
for issue in issues:
|
|
284
|
+
if issue['repo']:
|
|
285
|
+
if file_repo is None:
|
|
286
|
+
file_repo = issue['repo']
|
|
287
|
+
continue
|
|
288
|
+
if file_repo is not None:
|
|
289
|
+
issue['repo'] = file_repo
|
|
254
290
|
|
|
291
|
+
|
|
292
|
+
def _resolve_default_project(api_host: str, project_path: str, token: str) -> dict:
|
|
293
|
+
"""Pick the file-level _pm repo used by every issue that omits /repo."""
|
|
255
294
|
if not project_path:
|
|
256
295
|
raise RuntimeError(
|
|
257
296
|
'No /repo defined and no .git directory to auto-detect from. '
|
|
@@ -263,29 +302,45 @@ def _resolve_target_project(api_host: str, project_path: str, issue: dict, token
|
|
|
263
302
|
raise RuntimeError(
|
|
264
303
|
f'No _pm repo found anywhere in the namespace of {project_path!r}.'
|
|
265
304
|
)
|
|
305
|
+
return select_task_option(candidates, '_pm repo', 'path_with_namespace')
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def select_task_option(candidates: list, label: str, key: str):
|
|
309
|
+
"""Use the only option, or prompt when several are available."""
|
|
310
|
+
if not candidates:
|
|
311
|
+
return None
|
|
266
312
|
if len(candidates) == 1:
|
|
267
313
|
chosen = candidates[0]
|
|
268
|
-
print(f'[itw] Using
|
|
314
|
+
print(f'[itw] Using {label}: {chosen[key]}')
|
|
269
315
|
return chosen
|
|
270
316
|
if not sys.stdin.isatty():
|
|
271
|
-
names = ', '.join(
|
|
317
|
+
names = ', '.join(candidate[key] for candidate in candidates)
|
|
272
318
|
raise RuntimeError(
|
|
273
|
-
f'{len(candidates)}
|
|
319
|
+
f'{len(candidates)} {label}s found ({names}) and stdin is not a TTY — cannot prompt.'
|
|
274
320
|
)
|
|
275
|
-
print(f'[itw] Found {len(candidates)}
|
|
276
|
-
for
|
|
277
|
-
print(f' [{
|
|
321
|
+
print(f'[itw] Found {len(candidates)} {label}s:')
|
|
322
|
+
for index, candidate in enumerate(candidates, start=1):
|
|
323
|
+
print(f' [{index}] {candidate[key]}')
|
|
278
324
|
while True:
|
|
279
325
|
raw = input(f'Pick one (1-{len(candidates)}): ').strip()
|
|
280
326
|
try:
|
|
281
327
|
idx = int(raw)
|
|
282
328
|
if 1 <= idx <= len(candidates):
|
|
283
|
-
|
|
329
|
+
chosen = candidates[idx - 1]
|
|
330
|
+
print(f'[itw] Using {label}: {chosen[key]}')
|
|
331
|
+
return chosen
|
|
284
332
|
except ValueError:
|
|
285
333
|
pass
|
|
286
334
|
print('Invalid choice, try again.')
|
|
287
335
|
|
|
288
336
|
|
|
337
|
+
def _resolve_target_project(api_host: str, issue: dict, token: str, default_project: dict) -> dict:
|
|
338
|
+
if issue['repo']:
|
|
339
|
+
log_info(f'Using {issue["repo"]} repo')
|
|
340
|
+
return get_project_by_path(api_host, issue['repo'], token)
|
|
341
|
+
return default_project
|
|
342
|
+
|
|
343
|
+
|
|
289
344
|
def _resolve_labels(api_host: str, project_id: int, issue: dict, token: str) -> list:
|
|
290
345
|
if not issue['labels']:
|
|
291
346
|
return []
|
|
@@ -347,11 +402,109 @@ def build_frontend(ctx: Context, branch: str, ssr: bool = False) -> None:
|
|
|
347
402
|
ctx.run(f'npm run {script}')
|
|
348
403
|
|
|
349
404
|
|
|
350
|
-
def
|
|
351
|
-
"""
|
|
405
|
+
def prod_env_name(filename: str) -> str:
|
|
406
|
+
"""Read the environment name from a production environment filename."""
|
|
407
|
+
match = re.match(r'^environment(?:\.(.+))?\.prod\.ts$', filename)
|
|
408
|
+
if not match:
|
|
409
|
+
return filename
|
|
410
|
+
return match.group(1) or 'production'
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def list_prod_env_files() -> list:
|
|
414
|
+
"""List production environment files in the frontend project."""
|
|
415
|
+
env_dir = Path('src') / 'environments'
|
|
416
|
+
if not env_dir.is_dir():
|
|
417
|
+
return []
|
|
418
|
+
return sorted(path for path in env_dir.glob('*.prod.ts') if path.is_file())
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def angular_build_for_env(env_path: Path) -> tuple:
|
|
422
|
+
"""Find the Angular project and build configuration for an environment file."""
|
|
423
|
+
angular_json = _read_angular_json()
|
|
424
|
+
matches = []
|
|
425
|
+
for project_name, project in (angular_json.get('projects') or {}).items():
|
|
426
|
+
build = (project.get('architect') or {}).get('build') or {}
|
|
427
|
+
for config_name, config in (build.get('configurations') or {}).items():
|
|
428
|
+
for replacement in config.get('fileReplacements') or []:
|
|
429
|
+
target = str(replacement.get('with') or '').replace('\\', '/')
|
|
430
|
+
if os.path.basename(target) == env_path.name:
|
|
431
|
+
matches.append((project_name, config_name))
|
|
432
|
+
if not matches:
|
|
433
|
+
rel = env_path.as_posix()
|
|
434
|
+
raise RuntimeError(
|
|
435
|
+
f'No Angular build configuration replaces environment.ts with {rel}. '
|
|
436
|
+
'Add a fileReplacements entry in angular.json.'
|
|
437
|
+
)
|
|
438
|
+
if len(matches) > 1:
|
|
439
|
+
listed = ', '.join(f'{project}:{config}' for project, config in matches)
|
|
440
|
+
raise RuntimeError(
|
|
441
|
+
f'{env_path.name} is used by more than one build configuration: {listed}.'
|
|
442
|
+
)
|
|
443
|
+
return matches[0]
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
def select_prod_environments(branch: str, ssr: bool):
|
|
447
|
+
"""Choose which production environments to build."""
|
|
448
|
+
if ssr or branch != 'master':
|
|
449
|
+
return None
|
|
450
|
+
files = list_prod_env_files()
|
|
451
|
+
if len(files) <= 1:
|
|
452
|
+
return None
|
|
453
|
+
options = []
|
|
454
|
+
for path in files:
|
|
455
|
+
project, configuration = angular_build_for_env(path)
|
|
456
|
+
options.append({
|
|
457
|
+
'name': prod_env_name(path.name),
|
|
458
|
+
'project': project,
|
|
459
|
+
'configuration': configuration,
|
|
460
|
+
})
|
|
461
|
+
if not sys.stdin.isatty():
|
|
462
|
+
names = ', '.join(option['name'] for option in options)
|
|
463
|
+
raise RuntimeError(
|
|
464
|
+
f'{len(options)} production environments found ({names}) and stdin is not a TTY — cannot prompt.'
|
|
465
|
+
)
|
|
466
|
+
print(f'[itw] Found {len(options)} production environments:')
|
|
467
|
+
for index, option in enumerate(options, start=1):
|
|
468
|
+
print(f' [{index}] {option["name"]}')
|
|
469
|
+
all_index = len(options) + 1
|
|
470
|
+
print(f' [{all_index}] all')
|
|
471
|
+
while True:
|
|
472
|
+
raw = input(f'Pick one (1-{all_index}): ').strip()
|
|
473
|
+
try:
|
|
474
|
+
choice = int(raw)
|
|
475
|
+
except ValueError:
|
|
476
|
+
print('Invalid choice, try again.')
|
|
477
|
+
continue
|
|
478
|
+
if 1 <= choice <= len(options):
|
|
479
|
+
return [options[choice - 1]]
|
|
480
|
+
if choice == all_index:
|
|
481
|
+
return options
|
|
482
|
+
print('Invalid choice, try again.')
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
def build_frontend_configuration(ctx: Context, env: dict) -> None:
|
|
486
|
+
"""Build the frontend with one environment configuration."""
|
|
487
|
+
ng = os.path.join('node_modules', '.bin', 'ng')
|
|
488
|
+
if not os.path.isfile(ng):
|
|
489
|
+
raise RuntimeError('Angular CLI not found at node_modules/.bin/ng. Run npm install first.')
|
|
490
|
+
project = shlex.quote(env['project'])
|
|
491
|
+
configuration = shlex.quote(env['configuration'])
|
|
492
|
+
print(f'[itw] Building {env["name"]} with configuration {env["configuration"]}')
|
|
493
|
+
ctx.run(f'{ng} build {project} --configuration={configuration}')
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+
def dist_archive_name(environment: str = None) -> str:
|
|
497
|
+
"""Return the frontend release archive name."""
|
|
498
|
+
if environment:
|
|
499
|
+
return f'dist-{environment}.tar.gz'
|
|
500
|
+
return 'dist.tar.gz'
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
def package_dist(ctx: Context, archive_name: str = 'dist.tar.gz') -> None:
|
|
504
|
+
"""Pack the frontend build into an archive."""
|
|
352
505
|
if not os.path.isdir(os.path.join(os.getcwd(), 'dist')):
|
|
353
506
|
raise RuntimeError('No dist/ directory found after build — did `ng build` succeed?')
|
|
354
|
-
ctx.run('tar -czf
|
|
507
|
+
ctx.run(f'tar -czf {shlex.quote(archive_name)} -C dist .')
|
|
355
508
|
|
|
356
509
|
|
|
357
510
|
_GITLAB_API_HOST_MAP = {
|
|
@@ -359,26 +512,29 @@ _GITLAB_API_HOST_MAP = {
|
|
|
359
512
|
}
|
|
360
513
|
|
|
361
514
|
|
|
362
|
-
def upload_dist(ctx: Context, version) -> None:
|
|
363
|
-
"""Upload
|
|
515
|
+
def upload_dist(ctx: Context, version, archive_name: str = 'dist.tar.gz') -> None:
|
|
516
|
+
"""Upload the frontend archive to the package registry."""
|
|
364
517
|
from urllib.parse import quote
|
|
365
518
|
if not os.environ.get('GITLAB_TOKEN'):
|
|
366
519
|
raise RuntimeError('GITLAB_TOKEN not set; run `itw login` first.')
|
|
367
520
|
host, path = _parse_gitlab_remote(ctx)
|
|
368
521
|
host = _GITLAB_API_HOST_MAP.get(host, host)
|
|
369
522
|
encoded = quote(path, safe='')
|
|
370
|
-
url =
|
|
371
|
-
|
|
523
|
+
url = (
|
|
524
|
+
f'https://{host}/api/v4/projects/{encoded}/packages/generic/frontend/'
|
|
525
|
+
f'{version}/{archive_name}'
|
|
526
|
+
)
|
|
527
|
+
print(f'[itw] Uploading {archive_name} → {url}')
|
|
372
528
|
ctx.run(
|
|
373
529
|
'curl --fail --silent --show-error --insecure '
|
|
374
530
|
'--header "PRIVATE-TOKEN: $GITLAB_TOKEN" '
|
|
375
|
-
'--upload-file
|
|
531
|
+
f'--upload-file {shlex.quote(archive_name)} '
|
|
376
532
|
f'"{url}"'
|
|
377
533
|
)
|
|
378
534
|
print('[itw] Upload complete.')
|
|
379
535
|
|
|
380
536
|
|
|
381
|
-
def commit_version(ctx: Context, version: Version) -> None:
|
|
537
|
+
def commit_version(ctx: Context, version: Version, environment: str = None) -> None:
|
|
382
538
|
if detect_project_type() == 'frontend':
|
|
383
539
|
ctx.run('git add package.json')
|
|
384
540
|
else:
|
|
@@ -386,7 +542,10 @@ def commit_version(ctx: Context, version: Version) -> None:
|
|
|
386
542
|
ctx.run('git add CHANGELOG.md')
|
|
387
543
|
if gitignore_touched_this_run():
|
|
388
544
|
ctx.run('git add .gitignore')
|
|
389
|
-
|
|
545
|
+
message = f'version update to {version}'
|
|
546
|
+
if environment:
|
|
547
|
+
message = f'{message} ({environment})'
|
|
548
|
+
ctx.run(f'git commit -m {shlex.quote(message)}')
|
|
390
549
|
|
|
391
550
|
|
|
392
551
|
def push_version(ctx: Context) -> None:
|
|
@@ -468,37 +627,69 @@ def pushimage(ctx: Context):
|
|
|
468
627
|
ctx.run(f'podman push {container_registry_path} --tls-verify=false --compress --compression-level=9')
|
|
469
628
|
|
|
470
629
|
|
|
471
|
-
def
|
|
630
|
+
def package_and_upload(ctx: Context, version: Version, environment: str = None) -> None:
|
|
631
|
+
"""Package the frontend build and upload it."""
|
|
632
|
+
archive_name = dist_archive_name(environment)
|
|
633
|
+
step('package', package_dist, ctx, archive_name)
|
|
634
|
+
step('upload', upload_dist, ctx, version, archive_name)
|
|
635
|
+
if safe_remove(archive_name):
|
|
636
|
+
print(f'[itw] Removed local artifact {archive_name} after upload.')
|
|
637
|
+
|
|
638
|
+
|
|
639
|
+
def tag_build_push(ctx: Context, version: Version, skip_pipeline: bool = False, ssr: bool = False, tm=None) -> str:
|
|
640
|
+
"""Build the project and publish the version."""
|
|
472
641
|
install_sigint_guard()
|
|
473
642
|
project_type = detect_project_type()
|
|
643
|
+
merge_request = None
|
|
644
|
+
if tm is not None:
|
|
645
|
+
host, project_path = _parse_gitlab_remote(ctx)
|
|
646
|
+
api_host = _GITLAB_API_HOST_MAP.get(host, host)
|
|
647
|
+
merge_request = resolve_timesheet_merge_request(api_host, project_path, os.environ['GITLAB_TOKEN'], tm)
|
|
648
|
+
env_label = None
|
|
474
649
|
if not skip_pipeline:
|
|
475
650
|
step('tests', test, ctx, warn=False)
|
|
476
651
|
step('analyze', analyze, ctx)
|
|
652
|
+
if merge_request is not None:
|
|
653
|
+
step('timesheet', report_timesheet_release, version, merge_request)
|
|
477
654
|
step('tag', tag, ctx, version)
|
|
478
655
|
if project_type == 'backend':
|
|
479
656
|
step('build image', buildimage, ctx)
|
|
480
657
|
step('push image', pushimage, ctx)
|
|
481
658
|
else:
|
|
482
659
|
branch = get_current_branch(ctx)
|
|
483
|
-
|
|
484
|
-
|
|
660
|
+
targets = select_prod_environments(branch, ssr)
|
|
661
|
+
archive_names = ['dist.tar.gz']
|
|
662
|
+
if targets:
|
|
663
|
+
archive_names = [dist_archive_name(env['name']) for env in targets]
|
|
664
|
+
for archive_name in archive_names:
|
|
665
|
+
if ensure_gitignored(archive_name):
|
|
666
|
+
print(f'[itw] Added "{archive_name}" to .gitignore.')
|
|
485
667
|
pkg_path = Path(os.getcwd()) / 'package.json'
|
|
486
668
|
original_pkg = pkg_path.read_text(encoding='utf-8')
|
|
487
669
|
save_version(version)
|
|
488
670
|
print(f'[itw] Bumped package.json to {version.bare_semver()} before build.')
|
|
489
671
|
try:
|
|
490
|
-
|
|
672
|
+
if not targets:
|
|
673
|
+
build_frontend(ctx, branch, ssr=ssr)
|
|
674
|
+
else:
|
|
675
|
+
for env in targets:
|
|
676
|
+
if os.path.isdir('dist'):
|
|
677
|
+
shutil.rmtree('dist')
|
|
678
|
+
build_frontend_configuration(ctx, env)
|
|
679
|
+
abort_if_interrupted('build')
|
|
680
|
+
package_and_upload(ctx, version, env['name'])
|
|
491
681
|
except BaseException:
|
|
492
682
|
pkg_path.write_text(original_pkg, encoding='utf-8')
|
|
493
683
|
print('[itw] Build failed — reverted package.json to previous version.')
|
|
494
684
|
raise
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
685
|
+
if not targets:
|
|
686
|
+
abort_if_interrupted('build')
|
|
687
|
+
package_and_upload(ctx, version)
|
|
688
|
+
else:
|
|
689
|
+
env_label = ', '.join(env['name'] for env in targets)
|
|
500
690
|
step('changelog', changelog, ctx, version)
|
|
501
691
|
step('push', push, ctx)
|
|
692
|
+
return env_label
|
|
502
693
|
|
|
503
694
|
|
|
504
695
|
@task(name='tag-init')
|
|
@@ -529,9 +720,10 @@ def _ensure_unique_tag(ctx: Context, version: Version) -> None:
|
|
|
529
720
|
version.increment_release_candidate()
|
|
530
721
|
|
|
531
722
|
|
|
532
|
-
@task
|
|
533
|
-
def incrementrc(ctx: Context, skip_pipeline=False, pylintrc=None, ssr=False) -> None:
|
|
723
|
+
@task(optional=['tm'])
|
|
724
|
+
def incrementrc(ctx: Context, skip_pipeline=False, pylintrc=None, ssr=False, tm=None) -> None:
|
|
534
725
|
"""Increment release candidate version and deploy. Use --ssr for Angular SSR builds."""
|
|
726
|
+
tm = validate_timesheet_option(tm)
|
|
535
727
|
check_branch(ctx, required='staging')
|
|
536
728
|
ensure_gitlab_auth(ctx)
|
|
537
729
|
project_type = detect_project_type()
|
|
@@ -540,16 +732,17 @@ def incrementrc(ctx: Context, skip_pipeline=False, pylintrc=None, ssr=False) ->
|
|
|
540
732
|
_ensure_unique_tag(ctx, version)
|
|
541
733
|
if project_type == 'backend' and not skip_pipeline:
|
|
542
734
|
lint(ctx, pylintrc=pylintrc)
|
|
543
|
-
tag_build_push(ctx, version, skip_pipeline, ssr=ssr)
|
|
735
|
+
env_label = tag_build_push(ctx, version, skip_pipeline, ssr=ssr, tm=tm)
|
|
544
736
|
save_version(version)
|
|
545
|
-
commit_version(ctx, version)
|
|
737
|
+
commit_version(ctx, version, environment=env_label)
|
|
546
738
|
push_version(ctx)
|
|
547
739
|
propagate_changelog(ctx)
|
|
548
740
|
|
|
549
741
|
|
|
550
|
-
@task
|
|
551
|
-
def incrementpatch(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
742
|
+
@task(optional=['tm'])
|
|
743
|
+
def incrementpatch(ctx: Context, skip_pipeline=False, ssr=False, tm=None) -> None:
|
|
552
744
|
"""Increment patch version, build, and deploy. Use --ssr for Angular SSR builds."""
|
|
745
|
+
tm = validate_timesheet_option(tm)
|
|
553
746
|
production = check_branch(ctx)
|
|
554
747
|
ensure_gitlab_auth(ctx)
|
|
555
748
|
version = read_version(ctx)
|
|
@@ -557,16 +750,17 @@ def incrementpatch(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
|
557
750
|
if production and detect_project_type() == 'frontend':
|
|
558
751
|
version = maybe_override_release_version(version)
|
|
559
752
|
_ensure_unique_tag(ctx, version)
|
|
560
|
-
tag_build_push(ctx, version, skip_pipeline, ssr=ssr)
|
|
753
|
+
env_label = tag_build_push(ctx, version, skip_pipeline, ssr=ssr, tm=tm)
|
|
561
754
|
save_version(version)
|
|
562
|
-
commit_version(ctx, version)
|
|
755
|
+
commit_version(ctx, version, environment=env_label)
|
|
563
756
|
push_version(ctx)
|
|
564
757
|
propagate_changelog(ctx)
|
|
565
758
|
|
|
566
759
|
|
|
567
|
-
@task
|
|
568
|
-
def incrementminor(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
760
|
+
@task(optional=['tm'])
|
|
761
|
+
def incrementminor(ctx: Context, skip_pipeline=False, ssr=False, tm=None) -> None:
|
|
569
762
|
"""Increment minor version, build, and deploy. Use --ssr for Angular SSR builds."""
|
|
763
|
+
tm = validate_timesheet_option(tm)
|
|
570
764
|
production = check_branch(ctx)
|
|
571
765
|
ensure_gitlab_auth(ctx)
|
|
572
766
|
version = read_version(ctx)
|
|
@@ -574,16 +768,17 @@ def incrementminor(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
|
574
768
|
if production and detect_project_type() == 'frontend':
|
|
575
769
|
version = maybe_override_release_version(version)
|
|
576
770
|
_ensure_unique_tag(ctx, version)
|
|
577
|
-
tag_build_push(ctx, version, skip_pipeline, ssr=ssr)
|
|
771
|
+
env_label = tag_build_push(ctx, version, skip_pipeline, ssr=ssr, tm=tm)
|
|
578
772
|
save_version(version)
|
|
579
|
-
commit_version(ctx, version)
|
|
773
|
+
commit_version(ctx, version, environment=env_label)
|
|
580
774
|
push_version(ctx)
|
|
581
775
|
propagate_changelog(ctx)
|
|
582
776
|
|
|
583
777
|
|
|
584
|
-
@task
|
|
585
|
-
def incrementmajor(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
778
|
+
@task(optional=['tm'])
|
|
779
|
+
def incrementmajor(ctx: Context, skip_pipeline=False, ssr=False, tm=None) -> None:
|
|
586
780
|
"""Increment major version, build, and deploy. Use --ssr for Angular SSR builds."""
|
|
781
|
+
tm = validate_timesheet_option(tm)
|
|
587
782
|
production = check_branch(ctx)
|
|
588
783
|
ensure_gitlab_auth(ctx)
|
|
589
784
|
version = read_version(ctx)
|
|
@@ -591,16 +786,17 @@ def incrementmajor(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
|
591
786
|
if production and detect_project_type() == 'frontend':
|
|
592
787
|
version = maybe_override_release_version(version)
|
|
593
788
|
_ensure_unique_tag(ctx, version)
|
|
594
|
-
tag_build_push(ctx, version, skip_pipeline, ssr=ssr)
|
|
789
|
+
env_label = tag_build_push(ctx, version, skip_pipeline, ssr=ssr, tm=tm)
|
|
595
790
|
save_version(version)
|
|
596
|
-
commit_version(ctx, version)
|
|
791
|
+
commit_version(ctx, version, environment=env_label)
|
|
597
792
|
push_version(ctx)
|
|
598
793
|
propagate_changelog(ctx)
|
|
599
794
|
|
|
600
795
|
|
|
601
|
-
@task
|
|
602
|
-
def release(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
796
|
+
@task(optional=['tm'])
|
|
797
|
+
def release(ctx: Context, skip_pipeline=False, ssr=False, tm=None) -> None:
|
|
603
798
|
"""Promote release candidate to stable release and deploy. Use --ssr for Angular SSR builds."""
|
|
799
|
+
tm = validate_timesheet_option(tm)
|
|
604
800
|
production = check_branch(ctx, required='master')
|
|
605
801
|
ensure_gitlab_auth(ctx)
|
|
606
802
|
version = read_version(ctx)
|
|
@@ -608,9 +804,9 @@ def release(ctx: Context, skip_pipeline=False, ssr=False) -> None:
|
|
|
608
804
|
if production and detect_project_type() == 'frontend':
|
|
609
805
|
version = maybe_override_release_version(version)
|
|
610
806
|
_ensure_unique_tag(ctx, version)
|
|
611
|
-
tag_build_push(ctx, version, skip_pipeline, ssr=ssr)
|
|
807
|
+
env_label = tag_build_push(ctx, version, skip_pipeline, ssr=ssr, tm=tm)
|
|
612
808
|
save_version(version)
|
|
613
|
-
commit_version(ctx, version)
|
|
809
|
+
commit_version(ctx, version, environment=env_label)
|
|
614
810
|
push_version(ctx)
|
|
615
811
|
propagate_changelog(ctx)
|
|
616
812
|
|
|
@@ -757,8 +953,16 @@ def buildlocal(ctx: Context, ssr=False):
|
|
|
757
953
|
"""Build locally (Docker image for backend, ng build for frontend). Use --ssr for SSR build."""
|
|
758
954
|
if detect_project_type() == 'frontend':
|
|
759
955
|
branch = get_current_branch(ctx)
|
|
760
|
-
|
|
761
|
-
|
|
956
|
+
targets = select_prod_environments(branch, ssr)
|
|
957
|
+
if not targets:
|
|
958
|
+
build_frontend(ctx, branch, ssr=ssr)
|
|
959
|
+
print('✓ Built frontend (dist/)')
|
|
960
|
+
return
|
|
961
|
+
for env in targets:
|
|
962
|
+
if os.path.isdir('dist'):
|
|
963
|
+
shutil.rmtree('dist')
|
|
964
|
+
build_frontend_configuration(ctx, env)
|
|
965
|
+
print(f'✓ Built frontend ({env["name"]})')
|
|
762
966
|
return
|
|
763
967
|
current_branch = get_current_branch(ctx)
|
|
764
968
|
local_tag = f'myapp:{current_branch}-local'
|
|
@@ -1,28 +1,11 @@
|
|
|
1
|
-
<!-- OPTIONAL -->
|
|
2
1
|
/glab_host
|
|
3
2
|
/glab_username
|
|
4
3
|
/glab_token
|
|
5
|
-
|
|
6
|
-
<!-- REQUIRED -->
|
|
7
|
-
/title
|
|
8
|
-
|
|
9
|
-
<!-- OPTIONAL -->
|
|
10
4
|
/repo
|
|
11
5
|
/milestone
|
|
12
6
|
/milestone-start YYYY-MM-DD
|
|
13
7
|
/milestone-end YYYY-MM-DD
|
|
14
8
|
/assignee
|
|
15
|
-
/label
|
|
16
|
-
/estimate
|
|
17
|
-
/due YYYY-MM-DD
|
|
18
|
-
|
|
19
|
-
<!-- OPTIONAL -->
|
|
20
|
-
## Acceptance Criteria
|
|
21
|
-
- [ ]
|
|
22
|
-
- [ ]
|
|
23
|
-
## Acceptance Criteria
|
|
24
|
-
|
|
25
|
-
<!-- OPTIONAL -->
|
|
26
9
|
## Comment
|
|
27
10
|
|
|
28
11
|
## Comment
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import re
|
|
2
|
+
import sys
|
|
3
|
+
from datetime import datetime, time, timedelta, timezone
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from urllib.parse import quote, urlencode
|
|
6
|
+
|
|
7
|
+
import requests
|
|
8
|
+
from decouple import RepositoryEnv
|
|
9
|
+
|
|
10
|
+
from itw_python_builder.task_utils import _gitlab_api_list
|
|
11
|
+
from itw_python_builder.utils import _gitlab_api_call, detect_project_type
|
|
12
|
+
from itw_python_builder.version import Version
|
|
13
|
+
|
|
14
|
+
TIMESHEET_TIMEOUT = 15
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def read_timesheet_config() -> dict:
|
|
18
|
+
env_path = Path(__file__).resolve().parent.parent / '.env'
|
|
19
|
+
config = RepositoryEnv(str(env_path)).data if env_path.is_file() else {}
|
|
20
|
+
settings = {
|
|
21
|
+
'TIMESHEET_REPORT_URL': config.get('TIMESHEET_REPORT_URL', '').strip(),
|
|
22
|
+
'QA_ISSUE_API_KEY': config.get('QA_ISSUE_API_KEY', '').strip(),
|
|
23
|
+
}
|
|
24
|
+
missing = [name for name, value in settings.items() if not value]
|
|
25
|
+
if missing:
|
|
26
|
+
raise RuntimeError(f'Missing Timesheet configuration: {", ".join(missing)}.')
|
|
27
|
+
return settings
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def validate_timesheet_option(tm):
|
|
31
|
+
if tm is None:
|
|
32
|
+
return None
|
|
33
|
+
if detect_project_type() != 'frontend':
|
|
34
|
+
raise RuntimeError('The -tm option is only supported for frontend releases.')
|
|
35
|
+
if tm is True:
|
|
36
|
+
return True
|
|
37
|
+
if not re.fullmatch(r'[0-9]+', str(tm)) or int(tm) <= 0:
|
|
38
|
+
raise RuntimeError('The -tm option requires a positive merge request number, or no number to select one.')
|
|
39
|
+
return int(tm)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def today_merge_window() -> tuple:
|
|
43
|
+
today = datetime.now().date()
|
|
44
|
+
start = datetime.combine(today, time.min).astimezone(timezone.utc)
|
|
45
|
+
end = datetime.combine(today + timedelta(days=1), time.min).astimezone(timezone.utc)
|
|
46
|
+
return start, end
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def list_merged_requests_today(api_host: str, group: str, token: str) -> list:
|
|
50
|
+
start, end = today_merge_window()
|
|
51
|
+
query = urlencode({
|
|
52
|
+
'state': 'merged',
|
|
53
|
+
'scope': 'all',
|
|
54
|
+
'merged_after': start.isoformat(),
|
|
55
|
+
'merged_before': end.isoformat(),
|
|
56
|
+
'non_archived': 'false',
|
|
57
|
+
})
|
|
58
|
+
merge_requests = _gitlab_api_list(api_host, f'/groups/{quote(group, safe="")}/merge_requests?{query}', token)
|
|
59
|
+
matches = []
|
|
60
|
+
for merge_request in merge_requests:
|
|
61
|
+
if merge_request.get('state') != 'merged' or not merge_request.get('merged_at'):
|
|
62
|
+
continue
|
|
63
|
+
merged_at = datetime.fromisoformat(merge_request['merged_at'].replace('Z', '+00:00'))
|
|
64
|
+
if start <= merged_at < end:
|
|
65
|
+
matches.append(merge_request)
|
|
66
|
+
return matches
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def select_merge_request(merge_requests: list) -> dict:
|
|
70
|
+
if not sys.stdin.isatty():
|
|
71
|
+
raise RuntimeError('Merge request selection requires an interactive terminal. Use -tm <number> for a unique match.')
|
|
72
|
+
print('[itw] Select a merge request merged today:')
|
|
73
|
+
for index, merge_request in enumerate(merge_requests, start=1):
|
|
74
|
+
print(f' [{index}] {merge_request["repo"]}!{merge_request["iid"]} | '
|
|
75
|
+
f'{merge_request["title"]} | {merge_request["merged_at"]}')
|
|
76
|
+
while True:
|
|
77
|
+
try:
|
|
78
|
+
raw = input(f'Pick one (1-{len(merge_requests)}): ').strip()
|
|
79
|
+
except EOFError as exc:
|
|
80
|
+
raise RuntimeError('No merge request selected; release cancelled.') from exc
|
|
81
|
+
try:
|
|
82
|
+
choice = int(raw)
|
|
83
|
+
if 1 <= choice <= len(merge_requests):
|
|
84
|
+
return merge_requests[choice - 1]
|
|
85
|
+
except ValueError:
|
|
86
|
+
pass
|
|
87
|
+
print('Invalid choice, try again.')
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def resolve_timesheet_merge_request(api_host: str, project_path: str, token: str, merge_request_id=None) -> dict:
|
|
91
|
+
if '/' not in project_path:
|
|
92
|
+
raise RuntimeError(f'Project {project_path!r} has no GitLab group to search.')
|
|
93
|
+
group = project_path.rsplit('/', 1)[0]
|
|
94
|
+
candidates = list_merged_requests_today(api_host, group, token)
|
|
95
|
+
has_number = merge_request_id is not None and merge_request_id is not True
|
|
96
|
+
if has_number:
|
|
97
|
+
candidates = [item for item in candidates if item['iid'] == int(merge_request_id)]
|
|
98
|
+
if not candidates:
|
|
99
|
+
number = f' with ID {merge_request_id}' if has_number else ''
|
|
100
|
+
raise RuntimeError(f'No merge request{number} merged today was found in group {group}.')
|
|
101
|
+
projects = {}
|
|
102
|
+
options = []
|
|
103
|
+
for candidate in candidates:
|
|
104
|
+
project_id = candidate['project_id']
|
|
105
|
+
if project_id not in projects:
|
|
106
|
+
project = _gitlab_api_call(api_host, f'/projects/{project_id}', token)
|
|
107
|
+
projects[project_id] = project['path_with_namespace']
|
|
108
|
+
options.append({**candidate, 'repo': projects[project_id]})
|
|
109
|
+
chosen = options[0] if has_number and len(options) == 1 else select_merge_request(options)
|
|
110
|
+
print(f'[itw] Using merge request: {chosen["repo"]}!{chosen["iid"]} | {chosen["title"]}')
|
|
111
|
+
return chosen
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def report_timesheet_release(version: Version, merge_request: dict) -> None:
|
|
115
|
+
config = read_timesheet_config()
|
|
116
|
+
headers = {
|
|
117
|
+
'X-Api-Key': config['QA_ISSUE_API_KEY'],
|
|
118
|
+
'Version': version.bare_semver(),
|
|
119
|
+
'Merge-Request-Repo': merge_request['repo'],
|
|
120
|
+
'Merge-Request-Id': str(merge_request['iid']),
|
|
121
|
+
}
|
|
122
|
+
try:
|
|
123
|
+
response = requests.post(config['TIMESHEET_REPORT_URL'], headers=headers, timeout=TIMESHEET_TIMEOUT, allow_redirects=False)
|
|
124
|
+
except requests.RequestException as exc:
|
|
125
|
+
raise RuntimeError(f'Timesheet reporting failed: {exc}') from exc
|
|
126
|
+
if not 200 <= response.status_code < 300:
|
|
127
|
+
try:
|
|
128
|
+
body = response.json()
|
|
129
|
+
except ValueError:
|
|
130
|
+
body = None
|
|
131
|
+
message = (body.get('message') or body.get('MESSAGE')) if isinstance(body, dict) else None
|
|
132
|
+
raise RuntimeError(f'Timesheet reporting failed: {message or response.text.strip() or f"HTTP {response.status_code}"}')
|
|
133
|
+
print(f'[itw] Reported {headers["Merge-Request-Repo"]}!{headers["Merge-Request-Id"]} '
|
|
134
|
+
f'to Timesheet for version {headers["Version"]}.')
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: itw_python_builder
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.16
|
|
4
4
|
Summary: Standardized Django deployment pipeline with Docker, testing, and SonarQube integration
|
|
5
5
|
Author-email: IT-Works <contact@it-works.io>
|
|
6
6
|
License: MIT
|
|
@@ -101,6 +101,35 @@ itw incrementpatch
|
|
|
101
101
|
- `itw incrementrc` — Increment release candidate (staging)
|
|
102
102
|
- `itw release` — Promote RC to stable release (master)
|
|
103
103
|
|
|
104
|
+
### Timesheet Reporting for Frontend Releases
|
|
105
|
+
|
|
106
|
+
Pass `-tm` to any deployment command to report a merge request to Timesheet before publishing:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# Find a specific MR number merged today in the current GitLab group
|
|
110
|
+
itw incrementpatch -tm 110
|
|
111
|
+
|
|
112
|
+
# Choose from today's merged MRs in the terminal
|
|
113
|
+
itw incrementrc -tm
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`--tm` and `--tm=110` are also supported. The option is available on `incrementrc`, `incrementpatch`, `incrementminor`, `incrementmajor` and `release`, for frontend projects on their allowed `staging`/`master` branches. Backend projects reject it before authentication or release work.
|
|
117
|
+
|
|
118
|
+
The current group is the full namespace of the origin repository; the search includes its subgroups and all accessible projects, including backend repositories. It uses the existing GitLab login and matches `merged_at` against today's calendar day in the local timezone. The number is the project-local MR number shown in GitLab, such as `!110`. If that number matches several repositories, you must choose one. Without a number, you must choose from the listed MRs even when only one is available. An interactive selection requires a terminal; a unique explicit number works without one. No matching MR stops the release with a message naming the number and group.
|
|
119
|
+
|
|
120
|
+
Configure Timesheet in the builder's root `.env`:
|
|
121
|
+
|
|
122
|
+
```dotenv
|
|
123
|
+
TIMESHEET_REPORT_URL=https://tm.it-works.io/api/qa-issue-from-merge-request/
|
|
124
|
+
QA_ISSUE_API_KEY=<shared-api-key>
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Both values are required and are read only from the `.env` in the builder's root, resolved relative to the package location. The project being released and environment variables do not override this configuration.
|
|
128
|
+
|
|
129
|
+
After the tests and analysis pass, `itw` sends one POST to `TIMESHEET_REPORT_URL`, before creating a tag or changing/publishing the version. The request headers are `X-Api-Key` (the configured API key), `Version` (for example `1.2.3`, without the tag prefix or RC suffix), `Merge-Request-Repo` (the selected MR's `group/project`) and `Merge-Request-Id` (its MR number).
|
|
130
|
+
|
|
131
|
+
An HTTP error stops the release and displays the response's `message` (or `MESSAGE`); non-JSON error responses and connection/timeout failures also stop it with their error details. Reporting is not automatically retried. `--skip-pipeline` still reports to Timesheet, and SSR or multiple production environments report once per release. If a later build or upload fails, the existing Timesheet report is not rolled back. Commands without `-tm` do not search MRs or report to Timesheet.
|
|
132
|
+
|
|
104
133
|
### Authentication Commands
|
|
105
134
|
|
|
106
135
|
- `itw login` — Capture and cache a GitLab token (backend also logs into the container registry)
|
|
@@ -150,6 +179,8 @@ itw test --target=core.service
|
|
|
150
179
|
|
|
151
180
|
### SSR Builds
|
|
152
181
|
|
|
182
|
+
On a production frontend deploy (`master`), `itw` looks in `src/environments` for `*.prod.ts` files. One file keeps the existing `build-master` script and uploads `frontend/<version>/dist.tar.gz`. Several files prompt for which one to deploy, with `all` as the last choice. Each selected environment is built with the matching `angular.json` configuration, packaged as `dist-<environment>.tar.gz`, and uploaded before the next one starts, at `frontend/<version>/dist-<environment>.tar.gz`. The version commit then includes that name, for example `version update to v.1.2.3-release (lenslight)`. Choosing `all` lists every name: `version update to v.1.2.3-release (greenterrace, lenslight)`.
|
|
183
|
+
|
|
153
184
|
Frontend projects scaffolded with `itw ssr-init` build through the SSR npm scripts when `--ssr` is passed:
|
|
154
185
|
```bash
|
|
155
186
|
itw incrementrc --ssr
|
|
@@ -219,42 +250,25 @@ Then fill it in and run:
|
|
|
219
250
|
itw task --file=TASK.md
|
|
220
251
|
```
|
|
221
252
|
|
|
222
|
-
`task-init` refuses to overwrite an existing `TASK.md`.
|
|
253
|
+
`task-init` refuses to overwrite an existing `TASK.md`. Without `.git` in the current directory, repo and milestone stay empty. With `.git`, it reads the GitLab remote and finds the namespace's `_pm` repositories. A single repository is selected automatically; several prompt for a selection. Milestones from the selected project and its parent groups follow the same selection rule, and the chosen milestone's available start/end dates are filled in. When no milestones exist, the milestone stays empty and the date placeholders are kept.
|
|
223
254
|
|
|
224
255
|
### Template
|
|
225
256
|
|
|
226
257
|
```markdown
|
|
227
|
-
<!-- OPTIONAL -->
|
|
228
258
|
/glab_host
|
|
229
259
|
/glab_username
|
|
230
260
|
/glab_token
|
|
231
|
-
|
|
232
|
-
<!-- REQUIRED -->
|
|
233
|
-
/title
|
|
234
|
-
|
|
235
|
-
<!-- OPTIONAL -->
|
|
236
261
|
/repo
|
|
237
262
|
/milestone
|
|
238
263
|
/milestone-start YYYY-MM-DD
|
|
239
264
|
/milestone-end YYYY-MM-DD
|
|
240
265
|
/assignee
|
|
241
|
-
/label
|
|
242
|
-
/estimate
|
|
243
|
-
/due YYYY-MM-DD
|
|
244
|
-
|
|
245
|
-
<!-- OPTIONAL -->
|
|
246
|
-
## Acceptance Criteria
|
|
247
|
-
- [ ]
|
|
248
|
-
- [ ]
|
|
249
|
-
## Acceptance Criteria
|
|
250
|
-
|
|
251
|
-
<!-- OPTIONAL -->
|
|
252
266
|
## Comment
|
|
253
267
|
|
|
254
268
|
## Comment
|
|
255
269
|
```
|
|
256
270
|
|
|
257
|
-
|
|
271
|
+
Add a `/title <text>` line for each issue before running `itw task`. The parser also supports the optional directives listed below, including `/label`, `/estimate`, `/due` and custom acceptance criteria. Leaving a key empty is allowed — empty keys are simply not sent to GitLab. The date placeholders use `YYYY-MM-DD` and are treated as empty until you replace them.
|
|
258
272
|
|
|
259
273
|
Any text outside the directives becomes the issue description. Create several issues from one file by separating the blocks with a line containing only `===`.
|
|
260
274
|
|
|
@@ -283,7 +297,7 @@ When no credentials are given, `itw` uses the `.git` directory in the current fo
|
|
|
283
297
|
|
|
284
298
|
### Target Repository
|
|
285
299
|
|
|
286
|
-
|
|
300
|
+
The first `/repo` in the file is the file default. Every later block that leaves `/repo` empty uses that value. A block that sets its own `/repo` keeps it and does not change the default for the blocks below. Blocks above the first `/repo`, and a file with no `/repo` at all, are resolved once by walking the current repository's namespace upward looking for a `_pm` project. If several are found you are prompted to pick one, and that choice is reused for the remaining issues that still have no repo.
|
|
287
301
|
|
|
288
302
|
With `/repo`, leading and trailing slashes are stripped automatically, and full URLs or SSH remotes are accepted:
|
|
289
303
|
|
|
@@ -295,11 +309,11 @@ With `/repo`, leading and trailing slashes are stripped automatically, and full
|
|
|
295
309
|
|
|
296
310
|
### Milestones, Labels and Duplicates
|
|
297
311
|
|
|
298
|
-
Milestones, labels and issue titles are matched with a shared fuzzy comparison at 95% similarity, so small differences in spacing or casing still count as a match. The threshold is a single constant (`SIMILARITY_THRESHOLD` in `task_utils.py`) if you need to tune it.
|
|
312
|
+
Milestones, labels and issue titles are matched with a shared fuzzy comparison at 95% similarity, so small differences in spacing or casing still count as a match. Digit runs must be identical (`java 38` does not match `java 37`). The threshold is a single constant (`SIMILARITY_THRESHOLD` in `task_utils.py`) if you need to tune it.
|
|
299
313
|
|
|
300
|
-
- **Milestone** —
|
|
314
|
+
- **Milestone** — an existing project or parent-group milestone with the same title is reused; otherwise it is created (with the start/end dates when both are given).
|
|
301
315
|
- **Label** — a similar existing label is linked and logged in white; a new one is created and logged in green. An empty `/label` is never sent to GitLab.
|
|
302
|
-
- **Duplicate issues** — before creating, the target project is
|
|
316
|
+
- **Duplicate issues** — before creating, the target project is searched by title. A similar title is treated as a duplicate and skipped only when the milestone and assignees also match. An empty milestone or assignee matches only an issue where that field is also empty.
|
|
303
317
|
|
|
304
318
|
### Acceptance Criteria and Comments
|
|
305
319
|
|
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
itw_python_builder/.pylintrc,sha256=qaFcVwZntMr-8RTj924F7WtQrTpl5x-wKSlomle0qRI,24366
|
|
2
2
|
itw_python_builder/__init__.py,sha256=0HwEqonfK8w1kx80BOX80fvxSL8ov1hmUD2Zi4SU75w,119
|
|
3
|
-
itw_python_builder/cli.py,sha256=
|
|
3
|
+
itw_python_builder/cli.py,sha256=dYrpYch7RnC8Mw_QrNoge62PC86yW79zHqX9RrAxDLo,1894
|
|
4
4
|
itw_python_builder/notify.py,sha256=Rv1RA9QdDo00bvjMMSDq-oQh2pjDmjjj97yzzhgiwpM,3011
|
|
5
5
|
itw_python_builder/ssr_tasks.py,sha256=TqkuGLH9XJqDkTPBuRnFAKJywYqvdf0lSlSMXtVPM5Y,15730
|
|
6
|
-
itw_python_builder/task_utils.py,sha256=
|
|
7
|
-
itw_python_builder/tasks.py,sha256=
|
|
6
|
+
itw_python_builder/task_utils.py,sha256=971YjhkHpUNgeUadSFAwFfbKNgR-7dWYLQU2_fDW-JA,15740
|
|
7
|
+
itw_python_builder/tasks.py,sha256=cSqMzBStd9ErgTyMIXXx3GQtgKsYP8mUISXMyB62IVg,42537
|
|
8
|
+
itw_python_builder/timesheet.py,sha256=OhHQuWy7wpcqJf110OJhHbq6FBIWaPGllAuyFNxNhz0,5803
|
|
8
9
|
itw_python_builder/utils.py,sha256=wKNJM64Vl3O3KbyTSNYu6RelhWgmlapGOUGUokap9Pk,21294
|
|
9
10
|
itw_python_builder/version.py,sha256=RCSKNU4eblCP38-PH5hT6qMDKOyQq5YGCoT2mgpfZkE,3165
|
|
10
11
|
itw_python_builder/_pyruntime/sitecustomize.py,sha256=cjTrKErMdAFI-uVuW60BNNF8X41NrH9XDrIHJlNlEtY,805
|
|
11
12
|
itw_python_builder/templates/new_version_email.html,sha256=I0RuZ8UMBavXTaSzQJQP1wNjsDY5TtxrASgeGiX0NGo,2854
|
|
12
13
|
itw_python_builder/templates/server.sitemap.snippet.ts,sha256=qjRCLiAqzDUTqsWxJLeqlr-jOluK0IE2IoN_lbSx0ec,669
|
|
13
14
|
itw_python_builder/templates/sitemap.routes.ts,sha256=yOXZ8TpHq_-QKH7y_u75XpL8paCF4AEv5z5cvWeI7yY,1323
|
|
14
|
-
itw_python_builder/templates/task_template.md,sha256=
|
|
15
|
-
itw_python_builder-0.2.
|
|
16
|
-
itw_python_builder-0.2.
|
|
17
|
-
itw_python_builder-0.2.
|
|
18
|
-
itw_python_builder-0.2.
|
|
19
|
-
itw_python_builder-0.2.
|
|
20
|
-
itw_python_builder-0.2.
|
|
15
|
+
itw_python_builder/templates/task_template.md,sha256=k5CBfPhD5opt2G64ZxfjVvFyCb3l2dpSD8DpWMawi_g,148
|
|
16
|
+
itw_python_builder-0.2.16.dist-info/licenses/LICENSE,sha256=WZ75-earCxnov77gsH4vckNf7Ag8RPst-7y9dPaZTCU,1063
|
|
17
|
+
itw_python_builder-0.2.16.dist-info/METADATA,sha256=ngF82NNehwbImxWJFpgX_Vs3sj_Cg_fiqRo2Ee_9_g4,15424
|
|
18
|
+
itw_python_builder-0.2.16.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
19
|
+
itw_python_builder-0.2.16.dist-info/entry_points.txt,sha256=Rx2zlbaCxl954YX8kF4hey3Y25fxsaxiEPXjBPw3ky0,52
|
|
20
|
+
itw_python_builder-0.2.16.dist-info/top_level.txt,sha256=9djpWUrRa0vuSfdVrHc4-3gvAHbBvzh_lbyzgTHtkJA,19
|
|
21
|
+
itw_python_builder-0.2.16.dist-info/RECORD,,
|
{itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{itw_python_builder-0.2.14.dist-info → itw_python_builder-0.2.16.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|