Files
gitea-nova/Scripts/util/config.js
T
thatguygriffandClaude Opus 5 694609a3cc 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
2026-08-28 19:55:54 -03:00

128 lines
4.2 KiB
JavaScript

//
// 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/";