Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Send rg results to Trello lane
#!/usr/bin/env node
const readline = require('readline');
const request = require('request');
const RateLimiter = require('limiter').RateLimiter;
const limiter = new RateLimiter(5, 'second');
const TRELLO_KEY = process.env.TRELLO_KEY;
const TRELLO_TOKEN = process.env.TRELLO_TOKEN;
if (!TRELLO_KEY || !TRELLO_TOKEN) {
console.log('Must set TRELLO_KEY and TRELLO_TOKEN env vars. Exiting.')
process.exit(1);
}
process.stdin.setEncoding('utf8');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const cards = {};
rl.on('line', line => {
const arr = line.split(':');
const filename = arr.shift()
const match = arr.join();
if (cards[filename]) {
cards[filename].push(match);
} else {
cards[filename] = [match];
}
}).on('close', () => {
//console.log(cards);
createCards('59f748f54fa3f6a63e72b648', cards);
})
function createCards(id, data) {
/**
for (let filename in data) {
limiter.removeTokens(1, (err, remainingRequests) => {
console.log('creating card for', filename);
data[filename].forEach((checklistItem) => {
limiter.removeTokens(1, (err, remainingRequests) => {
console.log('adding checklist item:', checklistItem);
});
});
});
}
**/
for (let filename in data) {
limiter.removeTokens(1, (err, remainingRequests) => {
createTrelloCard(id, filename, (error, response, body) => {
if (error) console.log(error);
limiter.removeTokens(1, (err, remainingRequests) => {
createTrelloChecklist(body.id, (error, response, body) => {
if (error) console.log(error);
data[filename].forEach((checklistItem) => {
limiter.removeTokens(1, (err, remainingRequests) => {
createTrelloChecklistItem(body.id, checklistItem, (error, response, body) => {
if (error) console.log(error);
console.log(body);
})
});
});
});
});
});
});
};
}
function sleep (time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
const trelloPost = request.defaults({
method: 'POST',
baseUrl: 'https://api.trello.com/1/',
json: true,
qs: {
key: TRELLO_KEY,
token: TRELLO_TOKEN,
}
});
function createTrelloCard(id, name, cb) {
const api = `/cards?idList=${id}`;
return trelloPost({
url: api,
qs: {
idList: id,
name: name
}
}, cb);
}
function createTrelloChecklist(id, cb) {
const api = `/cards/${id}/checklists`;
return trelloPost({
url: api,
qs: {
name: 'Matching lines:'
}
}, cb);
}
function createTrelloChecklistItem(id, name, cb) {
const api = `/checklists/${id}/checkItems`;
return trelloPost({
url: api,
qs: {
name: name
}
}, cb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment