Blog Post

Automatically Adding and Removing Labels via Trello Webhooks

Automatically Adding and Removing Labels via Trello Webhooks

It’s been awhile since I’ve played around with the Trello API but my “To-Do” board was annoying me recently and I found something new to automate.

As I’ve previously mentioned, I’ve got a webhook listening to my “To-Do” boardI’ve also mentioned that this board consists of “Not Started,” “In Progress,” and “Done” columns.  In addition to that, this board has a “Blocked” label and the “In Progress” list has a separator card that I use to separate unblocked cards from blocked cards.

I had three small tasks that all became blocked after I started them (waiting for feedback, in this case) and it would have been easy enough to move the cards down below the separator and add the labels manually.  I figured that should happen automatically, though, so I coded that out.  And, while I was doing that, I figured any cards moved into the “Done” column must not be blocked anymore so I should remove that label if it’s present on any cards moved to that column.

That last part was the easy part because that previously-mentioned webhook already had code that explicitly looked for cards being transferred into the “Done” column.  All I had to do was add to that a check for what labels the moved card has and, if the “Blocked” label is included, remove that label and update the card.  It looks like this:

As per usual, I’m using my TrelloApi helper class here.

We hit /1/cards/<card_id> with a GET request to get all of the details about the moved card (with the webhook having given us the proper card ID as part of $data).  From that, we save off the array of label IDs on the card.

If that array contains the”Blocked” label ID (which is configured outside of this block of code), we do a neat little arrow function to find the blocked label ID in the array of label IDs and remove it.  Then we do a PUT request to /1/cards/<card_id> with the idLabels parameter set to our updated array, effectively removing the”Blocked” label from the card.

The other part of this check is a little bit more complex.

We enter this block of code on one of two conditions.  The first is if we’re moving a card into the “In Progress” list (with its list ID configured outside of this block of code).  The second is if we’re moving a card higher or lower within the “In Progress” list.  Again, we have data about the action that occurred in $data, supplied by the webhook.

We’re going to be checking a few things and we only want to update our card if we need to, so we set an $update flag to false right off the bat.  Then, as we did in our first block of code, we fetch the moved card’s data so we know what labels are already assigned to the card.  I could abstract this away into a single method since it’s used in two places but I’m not worried about duplicating two lines of code here.

We also need to get details about the separator card, because I don’t want to assume that its position is static (though moving the separator would break things so maybe I could make that assumption).  That’s a GET request to /1/cards/618c8febe82b3a2be18936dd (the card ID is hard-coded but I’ve obfuscated it here).  Now we’ve got all the data that we need.

We only care about two scenarios: If the moved card landed above the separator and had the blocked label or if the moved card landed below the separator and didn’t have the “Blocked” label.  To determine this, we compare the moved card’s position to the separator card’s position and we check for the presence of the “Blocked” label in the card’s array of label IDs (as we did in the first block of code).

If either of those conditions is true, we set our $update flag to true.  If the label ID is present and needs to be removed, we update our array of labels with that previously-mentioned neat arrow function.  If the label ID is not present and needs to be added, we just append it to the end of the array of label IDs.

It’s possible that we moved a card without the “Blocked” label around above the separator or moved one with the label around below the separator.  In that case, our default false case on the $update flag would hold.

If we are doing an update, it’s a PUT request to /1/cards/<card_id> with the idLabels parameter set to our updated array, just like we did in the first block of code.

As per usual, this is nothing groundbreaking.  It’s always fun for me to get to come back to the Trello API, though, and I hope it might be useful for someone else.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.