Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge development into main #211

Merged
merged 25 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c5b77f3
feat: initial impl
whilefoo Nov 3, 2024
e90a93d
feat: more context and tests
whilefoo Nov 7, 2024
164f247
fix: tests
whilefoo Nov 9, 2024
6d24748
feat: merge
whilefoo Nov 9, 2024
b58f0f0
fix: tests
whilefoo Nov 9, 2024
fcb4078
fix: remove test command
whilefoo Nov 9, 2024
aed0974
chore: merge
whilefoo Nov 10, 2024
01854e7
fix: imports
whilefoo Nov 10, 2024
639e4e3
feat: additional properties and required
whilefoo Nov 10, 2024
6916ad3
feat: interactive setup
whilefoo Nov 13, 2024
0421c30
fix: simplify array check
whilefoo Nov 15, 2024
d8af9f8
Merge pull request #198 from ubiquity-whilefoo/setup
0x4007 Nov 18, 2024
67de8cf
feat: manifest commands object
whilefoo Nov 20, 2024
8ee9490
Merge remote-tracking branch 'upstream/development' into command-inte…
whilefoo Nov 25, 2024
1fd1fbc
fix: tests
whilefoo Nov 25, 2024
a2726d7
Merge pull request #186 from ubiquity-whilefoo/command-interface
gentlementlegen Nov 26, 2024
1c293d8
fix(workflow): update worker-deploy and setup-kv scripts
gentlementlegen Nov 26, 2024
7fb513e
chore: update cspell word list
gentlementlegen Nov 26, 2024
5477763
Merge pull request #207 from gentlementlegen/development
gentlementlegen Nov 26, 2024
96aef6e
chore: add OPENAI_API_KEY to worker deploy workflow
gentlementlegen Nov 26, 2024
856f9a4
Merge pull request #208 from gentlementlegen/development
gentlementlegen Nov 26, 2024
9a658c9
chore: added context wait until to async operation
gentlementlegen Nov 29, 2024
73ea3fc
chore: fixed test
gentlementlegen Nov 29, 2024
72d1a60
chore: fixed test
gentlementlegen Nov 29, 2024
9c009d9
Merge pull request #210 from gentlementlegen/development
gentlementlegen Nov 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"ignorePaths": ["**/*.json", "**/*.css", "node_modules", "**/*.log"],
"useGitignore": true,
"language": "en",
"words": ["dataurl", "devpool", "fkey", "mswjs", "outdir", "servedir", "supabase", "typebox", "ubiquity-os", "smee", "tomlify", "hono", "cfworker"],
"words": ["dataurl", "devpool", "fkey", "mswjs", "outdir", "servedir", "supabase", "typebox", "ubiquity-os",
"smee", "tomlify", "hono", "cfworker", "pavlovcik", "ghijklmnopqrstuvwxyz", "GHIJKLMNOPQRSTUVWXYZ", "ubiquityos"],
"dictionaries": ["typescript", "node", "software-terms"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
"ignoreRegExpList": ["[0-9a-fA-F]{6}"]
Expand Down
1 change: 1 addition & 0 deletions .dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ WEBHOOK_PROXY_URL=https://smee.io/new
APP_WEBHOOK_SECRET=xxxxxx
APP_ID=123456
ENVIRONMENT=development | production
OPENAI_API_KEY=
9 changes: 3 additions & 6 deletions .github/workflows/worker-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ jobs:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20.10.0

- uses: oven-sh/setup-bun@v1
- uses: oven-sh/setup-bun@v2

- name: Run setup script
run: |
Expand Down Expand Up @@ -56,11 +51,13 @@ jobs:
APP_PRIVATE_KEY
APP_WEBHOOK_SECRET
ENVIRONMENT
OPENAI_API_KEY
env:
APP_WEBHOOK_SECRET: ${{ secrets.APP_WEBHOOK_SECRET }}
APP_ID: ${{ secrets.APP_ID }}
APP_PRIVATE_KEY: ${{ secrets.APP_PRIVATE_KEY }}
ENVIRONMENT: ${{ secrets.ENVIRONMENT }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}

- name: Write Deployment URL to Summary
run: |
Expand Down
Binary file modified bun.lockb
Binary file not shown.
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"format:prettier": "prettier --write .",
"format:cspell": "cspell **/*",
"prepare": "node .husky/install.mjs",
"deploy": "tsx ./scripts/deploy.ts",
"deploy-dev": "wrangler deploy --env dev",
"deploy-production": "wrangler deploy --env production",
"worker": "wrangler dev --env dev --port 8787",
Expand All @@ -30,7 +31,8 @@
"knip-ci": "knip --no-exit-code --reporter json --config .github/knip.ts",
"jest:test": "jest --coverage",
"plugin:hello-world": "tsx tests/__mocks__/hello-world-plugin.ts",
"setup-kv": "bun --env-file=.dev.vars deploy/setup-kv-namespace.ts"
"setup-kv": "bun --env-file=.dev.vars scripts/setup-kv-namespace.ts",
"setup": "tsx ./scripts/setup.ts"
},
"keywords": [
"typescript",
Expand All @@ -51,9 +53,10 @@
"@octokit/types": "^13.5.0",
"@octokit/webhooks": "13.3.0",
"@octokit/webhooks-types": "7.5.1",
"@sinclair/typebox": "^0.33.20",
"@ubiquity-os/plugin-sdk": "^1.0.11",
"@sinclair/typebox": "0.34.3",
"@ubiquity-os/plugin-sdk": "^1.1.0",
"dotenv": "16.4.5",
"openai": "^4.70.2",
"typebox-validators": "0.3.5",
"yaml": "2.4.5"
},
Expand All @@ -65,13 +68,15 @@
"@cspell/dict-software-terms": "3.4.6",
"@cspell/dict-typescript": "3.1.5",
"@eslint/js": "9.7.0",
"@inquirer/prompts": "^7.1.0",
"@jest/globals": "29.7.0",
"@mswjs/data": "0.16.1",
"@mswjs/http-middleware": "0.10.1",
"@swc/core": "1.6.5",
"@swc/jest": "0.2.36",
"@types/jest": "29.5.12",
"@types/node": "20.14.10",
"@types/node-rsa": "^1.1.4",
"cspell": "8.9.0",
"esbuild": "0.23.0",
"eslint": "9.7.0",
Expand All @@ -84,7 +89,10 @@
"jest-junit": "16.0.0",
"knip": "5.26.0",
"lint-staged": "15.2.7",
"node-rsa": "^1.1.1",
"npm-run-all": "4.1.5",
"open": "^10.1.0",
"ora": "^8.1.1",
"prettier": "3.3.3",
"smee-client": "^2.0.4",
"toml": "3.0.0",
Expand Down
230 changes: 230 additions & 0 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
import { confirm, input, select } from "@inquirer/prompts";
import { exec, execSync, spawn } from "child_process";
import { readFileSync, unlinkSync, writeFileSync } from "fs";
import ora from "ora";
import path from "path";
import { parse } from "dotenv";
import toml from "toml";
// @ts-expect-error No typings exist for this package
import * as tomlify from "tomlify-j0.4";

