-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrello_service.py
More file actions
146 lines (123 loc) · 5.92 KB
/
Copy pathtrello_service.py
File metadata and controls
146 lines (123 loc) · 5.92 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
from typing import Dict, List, Optional, Any
from api_client import TrelloAPIClient
# Trello service that orchestrates API calls
class TrelloService:
# Initialize with API client.
def __init__(self, api_client: TrelloAPIClient):
self.client = api_client
# Add a card with labels and comment to a specified column of a board.
def add_card_to_board(self, board_name: str, list_name: str, card_name: str,
description: Optional[str] = None,
label_colors: Optional[List[str]] = None,
comment: Optional[str] = None) -> Dict[str, Any]:
# Find board by name
board = self.client.get_board_by_name(board_name)
if not board:
raise ValueError(f"Board '{board_name}' not found")
# Find list by name within the board
target_list = self.client.get_list_by_name(str(board.get("id")), list_name)
if not target_list:
raise ValueError(f"List '{list_name}' not found in board '{board_name}'")
# Create the card
card = self.client.create_card(
list_id=str(target_list.get("id")),
name=card_name,
desc=description
)
results = {
"card": card,
"board": board.get("name"),
"list": target_list.get("name"),
"labels_added": [],
"comment_added": False
}
# Add labels if specified
if label_colors:
# Check existing labels first
label_info = self.find_or_create_labels(board_name, label_colors)
for label in label_info:
try:
if label.get("exists"):
# Use existing label
self.client.add_label_to_card(str(card.get("id")), label.get("id"))
list(results["labels_added"]).append({
"color": label.get("color"),
"id": label.get("id"),
"status": "existing_used"
})
else:
# Create new label
label_result = self.client.create_label_on_card(str(card.get("id")), str(label.get("color")))
list(results["labels_added"]).append({
"color": label.get("color"),
"id": label_result.get("id"),
"status": "newly_created"
})
except Exception as e:
list(results["labels_added"]).append({
"color": label.get("color"),
"error": str(e)
})
# Add comment if specified
if comment:
try:
comment_result = self.client.add_comment_to_card(str(card.get("id")), comment)
results["comment_added"] = True
results["comment"] = comment_result
except Exception as e:
results["comment_error"] = str(e)
return results
# Find existing labels by color or suggest creating new ones.
def find_or_create_labels(self, board_name: str, label_colors: List[str]) -> List[Dict[str, Any]]:
board = self.client.get_board_by_name(board_name)
if not board:
raise ValueError(f"Board '{board_name}' not found")
existing_labels = self.client.get_board_labels(str(board.get("id")))
label_map = {str(label.get("color")): label for label in existing_labels if label.get("color")}
results = []
for color in label_colors:
if color in label_map:
results.append({
"color": color,
"exists": True,
"id": label_map[color].get("id"),
"name": label_map[color].get("name")
})
else:
results.append({
"color": color,
"exists": False,
"id": None,
"name": None
})
return results
# Get all boards in the workspace.
def get_boards(self) -> List[Dict[str, Optional[str]]]:
boards = self.client.get_boards()
return [{"id": board.get("id"), "name": board.get("name")} for board in boards]
# Get board information including lists, labels, and cards.
def get_board_info(self, board_name: str) -> Optional[Dict[str, Any]]:
board = self.client.get_board_by_name(board_name)
if not board:
return None
lists = self.client.get_board_lists(str(board.get("id")))
labels = self.client.get_board_labels(str(board.get("id")))
cards = self.client.get_board_cards(str(board.get("id")))
return {
"board": {"id": board.get("id"), "name": board.get("name")},
"lists": [{"id": l.get("id"), "name": l.get("name")} for l in lists],
"labels": [{"id": l.get("id"), "name": l.get("name"), "color": l.get("color")} for l in labels],
"cards": [{"id": c.get("id"), "name": c.get("name"), "list_id": c.get("idList")} for c in cards]
}
# Get board by name, return name and id.
def get_board_by_name(self, board_name: str) -> Optional[Dict[str, Optional[str]]]:
board = self.client.get_board_by_name(board_name)
if not board:
return None
return {"id": board.get("id"), "name": board.get("name")}
# Get card by name within a board, return name and id.
def get_card_by_name(self, board_id: str, card_name: str) -> Optional[Dict[str, Optional[str]]]:
card = self.client.get_card_by_name(board_id, card_name)
if not card:
return None
return {"id": card.get("id"), "name": card.get("name")}