From b1086b7dc55e23b7c1619ccc58c742bb66227702 Mon Sep 17 00:00:00 2001 From: Patrick Donnelly Date: Thu, 20 Jun 2024 11:43:17 -0400 Subject: [PATCH] script/backport-create-issue: update tag custom field The redmineup_tags was added and, apparently, the old "Tags" custom field with id == 3 was deleted and then recreated with id == 31. This broke the script. Update and refactor the id for that "Tags" custom field. Signed-off-by: Patrick Donnelly --- src/script/backport-create-issue | 54 ++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/src/script/backport-create-issue b/src/script/backport-create-issue index 9fb627c6fac77..e265c164db074 100755 --- a/src/script/backport-create-issue +++ b/src/script/backport-create-issue @@ -39,9 +39,9 @@ from redminelib.exceptions import ResourceAttrError redmine_endpoint = "https://tracker.ceph.com" project_name = "Ceph" release_id = 16 -custom_field_tag = 'cf_3' -tag_separator = ' ' -tag_backport_processed = 'backport_processed' +CF_TAGS = 31 # Tags custom field: https://tracker.ceph.com/custom_fields/31/edit +TAG_SEPARATOR = ' ' +TAG_BACKPORT_PROCESSED = 'backport_processed' delay_seconds = 5 redmine_key_file="~/.redmine_key" redmine_key_env="REDMINE_API_KEY" @@ -87,7 +87,7 @@ def parse_arguments(): "them even if not in 'Pending Backport' status. " "Otherwise, process all issues in 'Pending Backport' " "status even if already processed " - f"(tag '{tag_backport_processed}' added)", + f"(tag '{TAG_BACKPORT_PROCESSED}' added)", action="store_true") return parser.parse_args() @@ -305,20 +305,34 @@ def mark_as_processed(r, issue): This script will add a custom Tag to indicate whether the tracker was already processed for backport tracker creation. """ - custom_fields = list(issue['custom_fields'].values()) - for i, field in enumerate(custom_fields): - if field['name'] == 'Tags': - if tag_backport_processed not in field['value']: - if field['value']: - custom_fields[i]['value'] += (tag_separator + - tag_backport_processed) - else: - custom_fields[i]['value'] = tag_backport_processed - logging.info("%s adding tag '%s'", url(issue), - tag_backport_processed) - r.issue.update(issue.id, custom_fields=custom_fields) - return + logging.debug("custom_fields: %s", list(issue['custom_fields'])) + + tags_cf = next(filter(lambda x: x['id'] == CF_TAGS, issue['custom_fields']), None) + + if tags_cf is None: + tags = '' + else: + tags = tags_cf.value + if tags is None: + tags = '' + else: + tags.strip() + + if TAG_BACKPORT_PROCESSED not in tags: + if tags: + tags += f"{TAG_SEPARATOR}{TAG_BACKPORT_PROCESSED}" + else: + tags = TAG_BACKPORT_PROCESSED + + logging.info("%s adding tag '%s', now '%s'", url(issue), TAG_BACKPORT_PROCESSED, tags) + tags_cf = { + 'id': CF_TAGS, + 'value': tags, + } + r.issue.update(issue.id, custom_fields=[tags_cf]) + else: + logging.debug("%s already has tag '%s'", url(issue), TAG_BACKPORT_PROCESSED) def iterate_over_backports(r, issues, dry_run=False): counter = 0 @@ -377,7 +391,7 @@ if __name__ == '__main__': if args.force or args.resolve_parent: if args.force: logging.warn("--force option was given: ignoring '%s' tag!", - tag_backport_processed) + TAG_BACKPORT_PROCESSED) issues = redmine.issue.filter(project_id=ceph_project_id, status_id=pending_backport_status_id) else: @@ -385,9 +399,9 @@ if __name__ == '__main__': issues = redmine.issue.filter(project_id=ceph_project_id, status_id=pending_backport_status_id, **{ - custom_field_tag: + f"cf_{CF_TAGS}": '!~' + - tag_backport_processed}) + TAG_BACKPORT_PROCESSED}) if force_create: logging.info("Processing {} issues regardless of status" .format(len(issues))) -- 2.39.5