Skip to content

Commit

Permalink
feat: initial run command (#6)
Browse files Browse the repository at this point in the history
* feat: initial run command

* chore: cleanup
  • Loading branch information
ndom91 authored Jul 26, 2021
1 parent bfc2495 commit 372e167
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 5 deletions.
26 changes: 26 additions & 0 deletions sdk/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readdir, readFile } = require('fs/promises')
const axios = require('axios')
const endpoints = require('./endpoints')

Expand All @@ -20,6 +21,31 @@ function init({ api, apiKey, baseURL }) {
return _api.get(endpoints.CHECKS, { limit, page })
},

async getAllLocal() {
const checks = await readdir(`.checkly/checks`)
return Promise.all(
checks.map((check) => readFile(`.checkly/checks/${check}`, 'utf-8'))
)
},

async run(check) {
const req = axios.create({
method: 'POST',
headers: {
'X-Checkly-Account': 'e46106d8-e382-4d1f-8182-9d63983ed6d4',
Authorization:
'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik9UTXhRakEzUVRkQk5EZEROMEpGUmtJMlJVVTROemhHTmtVelJrVTBNakE0T0RoRlEwWTRSQSJ9.eyJodHRwczovL2NoZWNrbHlocS5jb20vZW1haWwiOiJuaWNvQGNoZWNrbHlocS5jb20iLCJpc3MiOiJodHRwczovL2F1dGguY2hlY2tseWhxLmNvbS8iLCJzdWIiOiJnb29nbGUtb2F1dGgyfDEwMDQ3NTIyOTAwNzczMTMzNjU4MiIsImF1ZCI6WyJhcGktZGV2LmNoZWNrbHkucnVuIiwiaHR0cHM6Ly9jaGVja2x5LmV1LmF1dGgwLmNvbS91c2VyaW5mbyJdLCJpYXQiOjE2MjcwNDA1ODIsImV4cCI6MTYyNzQ3MjU4MiwiYXpwIjoiM0laNDhVTXZodFFxb2FodnJYWXJwQ3FjdXBoVURXZkgiLCJzY29wZSI6Im9wZW5pZCBwcm9maWxlIGVtYWlsIn0.cB0auCleZGd4IHO7T7-J4qvgA8RB1kTB238BxYD2Znr75d_j3D9NU835OeAtUPlOP1rqJLoklFy_4DaJNv0SUE33AkeZR_PL5dzPCQSNL3sjoo2KDmYXyo9NdW-f27-RL8vlsHGEMpipKZTc6ozY8n4ah4Oilk5hhmi3DS3G947tW020QjjQkEIucUgXMVFlNMgl4G0LelFI-DY_uENJdiJZyMSfJwp5UkJXiXqCZ2C52BEUefM66hLs3Gbe5lEqijt_f5VMmmlO_kA3Y9-p9XL5lIqGK7shSgpRTqFLv-ldD6BAWdKlw4D5CxfL-tXyyphfFt4EfBIorTe988FeEg',
'Content-Type': 'application/json',
Origin: 'http://localhost:8081',
Referer: 'http://localhost:8081/',
},
})
return req.post(
'http://localhost:3000/accounts/e46106d8-e382-4d1f-8182-9d63983ed6d4/browser-check-runs',
check[0]
)
},

create({ script, name, checkType = 'BROWSER', activated = true } = {}) {
return _api.post(endpoints.CHECKS, { name, script, checkType, activated })
},
Expand Down
12 changes: 10 additions & 2 deletions src/commands/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ class ChecksCommand extends Command {
required: true,
description: 'Specify the type of checks action to run',
default: 'list',
options: ['list', 'info'],
options: ['list', 'info', 'run'],
},
{
name: 'id',
required: false,
description: 'Specify the check di',
description: 'Specify the checkId',
},
]

