diff options
Diffstat (limited to 'tools/webhook_installer/installer.py')
-rw-r--r-- | tools/webhook_installer/installer.py | 189 |
1 files changed, 117 insertions, 72 deletions
diff --git a/tools/webhook_installer/installer.py b/tools/webhook_installer/installer.py index 362d51a..9cd74c6 100644 --- a/tools/webhook_installer/installer.py +++ b/tools/webhook_installer/installer.py @@ -32,6 +32,9 @@ parser.add_argument("--add", action="http://code.qt.io/store_true", help="Add webhooks.config to r " have it. Ignored if --repos is not specified") parser.add_argument("--production", action="http://code.qt.io/store_true", help="Update webhooks.config for" " production, defaults to staging if not set") +parser.add_argument("--update-rootconfig", action="http://code.qt.io/store_true", help="Update webhooks.config for the" + " 'All-Projects' repository with specific jira-closer-bot and " + "gerrit-webhooks-forwarder settings. Other repo arguments are ignored.") parser.add_argument("--updateConfig", action="http://code.qt.io/store_true", help="Update the entire" " qt-cherry-pick-bot section if it differs from the template") args = parser.parse_args() @@ -55,6 +58,26 @@ gerrit_repos_without_webhooks = [] gerrit_repos_with_webhooks_not_updated = [] +# Configuration for "All-Projects" repository when --update-rootconfig is used +ROOT_CONFIG_ALL_PROJECTS_PROD = """[remote "jira-closer-bot"] + url = https://qt-cherry-pick-bot.herokuapp.com/jiracloser + maxTries = 10 + event = change-merged + +[remote "gerrit-webhooks-forwarder"] + url = http://10.212.0.77:8080 + maxTries = 10""" + +ROOT_CONFIG_ALL_PROJECTS_STAGING = """[remote "jira-closer-bot"] + url = https://qt-cherry-pick-bot-staging.herokuapp.com/jiracloser + maxTries = 10 + event = change-merged + +# [remote "gerrit-webhooks-forwarder"] +# url = http://10.212.0.77:8080 +# maxTries = 10""" + + CONFIG_SECTION_HEADER = '[remote "qt-cherry-pick-bot"]' CONFIG_SECTION_HEADER_ESC = r'\[remote "qt-cherry-pick-bot"\]' # Escaped for regex @@ -301,84 +324,108 @@ def update_gerrit_repo_webhooks(repo, webhooks_config): def main(): - if len(args.repos) > 0: - gerrit_repos = [quote(repo, safe='') for repo in args.repos] + if args.update_rootconfig: + repo_name = "All-Projects" + encoded_repo_name = quote(repo_name, safe='') + config_content_root: str + env_type: str + + if args.production: + config_content_root = ROOT_CONFIG_ALL_PROJECTS_PROD + env_type = "production" + else: + config_content_root = ROOT_CONFIG_ALL_PROJECTS_STAGING + env_type = "staging" + + logger.info(f"Processing --update-rootconfig for '{repo_name}' with {env_type} settings.") + update_gerrit_repo_webhooks(encoded_repo_name, config_content_root) else: - gerrit_repos = get_gerrit_repos() - - # Regex to find the section header and capture everything until the next section or EOF - # DOTALL allows . to match newline, non-greedy .*? ensures we stop at the first \n[ or EOF - section_re = re.compile(rf"({CONFIG_SECTION_HEADER_ESC}(?:.|\n)*?(?=\n\[|$))", re.DOTALL) - # Regex to find the URL line within the section - url_re = re.compile(r"^\s*url\s*=\s*(.*)", re.MULTILINE) - - for repo in gerrit_repos: - needs_update = False - new_webhooks_config = None - webhooks_config = get_gerrit_repo_webhooks(repo) - - if webhooks_config: - section_match = section_re.search(webhooks_config) - if section_match: - existing_section = section_match.group(1).strip() - if args.updateConfig: - # Compare entire section with template - if existing_section != WEBHOOK_TEMPLATE.strip(): - logger.info("Config section differs from template for repo %s", unquote(repo)) - new_webhooks_config = section_re.sub(WEBHOOK_TEMPLATE, webhooks_config, count=1) - needs_update = True - else: - logger.info("Config section already matches template for repo %s", unquote(repo)) - gerrit_repos_with_webhooks_not_updated.append(repo) - else: - # Only update the URL line within the existing section if --updateConfig is not set - url_match = url_re.search(existing_section) - if url_match: - existing_url = url_match.group(1).strip() - if existing_url != TARGET_URL: - logger.info("Webhook URL needs update for repo %s", unquote(repo)) - # Replace only the URL line within the original config - new_url_line = f" url = {TARGET_URL}" - new_webhooks_config = url_re.sub(new_url_line, webhooks_config, count=1) + if len(args.repos) > 0: + gerrit_repos = [quote(repo, safe='') for repo in args.repos] + else: + gerrit_repos = get_gerrit_repos() + + # Regex to find the section header and capture everything until the next section or EOF + # DOTALL allows . to match newline, non-greedy .*? ensures we stop at the first \n[ or EOF + section_re = re.compile(rf"({CONFIG_SECTION_HEADER_ESC}(?:.|\n)*?(?=\n\[|$))", re.DOTALL) + # Regex to find the URL line within the section + url_re = re.compile(r"^\s*url\s*=\s*(.*)", re.MULTILINE) + + for repo in gerrit_repos: + needs_update = False + new_webhooks_config = None + webhooks_config = get_gerrit_repo_webhooks(repo) + + if webhooks_config: + section_match = section_re.search(webhooks_config) + if section_match: + existing_section = section_match.group(1).strip() + if args.updateConfig: + # Compare entire section with template + if existing_section != WEBHOOK_TEMPLATE.strip(): + logger.info("Config section differs from template for repo %s", unquote(repo)) + new_webhooks_config = section_re.sub(WEBHOOK_TEMPLATE, webhooks_config, count=1) needs_update = True else: - logger.info("Webhook URL already up-to-date for repo %s", unquote(repo)) + logger.info("Config section already matches template for repo %s", unquote(repo)) gerrit_repos_with_webhooks_not_updated.append(repo) else: - logger.error("Could not find URL line in existing section for repo %s", unquote(repo)) - gerrit_repos_with_webhooks_to_update_failed.append(repo) + # Only update the URL line within the existing section if --updateConfig is not set + url_match = url_re.search(existing_section) + if url_match: + existing_url = url_match.group(1).strip() + if existing_url != TARGET_URL: + logger.info("Webhook URL needs update for repo %s", unquote(repo)) + # Replace only the URL line within the original config + new_url_line = f" url = {TARGET_URL}" + # We need to be careful to replace the URL only within the matched section + # One way is to replace within existing_section and then replace existing_section in webhooks_config + updated_section = url_re.sub(new_url_line, existing_section, count=1) + new_webhooks_config = webhooks_config.replace(existing_section, updated_section, 1) + needs_update = True + else: + logger.info("Webhook URL already up-to-date for repo %s", unquote(repo)) + gerrit_repos_with_webhooks_not_updated.append(repo) + else: + # This case implies the section exists but has no URL, which is unlikely for this template + # but if it happens, and --updateConfig is false, we might want to add the URL or log an error. + # For now, let's assume if --updateConfig is false, we only modify existing URLs. + # If --updateConfig was true, the whole section would be replaced. + logger.error("Could not find URL line in existing section for repo %s, and --updateConfig is false.", unquote(repo)) + gerrit_repos_with_webhooks_to_update_failed.append(repo) + elif args.add: + # Section header not found, but file exists and --add is specified + logger.info("Adding webhook section to existing config for repo %s", unquote(repo)) + # Append template, ensuring a newline separates it from existing content if webhooks_config is not empty + separator = "\n" if webhooks_config.strip() else "" + new_webhooks_config = webhooks_config + separator + WEBHOOK_TEMPLATE + needs_update = True + else: + # Section header not found, file exists, --add not specified + logger.info("No %s section in config for %s and --add not specified.", + CONFIG_SECTION_HEADER, unquote(repo)) + gerrit_repos_with_webhooks_not_updated.append(repo) # Treat as not updated elif args.add: - # Section header not found, but file exists and --add is specified - logger.info("Adding webhook section to existing config for repo %s", unquote(repo)) - # Prepend template, ensuring a newline separates it from existing content - new_webhooks_config = WEBHOOK_TEMPLATE + "\n" + webhooks_config + # webhooks.config file does not exist and --add is specified + logger.info("Creating webhooks.config for repo %s", unquote(repo)) + new_webhooks_config = WEBHOOK_TEMPLATE needs_update = True else: - # Section header not found, file exists, --add not specified - logger.info("No %s section in config for %s and --add not specified.", - CONFIG_SECTION_HEADER, unquote(repo)) - gerrit_repos_with_webhooks_not_updated.append(repo) # Treat as not updated - elif args.add: - # webhooks.config file does not exist and --add is specified - logger.info("Creating webhooks.config for repo %s", unquote(repo)) - new_webhooks_config = WEBHOOK_TEMPLATE - needs_update = True - else: - # webhooks.config file does not exist, --add not specified - logger.info("No webhooks.config for %s and --add not specified.", unquote(repo)) - - if needs_update and new_webhooks_config is not None: - gerrit_repos_with_webhooks.append(repo) # Mark as attempted - if update_gerrit_repo_webhooks(repo, new_webhooks_config): - logger.info("Successfully updated/added webhook config for repo %s", unquote(repo)) - else: - # update_gerrit_repo_webhooks logs errors and adds to failed list - logger.error("Failed to update/add webhook config for repo %s", unquote(repo)) - elif needs_update and new_webhooks_config is None: - # Should not happen if logic is correct, but good to catch - logger.error("Internal error: Update flagged but no new config generated for %s", unquote(repo)) - gerrit_repos_with_webhooks_to_update_failed.append(repo) - + # webhooks.config file does not exist, --add not specified + logger.info("No webhooks.config for %s and --add not specified.", unquote(repo)) + gerrit_repos_without_webhooks.append(repo) # Keep track of repos without webhooks + + if needs_update and new_webhooks_config is not None: + gerrit_repos_with_webhooks.append(repo) # Mark as attempted + if update_gerrit_repo_webhooks(repo, new_webhooks_config): + logger.info("Successfully updated/added webhook config for repo %s", unquote(repo)) + else: + # update_gerrit_repo_webhooks logs errors and adds to failed list + logger.error("Failed to update/add webhook config for repo %s", unquote(repo)) + elif needs_update and new_webhooks_config is None: + # Should not happen if logic is correct, but good to catch + logger.error("Internal error: Update flagged but no new config generated for %s", unquote(repo)) + gerrit_repos_with_webhooks_to_update_failed.append(repo) logger.info("Summary:") logger.info("Succeeded: %s", str([unquote(repo) for repo in gerrit_repos_with_webhooks_to_update_succeeded])) @@ -388,8 +435,6 @@ def main(): str([unquote(repo) for repo in gerrit_repos_with_webhooks_not_updated])) logger.info("Repos without webhooks: %s", str([unquote(repo) for repo in gerrit_repos_without_webhooks])) - logger.info("Repos without webhooks: %s", - str([unquote(repo) for repo in gerrit_repos_without_webhooks])) logger.info("Repos with webhooks that were not updated: %s", str([unquote(repo) for repo in gerrit_repos_with_webhooks_not_updated])) |