|
1 | 1 | #!/usr/bin/env python3 |
| 2 | +# Requires Python 3.9+ |
| 3 | +"""Fetch release notes from a Jira issue.""" |
2 | 4 |
|
3 | | -import sys |
| 5 | +import argparse |
4 | 6 | import base64 |
5 | 7 | import json |
6 | | -import requests |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from urllib.error import HTTPError |
| 11 | +from urllib.request import Request, urlopen |
7 | 12 |
|
8 | | -SCRIPT_NAME = "jira_release_notes.py" |
| 13 | +SCRIPT_NAME = Path(__file__).name |
9 | 14 |
|
10 | 15 | def extract_text_from_content(content): |
11 | 16 | if isinstance(content, list): |
@@ -63,32 +68,43 @@ def parse_release_notes(response_json): |
63 | 68 | print(f"[{SCRIPT_NAME}] Error parsing release notes: {str(e)}", file=sys.stderr) |
64 | 69 | return '' |
65 | 70 |
|
| 71 | +def parse_args(): |
| 72 | + parser = argparse.ArgumentParser( |
| 73 | + description=__doc__, |
| 74 | + ) |
| 75 | + parser.add_argument("issue_id", help="RELEASE issue ID to fetch release notes from") |
| 76 | + parser.add_argument("jira_cloud_id", help="Atlassian Cloud ID - Can be retrieved from the `tenant_info` endpoint, e.g.: `https://<my-site-name>.atlassian.net/_edge/tenant_info`") |
| 77 | + parser.add_argument("jira_email", help="Email used to create the API token") |
| 78 | + parser.add_argument("jira_api_token", help="Jira API token - Generate one at: https://id.atlassian.com/manage-profile/security/api-tokens") |
| 79 | + return parser.parse_args() |
| 80 | + |
66 | 81 | def main(): |
67 | | - if len(sys.argv) != 4: |
68 | | - print(f"Usage: {sys.argv[0]} <issue_id> <jira_email> <jira_api_token>") |
69 | | - sys.exit(1) |
| 82 | + args = parse_args() |
70 | 83 |
|
71 | | - jira_issue_id = sys.argv[1] |
72 | | - jira_email = sys.argv[2] |
73 | | - jira_api_token = sys.argv[3] |
74 | | - jira_base_url = "https://bitwarden.atlassian.net" |
| 84 | + jira_issue_id = args.issue_id |
| 85 | + jira_cloud_id = args.jira_cloud_id |
| 86 | + jira_email = args.jira_email |
| 87 | + jira_api_token = args.jira_api_token |
| 88 | + jira_base_url = "https://api.atlassian.com/ex/jira" |
75 | 89 |
|
76 | 90 | auth = base64.b64encode(f"{jira_email}:{jira_api_token}".encode()).decode() |
77 | | - headers = { |
78 | | - "Authorization": f"Basic {auth}", |
79 | | - "Content-Type": "application/json" |
80 | | - } |
81 | | - |
82 | | - response = requests.get( |
83 | | - f"{jira_base_url}/rest/api/3/issue/{jira_issue_id}", |
84 | | - headers=headers |
| 91 | + request = Request( |
| 92 | + f"{jira_base_url}/{jira_cloud_id}/rest/api/3/issue/{jira_issue_id}", |
| 93 | + headers={ |
| 94 | + "Authorization": f"Basic {auth}", |
| 95 | + "Content-Type": "application/json" |
| 96 | + } |
85 | 97 | ) |
86 | 98 |
|
87 | | - if response.status_code != 200: |
88 | | - print(f"[{SCRIPT_NAME}] Error fetching Jira issue ({jira_issue_id}). Status code: {response.status_code}. Msg: {response.text}", file=sys.stderr) |
| 99 | + try: |
| 100 | + with urlopen(request) as response: |
| 101 | + response_json = json.loads(response.read().decode()) |
| 102 | + except HTTPError as error: |
| 103 | + error_text = error.read().decode().replace(jira_cloud_id, "[REDACTED]") |
| 104 | + print(f"[{SCRIPT_NAME}] Error fetching Jira issue ({jira_issue_id}). Status code: {error.code}. Msg: {error_text}", file=sys.stderr) |
89 | 105 | sys.exit(1) |
90 | 106 |
|
91 | | - release_notes = parse_release_notes(response.json()) |
| 107 | + release_notes = parse_release_notes(response_json) |
92 | 108 | print(release_notes) |
93 | 109 |
|
94 | 110 | if __name__ == "__main__": |
|
0 commit comments