This repository has been archived by the owner on Jul 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
server.js
174 lines (131 loc) · 4.25 KB
/
server.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
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const JiraClient = require('jira-connector')
const morgan = require('morgan')
const passport = require('passport')
const BasicStrategy = require('passport-http').BasicStrategy
const AnonymousStrategy = require('passport-anonymous')
// Instantiate our Jira client
const jira = new JiraClient({
host: process.env.JIRA_HOST,
basic_auth: {
username: process.env.JIRA_USER,
password: process.env.JIRA_PASS
}
})
// Setup an authentication strategy
let authenticationStrategy = null
if (process.env.HTTP_USER) {
passport.use(new BasicStrategy(
function (username, password, done) {
if (process.env.HTTP_USER == username &&
process.env.HTTP_PASS == password) {
return done(null, true)
}
return done(null, false)
}
))
authenticationStrategy = 'basic'
}
else {
// Default ot allowing anonymous access
passport.use(new AnonymousStrategy())
authenticationStrategy = 'anonymous'
}
app.use(bodyParser.json())
app.use(morgan('combined')) // We want to log all HTTP requests
app.use(passport.initialize())
// Should return 200 ok. Used for "Test connection" on the datasource config page.
app.get('/',
passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
httpRes.set('Content-Type', 'text/plain')
httpRes.send(new Date() + ': OK')
})
// Test the connection between Jira and this project
app.get('/test-jira',
passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
jira.myself.getMyself().then((jiraRes) => {
httpRes.json(jiraRes)
}).catch((jiraErr) => {
httpRes.json(JSON.parse(jiraErr))
})
})
// Used by the find metric options on the query tab in panels.
app.all('/search',
passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
// The JiraClient doesn't have any way to list filters so we need to do a custom query
jira.makeRequest({
uri: jira.buildURL('/filter')
}).then((jiraRes) => {
let result = jiraRes.map(filter => {
return {
text: filter.name,
value: filter.id
}
})
httpRes.json(result)
})
})
// Should return metrics based on input.
app.post('/query',
passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
let result = []
// Convert proper formatted Grafana data into the Jira mess
let from = new Date(httpReq.body.range.from).toISOString().replace(/T/, ' ').replace(/\:([^:]*)$/, '')
let to = new Date(httpReq.body.range.to).toISOString().replace(/T/, ' ').replace(/\:([^:]*)$/, '')
let p = httpReq.body.targets.map(target => {
// Default jql with time range
let jql = [`created >= "${from}"`, `created <= "${to}"`]
// Additional jql for targets
if ( target.target ) {
jql.push(`filter = "${target.target}"`)
}
return jira.search.search({
jql: jql.join(' AND ')
}).then((jiraRes) => {
if (target.type == 'timeserie') {
let datapoints = jiraRes.issues.map(issue => {
timestamp = Math.floor(new Date(issue.fields.created))
return [1, timestamp]
})
result.push({
target: target.target,
datapoints: datapoints
})
}
else if (target.type == 'table') {
let rows = jiraRes.issues.map(issue => {
return [
issue.key,
issue.fields.summary,
issue.fields.assignee ? issue.fields.assignee.displayName : '',
issue.fields.status ? issue.fields.status.name : '',
issue.fields.created
]
})
result.push({
columns: [
{ text: 'Key', 'type': 'string' },
{ text: 'Summary', 'type': 'string' },
{ text: 'Assignee', 'type': 'string' },
{ text: 'Status', 'type': 'string' },
{ text: 'Created', 'type': 'time' }
],
type: 'table',
rows: rows
})
}
})
})
// Once all promises resolve, return result
Promise.all(p).then(() => {
httpRes.json(result)
})
})
app.listen(3000)
console.log('Server is listening to port 3000')