|
| 1 | +# Utility Guide: JiraConfluenceUtil |
| 2 | + |
| 3 | +The `JiraConfluenceUtil` utility provides methods for interacting with Jira and Confluence, specifically for uploading Playwright test results and metadata to Jira tickets. |
| 4 | + |
| 5 | +> NOTE: For most use cases, you should use the [jira_upload.py](../../jira_upload.py) script as outlined in the [README](../../README.md). |
| 6 | +> This guide is for developers who want to use the utility directly in custom workflows or scripts. |
| 7 | +
|
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Utility Guide: JiraConfluenceUtil](#utility-guide-jiraconfluenceutil) |
| 11 | + - [Table of Contents](#table-of-contents) |
| 12 | + - [Using the JiraConfluenceUtil class](#using-the-jiraconfluenceutil-class) |
| 13 | + - [Required Environment Variables](#required-environment-variables) |
| 14 | + - [Initialise the class](#initialise-the-class) |
| 15 | + - [Public Methods](#public-methods) |
| 16 | + - [`get_issue_data`](#get_issue_data) |
| 17 | + - [`get_issue_summary_in_issue_data`](#get_issue_summary_in_issue_data) |
| 18 | + - [`check_attachment_exists_in_issue_data`](#check_attachment_exists_in_issue_data) |
| 19 | + - [`is_valid_jira_reference`](#is_valid_jira_reference) |
| 20 | + - [`determine_jira_reference_local`](#determine_jira_reference_local) |
| 21 | + - [`get_environment_metadata_if_available`](#get_environment_metadata_if_available) |
| 22 | + - [`is_file_is_less_than_jira_file_limit`](#is_file_is_less_than_jira_file_limit) |
| 23 | + - [`upload_test_results_dir_to_jira`](#upload_test_results_dir_to_jira) |
| 24 | + - [Example Usage](#example-usage) |
| 25 | + |
| 26 | +## Using the JiraConfluenceUtil class |
| 27 | + |
| 28 | +### Required Environment Variables |
| 29 | + |
| 30 | +The following environment variables need to be set (in local.env if running locally) for any Jira-based actions: |
| 31 | + |
| 32 | +- **JIRA_URL**: The Jira instance to upload to. |
| 33 | +- **JIRA_PROJECT_KEY**: The Jira project key that should be uploaded to. |
| 34 | +- **JIRA_API_KEY**: The API key to use to complete actions. Locally you should generate your own key, and use a bot in a pipeline/workflow. |
| 35 | + |
| 36 | +The following environment variables are optional: |
| 37 | + |
| 38 | +- **JIRA_TICKET_REFERENCE**: The Jira ticket to push to if set. If not, will attempt to derive the value from the git branch. |
| 39 | + |
| 40 | +The following environment variables need to be set for any Confluence-based actions: |
| 41 | + |
| 42 | +- **CONFLUENCE_URL**: The Confluence instance to upload to. |
| 43 | +- **CONFLUENCE_API_KEY**: The API key to use to complete actions. Locally you should generate your own key, and use a bot in a pipeline/workflow. |
| 44 | + |
| 45 | +### Initialise the class |
| 46 | + |
| 47 | +You can initialise the class by importing and creating an instance: |
| 48 | + |
| 49 | +```python |
| 50 | +from utils.jira_confluence_util import JiraConfluenceUtil |
| 51 | + |
| 52 | +util = JiraConfluenceUtil() |
| 53 | +``` |
| 54 | + |
| 55 | +You can also specify a custom results directory: |
| 56 | + |
| 57 | +```python |
| 58 | +util = JiraConfluenceUtil(results_dir="path/to/results") |
| 59 | +``` |
| 60 | + |
| 61 | +## Public Methods |
| 62 | + |
| 63 | +### `get_issue_data` |
| 64 | + |
| 65 | +```python |
| 66 | +get_issue_data(ticket_id: str) -> dict | None |
| 67 | +``` |
| 68 | + |
| 69 | +Checks if a Jira issue exists and returns its data as a dictionary, or `None` if not found. |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +### `get_issue_summary_in_issue_data` |
| 74 | + |
| 75 | +```python |
| 76 | +get_issue_summary_in_issue_data(issue_data: dict) -> str | None |
| 77 | +``` |
| 78 | + |
| 79 | +Returns a summary string for the given Jira issue data in the format "[Ticket]: [Summary Line]", or `None` if not available. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### `check_attachment_exists_in_issue_data` |
| 84 | + |
| 85 | +```python |
| 86 | +check_attachment_exists_in_issue_data(issue_data: dict, filename: str) -> bool |
| 87 | +``` |
| 88 | + |
| 89 | +Checks if a Jira issue already has an attachment with the specified filename. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +### `is_valid_jira_reference` |
| 94 | + |
| 95 | +```python |
| 96 | +is_valid_jira_reference(ticket_id: str) -> bool |
| 97 | +``` |
| 98 | + |
| 99 | +Validates that the Jira ticket reference is in the expected format (e.g., `SCM-1234` or `BSS2-5678`). |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +### `determine_jira_reference_local` |
| 104 | + |
| 105 | +```python |
| 106 | +determine_jira_reference_local() -> str |
| 107 | +``` |
| 108 | + |
| 109 | +Determines the Jira ticket reference from the current git branch or if `JIRA_TICKET_REFERENCE` has been set. |
| 110 | + |
| 111 | +This is currently configured to search for the format `feature/[Jira Reference]`, so for example: |
| 112 | + |
| 113 | +- `feature/TEST-1234` would return `TEST-1234`. |
| 114 | +- `feature/TEST-2345-feature-name` would return `TEST-2345`. |
| 115 | + |
| 116 | +> NOTE: Depending on your projects branch naming strategy, you may want to modify this method to suit your needs |
| 117 | +> accordingly. |
| 118 | +
|
| 119 | +--- |
| 120 | + |
| 121 | +### `get_environment_metadata_if_available` |
| 122 | + |
| 123 | +```python |
| 124 | +get_environment_metadata_if_available() -> str |
| 125 | +``` |
| 126 | + |
| 127 | +This is method designed to return metadata for the environment under test. In this project, it is a stub method |
| 128 | +designed to be overwritten. |
| 129 | + |
| 130 | +> NOTE: You will need to populate this method with the code required to get the metadata for your environment. |
| 131 | +> It is heavily recommended that you populate `results.json` with the data required, and then read the file |
| 132 | +> using this method to extract the data required. |
| 133 | +
|
| 134 | +--- |
| 135 | + |
| 136 | +### `is_file_is_less_than_jira_file_limit` |
| 137 | + |
| 138 | +```python |
| 139 | +is_file_is_less_than_jira_file_limit(file_path: Path) -> bool |
| 140 | +``` |
| 141 | + |
| 142 | +Checks if the file size is below the Jira upload limit (10MB). |
| 143 | + |
| 144 | +--- |
| 145 | + |
| 146 | +### `upload_test_results_dir_to_jira` |
| 147 | + |
| 148 | +```python |
| 149 | +upload_test_results_dir_to_jira( |
| 150 | + ticket_id: str, |
| 151 | + overwrite_files: bool = True, |
| 152 | + include_html: bool = True, |
| 153 | + include_trace_files: bool = True, |
| 154 | + include_screenshots: bool = True, |
| 155 | + include_csv: bool = True, |
| 156 | + include_env_metadata: bool = True, |
| 157 | + add_comment: bool = True, |
| 158 | + automatically_accept: bool = False, |
| 159 | +) -> None |
| 160 | +``` |
| 161 | + |
| 162 | +Uploads files from the results directory to the specified Jira ticket. |
| 163 | +Options allow you to control which file types are included, whether to overwrite existing files, add a comment, and auto-confirm the upload. |
| 164 | + |
| 165 | +For any files over 10MB, they will **not** be uploaded using this method as they will exceed the default file size limit |
| 166 | +set for Jira. |
| 167 | + |
| 168 | +Each of the following arguments relate to the following actions: |
| 169 | + |
| 170 | +- `overwrite_files` = If the file already exists on Jira, it will overwrite the file and use the same name if True. If false, it'll generate a unique filename based on the date/time of the upload. |
| 171 | +- `include_html` = Will check for any `.html` files in the root of the `results_dir` provided and include them if under 10MB if True. |
| 172 | +- `include_trace_files` = Will check for any `.zip` files in subdirectories of the `results_dir` provided and include them if under 10MB if True, renaming the file to include the subdirectory name. |
| 173 | +- `include_screenshots` = Will check for any `.png` files in the rood directory `results_dir` provided and the `screenshot/` subdirectory and include them if under 10MB if True. |
| 174 | +- `include_csv` = Will check for any `.csv` files in the root of the `results_dir` provided and include them if under 10MB if True. |
| 175 | +- `include_env_metadata` = Will check for any environment metadata generated by `get_environment_metadata_if_available` and include it in the comment if True. |
| 176 | +- `add_comment` = Will add a comment to Jira summarizing all the attachments and environment metadata if True. |
| 177 | +- `automatically_accept` = Will bypass generating a terminal message that needs to be accepted and assume the answer was `y` if True. |
| 178 | + |
| 179 | +--- |
| 180 | + |
| 181 | +## Example Usage |
| 182 | + |
| 183 | +```python |
| 184 | +from utils.jira_confluence_util import JiraConfluenceUtil |
| 185 | + |
| 186 | +util = JiraConfluenceUtil() |
| 187 | +ticket_id = util.determine_jira_reference_local() |
| 188 | +if util.is_valid_jira_reference(ticket_id): |
| 189 | + util.upload_test_results_dir_to_jira( |
| 190 | + ticket_id=ticket_id, |
| 191 | + overwrite_files=True, |
| 192 | + include_html=True, |
| 193 | + include_trace_files=True, |
| 194 | + include_screenshots=True, |
| 195 | + include_csv=True, |
| 196 | + include_env_metadata=True, |
| 197 | + add_comment=True, |
| 198 | + automatically_accept=False |
| 199 | + ) |
| 200 | +``` |
0 commit comments