This repository has been archived by the owner on Dec 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
createissue.js
153 lines (129 loc) · 3.61 KB
/
createissue.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
const express = require('express');
const GitHubApi = require('@octokit/rest');
const _ = require('lodash');
const bodyParser = require('body-parser');
const winston = require('winston');
const logger = winston.createLogger({
level: 'debug',
transports: [
new winston.transports.File({ filename: 'combined.log' })
]
});
function postBodyErrorHandler(err, req, res, next) {
if (_.get(err, 'type') === 'entity.parse.failed') {
res.status(400).type('application/json').send({
error: {
code: 400,
message: `POST body not parseable as JSON: ${err.body}`
}
});
} else {
next();
}
}
// verify that req.body contains an actual JSON object
function preconditionsCheck(req, res, next) {
if (!process.env.GITHUB_ACCESS_TOKEN) {
res.status(500).type('application/json').send({
error: {
code: 500,
message: 'GITHUB_ACCESS_TOKEN not defined in process environment'
}
});
} else if (_.isEmpty(req.body)) {
logger.error('POST body empty');
res.status(400).type('application/json').send({
error: {
code: 400,
message: 'POST body empty'
}
});
} else if (!_.has(req.body, 'location')) {
logger.error('POST body empty');
res.status(400).type('application/json').send({
error: {
code: 400,
message: 'POST body missing \'location\''
}
});
} else if (!_.has(req.body, 'emailAddress')) {
logger.error('POST body empty');
res.status(400).type('application/json').send({
error: {
code: 400,
message: 'POST body missing \'emailAddress\''
}
});
} else if (!_.has(req.body, 'dataUrl')) {
logger.error('POST body empty');
res.status(400).type('application/json').send({
error: {
code: 400,
message: 'POST body missing \'dataUrl\''
}
});
} else if (!_.has(req.body, 'comments')) {
logger.error('POST body empty');
res.status(400).type('application/json').send({
error: {
code: 400,
message: 'POST body missing \'comments\''
}
});
} else {
next();
}
}
// login to github
function authenticateWithGithub(req, res, next) {
res.locals.github = new GitHubApi();
res.locals.github.authenticate({
type: 'oauth',
token: process.env.GITHUB_ACCESS_TOKEN
});
next();
}
// create an issue
async function createIssue(req, res, next) {
try {
let content = `*Location*: ${req.body.location}\n`;
content += `*Email Address*: ${req.body.emailAddress}\n`;
content += `*Data URL*: ${req.body.dataUrl}\n`;
content += `*Comments*: ${req.body.comments}`;
const response = await res.locals.github.issues.create({
owner: 'openaddresses',
repo: 'openaddresses',
title: `Submit Service Data for ${req.body.location}`,
body: content
});
// create issue was successful so extract the url and set into locals
res.locals.issueUrl = response.data.html_url;
next();
} catch (err) {
logger.error(`Error creating issue: ${err}`);
res.status(500).type('application/json').send({
error: {
code: 500,
message: `Error creating issue: ${err}`
}
});
}
}
// send the pull request URL back to the caller
function output(req, res, next) {
// entire github pipeline was successful so return the PR URL
res.status(200).type('application/json').send({
response: {
url: res.locals.issueUrl
}
});
}
module.exports = express.Router()
.use(bodyParser.json())
.use(postBodyErrorHandler)
.post('/', [
preconditionsCheck,
authenticateWithGithub,
createIssue,
output
]);