-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
120 lines (98 loc) · 3.46 KB
/
app.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
const helpers = require('./helpers.js')
console.log('app.js running...')
module.exports = app => {
app.on('issues.opened', async context => {
const issueTitle = context.payload.issue.title
const triggerPattern = /bootstrap my board/gi
const triggerRegEx = triggerPattern.test(issueTitle)
var newIssues = []
if (triggerRegEx) {
console.log('bootstrap running...')
//move bootstrap issue into progress
const updateIssue = context.repo({
number: context.payload.issue.number,
title: '🗂 bootstrap my board 🏃🏃🏃',
labels: ['in progress']
})
context.github.issues.update(updateIssue)
//get card data from config
const cardData = await getCardData()
//create cards in reverse order for proper order on waffle.io board
const newIssues = await createCards(context, cardData.reverse())
//update cards with epic and dependency relationships
await updateCardRelationships(context, cardData, newIssues)
//close bootstrap issue
const closeIssue = context.repo({
number: context.payload.issue.number,
state: 'closed'
})
context.github.issues.update(closeIssue)
}
})
}
async function getCardData() {
const cardsData = await helpers.readFilePromise('./content/cards.json')
const cardsJSON = JSON.parse(cardsData)
return cardsJSON
}
async function createCards(context, cardData) {
let newIssues = []
for (const card of cardData) {
const response = await createIssue(context, card)
newIssues.push({ id: card.id, issueNumber: response.data.number })
}
return newIssues
}
async function updateCardRelationships(context, cardData, newIssues) {
for (const card of cardData) {
//console.log('id: ' + card.id + ' is child of id: ' + card.childOf)
const newIssue = newIssues.find(issue => issue.id === card.id)
//console.log('id: ' + card.id + ' is: ' + newIssue.issueNumber)
if (card.childOf || card.dependsOn) {
issue = await getIssue(context, newIssue.issueNumber)
newBody = issue.data.body + '\n'
if (card.childOf) {
const parentIssue = newIssues.find(issue => issue.id === card.childOf)
//console.log('parent id: ' + card.childOf + ' is: ' + parentIssue.issueNumber)
newBody = newBody + '\nchild of #' + parentIssue.issueNumber
}
if (card.dependsOn) {
for (const dependencyId of card.dependsOn) {
const dependencyIssue = newIssues.find(
issue => issue.id === dependencyId
)
newBody = newBody + '\ndepends on #' + dependencyIssue.issueNumber
}
}
await editIssue(context, newIssue.issueNumber, newBody)
}
}
}
async function getIssue(context, issueNumber) {
const issue = context.repo({
number: issueNumber
})
const response = await context.github.issues.get(issue)
return response
console.log(response)
}
async function createIssue(context, cardData) {
const cardContentData = await helpers.readFilePromise(
'./content/cards/' + cardData.id + '.md'
)
const newIssue = context.repo({
title: cardData.title,
labels: cardData.labels,
body: cardContentData
})
const response = await context.github.issues.create(newIssue)
return response
}
async function editIssue(context, issueNumber, body) {
const newIssue = context.repo({
number: issueNumber,
body: body
})
const response = await context.github.issues.update(newIssue)
return response
}