Module: ActiveProject::Adapters::Trello::Issues

Included in:
ActiveProject::Adapters::TrelloAdapter
Defined in:
lib/active_project/adapters/trello/issues.rb

Constant Summary collapse

DEFAULT_FIELDS =
%w[id name desc closed idList idBoard due dueComplete idMembers].freeze

Instance Method Summary collapse

Instance Method Details

#create_issue(_board_id, attributes) ⇒ ActiveProject::Resources::Issue

Creates a new Card in Trello.

Parameters:

  • _board_id (String)

    Ignored (context).

  • attributes (Hash)

    Card attributes. Required: :list_id, :title. Optional: :description, :assignee_ids, :due_on.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/active_project/adapters/trello/issues.rb', line 44

def create_issue(_board_id, attributes)
  list_id = attributes[:list_id]
  title = attributes[:title]

  unless list_id && title && !title.empty?
    raise ArgumentError, "Missing required attributes for Trello card creation: :list_id, :title"
  end

  path = "cards"
  query_params = {
    idList: list_id,
    name: title,
    desc: attributes[:description],
    idMembers: attributes[:assignee_ids]&.join(","),
    due: attributes[:due_on]&.iso8601
  }.compact

  card_data = make_request(:post, path, nil, query_params)
  map_card_data(card_data, card_data["idBoard"])
end

#delete_issue(card_id, _context = {}) ⇒ Boolean

Deletes a Trello card.

Parameters:

  • card_id (String)

    The ID of the Trello Card to delete.

  • context (Hash)

    Optional context (ignored).

Returns:

  • (Boolean)

    True if successfully deleted.



127
128
129
130
131
# File 'lib/active_project/adapters/trello/issues.rb', line 127

def delete_issue(card_id, _context = {})
  path = "cards/#{card_id}"
  make_request(:delete, path)
  true
end

#find_issue(card_id, context = {}) ⇒ ActiveProject::Resources::Issue

Finds a specific Card by its ID.

Parameters:

  • card_id (String)

    The ID of the Trello Card.

  • context (Hash) (defaults to: {})

    Optional context. Accepts :fields for specific field selection.

Returns:



30
31
32
33
34
35
36
37
38
# File 'lib/active_project/adapters/trello/issues.rb', line 30

def find_issue(card_id, context = {})
  path = "cards/#{card_id}"

  fields = context[:fields] ? Array(context[:fields]).join(",") : DEFAULT_FIELDS.join(",")
  query = { fields: fields, list: true }

  card_data = make_request(:get, path, nil, query)
  map_card_data(card_data, card_data["idBoard"])
end

#list_issues(board_id, options = {}) ⇒ Array<ActiveProject::Resources::Issue>

Lists Trello cards on a specific board.

Parameters:

  • board_id (String)

    The ID of the Trello board.

  • options (Hash) (defaults to: {})

    Optional filtering options. Accepts :filter and :fields.

Returns:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_project/adapters/trello/issues.rb', line 13

def list_issues(board_id, options = {})
  path = "boards/#{board_id}/cards"

  fields = options[:fields] ? Array(options[:fields]).join(",") : DEFAULT_FIELDS.join(",")
  query = { fields: fields, list: true }
  query[:filter] = options[:filter] if options[:filter]

  cards_data = make_request(:get, path, nil, query)
  return [] unless cards_data.is_a?(Array)

  cards_data.map { |card_data| map_card_data(card_data, board_id) }
end

#update_issue(card_id, attributes, context = {}) ⇒ ActiveProject::Resources::Issue

Updates an existing Card in Trello.

Parameters:

  • card_id (String)

    The ID of the Trello Card.

  • attributes (Hash)

    Attributes to update (e.g., :title, :description, :list_id, :closed, :due_on, :assignee_ids, :status).

  • context (Hash) (defaults to: {})

    Optional context. Accepts :fields for return data field selection.

Returns:



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
# File 'lib/active_project/adapters/trello/issues.rb', line 70

def update_issue(card_id, attributes, context = {})
  update_attributes = attributes.dup

  if update_attributes.key?(:status)
    target_status = update_attributes.delete(:status)

    board_id = update_attributes[:board_id] || begin
      find_issue(card_id).project_id
    rescue NotFoundError
      raise NotFoundError,
            "Trello card with ID '#{card_id}' not found."
    end

    unless board_id
      raise ApiError, "Could not determine board ID for card '#{card_id}' to perform status mapping."
    end

    board_mappings = @config.status_mappings[board_id]
    unless board_mappings
      raise ConfigurationError,
            "Trello status mapping not configured for board ID '#{board_id}'. Cannot map status ':#{target_status}'."
    end

    target_list_id = board_mappings.key(target_status)

    unless target_list_id
      raise ConfigurationError,
            "Target status ':#{target_status}' not found in configured Trello status mappings for board ID '#{board_id}'."
    end

    update_attributes[:list_id] = target_list_id
  end

  path = "cards/#{card_id}"

  query_params = {}
  query_params[:name] = update_attributes[:title] if update_attributes.key?(:title)
  query_params[:desc] = update_attributes[:description] if update_attributes.key?(:description)
  query_params[:closed] = update_attributes[:closed] if update_attributes.key?(:closed)
  query_params[:idList] = update_attributes[:list_id] if update_attributes.key?(:list_id)
  query_params[:due] = update_attributes[:due_on]&.iso8601 if update_attributes.key?(:due_on)
  query_params[:dueComplete] = update_attributes[:dueComplete] if update_attributes.key?(:dueComplete)
  if update_attributes.key?(:assignee_ids)
    query_params[:idMembers] =
      update_attributes[:assignee_ids]&.join(",")
  end

  return find_issue(card_id, context) if query_params.empty?

  card_data = make_request(:put, path, nil, query_params.compact)
  map_card_data(card_data, card_data["idBoard"])
end