async run() {
const { args, flags } = this.parse(ChecksCommand)

switch (args.action) {
case 'run':
return checks.run(args.id, { ...flags })
case 'info':
return checks.info(args.id, { ...flags })
default:
Expand All @@ -41,6 +43,12 @@ ChecksCommand.flags = {
default: defaultOutput,
options: ['text', 'json'],
}),
checkName: flags.string({
char: 'c',
description: 'Check upon which to execute action',
default: defaultOutput,
options: ['text', 'json'],
}),
}

module.exports = ChecksCommand
8 changes: 6 additions & 2 deletions src/commands/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ const consola = require('consola')
const { Command } = require('@oclif/command')
const { account } = require('./../services/api')
const config = require('./../services/config')
const { checkTemplate, settingsTemplate } = require('../templates/init')
const {
// checkTemplate,
defaultCheckTemplate,
settingsTemplate,
} = require('../templates/init')

class InitCommand extends Command {
static args = [
Expand Down Expand Up @@ -42,7 +46,7 @@ class InitCommand extends Command {
})

// Example Check YML
const exampleCheckYml = checkTemplate()
const exampleCheckYml = defaultCheckTemplate()

// Create Settings File
fs.writeFileSync(path.join(dirName, 'settings.yml'), accountSettingsYml)
Expand Down
4 changes: 3 additions & 1 deletion src/modules/checks/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const list = require('./list')
const info = require('./info')
const run = require('./run')

module.exports = {
list,
info
info,
run,
}
22 changes: 22 additions & 0 deletions src/modules/checks/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const consola = require('consola')
const YAML = require('yaml')
const { checks } = require('../../services/api')
const { print } = require('../../services/utils')

async function runCheck(checkName = '') {
try {
const rawChecks = await checks.getAllLocal(checkName)
const parsedChecks = rawChecks.map((rawCheck) => YAML.parse(rawCheck))
const selectedCheck = parsedChecks.filter(
(check) => check.name === checkName
)
const results = await checks.run(selectedCheck)
if (results.status === 202) {
print(' Check successfully submitted')
}
} catch (err) {
consola.error(err)
}
}

module.exports = runCheck
78 changes: 78 additions & 0 deletions src/templates/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,85 @@ url: https://jsonplaceholder.typicode.com/users
`
}

const defaultCheckTemplate = () => {
return `checkType: BROWSER
name: 'example'
frequency: 10
activated: true
muted: false
doubleCheck: true
locations:
- eu-central-1
- eu-west-3
script: |-
const { chromium } = require("playwright")
const expect = require("expect")
// Start a browser session
const browser = await chromium.launch()
const page = await browser.newPage()
// Go to a page. This waits till the 'load' event by default
await page.goto("https://google.com/")
// Assert a specific page item to be present
const title = await page.title()
expect(title).toBe("Google")
// Snap a screenshot
await page.screenshot({ path: "screen.png", fullScreen: false })
// Close the session
await browser.close()
alertSettings:
muted: false
escalationType: RUN_BASED
runBasedEscalation:
failedRunThreshold: 1
timeBasedEscalation:
minutesFailingThreshold: 5
reminders:
amount: 0
interval: 5
sslCertificates:
enabled: true
alertThreshold: 30
useGlobalAlertSettings: true
alertChannelSubscriptions:
- activated: true
alertChannelId: 4
alertChannel:
id: 4
type: EMAIL
config:
address: [email protected]
accountId: e46106d8-e382-4d1f-8182-9d63983ed6d4
created_at: '2021-07-01T10:22:10.559Z'
updated_at:
sendRecovery: true
sendFailure: true
sendDegraded: false
sslExpiry: false
sslExpiryThreshold: 30
subscriptions:
- id: 22
checkId:
alertChannelId: 4
activated: true
groupId: 6
check:
checkGroup:
id: 6
name: Big ol group
environmentVariables: []
tags: []
runtimeId: '2020.01'
websocketClientId: 6819de32-7723-421f-b3b1-76004c4ebfef
runLocation: eu-central-1`
}

module.exports = {
settingsTemplate,
checkTemplate,
defaultCheckTemplate,
}

0 comments on commit 372e167

Please sign in to comment.