const axios = require('axios');
const Program = require('commander');
const Chalk = require('chalk');

module.exports = async (pluginInfo) => {
    if (typeof Program.releaseEventURL !== 'undefined') {
        try {
            await axios
                .post(Program.releaseEventURL, {
                    pluginName: pluginInfo.getName(),
                    pluginVersion: pluginInfo.getCurrentVersion(),
                    pluginLabel: {
                        de: pluginInfo.getLabel('de'),
                        en: pluginInfo.getLabel('en'),
                    },
                    pluginChangelog: {
                        de: pluginInfo.getChangelog('de', null),
                        en: pluginInfo.getChangelog('en', null),
                    },
                });
            console.log(Chalk.green.bold('Publish release event succeeded'));
        } catch (err) {
            console.log(Chalk.red.bold(`Publish release event failed: ${err.message}`));
        }
    }

    return true;
};

const axios = require('axios');
const Program = require('commander');
const Chalk = require('chalk');

/**
 * Create a release event object from a pluginInfo object.
 *
 * @param {Object} pluginInfo
 * @return {Object}
 */
function getReleaseEventInfo(pluginInfo) {
    return {
        pluginName: pluginInfo.getName(),
        pluginVersion: pluginInfo.getCurrentVersion(),
        pluginLabel: {
            de: pluginInfo.getLabel('de'),
            en: pluginInfo.getLabel('en'),
        },
        pluginChangelog: {
            de: pluginInfo.getChangelog('de', null),
            en: pluginInfo.getChangelog('en', null),
        },
    };
}

/**
 * If set call the release event endpoint.
 *
 * @param {Object} pluginInfo
 * @return {Boolean}
 */
module.exports = async (pluginInfo) => {
    if (Program.releaseEventURL) {
        try {
            await axios.post(Program.releaseEventURL, getReleaseEventInfo(pluginInfo));
            console.log(Chalk.green.bold('Publish release event succeeded'));
        } catch (err) {
            console.log(Chalk.red.bold(`Publish release event failed: ${err.message}`));
        }
    }

    return true;
};

Move relese event object generation into function