|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +This script automates releasing a version in Jira and creating the next one. |
| 6 | +""" |
| 7 | + |
| 8 | +import argparse |
| 9 | +import os |
| 10 | +import sys |
| 11 | +import datetime |
| 12 | +from jira import JIRA |
| 13 | +from jira.exceptions import JIRAError |
| 14 | + |
| 15 | +# Jira server URLs |
| 16 | +JIRA_SANDBOX_URL = "https://sonarsource-sandbox-608.atlassian.net/" |
| 17 | +JIRA_PROD_URL = "https://sonarsource.atlassian.net/" |
| 18 | + |
| 19 | +def eprint(*args, **kwargs): |
| 20 | + """Prints messages to the standard error stream (stderr) for logging.""" |
| 21 | + print(*args, file=sys.stderr, **kwargs) |
| 22 | + |
| 23 | + |
| 24 | +# noinspection DuplicatedCode |
| 25 | +def get_jira_instance(use_sandbox=False): |
| 26 | + """ |
| 27 | + Initializes and returns a JIRA client instance. |
| 28 | + Authentication is handled via environment variables. |
| 29 | + """ |
| 30 | + jira_user = os.environ.get('JIRA_USER') |
| 31 | + jira_token = os.environ.get('JIRA_TOKEN') |
| 32 | + |
| 33 | + if not jira_user or not jira_token: |
| 34 | + eprint("Error: JIRA_USER and JIRA_TOKEN environment variables must be set.") |
| 35 | + sys.exit(1) |
| 36 | + |
| 37 | + jira_url = JIRA_SANDBOX_URL if use_sandbox else JIRA_PROD_URL |
| 38 | + |
| 39 | + eprint(f"Connecting to JIRA server at: {jira_url}") |
| 40 | + try: |
| 41 | + jira_client = JIRA(jira_url, basic_auth=(jira_user, jira_token)) |
| 42 | + # Verify connection |
| 43 | + jira_client.server_info() |
| 44 | + eprint("JIRA authentication successful.") |
| 45 | + return jira_client |
| 46 | + except JIRAError as e: |
| 47 | + eprint(f"Error: JIRA authentication failed. Status: {e.status_code}") |
| 48 | + eprint(f"Response text: {e.text}") |
| 49 | + sys.exit(1) |
| 50 | + except Exception as e: |
| 51 | + eprint(f"An unexpected error occurred during JIRA connection: {e}") |
| 52 | + sys.exit(1) |
| 53 | + |
| 54 | +def increment_version_string(version_name): |
| 55 | + """ |
| 56 | + Increments the last component of a version string (e.g., '1.2.3' -> '1.2.4'). |
| 57 | + """ |
| 58 | + parts = version_name.split('.') |
| 59 | + try: |
| 60 | + parts[-1] = str(int(parts[-1]) + 1) |
| 61 | + return ".".join(parts) |
| 62 | + except (ValueError, IndexError): |
| 63 | + eprint(f"Error: Could not auto-increment version '{version_name}'. It does not seem to follow a standard x.y.z format.") |
| 64 | + sys.exit(1) |
| 65 | + |
| 66 | +def main(): |
| 67 | + """Main function to orchestrate the release and creation process.""" |
| 68 | + parser = argparse.ArgumentParser( |
| 69 | + description="Releases a Jira version and creates the next one.", |
| 70 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 71 | + ) |
| 72 | + parser.add_argument("--project-key", required=True, help="The key of the Jira project (e.g., SONARIAC).") |
| 73 | + parser.add_argument("--jira-release-name", required=True, help="The name of the version to release.") |
| 74 | + parser.add_argument("--new-version-name", default="", help="The name for the next version.") |
| 75 | + parser.add_argument('--use-sandbox', action='store_true', help="Use the sandbox Jira server.") |
| 76 | + args = parser.parse_args() |
| 77 | + |
| 78 | + jira = get_jira_instance(args.use_sandbox) |
| 79 | + |
| 80 | + eprint(f"Searching for version '{args.jira_release_name}' in project '{args.project_key}'...") |
| 81 | + try: |
| 82 | + versions = jira.project_versions(args.project_key) |
| 83 | + except JIRAError as e: |
| 84 | + eprint(f"Error: Could not fetch versions for project '{args.project_key}'. Status: {e.status_code}") |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + version_to_release = None |
| 88 | + for v in versions: |
| 89 | + if v.name == args.jira_release_name: |
| 90 | + version_to_release = v |
| 91 | + break |
| 92 | + |
| 93 | + if not version_to_release: |
| 94 | + eprint(f"Error: Version '{args.jira_release_name}' not found in project '{args.project_key}'.") |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + if version_to_release.released: |
| 98 | + eprint(f"Warning: Version '{version_to_release.name}' is already released. Skipping release step.") |
| 99 | + else: |
| 100 | + eprint(f"Found version '{version_to_release.name}'. Releasing it now...") |
| 101 | + try: |
| 102 | + today = datetime.date.today().strftime('%Y-%m-%d') |
| 103 | + version_to_release.update(released=True, releaseDate=today) |
| 104 | + eprint(f"✅ Successfully released version '{version_to_release.name}'.") |
| 105 | + except JIRAError as e: |
| 106 | + eprint(f"Error: Failed to release version. Status: {e.status_code}, Text: {e.text}") |
| 107 | + sys.exit(1) |
| 108 | + |
| 109 | + |
| 110 | + if args.new_version_name: |
| 111 | + new_name = args.new_version_name |
| 112 | + eprint(f"Using provided name for new version: '{new_name}'.") |
| 113 | + else: |
| 114 | + new_name = increment_version_string(args.jira_release_name) |
| 115 | + eprint(f"Auto-incremented version name to: '{new_name}'.") |
| 116 | + |
| 117 | + eprint(f"Creating new version '{new_name}'...") |
| 118 | + try: |
| 119 | + new_version = jira.create_version(name=new_name, project=args.project_key) |
| 120 | + eprint(f"✅ Successfully created new version '{new_version.name}'.") |
| 121 | + except JIRAError as e: |
| 122 | + if "A version with this name already exists" in e.text: |
| 123 | + eprint(f"Warning: Version '{new_name}' already exists. Skipping creation.") |
| 124 | + else: |
| 125 | + eprint(f"Error: Failed to create new version. Status: {e.status_code}, Text: {e.text}") |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + print(f"new_version_name={new_name}") |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + main() |
0 commit comments