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

feat: conversation rewards #38

Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,5 @@ static/dist
keys
coverage
junit.xml

*.pem
Binary file modified bun.lockb
Binary file not shown.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"scripts": {
"dev": "run-p worker proxy",
"predev": "lsof -i tcp:8787 | grep LISTEN | awk '{print $2}' | (if [ -n \"$(awk '{print $2}')\" ]; then xargs kill -9; fi)",
"predev": "tsx predev.ts",
"format": "run-s format:lint format:prettier format:cspell",
"format:lint": "eslint --fix .",
"format:prettier": "prettier --write .",
Expand Down Expand Up @@ -41,6 +41,7 @@
"@octokit/types": "^12.6.0",
"@octokit/webhooks": "^12.0.10",
"@sinclair/typebox": "^0.32.5",
"@ubiquibot/configuration": "2.1.0",
"dotenv": "^16.4.4",
"smee-client": "^2.0.0",
"yaml": "^2.4.1"
Expand Down Expand Up @@ -85,5 +86,5 @@
"@commitlint/config-conventional"
]
},
"packageManager": "bun@1.0.23"
"packageManager": "bun@1.1.0"
}
30 changes: 30 additions & 0 deletions predev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { exec } from "child_process";

const port = "8787";

const isWindows = process.platform === "win32";

const command = isWindows ? `netstat -ano | findstr LISTENING | findstr :${port}` : `lsof -i tcp:${port} | grep LISTEN | awk '{print $2}'`;

exec(command, (error, stdout) => {
if (error) {
// The command will also fail on Windows if the process doesn't exist which is expected
console.error(`Error executing command: ${error.message}`);
return;
}

const pid = isWindows ? stdout.trim().split(/\s+/)[4] : stdout.trim();

if (pid) {
const killCommand = isWindows ? `taskkill /F /PID ${pid}` : `kill -9 ${pid}`;
exec(killCommand, (error) => {
if (error) {
console.error(`Error killing process: ${error.message}`);
return;
}
console.log(`Process ${pid} killed successfully.`);
});
} else {
console.log("No process found listening on port 8787.");
}
});
2 changes: 1 addition & 1 deletion src/github/handlers/repository-dispatch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GitHubContext } from "../github-context";
import { PluginChain } from "../types/plugin-configuration";
import { dispatchWorkflow, getDefaultBranch } from "../utils/workflow-dispatch";
import { Value } from "@sinclair/typebox/value";
import { DelegatedComputeInputs, PluginChainState, expressionRegex, pluginOutputSchema } from "../types/plugin";
import { PluginChain } from "../types/config";

export async function repositoryDispatch(context: GitHubContext<"repository_dispatch">) {
console.log("Repository dispatch event received", context.payload.client_payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Type as T } from "@sinclair/typebox";
import { StaticDecode } from "@sinclair/typebox";
import { githubWebhookEvents } from "./webhook-events";

const pluginNameRegex = new RegExp("^([0-9a-zA-Z-._]+)/([0-9a-zA-Z-._]+)(?::([0-9a-zA-Z-._]+))?(?:@([0-9a-zA-Z-._]+))?$");
const pluginNameRegex = new RegExp("^([0-9a-zA-Z-._]+)\\/([0-9a-zA-Z-._]+)(?::([0-9a-zA-Z-._]+))?(?:@([0-9a-zA-Z-._]+(?:\\/[0-9a-zA-Z-._]+)?))?$");

type GithubPlugin = {
owner: string;
Expand Down Expand Up @@ -58,4 +58,4 @@ export const configSchema = T.Object({
plugins: T.Record(T.Enum(githubWebhookEvents), handlerSchema, { default: {} }),
});

export type Config = StaticDecode<typeof configSchema>;
export type PluginConfiguration = StaticDecode<typeof configSchema>;
2 changes: 1 addition & 1 deletion src/github/types/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmitterWebhookEvent, EmitterWebhookEventName } from "@octokit/webhooks";
import { PluginChain } from "./config";
import { StaticDecode, Type } from "@sinclair/typebox";
import { PluginChain } from "./plugin-configuration";

export const expressionRegex = /^\s*\${{\s*(\S+)\s*}}\s*$/;

Expand Down
26 changes: 16 additions & 10 deletions src/github/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { Value } from "@sinclair/typebox/value";
import { GitHubContext } from "../github-context";
import YAML from "yaml";
import { Config, configSchema } from "../types/config";
import { expressionRegex } from "../types/plugin";
import { configSchema, PluginConfiguration } from "../types/plugin-configuration";
import { eventNames } from "../types/webhook-events";
import { BotConfig, generateConfiguration } from "@ubiquibot/configuration";

const UBIQUIBOT_CONFIG_FULL_PATH = ".github/ubiquibot-config.yml";
const UBIQUIBOT_CONFIG_FULL_PATH = ".github/.ubiquibot-config.yml";
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved

export async function getConfig(context: GitHubContext): Promise<Config | null> {
export async function getConfig(context: GitHubContext): Promise<BotConfig | null> {
gentlementlegen marked this conversation as resolved.
Show resolved Hide resolved
const payload = context.payload;
if (!("repository" in payload) || !payload.repository) throw new Error("Repository is not defined");
const defaultConfiguration = generateConfiguration();
if (!("repository" in payload) || !payload.repository) {
console.warn("Repository is not defined");
return defaultConfiguration;
}

const _repoConfig = parseYaml(
await download({
Expand All @@ -18,9 +23,9 @@ export async function getConfig(context: GitHubContext): Promise<Config | null>
owner: payload.repository.owner.login,
})
);
if (!_repoConfig) return null;
if (!_repoConfig) return defaultConfiguration;

let config: Config;
let config: PluginConfiguration;
try {
config = Value.Decode(configSchema, Value.Default(configSchema, _repoConfig));
} catch (error) {
Expand All @@ -30,10 +35,10 @@ export async function getConfig(context: GitHubContext): Promise<Config | null>

checkPluginChains(config);

return config;
return generateConfiguration(config as BotConfig);
}

function checkPluginChains(config: Config) {
function checkPluginChains(config: PluginConfiguration) {
for (const eventName of eventNames) {
const plugins = config.plugins[eventName];
for (const plugin of plugins) {
Expand All @@ -43,7 +48,7 @@ function checkPluginChains(config: Config) {
}
}

function checkPluginChainUniqueIds(plugin: Config["plugins"]["*"][0]) {
function checkPluginChainUniqueIds(plugin: PluginConfiguration["plugins"]["*"][0]) {
const allIds = new Set<string>();
for (const use of plugin.uses) {
if (!use.id) continue;
Expand All @@ -56,7 +61,7 @@ function checkPluginChainUniqueIds(plugin: Config["plugins"]["*"][0]) {
return allIds;
}

function checkPluginChainExpressions(plugin: Config["plugins"]["*"][0], allIds: Set<string>) {
function checkPluginChainExpressions(plugin: PluginConfiguration["plugins"]["*"][0], allIds: Set<string>) {
const calledIds = new Set<string>();
for (const use of plugin.uses) {
if (!use.id) continue;
Expand Down Expand Up @@ -101,6 +106,7 @@ async function download({ context, repository, owner }: { context: GitHubContext
});
return data as unknown as string; // this will be a string if media format is raw
} catch (err) {
console.error(err);
return null;
}
}
Expand Down
Loading