-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
141 lines (123 loc) · 3.93 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
const request = require("request")
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const morgan = require('morgan')
const passport = require('passport')
const BasicStrategy = require('passport-http').BasicStrategy
const AnonymousStrategy = require('passport-anonymous')
var accessToken = 'Wl7WiZq3vfOLJJ85bHkFKlS0U5ioDbJP';
var username1 = 'srinivasrajas54'
var password1 = 'Nepolian@1'
app.use(bodyParser.json())
app.use(morgan('combined'))
app.use(passport.initialize())
let authenticationStrategy = null
if (process.env.HTTP_USER) {
passport.use(new BasicStrategy(
function (username, password, done) {
if (username1 == username &&
password1 == password) {
return done(null, true)
}
return done(null, false)
}
))
authenticationStrategy = 'basic'
}
else {
// Default ot allowing anonymous access
passport.use(new AnonymousStrategy())
authenticationStrategy = 'anonymous'
}
getOutput = (arg_url) => {
return new Promise((resolve, reject) => {
request({
url: arg_url,
auth: {
user: username1,
password: password1
},
rejectUnauthorized: false
}, function(error,response,body){
if(!error)
var content = JSON.parse(body)
resolve(content)
})
}
)
}
app.get('/',
passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
httpRes.set('Content-Type', 'text/plain')
httpRes.send(new Date() + ': OK')
})
url1 = 'http://192.168.107.3/rest/api/2/filter/favourite'
app.all('/search',
//passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
getOutput(url1).then(content => {
let result = content.map(filter => {
return{
text: filter.id,
value: filter.name
}
})
httpRes.json(result)
})
})
app.post('/query',
//passport.authenticate(authenticationStrategy, { session: false }),
(httpReq, httpRes) => {
let result = []
let from = new Date(httpReq.body.range.from).toISOString().replace(/T/, ' ').replace([/-/], '/').replace(/\:([^:]*)$/, '').replace(/-/g, '/')
let to = new Date(httpReq.body.range.to).toISOString().replace(/T/, ' ').replace(/-/, '/').replace(/\:([^:]*)$/, '').replace(/-/g, '/')
let p = httpReq.body.targets.map(target => {
// Default jql with time range
let jql = [`created >= "${from}"`, `created <= "${to}"`]
if ( target.target ) {
jql.push(`filter = "${target.target}"`)
}
query=jql.join(' AND ')
var url2 = 'http://192.168.107.3/rest/api/2/search?jql='+query+'&maxResults=-1'
var url2 = url2.split(' ').join('+')
getOutput(url2).then(body => {
if (target.type == 'timeserie') {
let datapoints = body.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 = body.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
})
}
return httpRes.json(result)
})
})
})
app.listen(3000)
console.log('Server is listening to port 3000')