-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
158 lines (136 loc) · 4.5 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
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
module.exports = feedback
const Gitlab = require('gitlab/dist/es5').default
const bodyParser = require('body-parser')
const requiredOptions = ['url', 'token', 'repository']
function feedback (options) {
options = options || {}
options.auth = options.auth || null
options.labels = options.labels || ['new']
options.store = options.store || {}
options.store.repository = options.store.repository || options.repository
options.store.branch = options.store.branch || 'master'
options.store.path = options.store.path || 'screenshots'
options.store.limit = options.store.limit || '1mb'
requiredOptions.forEach(function checkForRequired (required) {
if (!options[required]) {
throw new Error('Setting in feedback configuration missing: ' + required)
}
})
const gitlabConfig = {
url: options.url,
token: options.token
}
if (options.auth) {
gitlabConfig.auth = [options.auth.user, options.auth.password]
}
const gitlab = new Gitlab(gitlabConfig)
function handler (req, res, next) {
// parse JSON body
bodyParser.json({ limit: options.store.limit }).call(this, req, res, function () {})
const filename = (new Date()).toJSON().replace('.', '-').replace(/:/g, '') + '.png'
let filepath = ''
if (options.store.path !== '') {
filepath = options.store.path.replace(/^\//, '').replace(/\/$/, '') + '/'
}
filepath += filename
const content = req.body.img.replace(/^data:([A-Za-z-+/]+);base64,/, '')
const screenshotUrl = options.url + '/' + options.store.repository.slug + '/raw/' + options.store.branch + '/' + filepath
const issue = generateIssue(req.body, screenshotUrl, options)
res.json({
result: 'OK'
})
gitlab.Issues.create(options.repository.id, issue).then(function (data) {
const issueId = data.iid
gitlab.RepositoryFiles.create(options.store.repository.id, filepath, options.store.branch, {
encoding: 'base64',
content: content,
commit_message: 'Screenshot for Issue #' + issueId
}).then(function (data) {
console.log('Issue #' + issueId + ' created')
})
})
}
return function (req, res, next) {
bodyParser.json({ limit: options.store.limit }).call(this, req, res, function (err) {
if (err) {
next(err)
return
}
const self = this
getRepositoryObject(options.repository, gitlab, function (_err, obj) {
options.repository = obj
getRepositoryObject(options.store.repository, gitlab, function (_err, obj) {
options.store.repository = obj
handler.call(self, req, res, next)
})
})
})
}
}
function getRepositoryObject (identifier, gitlab, callback) {
gitlab.Projects.all().then(function (repos) {
let i
if (typeof identifier === 'string') {
for (i = 0; i < repos.length; i++) {
if (repos[i].path_with_namespace === identifier.replace(/ /g, '')) {
callback(null, {
id: repos[i].id,
slug: repos[i].path_with_namespace
})
return
}
}
} else if (typeof identifier === 'number') {
for (i = 0; i < repos.length; i++) {
if (repos[i].id === identifier) {
callback(null, {
id: repos[i].id,
slug: repos[i].path_with_namespace
})
return
}
}
}
throw new Error('Repository not found: ' + identifier)
})
}
/**
* Generate the entity object for issue creation.
* @param {Object} body parsed request body
* @param {String} url screenshot URL
* @param {Object} options
* @return {Object} object that can be used by gitlab.issue.create()
*/
function generateIssue (body, url, options) {
const now = new Date()
let description = [
body.note,
'',
'## Browser Information',
'',
'- Platform: ' + body.browser.platform,
'- User Agent: ' + body.browser.userAgent,
'- Cookies enabled: ' + (body.browser.cookieEnabled ? 'yes' : 'no'),
'- Plugins: ' + body.browser.plugins.join(', '),
'',
'## Additional Information',
'',
'- URL: ' + body.url
]
if (body.contact) {
description.push('- Reported by: ' + body.contact)
}
description = description.concat([
'- Reported at: ' + now.toLocaleTimeString() + ' ' + now.toLocaleDateString(),
'',
'## Screenshot',
'',
''
])
const issue = {
title: body.note.slice(0, 30),
description: description.join('\n'),
labels: options.labels.join(',')
}
return issue
}