const axios  = require('axios')
const log    = require('../utils/log.utils')

async function processor(data) {
    log.out(`issueService.processor: Issue received`)
    let out = {}
    out.title = data.subject.replace(/<[^>]+>|[\*\$]/g, '');
    out.body = data.msg.replace(/<[^>]+>|[\*\$]/g, '')
    sendToGitea(out);
}

async function sendToGitea(body) {
    let key = process.env.OWL_GIT_ISSUEBOT
    let url = process.env.OWL_GIT_APIENDPOINT
    let opts = {
        headers: {
            Authorization: key
        }
    }
    var res = await axios.post(url, body, opts)
    // Need to read the output from the POST and pass the result upwards to the client.
    if (res.status == 201) {
        log.out("issueService.sendToGitea: Issue sent to Gitea")
        return {status: res.status,message:"issue created"}
    } else {
        log.out("issueService.sendToGitea: Failed to send issue to Gitea")
        return {status: res.status,message:"issue not created"}
    }
}

module.exports = {
    processor
}