1
0
Fork 0

feat(backend): Add support for (creating) issues

This commit is contained in:
joostdecock 2023-05-30 16:47:22 +02:00
parent e4e808f118
commit f6d6520fb8
6 changed files with 110 additions and 1 deletions

View file

@ -0,0 +1,74 @@
import fetch from 'node-fetch'
import { log } from '../utils/log.mjs'
import { UserModel } from './user.mjs'
export function IssueModel(tools) {
this.config = tools.config
this.prisma = tools.prisma
this.User = new UserModel(tools)
this.token = tools.config.github.token
return this
}
IssueModel.prototype.setResponse = function (status = 200, error = false, data = {}) {
this.response = {
status,
body: {
result: 'success',
...data,
},
}
if (status === 201) this.response.body.result = 'created'
else if (status > 204) {
this.response.body.error = error
this.response.body.result = 'error'
this.error = true
} else this.error = false
return this
}
IssueModel.prototype.sendResponse = async function (res) {
return res.status(this.response.status).send(this.response.body)
}
IssueModel.prototype.unguardedDelete = async function () {
await this.prisma.apikey.delete({ where: { id: this.record.id } })
this.record = null
this.clear = null
return this.setExists()
}
IssueModel.prototype.create = async function ({ body }) {
if (!this.token) return this.setResponse(400, 'notEnabled')
if (Object.keys(body).length < 1) return this.setResponse(400, 'postBodyMissing')
if (!body.title) return this.setResponse(400, 'titleMissing')
if (!body.body) return this.setResponse(400, 'bodyMissing')
const apiUrl = `https://api.github.com/repos/freesewing/freesewing/issues`
let response
try {
response = await fetch(apiUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${this.token}`,
Accept: 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})
if (response.status === 201) response = await response.json()
else {
console.log(response)
response = false
}
} catch (error) {
console.error('An error occurred while creating a GitHub issue:', error.message)
response = false
}
return response ? this.setResponse(201, 'created', { issue: response }) : this.setResponse(400)
}

View file

@ -145,8 +145,12 @@ PatternModel.prototype.publicRead = async function ({ params }) {
PatternModel.prototype.guardedRead = async function ({ params, user }) {
if (!this.rbac.readSome(user)) return this.setResponse(403, 'insufficientAccessLevel')
if (user.iss && user.status < 1) return this.setResponse(403, 'accountStatusLacking')
if (typeof params.id !== 'undefined' && !Number(params.id))
return this.setResponse(403, 'idNotNumeric')
await this.read({ id: parseInt(params.id) })
if (!this.record) return this.setResponse(404, 'notFound')
if (this.record.userId !== user.uid && !this.rbac.bughunter(user)) {
return this.setResponse(403, 'insufficientAccessLevel')
}