interface WranglerConfiguration {
name: string;
env: {
[env: string]: {
kv_namespaces?: {
id: string;
binding: string;
}[];
};
};
kv_namespaces: {
id: string;
binding: string;
}[];
}

const WRANGLER_PATH = path.resolve(__dirname, "..", "node_modules/.bin/wrangler");
const WRANGLER_TOML_PATH = path.resolve(__dirname, "..", "wrangler.toml");
const BINDING_NAME = "PLUGIN_CHAIN_STATE";

function checkIfWranglerInstalled() {
return new Promise((resolve) => {
exec(`${WRANGLER_PATH} --version`, (err, stdout, stderr) => {
if (err || stderr) {
resolve(false);
}
resolve(true);
});
});
}

function checkIfWranglerIsLoggedIn() {
return new Promise((resolve, reject) => {
exec(`${WRANGLER_PATH} whoami`, (err, stdout, stderr) => {
if (err) {
reject(err);
}
if (stdout.includes("You are not authenticated") || stderr.includes("You are not authenticated")) {
resolve(false);
} else {
resolve(true);
}
});
});
}

function wranglerLogin() {
return new Promise<void>((resolve, reject) => {
const loginProcess = spawn(WRANGLER_PATH, ["login"], { stdio: "inherit" });

loginProcess.on("close", (code) => {
if (code !== 0) {
reject();
} else {
resolve();
}
});
});
}

function wranglerBulkSecrets(env: string | null, filePath: string) {
return new Promise<void>((resolve, reject) => {
const args = env ? ["--env", env] : [];
const process = spawn(WRANGLER_PATH, ["secret", "bulk", filePath, ...args], { stdio: "inherit" });

process.on("close", (code) => {
if (code !== 0) {
reject();
} else {
resolve();
}
});
process.on("error", (err) => {
reject(err);
});
});
}

function wranglerDeploy(env: string | null) {
return new Promise<void>((resolve, reject) => {
const args = env ? ["--env", env] : [];
const process = spawn(WRANGLER_PATH, ["deploy", ...args], { stdio: "inherit" });

process.on("close", (code) => {
if (code !== 0) {
reject();
} else {
resolve();
}
});
});
}

function wranglerKvNamespace(projectName: string, namespace: string) {
const kvList = JSON.parse(execSync(`${WRANGLER_PATH} kv namespace list`).toString()) as { id: string; title: string }[];
const existingNamespace = kvList.find((o) => o.title === namespace || o.title === `${projectName}-${namespace}`);
if (existingNamespace) {
return existingNamespace.id;
}

const res = execSync(`${WRANGLER_PATH} kv namespace create ${namespace}`).toString();

const newId = res.match(/id = \s*"([^"]+)"/)?.[1];
if (!newId) {
console.log(res);
throw new Error(`The new ID could not be found.`);
}
return newId;
}

