Created
May 14, 2018 09:10
-
-
Save earnubs/40e34bf80f7ba45cbd2b14f361d8975b to your computer and use it in GitHub Desktop.
Send rg results to Trello lane
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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