All files / modules/octokit index.js

91.67% Statements 33/36
94.44% Branches 17/18
100% Functions 5/5
91.67% Lines 33/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      3x 3x 3x                       3x 4x 1x       1x   4x 1x       1x   4x 1x       1x   4x 1x       1x   4x             4x                 3x 4x                           3x 2x                           3x 2x                           3x 4x 4x 1x 1x   3x 3x                   3x       3x        
/**
 * @module octokit
 */
const {request} = require("@octokit/request");
const log = require("../../util/log");
const digest = require("../digest");
 
/**
 * Cryptographically anonymise the data of an issue body
 * to ensure privacy of users.
 * Anonymise the following parameters (if present):
 * email, ip, name, reCAPTCHA response.
 * The anonymised data can be used for reference by server
 * admins with the possession of the server's secret.
 * @param {Object} data
 * @return {Object} anonymised data
 */
const anonymiseData = (data) => {
    if (data.email) {
        const anonId = digest.hashPassWithSaltInHex(
            data.email,
            process.env.SERVER_SECRET,
        );
        data.email = anonId;
    }
    if (data.ip) {
        const anonIp = digest.hashPassWithSaltInHex(
            data.ip,
            process.env.SERVER_SECRET,
        );
        data.ip = anonIp;
    }
    if (data.name) {
        const anonName = digest.hashPassWithSaltInHex(
            data.name,
            process.env.SERVER_SECRET,
        );
        data.name = anonName;
    }
    if (data['g-recaptcha-response']) {
        const anonRes = digest.hashPassWithSaltInHex(
            data['g-recaptcha-response'],
            process.env.SERVER_SECRET,
        );
        data['g-recaptcha-response'] = anonRes;
    }
    Iif (data['_csrf']) {
        const anonRes = digest.hashPassWithSaltInHex(
            data['_csrf'],
            process.env.SERVER_SECRET,
        );
        data['_csrf'] = anonRes;
    }
    return data;
};
 
/**
 * Create markdown formatted issue body string
 * from pretty printed issue data and metadata.
 * @param {Object} data
 * @return {String} markdown formatted issue body
 */
const createIssueBody = (data) => {
    return `### ${data.subject ? data.subject : 'Route ' + data.originalUrl}\n\n` +
        `${data.report ? data.report : data.message}\n\n` +
        'Metadata:\n```yaml\n' +
        JSON.stringify(data, null, 4) +
        '\n```';
};
 
/**
 * Create bugreport Github issue:
 * Anonymise PII in bugreport, create
 * formatted issue body and post the
 * issue to Github via Github API.
 * @param {Object} bugreport
 */
const createBugreportIssue = async (bugreport) => {
    await createIssue(
        `[bugreport] ${bugreport.subject ? bugreport.subject : 'Auto bugreport for route ' + bugreport.originalUrl}`,
        createIssueBody(anonymiseData(bugreport)),
        "bugreport",
    );
};
 
/**
 * Create feature request Github issue:
 * Anonymise PII in feature request, create
 * formatted issue body and post the
 * issue to Github via Github API.
 * @param {Object} featureRequest
 */
const createFeatureRequestIssue = async (featureRequest) => {
    await createIssue(
        `[feature request] ${featureRequest.subject}`,
        createIssueBody(anonymiseData(featureRequest)),
        "feature request",
    );
};
 
/**
 * Create an issue on Github via Github API
 * with given issue title, body and label.
 * @param {String} title
 * @param {String} body
 * @param {String} label
 */
const createIssue = async (title, body, label) => {
    try {
        if (process.env.CREATE_GITHUB_ISSUES === '0') {
            log.info("Skipping creation of Github Issue - %s", title);
            return;
        }
        log.info("Creating Github Issue - %s", title);
        await request("POST /repos/almasen/rdf-mapped/issues", {
            headers: {
                authorization: `token ${process.env.GITHUB_TOKEN}`,
            },
            title,
            body,
            labels: [label],
        });
        log.info("Successfully created Github Issue - %s", title);
    } catch (error) {
        log.error("Github Issue creation FAILED, err: " + error.message);
    };
};
 
module.exports = {
    createBugreportIssue,
    createFeatureRequestIssue,
};