void (async () => {
const spinner = ora("Checking if Wrangler is installed").start();
const wranglerInstalled = await checkIfWranglerInstalled();
if (!wranglerInstalled) {
spinner.fail("Wrangler is not installed. Please install it before running this script");
process.exit(1);
} else {
spinner.succeed("Wrangler is installed");
}

spinner.start("Checking if Wrangler is logged in");
const wranglerLoggedIn = await checkIfWranglerIsLoggedIn();
if (!wranglerLoggedIn) {
spinner.warn("Wrangler is not logged in. Please login to Wrangler");
await wranglerLogin();
spinner.succeed("Wrangler is now logged in");
} else {
spinner.succeed("Wrangler is logged in");
}

spinner.start("Searching environments in wrangler.toml");
const wranglerToml: WranglerConfiguration = toml.parse(readFileSync(WRANGLER_TOML_PATH, "utf-8"));
if (!wranglerToml) {
spinner.fail("Error parsing wrangler.toml");
process.exit(1);
}
const envs = Object.keys(wranglerToml.env ?? {});
let selectedEnv: string | null = null;
if (envs.length === 0) {
spinner.warn("No environments found, choosing default environment");
} else if (envs.length === 1) {
spinner.warn(`Only one environment found: ${envs[0]}`);
selectedEnv = envs[0];
} else if (envs.length > 1) {
spinner.stop();
selectedEnv = await select({
message: "Select the environment to deploy to:",
choices: envs,
});
}

const willSetSecrets = await confirm({
message: "Do you want to set secrets?",
default: true,
});
if (willSetSecrets) {
const envFile = await input({
message: "Enter the name of the env file to use:",
default: `.${selectedEnv}.vars`,
});
const spinner = ora("Setting secrets").render();
try {
const env = readFileSync(path.resolve(__dirname, "..", envFile), { encoding: "utf-8" });
const parsedEnv = parse(env);
if (parsedEnv) {
const tmpPath = path.resolve(__dirname, "..", `${envFile}.json.tmp`);
writeFileSync(tmpPath, JSON.stringify(parsedEnv));
await wranglerBulkSecrets(selectedEnv, tmpPath);
unlinkSync(tmpPath); // deletes the temporary file
spinner.succeed("Secrets set successfully");
}
} catch (err) {
spinner.fail(`Error setting secrets: ${err}`);
process.exit(1);
}
}

spinner.start("Setting up KV namespace");
try {
const kvNamespace = selectedEnv ? `${selectedEnv}-plugin-chain-state` : `plugin-chain-state`;
const namespaceId = wranglerKvNamespace(wranglerToml.name, kvNamespace);
if (selectedEnv) {
const existingBinding = wranglerToml.env[selectedEnv]?.kv_namespaces?.find((o) => o.binding === BINDING_NAME);
if (!existingBinding) {
wranglerToml.env[selectedEnv] = wranglerToml.env[selectedEnv] ?? {};
wranglerToml.env[selectedEnv].kv_namespaces = wranglerToml.env[selectedEnv].kv_namespaces ?? [];
wranglerToml.env[selectedEnv].kv_namespaces?.push({
id: namespaceId,
binding: BINDING_NAME,
});
} else {
existingBinding.id = namespaceId;
}
} else {
const existingBinding = wranglerToml.kv_namespaces.find((o) => o.binding === BINDING_NAME);
if (!existingBinding) {
wranglerToml.kv_namespaces.push({
id: namespaceId,
binding: BINDING_NAME,
});
} else {
existingBinding.id = namespaceId;
}
}
writeFileSync(WRANGLER_TOML_PATH, tomlify.toToml(wranglerToml));
spinner.succeed(`Using KV namespace ${kvNamespace} with ID: ${namespaceId}`);
} catch (err) {
spinner.fail(`Error setting up KV namespace: ${err}`);
process.exit(1);
}

spinner.start("Deploying to Cloudflare Workers").stopAndPersist();
try {
await wranglerDeploy(selectedEnv);
spinner.succeed("Deployed successfully");
} catch (err) {
spinner.fail(`Error deploying: ${err}`);
process.exit(1);
}
})();
39 changes: 39 additions & 0 deletions scripts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Register a Github App</title>
<style>
body,
html {
height: 100%;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
.form-container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
flex-direction: column;
}
</style>
</head>
<body>
<div class="form-container">
<p>Click on the button and follow instructions</p>
<form action="https://github.com/settings/apps/new" method="post">
<input type="hidden" name="manifest" id="manifest" /><br />
<input type="submit" value="Register a GitHub App" />
</form>
</div>

<script>
const input = document.getElementById("manifest");
input.value = `{{ MANIFEST }}`;
</script>
</body>
</html>
Loading
Loading