Skip to content

Commit

Permalink
feat: add uptime robot functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dr5hn committed Oct 17, 2024
1 parent 2e05d70 commit 2740740
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 60 deletions.
55 changes: 29 additions & 26 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
name: Test
name: Deploy with Monitor Control

on:
push:
branches:
- main
pull_request:
branches: [ main ]

env:
STATUSCAKE_MONITOR_IDS: '7006566,7172927'
UPTIMEROBOT_MONITOR_IDS: '787122950,779173736'

jobs:
test:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- uses: actions/checkout@v4

- name: Pause monitor
uses: ./ # Uses an action in the root directory
with:
statuscake_api_token: ${{ secrets.STATUSCAKE_API_TOKEN }}
statuscake_monitor_ids: ${{ secrets.STATUSCAKE_MONITOR_IDS }}
action: 'pause'
- name: Pause Monitoring
uses: ./
with:
action: pause
statuscake_monitor_ids: ${{ env.STATUSCAKE_MONITOR_IDS }}
uptimerobot_monitor_ids: ${{ env.UPTIMEROBOT_MONITOR_IDS }}
statuscake_api_token: ${{ secrets.STATUSCAKE_API_TOKEN }}
uptimerobot_api_key: ${{ secrets.UPTIMEROBOT_API_KEY }}

- name: Wait for 30 seconds
run: sleep 30
- name: Simulate Deployment
run: |
echo "Starting deployment..."
sleep 30
echo "Deployment completed."
- name: Restart monitor
uses: ./ # Uses an action in the root directory
with:
statuscake_api_token: ${{ secrets.STATUSCAKE_API_TOKEN }}
statuscake_monitor_ids: ${{ secrets.STATUSCAKE_MONITOR_IDS }}
action: 'restart'
- name: Resume Monitoring
uses: ./
with:
action: resume
statuscake_monitor_ids: ${{ env.STATUSCAKE_MONITOR_IDS }}
uptimerobot_monitor_ids: ${{ env.UPTIMEROBOT_MONITOR_IDS }}
statuscake_api_token: ${{ secrets.STATUSCAKE_API_TOKEN }}
uptimerobot_api_key: ${{ secrets.UPTIMEROBOT_API_KEY }}
32 changes: 19 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
name: "StatusCake Monitor Control"
description: "Pauses and restarts one or more StatusCake monitors"
author: "Clive Walkden"
name: 'StatusCake and UptimeRobot Monitor Control'
description: 'Pause/Resume monitoring for StatusCake and UptimeRobot during deployments'
author: 'Clive Walkden, Darshan Gada'
branding:
icon: "check-circle"
color: "green"
icon: 'check-circle'
color: 'green'
inputs:
statuscake_api_token:
description: "StatusCake API token"
required: true
statuscake_monitor_ids:
description: "Comma-separated list of StatusCake monitor IDs"
required: true
description: 'Comma-separated list of StatusCake monitor IDs'
required: false
uptimerobot_monitor_ids:
description: 'Comma-separated list of UptimeRobot monitor IDs'
required: false
action:
description: "'pause' to pause the monitor, 'restart' to restart the monitor"
description: 'Action to perform (pause or resume)'
required: true
statuscake_api_token:
description: 'StatusCake API token'
required: false
uptimerobot_api_key:
description: 'UptimeRobot API key'
required: false
runs:
using: "node20"
main: "dist/index.js"
using: 'node20'
main: 'dist/index.js'
81 changes: 60 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,71 @@
const core = require("@actions/core");
const { Configuration, UptimeApi } = require("@statuscake/statuscake-js");
const axios = require("axios");

async function controlUptimeRobotMonitor(monitorId, pause, apiKey) {
const status = pause ? 0 : 1;
const { data } = await axios.post(
"https://api.uptimerobot.com/v2/editMonitor",
{
api_key: apiKey,
id: monitorId,
status,
format: "json",
},
{
headers: { "content-type": "application/x-www-form-urlencoded" },
}
);
return data;
}

async function handleStatusCake(monitorIds, pause, apiToken) {
const config = new Configuration({
headers: { Authorization: `Bearer ${apiToken}` },
});
const service = new UptimeApi(config);

for (const id of monitorIds) {
core.debug(`Processing StatusCake monitor ID: ${id}`);
await service.updateUptimeTest({ testId: id, paused: pause });
core.info(`StatusCake monitor ${id} ${pause ? "paused" : "resumed"}`);
}
}

async function handleUptimeRobot(monitorIds, pause, apiKey) {
for (const id of monitorIds) {
core.debug(`Processing UptimeRobot monitor ID: ${id}`);
const result = await controlUptimeRobotMonitor(id, pause, apiKey);
if (result.stat === "ok") {
core.info(`UptimeRobot monitor ${id} ${pause ? "paused" : "resumed"}`);
} else {
throw new Error(`Failed to ${pause ? "pause" : "resume"} UptimeRobot monitor ${id}`);
}
}
}

async function run() {
try {
const monitorIds = core.getInput("statuscake_monitor_ids").split(",");
const action = core.getInput("action");
const apiToken = core.getInput("statuscake_api_token");

const pause = action === "pause"; // Convert the action to a boolean

const config = new Configuration({
headers: {
Authorization: `Bearer ${apiToken}`,
},
});
const service = new UptimeApi(config);

for (const monitorId of monitorIds) {
// trim any whitespace
monitorId.trim();
core.debug(`Processing monitor ID: ${monitorId}`);
core.debug(`Pausing monitor: ${pause}`);
service
.updateUptimeTest({ testId: monitorId, paused: pause })
.then((tests) => core.info(JSON.stringify(tests)));
const pause = action === "pause";
const statusCakeApiToken = core.getInput("statuscake_api_token");
const uptimeRobotApiKey = core.getInput("uptimerobot_api_key");

core.debug(`Action: ${action}`);

if (statusCakeApiToken) {
const statusCakeMonitorIds = core.getInput("statuscake_monitor_ids").split(",").map(id => id.trim());
await handleStatusCake(statusCakeMonitorIds, pause, statusCakeApiToken);
}

if (uptimeRobotApiKey) {
const uptimeRobotMonitorIds = core.getInput("uptimerobot_monitor_ids").split(",").map(id => id.trim());
await handleUptimeRobot(uptimeRobotMonitorIds, pause, uptimeRobotApiKey);
}

core.setOutput("result", `Monitoring ${pause ? "paused" : "resumed"} successfully`);
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);
core.setFailed(`Action failed with error: ${error.message}`);
}
}

Expand Down

0 comments on commit 2740740

Please sign in to comment.