itw-python-builder 0.2.14__tar.gz → 0.2.16__tar.gz
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-0.2.14 → itw_python_builder-0.2.16}/PKG-INFO +38 -24
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/README.md +37 -23
- itw_python_builder-0.2.16/itw_python_builder/cli.py +63 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/task_utils.py +103 -16
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/tasks.py +261 -57
- itw_python_builder-0.2.16/itw_python_builder/templates/task_template.md +11 -0
- itw_python_builder-0.2.16/itw_python_builder/timesheet.py +134 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/PKG-INFO +38 -24
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/SOURCES.txt +1 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/pyproject.toml +1 -1
- itw_python_builder-0.2.14/itw_python_builder/cli.py +0 -35
- itw_python_builder-0.2.14/itw_python_builder/templates/task_template.md +0 -28
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/LICENSE +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/.pylintrc +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/__init__.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/_pyruntime/sitecustomize.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/notify.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/ssr_tasks.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/templates/new_version_email.html +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/templates/server.sitemap.snippet.ts +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/templates/sitemap.routes.ts +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/utils.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder/version.py +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/dependency_links.txt +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/entry_points.txt +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/requires.txt +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/itw_python_builder.egg-info/top_level.txt +0 -0
- {itw_python_builder-0.2.14 → itw_python_builder-0.2.16}/setup.cfg +0 -0
|
@@ -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
|
|
|
@@ -72,6 +72,35 @@ itw incrementpatch
|
|
|
72
72
|
- `itw incrementrc` — Increment release candidate (staging)
|
|
73
73
|
- `itw release` — Promote RC to stable release (master)
|
|
74
74
|
|
|
75
|
+
### Timesheet Reporting for Frontend Releases
|
|
76
|
+
|
|
77
|
+
Pass `-tm` to any deployment command to report a merge request to Timesheet before publishing:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
# Find a specific MR number merged today in the current GitLab group
|
|
81
|
+
itw incrementpatch -tm 110
|
|
82
|
+
|
|
83
|
+
# Choose from today's merged MRs in the terminal
|
|
84
|
+
itw incrementrc -tm
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`--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.
|
|
88
|
+
|
|
89
|
+
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.
|
|
90
|
+
|
|
91
|
+
Configure Timesheet in the builder's root `.env`:
|
|
92
|
+
|
|
93
|
+
```dotenv
|
|
94
|
+
TIMESHEET_REPORT_URL=https://tm.it-works.io/api/qa-issue-from-merge-request/
|
|
95
|
+
QA_ISSUE_API_KEY=<shared-api-key>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
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.
|
|
99
|
+
|
|
100
|
+
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).
|
|
101
|
+
|
|
102
|
+
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.
|
|
103
|
+
|
|
75
104
|
### Authentication Commands
|
|
76
105
|
|
|
77
106
|
- `itw login` — Capture and cache a GitLab token (backend also logs into the container registry)
|
|
@@ -121,6 +150,8 @@ itw test --target=core.service
|
|
|
121
150
|
|
|
122
151
|
### SSR Builds
|
|
123
152
|
|
|
153
|
+
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)`.
|
|
154
|
+
|
|
124
155
|
Frontend projects scaffolded with `itw ssr-init` build through the SSR npm scripts when `--ssr` is passed:
|
|
125
156
|
```bash
|
|
126
157
|
itw incrementrc --ssr
|
|
@@ -190,42 +221,25 @@ Then fill it in and run:
|
|
|
190
221
|
itw task --file=TASK.md
|
|
191
222
|
```
|
|
192
223
|
|
|
193
|
-
`task-init` refuses to overwrite an existing `TASK.md`.
|
|
224
|
+
`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.
|
|
194
225
|
|
|
195
226
|
### Template
|
|
196
227
|
|
|
197
228
|
```markdown
|
|
198
|
-
<!-- OPTIONAL -->
|
|
199
229
|
/glab_host
|
|
200
230
|
/glab_username
|
|
201
231
|
/glab_token
|
|
202
|
-
|
|
203
|
-
<!-- REQUIRED -->
|
|
204
|
-
/title
|
|
205
|
-
|
|
206
|
-
<!-- OPTIONAL -->
|
|
207
232
|
/repo
|
|
208
233
|
/milestone
|
|
209
234
|
/milestone-start YYYY-MM-DD
|
|
210
235
|
/milestone-end YYYY-MM-DD
|
|
211
236
|
/assignee
|
|
212
|
-
/label
|
|
213
|
-
/estimate
|
|
214
|
-
/due YYYY-MM-DD
|
|
215
|
-
|
|
216
|
-
<!-- OPTIONAL -->
|
|
217
|
-
## Acceptance Criteria
|
|
218
|
-
- [ ]
|
|
219
|
-
- [ ]
|
|
220
|
-
## Acceptance Criteria
|
|
221
|
-
|
|
222
|
-
<!-- OPTIONAL -->
|
|
223
237
|
## Comment
|
|
224
238
|
|
|
225
239
|
## Comment
|
|
226
240
|
```
|
|
227
241
|
|
|
228
|
-
|
|
242
|
+
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.
|
|
229
243
|
|
|
230
244
|
Any text outside the directives becomes the issue description. Create several issues from one file by separating the blocks with a line containing only `===`.
|
|
231
245
|
|
|
@@ -254,7 +268,7 @@ When no credentials are given, `itw` uses the `.git` directory in the current fo
|
|
|
254
268
|
|
|
255
269
|
### Target Repository
|
|
256
270
|
|
|
257
|
-
|
|
271
|
+
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.
|
|
258
272
|
|
|
259
273
|
With `/repo`, leading and trailing slashes are stripped automatically, and full URLs or SSH remotes are accepted:
|
|
260
274
|
|
|
@@ -266,11 +280,11 @@ With `/repo`, leading and trailing slashes are stripped automatically, and full
|
|
|
266
280
|
|
|
267
281
|
### Milestones, Labels and Duplicates
|
|
268
282
|
|
|
269
|
-
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.
|
|
283
|
+
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.
|
|
270
284
|
|
|
271
|
-
- **Milestone** —
|
|
285
|
+
- **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).
|
|
272
286
|
- **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.
|
|
273
|
-
- **Duplicate issues** — before creating, the target project is
|
|
287
|
+
- **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.
|
|
274
288
|
|
|
275
289
|
### Acceptance Criteria and Comments
|
|
276
290
|
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
import fcntl
|
|
3
|
+
import struct
|
|
4
|
+
import termios
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version as _pkg_version
|
|
6
|
+
|
|
7
|
+
from invoke import Argument, Collection, Program, runners, terminals
|
|
8
|
+
|
|
9
|
+
from itw_python_builder import tasks
|
|
10
|
+
|
|
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
|
+
|
|
26
|
+
def _get_version() -> str:
|
|
27
|
+
try:
|
|
28
|
+
return _pkg_version('itw_python_builder')
|
|
29
|
+
except PackageNotFoundError:
|
|
30
|
+
return 'unknown'
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class ItwProgram(Program):
|
|
34
|
+
def core_args(self):
|
|
35
|
+
args = super().core_args()
|
|
36
|
+
result = []
|
|
37
|
+
for arg in args:
|
|
38
|
+
if 'version' in arg.names:
|
|
39
|
+
result.append(Argument(
|
|
40
|
+
names=('version', 'v'),
|
|
41
|
+
kind=bool,
|
|
42
|
+
default=False,
|
|
43
|
+
help='Show version and exit.',
|
|
44
|
+
))
|
|
45
|
+
else:
|
|
46
|
+
result.append(arg)
|
|
47
|
+
return result
|
|
48
|
+
|
|
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
|
+
|
|
60
|
+
def main():
|
|
61
|
+
namespace = Collection.from_module(tasks)
|
|
62
|
+
program = ItwProgram(namespace=namespace, name='itw', version=_get_version())
|
|
63
|
+
program.run(argv=normalize_cli_args(sys.argv))
|
|
@@ -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(
|