| 1 |
#!/usr/bin/env python |
| 2 |
# PYTHON_ARGCOMPLETE_OK |
| 3 |
"""Create GitHub issues for deprecated features.""" |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import abc |
| 8 |
import argparse |
| 9 |
import dataclasses |
| 10 |
import os |
| 11 |
import pathlib |
| 12 |
import re |
| 13 |
import subprocess |
| 14 |
import sys |
| 15 |
import typing as t |
| 16 |
|
| 17 |
import yaml |
| 18 |
|
| 19 |
try: |
| 20 |
# noinspection PyPackageRequirements |
| 21 |
import argcomplete |
| 22 |
except ImportError: |
| 23 |
argcomplete = None |
| 24 |
|
| 25 |
from ansible.release import __version__ |
| 26 |
|
| 27 |
MAJOR_MINOR_VERSION = '.'.join(__version__.split('.')[:2]) |
| 28 |
PROJECT = f'ansible-core {MAJOR_MINOR_VERSION}' |
| 29 |
|
| 30 |
|
| 31 |
@dataclasses.dataclass(frozen=True) |
| 32 |
class Issue: |
| 33 |
title: str |
| 34 |
summary: str |
| 35 |
body: str |
| 36 |
project: str |
| 37 |
labels: list[str] | None = None |
| 38 |
assignee: str | None = None |
| 39 |
|
| 40 |
def create(self) -> str: |
| 41 |
cmd = ['gh', 'issue', 'create', '--title', self.title, '--body', self.body, '--project', self.project] |
| 42 |
|
| 43 |
if self.labels: |
| 44 |
for label in self.labels: |
| 45 |
cmd.extend(('--label', label)) |
| 46 |
|
| 47 |
if self.assignee: |
| 48 |
cmd.extend(('--assignee', self.assignee)) |
| 49 |
|
| 50 |
try: |
| 51 |
process = subprocess.run(cmd, capture_output=True, check=True, text=True) |
| 52 |
except subprocess.CalledProcessError as ex: |
| 53 |
print('>>> Note') |
| 54 |
print(f"You may need to run 'gh auth refresh -s project' if 'gh' reports it cannot find the project {self.project!r} when it exists.") |
| 55 |
print(f'>>> Standard Output\n{ex.stdout.strip()}\n>>> Standard Error\n{ex.stderr.strip()}\n>>> Exception') |
| 56 |
raise |
| 57 |
|
| 58 |
url = process.stdout.strip() |
| 59 |
return url |
| 60 |
|
| 61 |
|
| 62 |
@dataclasses.dataclass(frozen=True) |
| 63 |
class Feature: |
| 64 |
title: str |
| 65 |
summary: str |
| 66 |
component: str |
| 67 |
labels: list[str] | None = None |
| 68 |
assignee: str | None = None |
| 69 |
|
| 70 |
@staticmethod |
| 71 |
def from_dict(data: dict[str, t.Any]) -> Feature: |
| 72 |
title = data.get('title') |
| 73 |
summary = data.get('summary') |
| 74 |
component = data.get('component') |
| 75 |
labels = data.get('labels') |
| 76 |
assignee = data.get('assignee') |
| 77 |
|
| 78 |
if not isinstance(title, str): |
| 79 |
raise RuntimeError(f'`title` is not `str`: {title}') |
| 80 |
|
| 81 |
if not isinstance(summary, str): |
| 82 |
raise RuntimeError(f'`summary` is not `str`: {summary}') |
| 83 |
|
| 84 |
if not isinstance(component, str): |
| 85 |
raise RuntimeError(f'`component` is not `str`: {component}') |
| 86 |
|
| 87 |
if not isinstance(assignee, (str, type(None))): |
| 88 |
raise RuntimeError(f'`assignee` is not `str`: {assignee}') |
| 89 |
|
| 90 |
if not isinstance(labels, list) or not all(isinstance(item, str) for item in labels): |
| 91 |
raise RuntimeError(f'`labels` is not `list[str]`: {labels}') |
| 92 |
|
| 93 |
return Feature( |
| 94 |
title=title, |
| 95 |
summary=summary, |
| 96 |
component=component, |
| 97 |
labels=labels, |
| 98 |
assignee=assignee, |
| 99 |
) |
| 100 |
|
| 101 |
def create_issue(self, project: str) -> Issue: |
| 102 |
body = f''' |
| 103 |
### Summary |
| 104 |
|
| 105 |
{self.summary} |
| 106 |
|
| 107 |
### Issue Type |
| 108 |
|
| 109 |
Feature Idea |
| 110 |
|
| 111 |
### Component Name |
| 112 |
|
| 113 |
`{self.component}` |
| 114 |
''' |
| 115 |
|
| 116 |
return Issue( |
| 117 |
title=self.title, |
| 118 |
summary=self.summary, |
| 119 |
body=body.strip(), |
| 120 |
project=project, |
| 121 |
labels=self.labels, |
| 122 |
assignee=self.assignee, |
| 123 |
) |
| 124 |
|
| 125 |
|
| 126 |
@dataclasses.dataclass(frozen=True) |
| 127 |
class BugReport: |
| 128 |
title: str |
| 129 |
summary: str |
| 130 |
component: str |
| 131 |
labels: list[str] | None = None |
| 132 |
|
| 133 |
def create_issue(self, project: str) -> Issue: |
| 134 |
body = f''' |
| 135 |
### Summary |
| 136 |
|
| 137 |
{self.summary} |
| 138 |
|
| 139 |
### Issue Type |
| 140 |
|
| 141 |
Bug Report |
| 142 |
|
| 143 |
### Component Name |
| 144 |
|
| 145 |
`{self.component}` |
| 146 |
|
| 147 |
### Ansible Version |
| 148 |
|
| 149 |
{MAJOR_MINOR_VERSION} |
| 150 |
|
| 151 |
### Configuration |
| 152 |
|
| 153 |
N/A |
| 154 |
|
| 155 |
### OS / Environment |
| 156 |
|
| 157 |
N/A |
| 158 |
|
| 159 |
### Steps to Reproduce |
| 160 |
|
| 161 |
N/A |
| 162 |
|
| 163 |
### Expected Results |
| 164 |
|
| 165 |
N/A |
| 166 |
|
| 167 |
### Actual Results |
| 168 |
|
| 169 |
N/A |
| 170 |
''' |
| 171 |
|
| 172 |
return Issue( |
| 173 |
title=self.title, |
| 174 |
summary=self.summary, |
| 175 |
body=body.strip(), |
| 176 |
project=project, |
| 177 |
labels=self.labels, |
| 178 |
) |
| 179 |
|
| 180 |
|
| 181 |
@dataclasses.dataclass(frozen=True) |
| 182 |
class Deprecation(metaclass=abc.ABCMeta): |
| 183 |
@staticmethod |
| 184 |
@abc.abstractmethod |
| 185 |
def parse(message: str) -> Deprecation: |
| 186 |
pass |
| 187 |
|
| 188 |
@abc.abstractmethod |
| 189 |
def create_bug_report(self) -> BugReport: |
| 190 |
pass |
| 191 |
|
| 192 |
|
| 193 |
@dataclasses.dataclass(frozen=True) |
| 194 |
class DeprecatedConfig(Deprecation): |
| 195 |
path: str |
| 196 |
config: str |
| 197 |
version: str |
| 198 |
|
| 199 |
@staticmethod |
| 200 |
def parse(message: str) -> DeprecatedConfig: |
| 201 |
match = re.search('^(?P<path>.*):[0-9]+:[0-9]+: (?P<config>.*) is scheduled for removal in (?P<version>[0-9.]+)$', message) |
| 202 |
|
| 203 |
if not match: |
| 204 |
raise Exception(f'Unable to parse: {message}') |
| 205 |
|
| 206 |
return DeprecatedConfig( |
| 207 |
path=match.group('path'), |
| 208 |
config=match.group('config'), |
| 209 |
version=match.group('version'), |
| 210 |
) |
| 211 |
|
| 212 |
def create_bug_report(self) -> BugReport: |
| 213 |
return BugReport( |
| 214 |
title=f'Remove deprecated {self.config}', |
| 215 |
summary=f'The config option `{self.config}` should be removed from `{self.path}`. It was scheduled for removal in {self.version}.', |
| 216 |
component=self.path, |
| 217 |
) |
| 218 |
|
| 219 |
|
| 220 |
@dataclasses.dataclass(frozen=True) |
| 221 |
class UpdateBundled(Deprecation): |
| 222 |
path: str |
| 223 |
package: str |
| 224 |
old_version: str |
| 225 |
new_version: str |
| 226 |
json_link: str |
| 227 |
link: str = dataclasses.field(default='', init=False) |
| 228 |
|
| 229 |
def __post_init__(self) -> None: |
| 230 |
object.__setattr__(self, 'link', re.sub('/json$', '', self.json_link)) |
| 231 |
|
| 232 |
@staticmethod |
| 233 |
def parse(message: str) -> UpdateBundled: |
| 234 |
match = re.search('^(?P<path>.*):[0-9]+:[0-9]+: UPDATE (?P<package>.*) from (?P<old>[0-9.]+) to (?P<new>[0-9.]+) (?P<link>https://.*)$', message) |
| 235 |
|
| 236 |
if not match: |
| 237 |
raise Exception(f'Unable to parse: {message}') |
| 238 |
|
| 239 |
return UpdateBundled( |
| 240 |
path=match.group('path'), |
| 241 |
package=match.group('package'), |
| 242 |
old_version=match.group('old'), |
| 243 |
new_version=match.group('new'), |
| 244 |
json_link=match.group('link'), |
| 245 |
) |
| 246 |
|
| 247 |
def create_bug_report(self) -> BugReport: |
| 248 |
return BugReport( |
| 249 |
title=f'Update bundled {self.package} to {self.new_version}', |
| 250 |
summary=f'Update the bundled package [{self.package}]({self.link}) from `{self.old_version}` to `{self.new_version}`.', |
| 251 |
component=self.path, |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
TEST_OPTIONS = { |
| 256 |
'update-bundled': UpdateBundled, |
| 257 |
'deprecated-config': DeprecatedConfig, |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
@dataclasses.dataclass(frozen=True) |
| 262 |
class Args: |
| 263 |
create: bool |
| 264 |
verbose: bool |
| 265 |
|
| 266 |
def run(self) -> None: |
| 267 |
raise NotImplementedError() |
| 268 |
|
| 269 |
|
| 270 |
@dataclasses.dataclass(frozen=True) |
| 271 |
class DeprecationArgs(Args): |
| 272 |
tests: list[str] |
| 273 |
|
| 274 |
def run(self) -> None: |
| 275 |
deprecated_command(self) |
| 276 |
|
| 277 |
|
| 278 |
@dataclasses.dataclass(frozen=True) |
| 279 |
class FeatureArgs(Args): |
| 280 |
source: pathlib.Path |
| 281 |
|
| 282 |
def run(self) -> None: |
| 283 |
feature_command(self) |
| 284 |
|
| 285 |
|
| 286 |
def parse_args() -> Args: |
| 287 |
parser = argparse.ArgumentParser() |
| 288 |
|
| 289 |
create_common_arguments(parser) |
| 290 |
|
| 291 |
subparser = parser.add_subparsers(required=True) |
| 292 |
|
| 293 |
create_deprecation_parser(subparser) |
| 294 |
create_feature_parser(subparser) |
| 295 |
|
| 296 |
args = invoke_parser(parser) |
| 297 |
|
| 298 |
return args |
| 299 |
|
| 300 |
|
| 301 |
def create_deprecation_parser(subparser) -> None: |
| 302 |
parser: argparse.ArgumentParser = subparser.add_parser('deprecation') |
| 303 |
parser.set_defaults(type=DeprecationArgs) |
| 304 |
parser.set_defaults(command=deprecated_command) |
| 305 |
|
| 306 |
parser.add_argument( |
| 307 |
'--test', |
| 308 |
dest='tests', |
| 309 |
choices=tuple(TEST_OPTIONS), |
| 310 |
action='append', |
| 311 |
help='sanity test name', |
| 312 |
) |
| 313 |
|
| 314 |
create_common_arguments(parser) |
| 315 |
|
| 316 |
|
| 317 |
def create_feature_parser(subparser) -> None: |
| 318 |
epilog = """ |
| 319 |
Example source YAML: |
| 320 |
|
| 321 |
default: |
| 322 |
component: ansible-test |
| 323 |
labels: |
| 324 |
- ansible-test |
| 325 |
- feature |
| 326 |
assignee: "@me" |
| 327 |
features: |
| 328 |
- title: Some title goes here |
| 329 |
summary: A summary goes here. |
| 330 |
""" |
| 331 |
|
| 332 |
parser: argparse.ArgumentParser = subparser.add_parser('feature', epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 333 |
parser.set_defaults(type=FeatureArgs) |
| 334 |
parser.set_defaults(command=feature_command) |
| 335 |
|
| 336 |
parser.add_argument( |
| 337 |
'--source', |
| 338 |
type=pathlib.Path, |
| 339 |
default=pathlib.Path('issues.yml'), |
| 340 |
help='YAML file containing issue details (default: %(default)s)', |
| 341 |
) |
| 342 |
|
| 343 |
create_common_arguments(parser) |
| 344 |
|
| 345 |
|
| 346 |
def create_common_arguments(parser: argparse.ArgumentParser) -> None: |
| 347 |
parser.add_argument( |
| 348 |
'--create', |
| 349 |
action='store_true', |
| 350 |
help='create issues on GitHub', |
| 351 |
) |
| 352 |
|
| 353 |
parser.add_argument( |
| 354 |
'-v', |
| 355 |
dest='verbose', |
| 356 |
action='store_true', |
| 357 |
help='verbose output', |
| 358 |
) |
| 359 |
|
| 360 |
|
| 361 |
def invoke_parser(parser: argparse.ArgumentParser) -> Args: |
| 362 |
if argcomplete: |
| 363 |
argcomplete.autocomplete(parser) |
| 364 |
|
| 365 |
parsed_args = parser.parse_args() |
| 366 |
|
| 367 |
kvp = {} |
| 368 |
args_type = parsed_args.type |
| 369 |
|
| 370 |
for field in dataclasses.fields(args_type): |
| 371 |
kvp[field.name] = getattr(parsed_args, field.name) |
| 372 |
|
| 373 |
args = args_type(**kvp) |
| 374 |
|
| 375 |
return args |
| 376 |
|
| 377 |
|
| 378 |
def run_sanity_test(test_name: str) -> list[str]: |
| 379 |
cmd = ['ansible-test', 'sanity', '--test', test_name, '--lint', '--failure-ok'] |
| 380 |
skip_path = 'test/sanity/code-smell/skip.txt' |
| 381 |
skip_temp_path = skip_path + '.tmp' |
| 382 |
|
| 383 |
os.rename(skip_path, skip_temp_path) # make sure ansible-test isn't configured to skip any tests |
| 384 |
|
| 385 |
try: |
| 386 |
process = subprocess.run(cmd, capture_output=True, check=True) |
| 387 |
finally: |
| 388 |
os.rename(skip_temp_path, skip_path) # restore the skip entries |
| 389 |
|
| 390 |
messages = process.stdout.decode().splitlines() |
| 391 |
|
| 392 |
return messages |
| 393 |
|
| 394 |
|
| 395 |
def create_issues_from_deprecation_messages(test_type: t.Type[Deprecation], messages: list[str]) -> list[Issue]: |
| 396 |
deprecations = [test_type.parse(message) for message in messages] |
| 397 |
bug_reports = [deprecation.create_bug_report() for deprecation in deprecations] |
| 398 |
issues = [bug_report.create_issue(PROJECT) for bug_report in bug_reports] |
| 399 |
return issues |
| 400 |
|
| 401 |
|
| 402 |
def info(message: str) -> None: |
| 403 |
print(message, file=sys.stderr) |
| 404 |
|
| 405 |
|
| 406 |
def main() -> None: |
| 407 |
args = parse_args() |
| 408 |
args.run() |
| 409 |
|
| 410 |
|
| 411 |
def deprecated_command(args: DeprecationArgs) -> None: |
| 412 |
issues: list[Issue] = [] |
| 413 |
|
| 414 |
for test in args.tests or list(TEST_OPTIONS): |
| 415 |
test_type = TEST_OPTIONS[test] |
| 416 |
info(f'Running "{test}" sanity test...') |
| 417 |
messages = run_sanity_test(test) |
| 418 |
issues.extend(create_issues_from_deprecation_messages(test_type, messages)) |
| 419 |
|
| 420 |
create_issues(args, issues) |
| 421 |
|
| 422 |
|
| 423 |
def feature_command(args: FeatureArgs) -> None: |
| 424 |
with args.source.open() as source_file: |
| 425 |
source = yaml.safe_load(source_file) |
| 426 |
|
| 427 |
default: dict[str, t.Any] = source.get('default', {}) |
| 428 |
features: list[dict[str, t.Any]] = source.get('features', []) |
| 429 |
|
| 430 |
if not isinstance(default, dict): |
| 431 |
raise RuntimeError('`default` must be `dict[str, ...]`') |
| 432 |
|
| 433 |
if not isinstance(features, list): |
| 434 |
raise RuntimeError('`features` must be `list[dict[str, ...]]`') |
| 435 |
|
| 436 |
issues: list[Issue] = [] |
| 437 |
|
| 438 |
for feature in features: |
| 439 |
data = default.copy() |
| 440 |
data.update(feature) |
| 441 |
|
| 442 |
feature = Feature.from_dict(data) |
| 443 |
issues.append(feature.create_issue(PROJECT)) |
| 444 |
|
| 445 |
create_issues(args, issues) |
| 446 |
|
| 447 |
|
| 448 |
def create_issues(args: Args, issues: list[Issue]) -> None: |
| 449 |
if not issues: |
| 450 |
info('No issues found.') |
| 451 |
return |
| 452 |
|
| 453 |
info(f'Found {len(issues)} issue(s) to report:') |
| 454 |
|
| 455 |
for issue in issues: |
| 456 |
info(f'[{issue.title}] {issue.summary}') |
| 457 |
|
| 458 |
if args.verbose: |
| 459 |
info('>>>') |
| 460 |
info(issue.body) |
| 461 |
info('>>>') |
| 462 |
|
| 463 |
if args.create: |
| 464 |
url = issue.create() |
| 465 |
info(url) |
| 466 |
|
| 467 |
if not args.create: |
| 468 |
info('Pass the "--create" option to create these issues on GitHub.') |
| 469 |
|
| 470 |
|
| 471 |
if __name__ == '__main__': |
| 472 |
main() |