Skip to content

Commit

Permalink
chore: fix snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
devthejo authored and Julien Bouquillon committed Dec 17, 2023
1 parent a37d051 commit 7e8a5db
Show file tree
Hide file tree
Showing 42 changed files with 1,173 additions and 259 deletions.
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ type CronJob = import("kubernetes-models/batch/v1").ICronJob;
type ConfigMap = import("kubernetes-models/v1").IConfigMap;
type Secret = import("kubernetes-models/v1").ISecret;

//type Utils = import("./packages/common/utils").Utils;

type Utils = import("./packages/common/utils").Utils;

// todo: use officiel model
interface KappConfig {
Expand Down Expand Up @@ -93,7 +94,7 @@ declare namespace Kontinuous {
};

type PatchContext = {
//utils: Utils;
utils: Utils;
//@ts-ignore
logger;
//@ts-ignore
Expand Down
34 changes: 24 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@
"packages/*"
],
"devDependencies": {
"@types/lodash.camelcase": "^4.3.7",
"@types/lodash.clonedeep": "^4.5.7",
"@types/lodash.defaults": "^4.2.7",
"@types/lodash.get": "^4.4.7",
"@types/lodash.omit": "^4.5.7",
"@types/lodash.pick": "^4.4.7",
"@types/lodash.set": "^4.3.7",
"@types/micromatch": "^4.0.2",
"@types/node": "^18.11.18",
"@types/pino": "^7.0.5",
"commit-and-tag-version": "^11.0.0",
"docsify-cli": "^4.4.4",
"eslint": "^8.26.0",
"eslint": "^8.31.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.0.0",
"eslint-plugin-jest": "^27.2.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.0.0",
"fs-extra": "^11.1.1",
"husky": "^8.0.0",
"eslint-plugin-prettier": "^4.2.1",
"fs-extra": "^11.1.0",
"husky": "^8.0.3",
"js-yaml": "^4.1.0",
"kubernetes-models": "^4.3.1",
"lint-staged": "^13.0.0",
"prettier": "^2.6.2",
"kubernetes-models": "^4.1.0",
"lint-staged": "^13.1.0",
"prettier": "^2.8.1",
"renovate": "^35.0.0",
"standard-version": "^9.5.0",
"typescript": "^4.9.4",
"~dev-tools": "workspace:^"
},
"resolutions": {
Expand Down Expand Up @@ -66,7 +78,9 @@
"docker:kontinuous:build": "DOCKER_BUILDKIT=1 docker build --progress=plain -f packages/kontinuous/Dockerfile -t kontinuous .",
"docker:kontinuous:run": "docker run -it -v $PWD:/workspace -v $(dirname $KUBECONFIG):/home/node/.kube -e KUBECONFIG=/home/node/.kube/$(basename $KUBECONFIG) kontinuous",
"docker:kontinuous": "yarn docker:kontinuous:build && yarn docker:kontinuous:run",
"logs:remote-webhook": "kubectl --context prod -n kontinuous-webhook logs -f --all-containers -l component=kontinuous-webhook"
"logs:remote-webhook": "kubectl --context prod -n kontinuous-webhook logs -f --all-containers -l component=kontinuous-webhook",
"upgrade:modjo": "yarn up -R \"@modjo-plugins/*\"",
"ts:check": "tsc -p tsconfig.json"
},
"lint-staged": {
"*.js": [
Expand Down
22 changes: 21 additions & 1 deletion packages/common/utils/async-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ const getLogger = require("./get-logger")
const parseCommand = require("./parse-command")
const ProgramError = require("./program-error.class")

/**
*
* @param {import("child_process").ChildProcessWithoutNullStreams} child
* @param {null | ((child: import("child_process").ChildProcessWithoutNullStreams) => void)} callback
* @param {Kontinuous.PatchContext["logger"]} logger
* @param {{ignoreErrors?: string[]}} extraOptions
* @returns
*/

/**
*
* @param {import("child_process").ChildProcessWithoutNullStreams} child
Expand All @@ -16,10 +25,12 @@ const ProgramError = require("./program-error.class")
const promiseFromChildProcess = (child, callback, logger, extraOptions) => {
const { ignoreErrors = [] } = extraOptions
child.on("error", () => {}) // avoid crash on not found executable
/** @type {Buffer[]} */
const out = []
child.stdout.on("data", (data) => {
out.push(data)
})
/** @type {Buffer[]} */
const err = []
child.stderr.on("data", (data) => {
if (ignoreErrors.some((errCatch) => data.includes(errCatch))) {
Expand Down Expand Up @@ -48,7 +59,15 @@ const promiseFromChildProcess = (child, callback, logger, extraOptions) => {
})
})
}

/**
*
* @param {string|string[]} arg
* @param {Record<string, any>} options
* @param {null | ((child: import("child_process").ChildProcessWithoutNullStreams) => void)} callback
* @param {Kontinuous.PatchContext["logger"]} logger
* @param {{ignoreErrors?: string[]}} extraOptions
* @returns
*/
module.exports = (
arg,
options = {},
Expand All @@ -57,6 +76,7 @@ module.exports = (
extraOptions = {}
) => {
const [cmd, args] = parseCommand(arg)
/** @type {Record<string, any>} */
const defaultOptions = { encoding: "utf-8" }
const childProcess = spawn(cmd, args, {
...defaultOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/common/utils/axios-retry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require("axios")
const axiosRetry = require("axios-retry")
const { default: axios } = require("axios")
const { default: axiosRetry } = require("axios-retry")

const kontinuousVersion = require("./kontinuous-version")

Expand Down
11 changes: 11 additions & 0 deletions packages/common/utils/degit-tag-has-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const asyncShell = require("./async-shell")
const normalizeRepositoryUrl = require("./normalize-repository-url")
const getLogger = require("./get-logger")

/** @type {Record<string, string>} */
const supported = {
github: ".com",
gitlab: ".com",
Expand All @@ -15,6 +16,11 @@ const supported = {
"git.sr.ht": ".ht",
}

/**
*
* @param {string} src
* @returns {{site:string, user:string, name:string, ref:string, url:string, ssh:string, subdir:string, mode:string}}
*/
function parse(src) {
const match =
/^(?:(?:https:\/\/)?([^:/]+\.[^:/]+)\/|git@([^:/]+)[:/]|([^/]+):)?([^/\s]+)\/([^/\s#]+)(?:((?:\/[^/\s#]+)+))?(?:\/)?(?:#(.+))?/.exec(
Expand Down Expand Up @@ -46,6 +52,11 @@ function parse(src) {
return { site: siteName, user, name, ref, url, ssh, subdir, mode }
}

/**
*
* @param {string} target
* @returns boolean
*/
module.exports = async (target, logger = getLogger()) => {
const base = path.join(os.homedir(), ".degit")
const repo = parse(target)
Expand Down
30 changes: 30 additions & 0 deletions packages/common/utils/export-dir-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const fs = require("fs")
const path = require("path")
const camelcase = require("lodash.camelcase")
const ucfirst = require("./ucfirst")

/**
*
* @param {string} dir
* @param {{filter?: null | ((s: string) => boolean)}} param1
* @returns Record<string, any>
*/
module.exports = (dir, { filter = null } = {}) => {
let files = fs.readdirSync(dir)
if (filter) {
files = files.filter(filter)
}
return files.reduce(
/** @param {Record<string, any>} acc */
(acc, file) => {
const { name } = path.parse(file)
let key = camelcase(name)
if (key.endsWith("Class")) {
key = ucfirst(key.slice(0, -5))
}
acc[key] = require(`${dir}/${file}`)
return acc
},
{}
)
}
4 changes: 4 additions & 0 deletions packages/common/utils/ignore-yarn-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const yarnNotIgnore = [
".yarn/cache",
]

/**
* @param {string} src
* @returns boolean
*/
module.exports = (src) => {
if (src.includes("node_modules/") || src.endsWith("node_modules")) {
return false
Expand Down
41 changes: 41 additions & 0 deletions packages/common/utils/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// import JSDoc definitions
type Slug = typeof import("./slug");
type KindIsRunnable = typeof import("./kind-is-runnable");
type AsyncShell = typeof import("./async-shell");
type IsVersionTag = typeof import("./is-version-tag");
type SanitizeLabel = typeof import("./sanitize-label");
type IgnoreYarnState = typeof import("./ignore-yarn-state");
type PatternMatch = typeof import("./pattern-match");
type KubectlRetry = typeof import("./kubectl-retry");

// TODO : fetch from ./*.js
// for some reason the description from source JSDoc isnt displayed
interface Utils {
/** execute some async shell command */
asyncShell: AsyncShell;
/** Return slugified, hostname-friendly string */
slug: Slug;
/** Check if some branch is valid semantic-release tag: vx.y.z */
isVersionTag: IsVersionTag;
/** exclude non-alphanums chars */
sanitizeLabel: SanitizeLabel;
//@ts-ignore
yaml;
//@ts-ignore
logger;
//@ts-ignore
KontinuousPluginError;
//@ts-ignore
patternMatch: PatternMatch;
// check if the manifest is "runnable" in kubernetes
// act as a typeguard
kindIsRunnable: KindIsRunnable;
/** Run kubectl with exponential backoff */
kubectlRetry: KubectlRetry;
//@ts-ignore
needKubectl;
/** check if some path should be ignored */
ignoreYarnState: IgnoreYarnState;
}

export { Utils };
4 changes: 4 additions & 0 deletions packages/common/utils/is-version-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
const versionTagSemverRe =
/^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/

/**
* @param {string} tag
* @returns boolean
*/
module.exports = (tag) => versionTagSemverRe.test(tag)
8 changes: 8 additions & 0 deletions packages/common/utils/kind-is-runnable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const kindsRunnable = require("./kinds-runnable")

Check failure on line 1 in packages/common/utils/kind-is-runnable.js

View workflow job for this annotation

GitHub Actions / Lint

Unable to resolve path to module './kinds-runnable'

Check failure on line 1 in packages/common/utils/kind-is-runnable.js

View workflow job for this annotation

GitHub Actions / Lint

Missing file extension for "./kinds-runnable"

Check failure on line 1 in packages/common/utils/kind-is-runnable.js

View workflow job for this annotation

GitHub Actions / Lint

"./kinds-runnable" is not found

/**
*
* @param {Kontinuous.Manifest} manifest
* @returns {manifest is Kontinuous.RunnableManifest}
*/
module.exports = (manifest) => kindsRunnable.includes(manifest.kind)
32 changes: 32 additions & 0 deletions packages/common/utils/kubectl-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ const getLogger = require("./get-logger")
const retriableOnBrokenCluster = require("./retriable-on-broken-cluster")
const retriableNetwork = require("./retriable-network")

/**
* @typedef {Object} KubectlRunOptions
* @prop {string} [kubeconfig]
* @prop {string} [kubeconfigContext]
* @prop {string[]} [ignoreErrors]
* @prop {string} [stdin]
* @prop {import('child_process').ChildProcess[]} [collectProcesses]
*/

/**
*
* @param {string|string[]} kubectlArgs
* @param {KubectlRunOptions} options
* @returns void
*/
const kubectlRun = async (kubectlArgs, options = {}) => {
const {
kubeconfig,
Expand Down Expand Up @@ -79,6 +94,23 @@ const kubectlRun = async (kubectlArgs, options = {}) => {
})
}

/**
* @typedef {Object} KubectlRetryOptions
* @prop {any} [logger]
* @prop {any} [sentry]
* @prop {Record<string, any>} [retryOptions]
* @prop {boolean} [logError]
* @prop {boolean} [logInfo]
* @prop {import('child_process').ChildProcess[]} [collectProcesses]
* @prop {boolean} [surviveOnBrokenCluster]
*/

/**
*
* @param {string|string[]} kubectlArgs
* @param {KubectlRetryOptions} options
* @returns string
*/
module.exports = async (kubectlArgs, options = {}) => {
const {
logger = getLogger(),
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-helm.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-kapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-kubectl.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-rollout-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
2 changes: 1 addition & 1 deletion packages/common/utils/need-stern.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const download = async (options) => {
switch (platform) {
case "darwin":
break
case "windows":
case "win32":
ext = ".exe"
break
case "linux":
Expand Down
5 changes: 5 additions & 0 deletions packages/common/utils/parse-command.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
*
* @param {string|string[]} arg
* @returns {[cmd: string, args:string[]]}
*/
module.exports = (arg) => {
if (typeof arg === "string") {
arg = arg
Expand Down
6 changes: 6 additions & 0 deletions packages/common/utils/pattern-match.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const micromatch = require("micromatch")

/**
* @param {string[]} str
* @param {string} pattern
* @param {Record<string, any>} options
* @returns
*/
module.exports = (str, pattern, options = {}) =>
micromatch.match(str, pattern, {
regex: true,
Expand Down
5 changes: 5 additions & 0 deletions packages/common/utils/sanitize-label.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @param {string} label
* @param {string} prefixIfNeeded
* @returns string
*/
module.exports = (label, prefixIfNeeded = "v") => {
label = label.replace(/[^a-zA-Z0-9._-]+/g, "-")
if (!/^[a-zA-Z0-9]/.test(label)) {
Expand Down
Loading

0 comments on commit 7e8a5db

Please sign in to comment.