Skip to content

Commit

Permalink
feat: catch buckets events
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 19, 2024
1 parent 7175c74 commit f4e7dcf
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 58 deletions.
4 changes: 4 additions & 0 deletions packages/common/config/load-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const os = require("os")
const path = require("path")
const { mkdtemp } = require("fs/promises")
const { v4: uuidv4 } = require("uuid")

const { satisfies } = require("compare-versions")
const fs = require("fs-extra")
Expand Down Expand Up @@ -241,6 +242,9 @@ const loadConfig = async (
env: "KS_FORCE_NEW_DEPLOY",
envParser: envParserYaml,
},
pipelineUUID: {
defaultFunction: () => uuidv4(),
},
pipelineId: {
defaultFunction: (config) => {
const {
Expand Down
1 change: 1 addition & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"slugify": "^1.6.5",
"sonic-boom": "^3.0.0",
"tiged": "^2.12.4",
"uuid": "^10.0.0",
"which": "^3.0.0",
"yaml": "^2.3.1",
"zx": "^7.1.1"
Expand Down
6 changes: 4 additions & 2 deletions packages/common/utils/flatten-aggregate-error.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const indent = require("./indent")

module.exports = (aggregateError) =>
new Error(
module.exports = (aggregateError) => {
console.log("aggregateError", aggregateError)
return new Error(
`${aggregateError.name} ${aggregateError.message}: \n${indent(
aggregateError.errors
.map((error) => `${error.stack.toString()}`)
.join("\n"),
2
)}`
)
}
38 changes: 0 additions & 38 deletions plugins/contrib/deploy-sidecars/kontinuous-ui.js

This file was deleted.

109 changes: 109 additions & 0 deletions plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const { setTimeout: sleep } = require("timers/promises")
const supabase = require("../lib/supabase")

module.exports = async (_options, { config, dryRun, ctx }) => {
if (dryRun) {
return
}

const {
gitSha,
projectName,
environment,
gitBranch,
repositoryName,
gitRepositoryUrl,
pipelineUUID,
} = config

const values = {
uuid: pipelineUUID,
commit_hash: gitSha,
project: projectName,
environment,
branch: gitBranch,
repository: repositoryName,
repository_url: gitRepositoryUrl,
}

const eventsBucket = ctx.require("eventsBucket")
// const abortController = ctx.require("abortController")

// resource:waiting
// resource:failed
// resource:ready
// resource:closed

async function insertValues(data) {
const { error } = await supabase.from("deployments_logs").insert([data])
if (error) throw error
}

const waitingFor = []
eventsBucket.on("resource:waiting", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "waiting" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

eventsBucket.on("resource:ready", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "ready" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

eventsBucket.on("resource:failed", () => {
waitingFor.push(
new Promise(async (resolve, reject) => {
try {
const valuesToInsert = { ...values, status: "failed" }
await insertValues(valuesToInsert)
resolve()
} catch (e) {
reject(e)
}
// await
})
)
})

let finished
eventsBucket.on("deploy-with:finish", () => {
finished = true
})

return new Promise(async (res, rej) => {
while (true) {

Check warning on line 94 in plugins/kontinuous-ui/deploy-sidecars/kontinuous-ui.js

View workflow job for this annotation

GitHub Actions / Lint

Unexpected constant condition
if (finished) {
console.log("WAITING FOR...")
try {
await Promise.all(waitingFor)
res()
} catch (err) {
console.log("ERROR", err)
rej(err)
}
return
}
await sleep(1)
}
})
}
7 changes: 7 additions & 0 deletions plugins/kontinuous-ui/lib/supabase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { createClient } = require("@supabase/supabase-js")

Check failure on line 1 in plugins/kontinuous-ui/lib/supabase.js

View workflow job for this annotation

GitHub Actions / Lint

Unable to resolve path to module '@supabase/supabase-js'

Check failure on line 1 in plugins/kontinuous-ui/lib/supabase.js

View workflow job for this annotation

GitHub Actions / Lint

"@supabase/supabase-js" is not found

const { SUPABASE_URL: supabaseUrl, SUPABASE_KEY: supabaseKey } = process.env

const supabase = createClient(supabaseUrl, supabaseKey)

module.exports = supabase
36 changes: 18 additions & 18 deletions plugins/kontinuous-ui/pre-deploy/01-deployment_event.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const { createClient } = require("@supabase/supabase-js")

module.exports = async (_manifests, _options, { config, ctx }) => {
const processEnv = ctx.get("env") || process.env
const { SUPABASE_URL: supabaseUrl, SUPABASE_KEY: supabaseKey } = processEnv
const supabase = require("../lib/supabase")

module.exports = async (_manifests, _options, { config, logger }) => {
const {
gitSha,
projectName,
environment,
refLabelValue,
gitBranch,
repositoryName,
actionCommandName,
gitRepositoryUrl,
pipelineUUID,
} = config

const supabase = createClient(supabaseUrl, supabaseKey)
const values = {
uuid: pipelineUUID,
status: "pre-deploy",
commit_hash: gitSha,
project: projectName,
environment,
branch: gitBranch,
repository: repositoryName,
repository_url: gitRepositoryUrl,
}

const { error } = await supabase.from("deployments_logs").insert([
{
status: actionCommandName,
commit_hash: gitSha,
project: projectName,
environment,
branch: refLabelValue,
repository: repositoryName,
},
])
const { error } = await supabase.from("deployments_logs").insert([values])

if (error) throw error

logger.info(values, "FINISHED")
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -15698,6 +15698,15 @@ __metadata:
languageName: node
linkType: hard

"uuid@npm:^10.0.0":
version: 10.0.0
resolution: "uuid@npm:10.0.0"
bin:
uuid: dist/bin/uuid
checksum: 9ea91ef753d88b03746de0a22192251c355bbe595d9cb3a67520aacf693c793df7e85c4ad0378aecb133c372a5e7bb7ec0f3b14db477ab7e3b6ff29792e047b9
languageName: node
linkType: hard

"uuid@npm:^9.0.0":
version: 9.0.0
resolution: "uuid@npm:9.0.0"
Expand Down Expand Up @@ -16253,6 +16262,7 @@ __metadata:
slugify: "npm:^1.6.5"
sonic-boom: "npm:^3.0.0"
tiged: "npm:^2.12.4"
uuid: "npm:^10.0.0"
which: "npm:^3.0.0"
yaml: "npm:^2.3.1"
zx: "npm:^7.1.1"
Expand Down

0 comments on commit f4e7dcf

Please sign in to comment.