opencode-omoa-toggle/git-ops.mjs

132 lines
4.7 KiB
JavaScript

/**
* git-ops.mjs — OpenCode skill for git operations with Hibbhome Gitea server
*
* PURPOSE:
* Documents the git server setup and provides helper functions for
* common git operations. New sessions can load this skill to immediately
* know how to work with the Hibbhome Gitea server.
*
* SERVER DETAILS:
* - Web UI / API: https://git.hibbhome.com
* - SSH hostname: wwwdb.dmz.home.hibbhome.com
* - SSH clone URL: git@wwwdb.dmz.home.hibbhome.com:<org>/<repo>.git
* - API key: fc4332a8a1a87b1f8cbcc01668ed94803dcd3a81
* - Organization: Hibbhome
*
* SSH KEY:
* - Location: ~/.ssh/id_gitea (ed25519)
* - Added to Gitea server for passwordless push
*
* USAGE:
* Load this skill when you need to:
* - Create a new repo on the Gitea server
* - Push code to the server
* - Clone repos from the server
* - Check repo status
*
* EXAMPLES:
* skill("git-ops") — Load this skill
* Then use the helper functions or follow the documentation
*
* UPSTREAM REPO:
* https://git.hibbhome.com/Hibbhome/opencode-omoa-toggle
*
* CREATED: 2026-05-03
*/
import { tool } from "@opencode-ai/plugin/tool";
import { execSync } from "node:child_process";
const GITEA_API = "https://git.hibbhome.com";
const GITEA_SSH = "git@wwwdb.dmz.home.hibbhome.com";
const GITEA_TOKEN = "fc4332a8a1a87b1f8cbcc01668ed94803dcd3a81";
const GITEA_ORG = "Hibbhome";
export const GitOpsPlugin = async (_ctx) => {
return {
tool: {
/**
* Create a new repo on the Gitea server
*/
git_create_repo: tool({
description: "Create a new repo on the Hibbhome Gitea server",
args: {
name: tool.schema.string().describe("Repository name (e.g., my-project)"),
description: tool.schema.string().describe("Repository description"),
private: tool.schema.boolean().describe("Whether the repo should be private").default(false),
},
async execute(args) {
try {
const result = execSync(`curl -s -X POST "${GITEA_API}/api/v1/orgs/${GITEA_ORG}/repos" \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"name":"${args.name}","description":"${args.description}","private":${args.private}}'`,
{ encoding: "utf-8" }
);
const repo = JSON.parse(result);
return `Repo created: ${repo.clone_url}\nSSH: ${GITEA_SSH}:${GITEA_ORG}/${args.name}.git`;
} catch (err) {
return `Error creating repo: ${err.message}`;
}
},
}),
/**
* Push a local directory to a Gitea repo
*/
git_push: tool({
description: "Push a local directory to a Gitea repo",
args: {
local_path: tool.schema.string().describe("Local directory path"),
repo_name: tool.schema.string().describe("Repository name on Gitea"),
branch: tool.schema.string().describe("Branch name").default("master"),
},
async execute(args) {
try {
const remote_url = `${GITEA_SSH}:${GITEA_ORG}/${args.repo_name}.git`;
// Check if git repo exists
const is_git = execSync(`cd ${args.local_path} && git rev-parse --is-inside-work-tree 2>/dev/null || echo "false"`, { encoding: "utf-8" }).trim();
if (is_git !== "true") {
execSync(`cd ${args.local_path} && git init && git add . && git commit -m "Initial commit"`, { encoding: "utf-8" });
}
// Add remote if not exists
try {
execSync(`cd ${args.local_path} && git remote add origin ${remote_url}`, { encoding: "utf-8" });
} catch (e) {
// Remote already exists
}
execSync(`cd ${args.local_path} && git push -u origin ${args.branch}`, { encoding: "utf-8" });
return `Pushed to ${remote_url}`;
} catch (err) {
return `Error pushing: ${err.message}`;
}
},
}),
/**
* List repos in the Hibbhome organization
*/
git_list_repos: tool({
description: "List repos in the Hibbhome organization",
args: {},
async execute() {
try {
const result = execSync(`curl -s "${GITEA_API}/api/v1/orgs/${GITEA_ORG}/repos" \
-H "Authorization: token ${GITEA_TOKEN}"`,
{ encoding: "utf-8" }
);
const repos = JSON.parse(result);
return repos.map(r => `- ${r.name}: ${r.description || 'No description'}\n ${r.clone_url}`).join("\n");
} catch (err) {
return `Error listing repos: ${err.message}`;
}
},
}),
},
};
};