Files
gitea-nova/Scripts/controllers/auth.js
T
thatguygriffandClaude Opus 5 87851de921
CI / Tests (push) Successful in 36s
CI / Generated images (push) Successful in 1m1s
Support instances that serve SSH from another hostname
Discovery matched a git remote's host against the instance URL's host, so
a server answering SSH on a different name than its web UI resolved to
nothing — and said so only in a debug log.

Resolution now runs in three stages. A remote host is matched directly,
then against configured aliases, and failing both the instances are asked
for the repository: Gitea publishes its SSH hostname in a repository's
ssh_url, so the right instance identifies itself. What that turns up is
saved as a host alias, so later repositories on the same host resolve
with no lookup at all, and the mapping is visible and editable rather
than hidden. Each unknown host is probed at most once per session.

When nothing resolves the sidebar now names the unmatched host and offers
Add Host Alias, instead of showing an empty section.

Aliases can also be written by hand as "remote-host = instance URL",
accepting =, -> and =>, ignoring ports, and skipping # comments. List
preferences now merge workspace entries onto global ones rather than
letting an empty global array mask them.

Adds Tests/host-aliases.test.js covering both directions: unmatched hosts
reported and nothing persisted, a hand-written alias, detection from
ssh_url, and a later repository resolving from the stored alias without a
probe. 131 checks across three suites.

Also adds CLAUDE.md, and .gitea/workflows/ci.yml running the suites,
script syntax checks, manifest validation, and a generated-image check.
Tools/make-icons.py gains --check, which compares decompressed pixels so
a differing zlib version cannot fail it spuriously.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MQuusXgZC2dzwpJJ1qhtti
2026-08-28 20:10:05 -03:00

183 lines
6.7 KiB
JavaScript

//
// Instance and token management: sign in, sign out, test, add, remove.
//
const config = require("../util/config.js");
const credentials = require("../gitea/credentials.js");
const selection = require("./selection.js");
const ui = require("../ui.js");
const TOKEN_HELP =
"Create one in Gitea under Settings → Applications → Access Tokens with " +
"repository, issue, and Actions read scopes (plus write scopes for reviews, " +
"secrets, and variables).";
/** Picks the instance a command applies to, prompting only when ambiguous. */
async function resolveInstance(argument, prompt) {
const node = selection.selectedNode(argument);
if (node && node.data && node.data.baseUrl) return node.data.baseUrl;
const repo = node && node.repo ? node.repo : null;
if (repo) return repo.baseUrl;
const urls = config.instances();
if (!urls.length) {
ui.warn("No Gitea instance is configured. Open the extension preferences first.");
return null;
}
if (urls.length === 1) return urls[0];
return await ui.choose(urls, { placeholder: prompt });
}
function register(store, refresh) {
nova.commands.register("gitea.setToken", async (argument) => {
const baseUrl = await resolveInstance(argument, "Set a token for which instance?");
if (!baseUrl) return;
const token = await ui.input(`Personal access token for ${baseUrl}`, {
placeholder: "Access token",
prompt: TOKEN_HELP,
secure: true,
});
if (!token) return;
try {
credentials.setToken(baseUrl, token.trim());
} catch (error) {
ui.error(`Could not save the token to the Keychain: ${error.message || error}`);
return;
}
store.router.invalidate();
const api = store.router.apiFor(baseUrl);
try {
const [version, user] = await Promise.all([api.version(), api.currentUser()]);
ui.info(`Signed in to ${baseUrl} as ${user.login} (Gitea ${version}).`);
} catch (error) {
ui.error(`Token saved, but ${baseUrl} rejected it: ${error.message || error}`);
}
await refresh({ rediscover: true });
});
nova.commands.register("gitea.clearToken", async (argument) => {
const baseUrl = await resolveInstance(argument, "Sign out of which instance?");
if (!baseUrl) return;
if (!(await ui.confirm(`Remove the stored token for ${baseUrl}?`, "Sign Out"))) return;
credentials.removeToken(baseUrl);
store.router.invalidate();
ui.info(`Signed out of ${baseUrl}.`);
await refresh({ rediscover: true });
});
nova.commands.register("gitea.testConnection", async (argument) => {
const baseUrl = await resolveInstance(argument, "Test which instance?");
if (!baseUrl) return;
if (!credentials.hasToken(baseUrl)) {
ui.warn(`No token stored for ${baseUrl}. Run “Set Token…” first.`);
return;
}
const api = store.router.apiFor(baseUrl);
try {
const [version, user] = await Promise.all([api.version(), api.currentUser()]);
ui.info(`${baseUrl} is reachable — Gitea ${version}, signed in as ${user.login}.`);
} catch (error) {
ui.error(`${baseUrl} failed: ${error.message || error}`);
}
await refresh();
});
nova.commands.register("gitea.addInstance", async () => {
const entered = await ui.input("Add a Gitea instance", {
placeholder: "https://gitea.example.com",
prompt: "Repositories are routed to an instance by matching their git remote host.",
});
if (!entered) return;
const baseUrl = config.normalizeBaseUrl(entered);
if (!baseUrl) {
ui.warn("That does not look like a URL.");
return;
}
if (config.instances().includes(baseUrl)) {
ui.info(`${baseUrl} is already configured.`);
return;
}
const extra = nova.config.get("gitea.instances", "array") || [];
config.setInstances([...extra, baseUrl]);
store.router.invalidate();
if (await ui.confirm(`Added ${baseUrl}. Set a token for it now?`, "Set Token")) {
await nova.commands.invoke("gitea.setToken", { kind: "instance", data: { baseUrl } });
} else {
await refresh({ rediscover: true });
}
});
nova.commands.register("gitea.removeInstance", async (argument) => {
const extra = nova.config.get("gitea.instances", "array") || [];
if (!extra.length) {
ui.info("The default instance is set in preferences and cannot be removed here.");
return;
}
const node = selection.selectedNode(argument);
let baseUrl = node && node.data ? node.data.baseUrl : null;
if (!baseUrl || !extra.includes(baseUrl)) {
baseUrl = await ui.choose(extra, { placeholder: "Remove which instance?" });
}
if (!baseUrl) return;
if (!(await ui.confirm(`Remove ${baseUrl} and its stored token?`, "Remove"))) return;
credentials.removeToken(baseUrl);
config.setInstances(extra.filter((url) => url !== baseUrl));
store.router.invalidate();
await refresh({ rediscover: true });
});
// Reachable from the warning row a repository shows when its remote host
// matches nothing, which is where users actually hit this.
nova.commands.register("gitea.addHostAlias", async (argument) => {
const node = selection.selectedNode(argument);
const suggested = node && node.data ? node.data.host : null;
const host =
suggested ||
(await ui.input("Which git remote host should be mapped?", {
placeholder: "ssh.example.com",
prompt:
"The hostname your git remotes use, when it differs from the " +
"instance's web address.",
}));
if (!host) return;
const urls = config.instances();
if (!urls.length) {
ui.warn("Configure a Gitea instance before adding an alias for one.");
return;
}
const baseUrl =
urls.length === 1
? urls[0]
: await ui.choose(urls, { placeholder: `Map ${host} onto which instance?` });
if (!baseUrl) return;
config.addHostAlias(host, baseUrl);
store.router.invalidate();
ui.info(`Remotes on ${host} now resolve to ${baseUrl}.`);
await refresh({ rediscover: true });
});
nova.commands.register("gitea.openPreferences", () => {
nova.openConfig();
});
}
exports.register = register;
exports.resolveInstance = resolveInstance;