Skip to content

Instantly share code, notes, and snippets.

@faizahmedfarooqui
Last active May 22, 2020 08:59
Show Gist options
  • Select an option

  • Save faizahmedfarooqui/955740c60ff632cf2fea7972de6b82f7 to your computer and use it in GitHub Desktop.

Select an option

Save faizahmedfarooqui/955740c60ff632cf2fea7972de6b82f7 to your computer and use it in GitHub Desktop.
Adds a specified label to all the cards into the specified list.
const fetch = require('node-fetch');
const throttle = require('promise-ratelimit')(10000);
const _baseUrl = 'https://api.trello.com/1';
const _listId = '';
const _labelId = '';
const _token = '';
const _key = '';
let array = [];
Array.chunk = function (arr, len) {
var chunks = [],
i = 0,
n = arr.length;
while (i < n) {
chunks.push(arr.slice(i, i += len));
}
return chunks;
};
const addLabelToCard = _url =>
fetch(_url, {
method: 'POST'
})
.then(response => {
console.log(
`Response: ${response.status} ${response.statusText}`
);
return response.text();
})
.then(text => console.log(text))
.catch(err => console.error('>> Error:', err));
const getAllCards = _listId =>
fetch(`${_baseUrl}/lists/${_listId}/cards?token=${_token}&key=${_key}`, {
method: 'GET'
})
.then(response => response.json())
.then(_cards =>
_cards.forEach(
_card => array.push(`${_baseUrl}/cards/${_card.id}/idLabels?token=${_token}&key=${_key}&value=${_labelId}`)
)
)
.catch(err => console.error(err));
getAllCards(_listId)
.then(() => {
const chunks = Array.chunk(array, 99);
for (let index = 0; index < chunks.length; index++) {
throttle()
.then(() => {
const chunk = chunks[index];
for (let k = 0; k < chunk.length; k++) {
addLabelToCard(chunk[k]);
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment