Files
gitea-nova/Scripts/controllers/reviewComments.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

156 lines
5.2 KiB
JavaScript

//
// Inline review comments. Nova has no editor decoration or comment-thread API,
// so pull request review comments for the current branch are published as
// issues: they appear in the gutter, on hover, and in the Issues sidebar.
//
const config = require("../util/config.js");
const log = require("../util/log.js");
const { lineFromDiffHunk } = require("./review.js");
const DEBOUNCE_MS = 750;
function uriFor(path) {
return `file://${encodeURI(path).replace(/#/g, "%23")}`;
}
function severityFor(comment) {
if (comment.resolver && comment.resolver.login) return IssueSeverity.Info;
const state = String(comment.review ? comment.review.state : "").toUpperCase();
return state === "REQUEST_CHANGES" ? IssueSeverity.Error : IssueSeverity.Warning;
}
class ReviewCommentsController {
constructor(store) {
this.store = store;
this.collection = new IssueCollection("Gitea Review");
this._paths = new Set();
this._signature = null;
this._timer = null;
this._running = false;
this._subscription = store.onDidChange(() => this.scheduleReload());
}
/** Coalesces the frequent store updates into one rebuild. */
scheduleReload() {
if (this._timer) clearTimeout(this._timer);
this._timer = setTimeout(() => {
this._timer = null;
this.reload();
}, DEBOUNCE_MS);
}
/**
* Cheap fingerprint of the pull requests in scope. When it is unchanged the
* comment fetch is skipped, which keeps idle polling free of API calls.
*/
signature(targets) {
return targets
.map(({ repo, pull }) => `${repo.key}:${pull.number}:${pull.updated_at}`)
.sort()
.join("|");
}
/** Pull requests whose head branch matches the branch filter, per repo. */
targets() {
const found = [];
for (const repo of this.store.repos) {
if (!repo.dir) continue; // Without a checkout there is no file to annotate.
const branch = this.store.branchFor(repo);
for (const pull of this.store.pullRequestsFor(repo.key).items) {
if (branch && (!pull.head || pull.head.ref !== branch)) continue;
found.push({ repo, pull });
}
}
return found;
}
async reload({ force = false } = {}) {
if (this._running) return;
if (!config.reviewCommentsEnabled()) {
this.clear();
this._signature = null;
return;
}
const targets = this.targets();
const signature = this.signature(targets);
if (!force && signature === this._signature) return;
this._running = true;
try {
const byPath = new Map();
for (const { repo, pull } of targets) {
const detail =
this.store.detailFor(repo.key, pull.number) ||
(await this.store.loadPullRequestDetail(repo, pull));
if (!detail) continue;
for (const comment of detail.comments) {
if (!comment.path) continue;
const path = nova.path.join(repo.dir, comment.path);
if (!nova.fs.access(path, nova.fs.F_OK)) continue;
const issue = new Issue();
issue.source = `Gitea #${pull.number}`;
issue.code = String(comment.id);
issue.severity = severityFor(comment);
issue.message = this.describe(comment);
const line = lineFromDiffHunk(comment.diff_hunk, comment.position) || 1;
issue.line = line;
issue.column = 1;
issue.endLine = line;
issue.endColumn = 1;
if (!byPath.has(path)) byPath.set(path, []);
byPath.get(path).push(issue);
}
}
this.apply(byPath);
this._signature = signature;
} catch (error) {
log.error("review comments failed", String(error && error.stack ? error.stack : error));
} finally {
this._running = false;
}
}
describe(comment) {
const author = (comment.user && comment.user.login) || "someone";
const resolved = comment.resolver && comment.resolver.login ? " (resolved)" : "";
const body = String(comment.body || "").trim() || "(empty comment)";
return `${author}${resolved}: ${body}`;
}
/** Replaces the published set, clearing files that no longer have comments. */
apply(byPath) {
for (const path of this._paths) {
if (!byPath.has(path)) this.collection.remove(uriFor(path));
}
for (const [path, issues] of byPath) {
this.collection.set(uriFor(path), issues);
}
this._paths = new Set(byPath.keys());
}
clear() {
this.collection.clear();
this._paths = new Set();
}
dispose() {
if (this._timer) clearTimeout(this._timer);
if (this._subscription) this._subscription.dispose();
this.collection.dispose();
}
}
exports.ReviewCommentsController = ReviewCommentsController;
exports.uriFor = uriFor;