Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement and document creating cards in Trello #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/trello_card_creator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Summary
The `TrelloCardCreator` operator creates a card in a list using the Trello API with the specified content.

# Inputs
- `name`: A string containing the name of the card to be created.
- `description`: A string containing the description of the card to be created.
- `label_ids`: A list of strings representing the label IDs to optionally assign to the card.
Toricane marked this conversation as resolved.
Show resolved Hide resolved

# Parameters
- `list_id`: A string representing the ID of the list where the card will be created.

# Outputs
- `created_card`: A JSON string representing the created card object.

# Functionality
The `run_step` function is responsible for the main logic of the operator. The list ID is read from the parameters, while the name, description, and label IDs are read from the inputs. The API key and token are retrieved from the AI context. Finally, the `create_card` helper function is called with the necessary parameters to create the card.

The `create_card` function constructs and sends the payload for creating a card in the specified list using the Trello API. If the card creation is successful, the created card object is returned as a JSON string. If an error occurs during the process, the error message from the Trello API is logged and there is no output.
Toricane marked this conversation as resolved.
Show resolved Hide resolved
106 changes: 106 additions & 0 deletions operators/trello_card_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import requests

from .base_operator import BaseOperator
from ai_context import AiContext


class TrelloCardCreator(BaseOperator):
@staticmethod
def declare_name():
return "trello_card_creator"

@staticmethod
def declare_category():
return BaseOperator.OperatorCategory.ACT.value

@staticmethod
def declare_parameters():
return [
{
"name": "list_id",
"data_type": "string",
"placeholder": "Enter the list ID",
},
]

@staticmethod
def declare_inputs():
return [
{
"name": "name",
"data_type": "string",
"placeholder": "Enter the name of the card",
},
{
"name": "description",
"data_type": "string",
"placeholder": "Enter the description of the card",
},
{
"name": "label_ids",
"data_type": "string[]",
"placeholder": "Comma-separated list of label IDs to add to the card",
},
]

@staticmethod
def declare_outputs():
return [
{
"name": "created_card",
"data_type": "json",
},
]

def run_step(self, step, ai_context: AiContext):
try:
params: dict[str, str] = step["parameters"]
list_id: str = params.get("list_id")

name: str = ai_context.get_input("name", self)
description: str = ai_context.get_input("description", self)
label_ids: list[str] = ai_context.get_input("label_ids", self)

key: str = ai_context.get_secret("trello_key")
token: str = ai_context.get_secret("trello_token")

created_card = self.create_card(
list_id, name, description, label_ids, key, token, ai_context
)
ai_context.set_output("created_card", created_card, self)
except Exception as error:
ai_context.add_to_log(f"An error occurred: {str(error)}")

def create_card(
self,
list_id: str,
name: str,
description: str,
label_ids: list[str],
key: str,
token: str,
ai_context: AiContext,
):
url = "https://api.trello.com/1/cards"
headers = {"Accept": "application/json"}
query = {
"idList": list_id,
"key": key,
"token": token,
"name": name,
"desc": description,
"idLabels": ",".join(label_ids),
}

try:
session = requests.Session()
response = session.post(url, headers=headers, params=query)

if not 200 <= response.status_code < 300:
ai_context.add_to_log(f"An error occurred: {response.text}")
return

return response.text
except requests.RequestException as error:
ai_context.add_to_log(f"An error occurred: {str(error)}")
return