-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjira-tool.py
More file actions
365 lines (300 loc) · 11.6 KB
/
jira-tool.py
File metadata and controls
365 lines (300 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import csv
import json
import os
import logging
import sys
import getpass
import subprocess
from enum import Enum
from dotenv import load_dotenv
from datetime import datetime
from dataclasses import dataclass
from typing import List, Tuple, Optional, Dict
from jira import JIRA, Issue
import ollama
load_dotenv()
# Uncomment to enable logging
# logging.basicConfig(level=logging.INFO)
# Constants
JIRA_SERVER_URL = "https://new-work.atlassian.net"
CONFIG_FOLDER = "config"
CUSTOMFIELD_INVESTMENT_PROFILE = "customfield_10089"
DEFAULT_OLLAMA_MODEL = "llama3.2"
# Enum for input methods
class InputMethod(Enum):
"""Input method enumeration for selecting how to input credentials."""
ONEPASSWORD = "1"
MANUAL = "2"
@dataclass
class IssueLink:
"""Data structure for issue link information."""
link_type: str
issue_key: str
# Data structure for Jira ticket information
@dataclass
class JiraTicket:
issue_type: str
title: str
investment_profile: str
due_date: Optional[str] = None
labels: Optional[List[str]] = None
parent: Optional[str] = None
issue_link: Optional[IssueLink] = None
def get_jira_credentials() -> Tuple[Optional[str], Optional[str]]:
"""Get Jira credentials from the user or 1Password."""
item_name = os.getenv("JIRA_OP_ITEM_NAME")
if item_name:
credentials = fetch_jira_credentials(item_name)
if all(credentials):
return credentials
choice = input_with_clear("Choose input method:\n1. 1Password CLI\n2. Manual input\n")
if choice == InputMethod.ONEPASSWORD.value:
item_name = input_with_clear(
"### Hint - you can store it in the environment variable \"JIRA_OP_ITEM_NAME\" for easier usage in the future ### \n"
"Enter the 1Password item name: ")
return fetch_jira_credentials(item_name) or (None, None)
jira_email = input_with_clear("Enter your Jira email: ")
jira_api_token = getpass.getpass("Enter your Jira API token: ")
return jira_api_token, jira_email
# Load project configuration
def load_project_config(project_name: str) -> Dict:
config_path = os.path.join(CONFIG_FOLDER, f"{project_name}.json")
try:
with open(config_path, 'r') as file:
return json.load(file)
except FileNotFoundError:
logging.error(f"Project config file not found: {config_path}")
sys.exit(1)
# Connect to Jira
def connect_to_jira(jira_email: str, jira_api_token: str) -> JIRA:
return JIRA(server=JIRA_SERVER_URL, basic_auth=(jira_email, jira_api_token))
# Load tickets from CSV
def load_tickets_from_csv(csv_path: str) -> List[JiraTicket]:
tickets = []
with open(csv_path, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row.get('issueLink'):
issue_link = parse_issue_link(row.get('issueLink', '').strip())
else:
issue_link = None
tickets.append(JiraTicket(
issue_type=row['issueType'].strip(),
title=row['title'].strip(),
investment_profile=row['investmentProfile'].strip(),
due_date=row.get('duedate').strip(),
labels=row.get('labels', '').strip().split(','),
parent=row.get('parent').strip(),
issue_link=issue_link
))
return tickets
# Generate description using Ollama
def generate_description(issue_type: str, title: str) -> str:
templates = {
"Task": """
h2. User Story
AS A customer
I WANT TO be able to
SO THAT
h2. Acceptance Criteria
GIVEN I'm at
WHEN I click on
THEN I see a
h2. Dependencies
This changes affects this, this and this
h2. Open questions
Does a user need to log in to see the briefing?
h2. Pending
Share icon
Google analytics tracking code
h2. Translations
""",
"Investigation": """
h2. Why?
Why the investigation is needed.
h2. What?
What should be investigated providing as many details as possible.
h2. Outcome
A list of tasks that when completed, would implement
""",
"Technical Debt": """
h2. Why?
Why is this technical debt task necessary? What do we stand to gain from the investment in effort?
h2. What?
What exactly are we trying to avoid or improve?
h2. Technical Details
What needs to be done, described with as much technical details as possible.
""",
"User Story": """
h2. Summary
Brief summary based on title
h2. Context
Extra context to understand the requirements...
AS A customer
I WANT TO be able to
SO THAT
h2. Acceptance criteria
GIVEN I'm a ....
WHEN I ...
THEN I ...
h2. Other information
Any other information (to be added once the issue is created)
""",
"Epic": """
h2. Why?
Brief summary based on title
h2. What?
Why should this epic be done.
h2. Other information
Any other information (to be added once the issue is created)
""",
"Bug": """
h2. What?
What is the bug?
h2. Affected?
Who or what is affected?
h2. Steps to reproduce
1. Do this
2. Do that
3. Boom
h2. Extra information
Any other info
"""
}
template = templates.get(issue_type, "")
if not template:
raise ValueError(f"Invalid issue type: {issue_type}")
# Analysis prompt for evaluating comments
analysis_prompt = (
f"""Given the following jira issue structure for a '{issue_type}':
```
{template}
``` and the following title: `{title}` then output the exact same text but prefill the h2. sections accordingly given the title.
"""
)
try:
response = ollama.chat(model=get_ollama_model(), messages=[{"role": "user", "content": analysis_prompt}])
description = response["message"]["content"].strip()
return description
except Exception as e:
logging.error(f"Error analyzing comments: {e}")
return "Error during analysis.", False
def parse_issue_link(link_str: str) -> Optional[IssueLink]:
"""Parse issue link string in the format 'link_type:issue_key'."""
if not link_str:
return None
try:
link_type, issue_key = link_str.strip().split(':')
return IssueLink(link_type=link_type, issue_key=issue_key)
except ValueError:
logging.warning(f"Invalid issue link format: {link_str}")
return None
def create_issue_link(jira: JIRA, source_issue: Issue, link: IssueLink) -> None:
"""Create a link between two issues."""
try:
jira.create_issue_link(
type=link.link_type,
inwardIssue=source_issue.key,
outwardIssue=link.issue_key
)
print(f"Created '{link.link_type}' link to {link.issue_key}")
except Exception as e:
logging.error(f"Error creating issue link: {e}")
# Create Jira ticket
def create_jira_ticket(jira: JIRA, project_config: Dict, ticket: JiraTicket) -> Issue:
issue_type_id = project_config['issueTypes'].get(ticket.issue_type)
investment_profile_id = next(
(profile['id'] for profile in project_config['investmentProfiles'] if profile['value'] == ticket.investment_profile),
None
)
if not issue_type_id or not investment_profile_id:
logging.error(f"Invalid issue type or investment profile for ticket: {ticket.title}")
return None
description = generate_description(ticket.issue_type, ticket.title)
fields = {
"project": {"id": project_config["projectId"]},
"summary": ticket.title,
"issuetype": {"id": issue_type_id},
"description": description,
CUSTOMFIELD_INVESTMENT_PROFILE: {"id": investment_profile_id}
}
if ticket.due_date:
try:
due_date_formatted = datetime.strptime(ticket.due_date, "%Y-%m-%d").strftime("%Y-%m-%d")
fields["duedate"] = due_date_formatted
except ValueError:
logging.error(f"Invalid date format for ticket: {ticket.title}")
if ticket.labels:
fields["labels"] = ticket.labels
if ticket.parent:
fields["parent"] = {"key": ticket.parent}
try:
created_issue = jira.create_issue(fields=fields)
logging.info(f"Created ticket: {ticket.title} - {created_issue.key} (link: {ticket.issue_link})")
# Create issue link if specified
if ticket.issue_link:
try:
create_issue_link(jira, created_issue, ticket.issue_link)
except Exception as e:
logging.error(f"Error creating link to ticket {ticket.title} - {ticket.issue_link}: {e}")
return created_issue
except Exception as e:
logging.error(f"Error creating ticket {ticket.title}: {e}")
return None
## Utility Functions
def clear_terminal() -> None:
"""Clear the terminal screen."""
os.system("cls" if os.name == "nt" else "clear")
def input_with_clear(prompt: str) -> str:
"""Prompt for input and clear the terminal afterwards."""
user_input = input(prompt)
clear_terminal()
return user_input
def fetch_jira_credentials(item_name: str) -> Tuple[Optional[str], Optional[str]]:
"""Fetch Jira credentials from 1Password using the given item name."""
try:
result = subprocess.run(
["op", "item", "get", item_name, "--format", "json"],
capture_output=True, text=True, check=True
)
item = json.loads(result.stdout)
return item["fields"][1]["value"], item["fields"][0]["value"]
except subprocess.CalledProcessError as e:
logging.error(f"Error fetching credentials: {e}")
return None, None
def check_for_running_ollama() -> None:
"""Check if the Ollama service is running."""
try:
ollama.list()
except Exception as e:
print(f"Ollama is not working: {e}")
sys.exit(1)
def get_ollama_model() -> str:
model_name = os.getenv("OLLAMA_MODEL", DEFAULT_OLLAMA_MODEL)
try:
ollama.show(model_name)
return model_name
except ollama.ResponseError as e:
if e.status_code == 404:
print(f"Pulling Ollama model: {model_name}")
ollama.pull(model_name)
return model_name
else:
print(f"Error using Ollama model: {e}")
sys.exit(1)
# Main function
def main():
check_for_running_ollama()
jira_api_token, jira_email = get_jira_credentials()
jira = connect_to_jira(jira_email, jira_api_token)
project_name = input("Enter the project: ")
csv_path = f"./projects/{project_name}.csv"
project_config = load_project_config(project_name)
tickets = load_tickets_from_csv(csv_path)
logging.info(f"Loaded {len(tickets)} tickets from CSV")
for ticket in tickets:
created_ticket = create_jira_ticket(jira, project_config, ticket)
if created_ticket:
print(f"Created ticket: {created_ticket.key} - {ticket.title}")
if __name__ == "__main__":
main()