ronantakizawa/codereview-bench huggingface dataset:
Repository: louislam/uptime-kuma
File: server/notification-providers/smspartner.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SMSPartner extends NotificationProvider {
    name = "SMSPartner";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://api.smspartner.fr/v1/send";

        try {
            //Lets remove non ascii char
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "");

            let data = {
                "apiKey": notification.smspartnerApikey,
                "gamme": 1,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg.substring(0, 639)
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                let error = "Something gone wrong. Api returned " + resp.data.response.status + ".";
                this.throwGeneralAxiosError(error);
            }

            return okMsg;

Review comment (suggestion):
Lets do all `msg`-saintisation here.

```suggestion
            // smspartner does not support non ascii characters and only a maximum 639 characters
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "").substring(0, 639);
```

Code after:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SMSPartner extends NotificationProvider {
    name = "SMSPartner";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://api.smspartner.fr/v1/send";

        try {
            // smspartner does not support non ascii characters and only a maximum 639 characters
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "").substring(0, 639);

            let data = {
                "apiKey": notification.smspartnerApikey,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg,
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                throw Error(`Api returned ${resp.data.response.status}.`);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
Repository: louislam/uptime-kuma
File: server/notification-providers/smspartner.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SMSPartner extends NotificationProvider {
    name = "SMSPartner";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://api.smspartner.fr/v1/send";

        try {
            //Lets remove non ascii char
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "");

            let data = {
                "apiKey": notification.smspartnerApikey,
                "gamme": 1,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg.substring(0, 639)
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                let error = "Something gone wrong. Api returned " + resp.data.response.status + ".";
                this.throwGeneralAxiosError(error);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SMSPartner;

Review comment (suggestion):
see above

```suggestion
                "message": cleanMsg,
```

Code after:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");

class SMSPartner extends NotificationProvider {
    name = "SMSPartner";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";
        const url = "https://api.smspartner.fr/v1/send";

        try {
            // smspartner does not support non ascii characters and only a maximum 639 characters
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "").substring(0, 639);

            let data = {
                "apiKey": notification.smspartnerApikey,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg,
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                throw Error(`Api returned ${resp.data.response.status}.`);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SMSPartner;
Repository: louislam/uptime-kuma
File: server/notification-providers/smspartner.js (JavaScript)

Code before:

        try {
            //Lets remove non ascii char
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "");

            let data = {
                "apiKey": notification.smspartnerApikey,
                "gamme": 1,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg.substring(0, 639)
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                let error = "Something gone wrong. Api returned " + resp.data.response.status + ".";
                this.throwGeneralAxiosError(error);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SMSPartner;


Review comment (suggestion):
nit: can be shortened.

Also lets not use `throwGeneralAxiosError` twice, as that would lead to `Error: Error:` in the output ^^

```suggestion
                throw Error(`Api returned ${resp.data.response.status}.`);
```

Code after:

        try {
            // smspartner does not support non ascii characters and only a maximum 639 characters
            let cleanMsg = msg.replace(/[^\x00-\x7F]/g, "").substring(0, 639);

            let data = {
                "apiKey": notification.smspartnerApikey,
                "sender": notification.smspartnerSenderName.substring(0, 11),
                "phoneNumbers": notification.smspartnerPhoneNumber,
                "message": cleanMsg,
            };

            let config = {
                headers: {
                    "Content-Type": "application/json",
                    "cache-control": "no-cache",
                    "Accept": "application/json",
                }
            };

            let resp = await axios.post(url, data, config);

            if (resp.data.success !== true) {
                throw Error(`Api returned ${resp.data.response.status}.`);
            }

            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SMSPartner;
Repository: louislam/uptime-kuma
File: src/components/HeartbeatBar.vue (Vue)

Code before:
<template>
    <div ref="wrap" class="wrap" :style="wrapStyle">
        <div class="hp-bar-big" :style="barStyle">
            <div
                v-for="(beat, index) in shortBeatList"
                :key="index"
                class="beat"
                :class="{ 'empty': (beat === 0), 'down': (beat.status === 0), 'pending': (beat.status === 2), 'maintenance': (beat.status === 3) }"
                :style="beatStyle"
                :title="getBeatTitle(beat)"
            />
        </div>
        <div
            v-if="!$root.isMobile && size !== 'small' && beatList.length > 4 && $root.styleElapsedTime !== 'none'"
            class="d-flex justify-content-between align-items-center word" :style="timeStyle"
        >
            <div>{{ $t("time ago", [timeSinceFirstBeat]) }}</div>
            <div v-if="$root.styleElapsedTime === 'with-line'" class="connecting-line"></div>
            <div>{{ timeSinceLastBeat }}</div>
        </div>
    </div>
</template>

<script>
import dayjs from "dayjs";

export default {
    props: {
        /** Size of the heartbeat bar */
        size: {
            type: String,
            default: "big",
        },
        /** ID of the monitor */
        monitorId: {
            type: Number,
            required: true,
        },
        /** Array of the monitors heartbeats */
        heartbeatList: {
            type: Array,
            default: null,

Review comment (suggestion):
```suggestion
            <div>{{ timeSinceFirstBeat }}</div>
```

Code after:
<template>
    <div ref="wrap" class="wrap" :style="wrapStyle">
        <div class="hp-bar-big" :style="barStyle">
            <div
                v-for="(beat, index) in shortBeatList"
                :key="index"
                class="beat"
                :class="{ 'empty': (beat === 0), 'down': (beat.status === 0), 'pending': (beat.status === 2), 'maintenance': (beat.status === 3) }"
                :style="beatStyle"
                :title="getBeatTitle(beat)"
            />
        </div>
        <div
            v-if="!$root.isMobile && size !== 'small' && beatList.length > 4 && $root.styleElapsedTime !== 'none'"
            class="d-flex justify-content-between align-items-center word" :style="timeStyle"
        >
            <div>{{ timeSinceFirstBeat }}</div>
            <div v-if="$root.styleElapsedTime === 'with-line'" class="connecting-line"></div>
            <div>{{ timeSinceLastBeat }}</div>
        </div>
    </div>
</template>

<script>
import dayjs from "dayjs";

export default {
    props: {
        /** Size of the heartbeat bar */
        size: {
            type: String,
            default: "big",
        },
        /** ID of the monitor */
        monitorId: {
            type: Number,
            required: true,
        },
        /** Array of the monitors heartbeats */
        heartbeatList: {
            type: Array,
            default: null,
Repository: louislam/uptime-kuma
File: src/util.ts (TypeScript)

Code before:
    "WARN": CONSOLE_STYLE_FgYellow,
    "ERROR": CONSOLE_STYLE_FgRed,
    "DEBUG": CONSOLE_STYLE_FgGray,
};

export const badgeConstants = {
    naColor: "#999",
    defaultUpColor: "#66c20a",
    defaultWarnColor: "#eed202",
    defaultDownColor: "#c2290a",
    defaultPendingColor: "#f8a306",
    defaultMaintenanceColor: "#1747f5",
    defaultPingColor: "blue",  // as defined by badge-maker / shields.io
    defaultStyle: "flat",
    defaultPingValueSuffix: "ms",
    defaultPingLabelSuffix: "h",
    defaultUptimeValueSuffix: "%",
    defaultUptimeLabelSuffix: "h",
    defaultCertExpValueSuffix: " days",
    defaultCertExpLabelSuffix: "h",
    // Values Come From Default Notification Times
    defaultCertExpireWarnDays: "14",
    defaultCertExpireDownDays: "7"
};

export const durationUnits = {
    MINUTE: "m",
    HOUR: "h",
    DAY: "d",
    WEEK: "w",
    MONTH: "M",
    YEAR: "y"
};

/**
 * Flip the status of s
 * @param s input status: UP or DOWN
 * @returns {number} UP or DOWN
 */
export function flipStatus(s: number) {
    if (s === UP) {
        return DOWN;
    }

    if (s === DOWN) {
        return UP;
    }

    return s;
}


Review comment (refactor):
Don't think we need to extract this.
Also I think in v1.0 we did not have `m` be minutes, but would need to look this up

Code after:
    "WARN": CONSOLE_STYLE_FgYellow,
    "ERROR": CONSOLE_STYLE_FgRed,
    "DEBUG": CONSOLE_STYLE_FgGray,
};

/**
 * Flip the status of s
 * @param s input status: UP or DOWN
 * @returns {number} UP or DOWN
 */
export const badgeConstants = {
    naColor: "#999",
    defaultUpColor: "#66c20a",
    defaultWarnColor: "#eed202",
    defaultDownColor: "#c2290a",
    defaultPendingColor: "#f8a306",
    defaultMaintenanceColor: "#1747f5",
    defaultPingColor: "blue",  // as defined by badge-maker / shields.io
    defaultStyle: "flat",
    defaultPingValueSuffix: "ms",
    defaultPingLabelSuffix: "h",
    defaultUptimeValueSuffix: "%",
    defaultUptimeLabelSuffix: "h",
    defaultCertExpValueSuffix: " days",
    defaultCertExpLabelSuffix: "h",
    // Values Come From Default Notification Times
    defaultCertExpireWarnDays: "14",
    defaultCertExpireDownDays: "7"
};

/** Flip the status of s */
export function flipStatus(s: number) {
    if (s === UP) {
        return DOWN;
    }

    if (s === DOWN) {
        return UP;
    }

    return s;
}

/**
 * Delays for specified number of seconds
 * @param ms Number of milliseconds to sleep for
 * @returns {Promise<void>} Promise that resolves after ms
 */
export function sleep(ms: number) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
Repository: louislam/uptime-kuma
File: src/components/notifications/Onesender.vue (Vue)

Code before:
<template>
    <div class="mb-3">
        <label for="host-onesender" class="form-label">{{ $t("HostOnesender") }}</label>
        <input
            id="host-onesender"
            v-model="$parent.notification.onesenderURL"
            type="url"
            placeholder="https://xxxxxxxxxxx.com/api/v1/messages"
            pattern="https?://.+"
            class="form-control"
            required
        />
    </div>

    <div class="mb-3">
        <label for="receiver-onesender" class="form-label">{{ $t("TokenOnesender") }}</label>
        <HiddenInput id="receiver-onesender" v-model="$parent.notification.onesenderToken" :required="true" autocomplete="false"></HiddenInput>
        <i18n-t tag="div" keypath="wayToGetOnesenderUrlandToken" class="form-text">
            <a href="https://onesender.net/" target="_blank">{{ $t("here") }}</a>
        </i18n-t>
    </div>

    <div class="mb-3">
        <label for="webhook-request-body" class="form-label">{{ $t("TypeOnesender") }}</label>
        <select
            id="webhook-request-body"
            v-model="$parent.notification.onesenderTypeReceiver"
            class="form-select"

Review comment (suggestion):
```suggestion
        <label for="host-onesender" class="form-label">{{ $t("Host Onesender") }}</label>
```

Code after:
<template>
    <div class="mb-3">
        <label for="host-onesender" class="form-label">{{ $t("Host Onesender") }}</label>
        <input
            id="host-onesender"
            v-model="$parent.notification.onesenderURL"
            type="url"
            placeholder="https://xxxxxxxxxxx.com/api/v1/messages"
            pattern="https?://.+"
            class="form-control"
            required
        />
    </div>

    <div class="mb-3">
        <label for="receiver-onesender" class="form-label">{{ $t("Token Onesender") }}</label>
        <HiddenInput id="receiver-onesender" v-model="$parent.notification.onesenderToken" :required="true" autocomplete="false"></HiddenInput>
        <i18n-t tag="div" keypath="wayToGetOnesenderUrlandToken" class="form-text">
            <a href="https://onesender.net/" target="_blank">{{ $t("here") }}</a>
        </i18n-t>
    </div>

    <div class="mb-3">
        <label for="webhook-request-body" class="form-label">{{ $t("Recipient Type") }}</label>
        <select
            id="webhook-request-body"
            v-model="$parent.notification.onesenderTypeReceiver"
            class="form-select"
Repository: louislam/uptime-kuma
File: server/notification-providers/signl4.js (JavaScript)

Code before:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { Liquid } = require("liquidjs");
const { UP, DOWN } = require("../../src/util");

class SIGNL4 extends NotificationProvider {

    name = "SIGNL4";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        let okMsg = "Sent Successfully.";

        try {
            let data = {
                heartbeat: heartbeatJSON,
                monitor: monitorJSON,
                msg,
            };
            let config = {
                headers: {
                    "Content-Type": "application/json"
                }
            };

            // Source system
            data["X-S4-SourceSystem"] = "UptimeKuma";

            // Monitor URL
            let monitorUrl;
            if (monitorJSON) {
                if (monitorJSON.type === "port") {
                    monitorUrl = monitorJSON.hostname;
                    if (monitorJSON.port) {
                        monitorUrl += ":" + monitorJSON.port;
                    }
                } else if (monitorJSON.hostname != null) {

Review comment (bug):
please prefer const where possible. (this communicate intentions a bit clearer, I know that this is not effective in stopping bugs)

```suggestion
        const okMsg = "Sent Successfully.";
```

Code after:
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { UP, DOWN } = require("../../src/util");

class SIGNL4 extends NotificationProvider {
    name = "SIGNL4";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let data = {
                heartbeat: heartbeatJSON,
                monitor: monitorJSON,
                msg,
                // Source system
                "X-S4-SourceSystem": "UptimeKuma",
                monitorUrl: this.extractAdress(monitorJSON),
            };

            const config = {
                headers: {
                    "Content-Type": "application/json"
                }
            };

            if (heartbeatJSON == null) {
                // Test alert
                data.title = "Uptime Kuma Alert";
                data.message = msg;
            } else if (heartbeatJSON.status === UP) {
                data.title = "Uptime Kuma Monitor ✅ Up";
                data["X-S4-ExternalID"] = "UptimeKuma-" + monitorJSON.monitorID;
                data["X-S4-Status"] = "resolved";
            } else if (heartbeatJSON.status === DOWN) {
                data.title = "Uptime Kuma Monitor 🔴 Down";
Repository: louislam/uptime-kuma
File: server/notification-providers/signl4.js (JavaScript)

Code before:

            // Source system
            data["X-S4-SourceSystem"] = "UptimeKuma";

            // Monitor URL
            let monitorUrl;
            if (monitorJSON) {
                if (monitorJSON.type === "port") {
                    monitorUrl = monitorJSON.hostname;
                    if (monitorJSON.port) {
                        monitorUrl += ":" + monitorJSON.port;
                    }
                } else if (monitorJSON.hostname != null) {
                    monitorUrl = monitorJSON.hostname;
                } else {
                    monitorUrl = monitorJSON.url;
                }
            }

            if (heartbeatJSON == null) {
                // Test alert
                data.title = "Uptime Kuma Alert";
                data.message = "Uptime Kuma Test Alert";
            } else if (heartbeatJSON.status === UP) {
                data.title = "Uptime Kuma Monitor ✅ Up";
                data["X-S4-ExternalID"] = "UptimeKuma" + monitorUrl;
                data["X-S4-Status"] = "resolved";
            } else if (heartbeatJSON.status === DOWN) {
                data.title = "Uptime Kuma Monitor 🔴 Down";
                data["X-S4-ExternalID"] = "UptimeKuma" + monitorUrl;
                data["X-S4-Status"] = "new";
            }

            if (notification.webhookContentType === "custom") {
                // Initialize LiquidJS and parse the custom Body Template
                const engine = new Liquid();
                const tpl = engine.parse(notification.webhookCustomBody);

                // Insert templated values into Body
                data = await engine.render(tpl,
                    {
                        msg,
                        heartbeatJSON,
                        monitorJSON
                    });
            }

            await axios.post(notification.webhookURL, data, config);
            return okMsg;

        } catch (error) {

Review comment (suggestion):
the monitoring url is likely a bad ID as it could not be set.
I think there should be the id for the monitor in either `heartbeatJSON` or `monitorJSON`

Code after:
                }
            };

            if (heartbeatJSON == null) {
                // Test alert
                data.title = "Uptime Kuma Alert";
                data.message = msg;
            } else if (heartbeatJSON.status === UP) {
                data.title = "Uptime Kuma Monitor ✅ Up";
                data["X-S4-ExternalID"] = "UptimeKuma-" + monitorJSON.monitorID;
                data["X-S4-Status"] = "resolved";
            } else if (heartbeatJSON.status === DOWN) {
                data.title = "Uptime Kuma Monitor 🔴 Down";
                data["X-S4-ExternalID"] = "UptimeKuma-" + monitorJSON.monitorID;
                data["X-S4-Status"] = "new";
            }

            await axios.post(notification.webhookURL, data, config);
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = SIGNL4;