|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Copyright (c) 2024 Red Hat, Inc. |
| 4 | +This program, and the accompanying materials are made |
| 5 | +available under the terms of the Apache Public License 2.0, |
| 6 | +available at http://www.apache.org/licenses/ |
| 7 | +
|
| 8 | +SPDX-License-Identifier: Apache-2.0 |
| 9 | +
|
| 10 | +Prerequisites: |
| 11 | +$ pip3 install --requirement requirements.txt |
| 12 | +
|
| 13 | +Generate the AsciiDoc files for the release notes and known issues from JIRA content. |
| 14 | +""" |
| 15 | +import glob |
| 16 | +import jinja2 |
| 17 | +import os |
| 18 | +import yaml |
| 19 | +from jira import JIRA |
| 20 | + |
| 21 | +# Define location for product attributes, templates, and generated files. |
| 22 | +root_dir = os.path.normpath( |
| 23 | + os.path.normpath( |
| 24 | + os.path.dirname( |
| 25 | + __file__ |
| 26 | + ) |
| 27 | + ) + '/../..' |
| 28 | +) |
| 29 | +product_attributes = root_dir + '/artifacts/product-attributes.adoc' |
| 30 | +templates_dir = root_dir + '/build/templates/' |
| 31 | +assemblies_dir = root_dir + '/assemblies/' |
| 32 | +modules_dir = root_dir + '/modules/release-notes/' |
| 33 | +# Load Jinja2 templates. |
| 34 | +env = jinja2.Environment( |
| 35 | + loader=jinja2.FileSystemLoader( |
| 36 | + templates_dir |
| 37 | + ) |
| 38 | +) |
| 39 | +# Load configuration file |
| 40 | +with open( |
| 41 | + root_dir + '/jira2asciidoc.yml', |
| 42 | + 'r' |
| 43 | +) as file: |
| 44 | + config = yaml.safe_load(file) |
| 45 | +# Load AsciiDoc attributes. |
| 46 | +product_version_minor_glob = config['product']['version']['minor_glob'] |
| 47 | +product_version_patch = config['product']['version']['patch'] |
| 48 | +# Configure access to Jira using kerberos |
| 49 | +jira = JIRA( |
| 50 | + server=config['jira']['server'], |
| 51 | + token_auth=os.environ.get( |
| 52 | + 'JIRA_TOKEN' |
| 53 | + ) |
| 54 | +) |
| 55 | +# Delete old file files. |
| 56 | +fileList = glob.glob( |
| 57 | + modules_dir + 'snip-*.adoc' |
| 58 | +) |
| 59 | +for filePath in fileList: |
| 60 | + os.remove(filePath) |
| 61 | +# Generate the release notes and known issues assemblies and files |
| 62 | +for section in config['sections']: |
| 63 | + # Search in Jira for issues to publish defined in jira_query |
| 64 | + query = section["query"].format( |
| 65 | + version_minor_glob=product_version_minor_glob, |
| 66 | + version_patch=product_version_patch |
| 67 | + ) |
| 68 | + print(query) |
| 69 | + issues = jira.search_issues(query) |
| 70 | + # Create the assembly file |
| 71 | + assembly_file = open( |
| 72 | + assemblies_dir + 'assembly-release-notes-' + section["id"] + '.adoc', |
| 73 | + 'w' |
| 74 | + ) |
| 75 | + assembly_template = env.get_template( |
| 76 | + 'assembly.adoc.jinja' |
| 77 | + ) |
| 78 | + print( |
| 79 | + assembly_template.render( |
| 80 | + assembly_id=section["id"], |
| 81 | + assembly_title=section["title"], |
| 82 | + assembly_introduction=section["description"], |
| 83 | + vars=issues, |
| 84 | + ), |
| 85 | + file=assembly_file |
| 86 | + ) |
| 87 | + # Create the file files |
| 88 | + for issue in issues: |
| 89 | + # Collect values from these fields: |
| 90 | + issue_key = format(issue.key) # Issue id |
| 91 | + issue_rn_status = format(issue.fields.customfield_12310213) # Release Note Status |
| 92 | + issue_rn_text = format(issue.fields.customfield_12317313) # Release Note Text |
| 93 | + issue_rn_type = format(issue.fields.customfield_12320850) # Release Note Type |
| 94 | + issue_template = section["template"] |
| 95 | + issue_title = format(issue.fields.summary) # Issue title |
| 96 | + # Define AsciiDoc file id, file, and content |
| 97 | + file_id = format(issue_rn_type + "-" + issue_key).lower().replace(" ", "-") |
| 98 | + snippet_file = open( |
| 99 | + modules_dir + 'snip-' + file_id + '.adoc', |
| 100 | + 'w' |
| 101 | + ) |
| 102 | + snippet_template = env.get_template( |
| 103 | + 'snippet-' + issue_template + '.adoc.jinja2' |
| 104 | + ) |
| 105 | + print( |
| 106 | + snippet_template.render( |
| 107 | + id=file_id, |
| 108 | + key=issue_key, |
| 109 | + text=issue_rn_text, |
| 110 | + title=issue_title, |
| 111 | + ), |
| 112 | + file=snippet_file |
| 113 | + ) |
| 114 | +# Report final status |
| 115 | +print( |
| 116 | + 'INFO: Single-sourced release notes from Jira for version {version} in {dir}' |
| 117 | + .format( |
| 118 | + version=product_version_patch, |
| 119 | + dir=modules_dir |
| 120 | + ) |
| 121 | +) |
0 commit comments