-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (106 loc) · 3.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
122
#!/usr/bin/env nodejs
const config = require('./config')
const request = require('request')
const trello = require('trello')
// instantiate new Trello API helper
const trlo = new trello(config.trello.key, config.trello.token)
// instantiate recursive Trello card function
const addCard = addTrelloCard()
// authenticate with TeamGantt
request(
{
method: 'POST',
url: 'https://api.teamgantt.com/v1/authenticate',
headers: {
'Content-Type': 'application/json',
'TG-Authorization': `Bearer ${config.teamgantt.bearer}`
},
body: `{ "user": "${config.teamgantt.username}", "pass": "${
config.teamgantt.password
}" }`
},
(error, response, body) => {
// parse auth keys
const authData = JSON.parse(body)
// fetch all open tasks from TeamGantt
request(
{
method: 'GET',
url: 'https://api.teamgantt.com/v1/tasks?hide_completed=true',
headers: {
'Content-Type': 'application/json',
'TG-Authorization': `Bearer ${config.teamgantt.bearer}`,
'TG-Api-Key': authData.api_key,
'TG-User-Token': authData.user_token
}
},
(error, response, body) => {
const tasks = JSON.parse(body)
// delete all trello cards in list
// create Trello card
trlo
.makeRequest('post', `/1/lists/${config.trello.list}/archiveAllCards`)
.then(res => {
console.log('Archived cards')
// recursively loop through TeamGantt tasks
addCard(tasks)
})
.catch(err => {
console.log(err)
})
}
)
}
)
// return a function that handles recusively looping through tasks
function addTrelloCard() {
let i = 0
return tasks => {
const task = tasks[i]
// ignore tasks with no end date or no resources
if (!task.end_date || !task.resources.length) {
if (++i < tasks.length) {
addCard(tasks)
}
return
}
// loop over resources and create comma separated list of Trello member IDs
let members = ''
task.resources.forEach(resource => {
// loop through members array until we find this one
config.members.every(member => {
// found a match so add to comma separated string and end loop
if (member.name === resource.name) {
members += `${member.id},`
return false
}
return true
})
})
// format date for Trello
const dateParts = task.end_date.split('-')
const formattedDate = `${dateParts[1]}/${dateParts[2]}/${dateParts[0]}`
// create Trello card
trlo
.makeRequest('post', '/1/cards', {
name: `${task.project_name} - ${task.name}`,
idList: config.trello.list,
due: formattedDate,
idMembers: members.slice(0, -1)
})
.then(res => {
// success so log info to console
console.log('Card added')
console.log(`Project: ${task.project_name}`)
console.log(`Task: ${task.name}`)
console.log(`Due Date: ${formattedDate}\r\n`)
if (++i < tasks.length) {
addCard(tasks)
}
})
.catch(err => {
console.log(`Error adding ${task.project_name} - ${task.name}`)
console.log(err)
})
}
}