-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastlane.js
182 lines (164 loc) · 4.8 KB
/
fastlane.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict"
const githubUsername = process.env.HUBOT_GITHUB_USERNAME
const githubToken = process.env.HUBOT_GITHUB_TOKEN
const organisation = "owncloud"
const projectName = "QA/CI/TestAutomation"
const projectNumber = 386
const fastLaneColumnName = "Fastlane"
const itemsPerRequest = 50 // number of items (issues/PRs) to fetch from the project board
const intervalInS = 3600
const room = "developers"
const teamupCalendarKey = "ks1c2vhnot2ttfvawo"
const teamupToken = process.env.HUBOT_TEAMUP_TOKEN
const GITHUB_GRAPHQL_API_URL = "https://api.github.com/graphql"
const interval = intervalInS * 1000
const auth = Buffer.from(`${githubUsername}:${githubToken}`, "binary").toString(
"base64"
)
let endCursor = null // graphql pagination
const fastlaneCards = []
const filterFastlaneCards = (cards) => {
cards.forEach((card) => {
const url = card?.content?.url
const columnName = card?.fieldValueByName?.name
if (url && columnName && columnName === fastLaneColumnName) {
fastlaneCards.push(url)
}
})
return fastlaneCards
}
const generateQuery = (itemArg) => {
return `query {
organization(login: "${organisation}"){
projectV2(number: ${projectNumber}){
title
items(${itemArg}) {
nodes{
content{
...on Issue {
url
}
...on PullRequest {
url
}
}
fieldValueByName(name: "Status") {
...on ProjectV2ItemFieldSingleSelectValue {
name
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
}`
}
const reportFastlaneCards = (robot) =>
robot
.http(GITHUB_GRAPHQL_API_URL)
.headers({ Accept: "application/json", Authorization: `Basic ${auth}` })
.post(
JSON.stringify({
query: generateQuery(
`first: ${itemsPerRequest}, after: "${endCursor}"`
),
})
)((err, _, body) => {
if (err) {
robot.emit("error", `problem getting projects list: '${err}'`)
return
}
const parsedBody = JSON.parse(body)
if (Object.hasOwn(parsedBody, "errors")) {
const errorTypes = parsedBody.errors.map((error) => error.type)
if (errorTypes.every((type) => type === "INSUFFICIENT_SCOPES")) {
robot.emit(
"error",
`could not find project '${projectName}' . Do you have the right permissions?`
)
return
}
if (errorTypes.every((type) => type === "NOT_FOUND")) {
console.log("I am error")
robot.emit(
"error",
`could not find project'${projectName}' with number ${projectNumber}`
)
return
}
}
if (!parsedBody.data) {
robot.emit("error", `Response doesn't have data: '${err}'`)
return
}
if (parsedBody.data.organization.projectV2.title !== projectName) {
robot.emit("error", `Project title from response doesn't match`)
return
}
const items = parsedBody.data.organization.projectV2.items
endCursor = items.pageInfo.endCursor
filterFastlaneCards(items.nodes)
if (items.pageInfo.hasNextPage) {
reportFastlaneCards(robot)
} else {
let text = ""
if (fastlaneCards.length === 1) {
text = `is one card`
} else if (fastlaneCards.length > 1) {
text = `are ${fastlaneCards.length} cards`
} else {
return
}
pingScrumMasters(robot, text)
}
})
const pingScrumMasters = (robot, text) => {
const dateString = new Date().toISOString().slice(0, 10)
robot
.http(
`https://api.teamup.com/${teamupCalendarKey}/events?startDate=${dateString}&endDate=${dateString}`
)
.headers({ "Teamup-Token": teamupToken })
.get()((err, response, body) => {
if (err) {
robot.emit("error", `problem getting scrummaster: '${err}'`)
return
}
let parsedBody = {}
try {
parsedBody = JSON.parse(body)
} catch (e) {
robot.emit("error", `problem parsing '${body}' as JSON`)
return
}
if (
typeof parsedBody.events === "undefined" ||
parsedBody.events.length === 0
) {
robot.emit("error", `scrummasters not found: '${body}'`)
return
}
const scrummaster = parsedBody.events[0].title
// NOTE: Link preview is not shown if there are more than 5 links.
const links = fastlaneCards.join("\n")
robot.send(
{ room },
`${scrummaster} there ${text} in the fastlane\n${links}`
)
})
}
module.exports = (robot) => {
robot.error(function (err) {
robot.logger.error(err)
robot.send({ room: room }, `Error while running fastlane bot: ${err}`)
})
return setInterval(() => {
endCursor = null
fastlaneCards.length = 0
reportFastlaneCards(robot)
}, interval)
}