Implement the Gitea extension
The repository was a bare Nova scaffold. This builds it out to match the
capabilities of the official Gitea VS Code extension.
Sidebar sections:
- Current Branch: pull requests and workflow runs for the checked-out
branch, with a current/all/pinned branch filter
- Workflows: runs grouped by workflow file, expanding into jobs, steps,
and artifacts
- Pull Requests: open pull requests across repositories, expanding into
reviews, review comments, and changed files
- Settings: per-instance connection state, plus repository Actions
secrets and variables
Commands cover run control (re-run, re-run failed jobs, re-run a job,
cancel), job logs, artifact download/reveal/open, pull request overview,
diff, checkout, creation, merge and close, the full review cycle, and
secret and variable management. Multiple instances are supported, routed
by git remote host, with tokens held per instance in the Keychain.
Nova exposes no webview, diff editor, editor decorations, or extension
status bar, so four features are shaped differently from the VS Code
original: the pull request timeline renders as Markdown, diffs open as
unified .diff documents, review comments are published through an
IssueCollection so they appear in the gutter and the Issues sidebar, and
a failed run posts a notification. OAuth and insecureSkipVerify have no
Nova equivalent and are omitted. README.md records all of this.
Endpoints were taken from Gitea's published swagger.v1.json. Servers
predating the workflow runs API fall back to /actions/tasks.
Tests/ runs the extension's real code under Node against a stubbed Nova
runtime and a canned Gitea instance: 88 checks, no install step and no
network. Images are generated by Tools/make-icons.py.
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQuusXgZC2dzwpJJ1qhtti
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// Preference access. Every key may be overridden per-workspace; workspace
|
||||
// values win when they are actually set, otherwise the global value applies.
|
||||
//
|
||||
|
||||
const DEFAULTS = {
|
||||
"gitea.baseUrl": "https://gitea.com",
|
||||
"gitea.discovery.mode": "workspace",
|
||||
"gitea.refresh.runningIntervalSeconds": 15,
|
||||
"gitea.refresh.idleIntervalSeconds": 60,
|
||||
"gitea.maxRunsPerRepo": 20,
|
||||
"gitea.maxJobsPerRun": 50,
|
||||
"gitea.reviewComments.enabled": true,
|
||||
"gitea.jobLogs.saveToRepo": true,
|
||||
"gitea.artifacts.downloadPath": ".tmp/gitea-artifacts/",
|
||||
"gitea.notifications.failedRuns": true,
|
||||
"gitea.branchFilter": "current",
|
||||
};
|
||||
|
||||
function isUnset(value) {
|
||||
return value === null || value === undefined || value === "";
|
||||
}
|
||||
|
||||
function get(key, type) {
|
||||
let value = null;
|
||||
if (nova.workspace && nova.workspace.config) {
|
||||
value = nova.workspace.config.get(key, type);
|
||||
}
|
||||
if (isUnset(value)) {
|
||||
value = nova.config.get(key, type);
|
||||
}
|
||||
if (isUnset(value)) {
|
||||
value = DEFAULTS[key];
|
||||
}
|
||||
return isUnset(value) ? null : value;
|
||||
}
|
||||
|
||||
function num(key) {
|
||||
const value = Number(get(key, "number"));
|
||||
return Number.isFinite(value) ? value : DEFAULTS[key];
|
||||
}
|
||||
|
||||
function bool(key) {
|
||||
const value = get(key, "boolean");
|
||||
return value === null ? DEFAULTS[key] === true : value === true;
|
||||
}
|
||||
|
||||
/** Trailing slashes make every later URL join ambiguous, so strip them once here. */
|
||||
function normalizeBaseUrl(url) {
|
||||
if (!url) return null;
|
||||
let trimmed = String(url).trim().replace(/\/+$/, "");
|
||||
if (!trimmed) return null;
|
||||
if (!/^https?:\/\//i.test(trimmed)) trimmed = "https://" + trimmed;
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
/** The default instance plus any extras, de-duplicated and order-preserving. */
|
||||
function instances() {
|
||||
const list = [];
|
||||
const seen = new Set();
|
||||
const add = (url) => {
|
||||
const normalized = normalizeBaseUrl(url);
|
||||
if (normalized && !seen.has(normalized)) {
|
||||
seen.add(normalized);
|
||||
list.push(normalized);
|
||||
}
|
||||
};
|
||||
|
||||
add(get("gitea.baseUrl", "string"));
|
||||
|
||||
const extra =
|
||||
nova.config.get("gitea.instances", "array") ||
|
||||
(nova.workspace && nova.workspace.config.get("gitea.instances", "array")) ||
|
||||
[];
|
||||
for (const url of extra) add(url);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
function setInstances(list) {
|
||||
const cleaned = [];
|
||||
const primary = normalizeBaseUrl(get("gitea.baseUrl", "string"));
|
||||
for (const url of list) {
|
||||
const normalized = normalizeBaseUrl(url);
|
||||
if (normalized && normalized !== primary && !cleaned.includes(normalized)) {
|
||||
cleaned.push(normalized);
|
||||
}
|
||||
}
|
||||
nova.config.set("gitea.instances", cleaned);
|
||||
}
|
||||
|
||||
function branchFilter() {
|
||||
const custom = get("gitea.branchFilter.custom", "string");
|
||||
if (!isUnset(custom)) return { mode: "custom", branch: String(custom).trim() };
|
||||
const mode = get("gitea.branchFilter", "string");
|
||||
return { mode: mode === "all" ? "all" : "current", branch: null };
|
||||
}
|
||||
|
||||
function setBranchFilter(mode, branch) {
|
||||
const scope = nova.workspace && nova.workspace.path ? nova.workspace.config : nova.config;
|
||||
if (mode === "custom") {
|
||||
scope.set("gitea.branchFilter", "current");
|
||||
scope.set("gitea.branchFilter.custom", branch);
|
||||
} else {
|
||||
scope.set("gitea.branchFilter", mode);
|
||||
scope.remove("gitea.branchFilter.custom");
|
||||
}
|
||||
}
|
||||
|
||||
exports.get = get;
|
||||
exports.num = num;
|
||||
exports.bool = bool;
|
||||
exports.instances = instances;
|
||||
exports.setInstances = setInstances;
|
||||
exports.normalizeBaseUrl = normalizeBaseUrl;
|
||||
exports.branchFilter = branchFilter;
|
||||
exports.setBranchFilter = setBranchFilter;
|
||||
exports.discoveryMode = () => get("gitea.discovery.mode", "string") || "workspace";
|
||||
exports.maxRuns = () => num("gitea.maxRunsPerRepo");
|
||||
exports.maxJobs = () => num("gitea.maxJobsPerRun");
|
||||
exports.runningInterval = () => num("gitea.refresh.runningIntervalSeconds");
|
||||
exports.idleInterval = () => num("gitea.refresh.idleIntervalSeconds");
|
||||
exports.reviewCommentsEnabled = () => bool("gitea.reviewComments.enabled");
|
||||
exports.saveLogsToRepo = () => bool("gitea.jobLogs.saveToRepo");
|
||||
exports.notifyFailedRuns = () => bool("gitea.notifications.failedRuns");
|
||||
exports.artifactDownloadPath = () =>
|
||||
get("gitea.artifacts.downloadPath", "string") || ".tmp/gitea-artifacts/";
|
||||
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Promise wrapper around Nova's Process API. Nova has no synchronous exec, so
|
||||
// everything that shells out (git, mostly) funnels through here.
|
||||
//
|
||||
|
||||
const log = require("./log.js");
|
||||
|
||||
function exec(command, args, options = {}) {
|
||||
return new Promise((resolve) => {
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
|
||||
let process;
|
||||
try {
|
||||
process = new Process(command, {
|
||||
args: args,
|
||||
cwd: options.cwd,
|
||||
env: options.env,
|
||||
shell: false,
|
||||
stdio: "pipe",
|
||||
});
|
||||
} catch (error) {
|
||||
resolve({ status: -1, stdout: "", stderr: String(error) });
|
||||
return;
|
||||
}
|
||||
|
||||
process.onStdout((line) => {
|
||||
stdout += line;
|
||||
});
|
||||
process.onStderr((line) => {
|
||||
stderr += line;
|
||||
});
|
||||
process.onDidExit((status) => {
|
||||
log.debug(`exec ${command} ${(args || []).join(" ")} -> ${status}`);
|
||||
resolve({ status: status, stdout: stdout, stderr: stderr });
|
||||
});
|
||||
|
||||
try {
|
||||
process.start();
|
||||
} catch (error) {
|
||||
resolve({ status: -1, stdout: "", stderr: String(error) });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Runs a command and resolves to trimmed stdout, or null on any failure. */
|
||||
async function output(command, args, options) {
|
||||
const result = await exec(command, args, options);
|
||||
if (result.status !== 0) return null;
|
||||
return result.stdout.trim();
|
||||
}
|
||||
|
||||
exports.exec = exec;
|
||||
exports.output = output;
|
||||
@@ -0,0 +1,138 @@
|
||||
//
|
||||
// Thin git wrapper. Nova cannot read a repository's state directly, so branch,
|
||||
// remote, and checkout information all come from the git binary.
|
||||
//
|
||||
|
||||
const { exec, output } = require("./exec.js");
|
||||
const log = require("./log.js");
|
||||
|
||||
const GIT = "/usr/bin/env";
|
||||
|
||||
function git(dir, args) {
|
||||
return exec(GIT, ["git", ...args], { cwd: dir });
|
||||
}
|
||||
|
||||
function gitOutput(dir, args) {
|
||||
return output(GIT, ["git", ...args], { cwd: dir });
|
||||
}
|
||||
|
||||
async function isRepository(dir) {
|
||||
const result = await gitOutput(dir, ["rev-parse", "--is-inside-work-tree"]);
|
||||
return result === "true";
|
||||
}
|
||||
|
||||
async function repositoryRoot(dir) {
|
||||
return await gitOutput(dir, ["rev-parse", "--show-toplevel"]);
|
||||
}
|
||||
|
||||
async function currentBranch(dir) {
|
||||
const branch = await gitOutput(dir, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
||||
// A detached HEAD reports "HEAD", which is not a branch anyone can filter by.
|
||||
return branch && branch !== "HEAD" ? branch : null;
|
||||
}
|
||||
|
||||
async function headSha(dir) {
|
||||
return await gitOutput(dir, ["rev-parse", "HEAD"]);
|
||||
}
|
||||
|
||||
/** Returns [{ name, url }] for every configured remote. */
|
||||
async function remotes(dir) {
|
||||
const text = await gitOutput(dir, ["config", "--get-regexp", "^remote\\..*\\.url$"]);
|
||||
if (!text) return [];
|
||||
|
||||
const found = [];
|
||||
for (const line of text.split("\n")) {
|
||||
const match = line.trim().match(/^remote\.(.+)\.url\s+(.+)$/);
|
||||
if (match) found.push({ name: match[1], url: match[2] });
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/** Prefers "origin" when present; otherwise the first remote defined. */
|
||||
async function primaryRemote(dir) {
|
||||
const list = await remotes(dir);
|
||||
if (!list.length) return null;
|
||||
return list.find((remote) => remote.name === "origin") || list[0];
|
||||
}
|
||||
|
||||
async function hasLocalBranch(dir, branch) {
|
||||
const result = await git(dir, ["rev-parse", "--verify", "--quiet", `refs/heads/${branch}`]);
|
||||
return result.status === 0;
|
||||
}
|
||||
|
||||
async function isDirty(dir) {
|
||||
const text = await gitOutput(dir, ["status", "--porcelain"]);
|
||||
return Boolean(text && text.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the remote and switches to `branch`, creating a tracking branch when
|
||||
* it only exists on the remote. Resolves to an error string, or null on success.
|
||||
*/
|
||||
async function checkoutBranch(dir, branch, remoteName = "origin") {
|
||||
if (await isDirty(dir)) {
|
||||
return "The working tree has uncommitted changes.";
|
||||
}
|
||||
|
||||
const fetched = await git(dir, ["fetch", remoteName, branch]);
|
||||
if (fetched.status !== 0) {
|
||||
log.debug("fetch failed", fetched.stderr);
|
||||
}
|
||||
|
||||
if (await hasLocalBranch(dir, branch)) {
|
||||
const result = await git(dir, ["checkout", branch]);
|
||||
return result.status === 0 ? null : result.stderr.trim() || "Checkout failed.";
|
||||
}
|
||||
|
||||
const result = await git(dir, [
|
||||
"checkout",
|
||||
"-b",
|
||||
branch,
|
||||
"--track",
|
||||
`${remoteName}/${branch}`,
|
||||
]);
|
||||
return result.status === 0 ? null : result.stderr.trim() || "Checkout failed.";
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds git repositories at `root` and one level below it, which covers both a
|
||||
* single-project workspace and the common "folder of repos" layout.
|
||||
*/
|
||||
async function discoverRepositories(root) {
|
||||
if (!root) return [];
|
||||
|
||||
const found = [];
|
||||
if (await isRepository(root)) {
|
||||
const top = (await repositoryRoot(root)) || root;
|
||||
found.push(top);
|
||||
}
|
||||
|
||||
let entries = [];
|
||||
try {
|
||||
entries = nova.fs.listdir(root);
|
||||
} catch (error) {
|
||||
log.debug("listdir failed", root, String(error));
|
||||
}
|
||||
|
||||
for (const entry of entries) {
|
||||
if (entry.startsWith(".")) continue;
|
||||
const child = nova.path.join(root, entry);
|
||||
const stats = nova.fs.stat(child);
|
||||
if (!stats || !stats.isDirectory()) continue;
|
||||
if (!nova.fs.access(nova.path.join(child, ".git"), nova.fs.F_OK)) continue;
|
||||
if (!found.includes(child)) found.push(child);
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
exports.git = git;
|
||||
exports.isRepository = isRepository;
|
||||
exports.repositoryRoot = repositoryRoot;
|
||||
exports.currentBranch = currentBranch;
|
||||
exports.headSha = headSha;
|
||||
exports.remotes = remotes;
|
||||
exports.primaryRemote = primaryRemote;
|
||||
exports.checkoutBranch = checkoutBranch;
|
||||
exports.isDirty = isDirty;
|
||||
exports.discoverRepositories = discoverRepositories;
|
||||
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Logging. Debug output is gated behind the gitea.logging.debug preference so
|
||||
// that a normal session stays quiet in the extension console.
|
||||
//
|
||||
|
||||
const PREFIX = "[gitea]";
|
||||
|
||||
function debugEnabled() {
|
||||
return nova.config.get("gitea.logging.debug", "boolean") === true;
|
||||
}
|
||||
|
||||
exports.debug = function (...args) {
|
||||
if (debugEnabled()) {
|
||||
console.log(PREFIX, ...args);
|
||||
}
|
||||
};
|
||||
|
||||
exports.info = function (...args) {
|
||||
console.log(PREFIX, ...args);
|
||||
};
|
||||
|
||||
exports.warn = function (...args) {
|
||||
console.warn(PREFIX, ...args);
|
||||
};
|
||||
|
||||
exports.error = function (...args) {
|
||||
console.error(PREFIX, ...args);
|
||||
};
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// Timestamp helpers. Gitea returns RFC3339 strings and uses the Go zero time
|
||||
// ("0001-01-01T00:00:00Z") to mean "never", which must not be shown as a date.
|
||||
//
|
||||
|
||||
const ZERO_YEAR = 1;
|
||||
|
||||
function parse(value) {
|
||||
if (!value) return null;
|
||||
if (typeof value === "number") {
|
||||
// Gitea occasionally hands back Unix seconds for job timestamps.
|
||||
return value > 0 ? new Date(value * 1000) : null;
|
||||
}
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime()) || date.getUTCFullYear() <= ZERO_YEAR) return null;
|
||||
return date;
|
||||
}
|
||||
|
||||
function relative(value) {
|
||||
const date = parse(value);
|
||||
if (!date) return "";
|
||||
const seconds = Math.round((Date.now() - date.getTime()) / 1000);
|
||||
const future = seconds < 0;
|
||||
const abs = Math.abs(seconds);
|
||||
|
||||
let text;
|
||||
if (abs < 45) text = "just now";
|
||||
else if (abs < 90) text = "a minute";
|
||||
else if (abs < 3600) text = `${Math.round(abs / 60)}m`;
|
||||
else if (abs < 86400) text = `${Math.round(abs / 3600)}h`;
|
||||
else if (abs < 2592000) text = `${Math.round(abs / 86400)}d`;
|
||||
else if (abs < 31536000) text = `${Math.round(abs / 2592000)}mo`;
|
||||
else text = `${Math.round(abs / 31536000)}y`;
|
||||
|
||||
if (text === "just now") return text;
|
||||
return future ? `in ${text}` : `${text} ago`;
|
||||
}
|
||||
|
||||
function duration(start, end) {
|
||||
const from = parse(start);
|
||||
if (!from) return "";
|
||||
const to = parse(end) || new Date();
|
||||
let seconds = Math.max(0, Math.round((to.getTime() - from.getTime()) / 1000));
|
||||
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
seconds -= hours * 3600;
|
||||
const minutes = Math.floor(seconds / 60);
|
||||
seconds -= minutes * 60;
|
||||
|
||||
if (hours) return `${hours}h ${minutes}m`;
|
||||
if (minutes) return `${minutes}m ${seconds}s`;
|
||||
return `${seconds}s`;
|
||||
}
|
||||
|
||||
function absolute(value) {
|
||||
const date = parse(value);
|
||||
if (!date) return "";
|
||||
return date.toLocaleString();
|
||||
}
|
||||
|
||||
exports.parse = parse;
|
||||
exports.relative = relative;
|
||||
exports.duration = duration;
|
||||
exports.absolute = absolute;
|
||||
Reference in New Issue
Block a user