Файли
openidea/src/features/github/actions/ActionsToolWindow.tsx
T
LU XIANGRUIтаwildcatDownstairs 7ab456b1e7 feat(github): GitHub Actions tool window with job logs and re-runs (#6767)
Adds a GitHub Actions tool window listing the workflow runs of the current branch (including its pull request runs), with a toggle for every branch. Expanding a run lists its jobs; selecting a job shows its log with runner timestamps and group markers removed and error lines highlighted. The toolbar has Re-run Failed Jobs, Re-run All Jobs, Re-run Job, Open in Browser and Refresh, and the list refreshes every 15 seconds while anything runs.

gh 2.104 refuses to print responses containing terminal escape sequences unless --allow-escape-sequences is passed; ide_github::api retries a GET with the flag only when gh refused for that reason, so older gh versions keep working.

resetConnection now clears a GitHub check still running for the previous project, which left the connection stuck in "checking" after switching projects.

The tool window uses the github icon (the build tool window already uses toolwindow-build), and "GitHub Actions" is kept untranslated in zh.

Closes #6716

Co-authored-by: wildcatDownstairs <[email protected]>
2026-11-29 13:59:25 +08:00

181 рядки
6.6 KiB
TypeScript

/**
* GitHub Actions tool window: the connected repository's workflow runs (current branch by
* default) with their jobs, and the selected job's log.
*/
import { useEffect } from "react";
import { useVcs } from "@/features/vcs/store";
import { executeAction } from "@/platform/actions";
import { tx } from "@/platform/i18n";
import { Button, IconButton, LogPane, SplitGroup, SplitHandle, SplitPanel, T, Toolbar, Tree } from "@/ui";
import { STATUS_ICON } from "../details/model";
import { refreshConnection, useGhRepository } from "../data/repository";
import { prettyDate } from "../ui/prettyDate";
import { Placeholder } from "../ui/Placeholder";
import { canRerun, canRerunFailed, duration, logLines, statusIcon, type WorkflowJob, type WorkflowRun } from "./model";
import {
anyActive,
jobLog,
loadJobs,
refreshRuns,
resetActions,
selection,
setBranchOnly,
useGhActions,
} from "./store";
import styles from "./Actions.module.css";
type Node = { kind: "run"; run: WorkflowRun } | { kind: "job"; job: WorkflowJob };
const key = (n: Node) => (n.kind === "run" ? `run:${n.run.id}` : `job:${n.job.id}`);
const POLL_MS = 15_000;
function runDetail(run: WorkflowRun) {
const pr = run.pull_requests?.[0];
return [
`${run.name ?? ""} #${run.run_number}`,
run.event,
pr ? `#${pr.number}` : run.head_branch,
run.head_sha.slice(0, 7),
prettyDate(run.created_at),
]
.filter(Boolean)
.join(" · ");
}
function JobLog({ job }: { job: WorkflowJob }) {
return (
<LogPane
// Loads again when the job changes (finishes), not on every refresh of the list.
sourceKey={`${job.id}:${job.status}:${job.completed_at ?? ""}`}
live={false}
ariaLabel={tx`Log of ${job.name}`}
start={(onLine, onError) => {
let stopped = false;
jobLog(job.id).then(
(text) => {
if (!stopped) for (const l of logLines(text)) onLine(l);
},
(e: unknown) => {
if (stopped) return;
// GitHub has no log for a job that hasn't finished (or started).
onError(
job.status === "completed"
? String(e instanceof Error ? e.message : e)
: tx("The log is available once the job finishes."),
);
},
);
return () => {
stopped = true;
};
}}
/>
);
}
export function ActionsToolWindow() {
const connected = useGhRepository((s) => !!s.connection);
const repository = useGhRepository((s) => {
const c = s.connection;
return c ? `${c.coordinates.host}/${c.coordinates.owner}/${c.coordinates.name}@${c.root ?? ""}` : null;
});
const head = useVcs((s) => s.branch?.head ?? null);
// Not checked yet, or reset by a project switch: check (again).
const unchecked = useGhRepository((s) => !s.availability && !s.checking);
const state = useGhActions();
const { run, job } = selection(state);
const active = anyActive(state);
useEffect(() => {
resetActions();
if (repository) void refreshRuns();
}, [repository]);
useEffect(() => {
if (!connected && unchecked) void refreshConnection();
}, [connected, unchecked]);
// Another branch checked out: its runs.
useEffect(() => {
if (useGhRepository.getState().connection && useGhActions.getState().branchOnly) void refreshRuns();
}, [head]);
useEffect(() => {
if (!connected || !active) return;
const t = setInterval(() => void refreshRuns(), POLL_MS);
return () => clearInterval(t);
}, [connected, active]);
if (!connected)
return (
<Placeholder title="Not connected to a GitHub repository">
<Button onClick={() => void executeAction("github.pullRequests")}>
<T>View Pull Requests</T>
</Button>
</Placeholder>
);
const roots: Node[] = state.runs.map((r) => ({ kind: "run", run: r }));
return (
<div className={styles.view}>
<Toolbar aria-label="GitHub Actions" orientation="vertical" className={styles.toolbar}>
<IconButton icon="refresh" size="small" action="github.actions.refresh" disabled={state.loading} />
<IconButton
icon="branch"
size="small"
title="Current Branch Only"
pressed={state.branchOnly}
onClick={() => setBranchOnly(!state.branchOnly)}
/>
<IconButton
icon="rerun"
tone="danger"
size="small"
action="github.actions.rerunFailedJobs"
disabled={!run || !canRerunFailed(run)}
/>
<IconButton icon="rerun" size="small" action="github.actions.rerunAllJobs" disabled={!run || !canRerun(run)} />
<IconButton icon="run" size="small" action="github.actions.rerunJob" disabled={!job || !canRerun(job)} />
<IconButton icon="external-link" size="small" action="github.actions.openInBrowser" disabled={!run} />
</Toolbar>
<SplitGroup orientation="horizontal" storageKey="openidea.github.actions.split" className={styles.split}>
<SplitPanel id="runs" defaultSize="45%" minSize="20%" className={styles.runs}>
{state.error && <div className={styles.error}>{state.error}</div>}
<Tree<Node>
aria-label={tx("Workflow runs")}
roots={roots}
getKey={key}
getLabel={(n) => (n.kind === "run" ? n.run.display_title : n.job.name)}
getIcon={(n) => STATUS_ICON[statusIcon(n.kind === "run" ? n.run : n.job)]}
getDetail={(n) => (n.kind === "run" ? runDetail(n.run) : duration(n.job.started_at, n.job.completed_at))}
getChildren={(n) =>
n.kind === "run" ? state.jobs[n.run.id]?.map((j): Node => ({ kind: "job", job: j })) : undefined
}
isExpandable={(n) => n.kind === "run"}
loadChildren={(n) => (n.kind === "run" ? loadJobs(n.run.id) : undefined)}
selected={state.selected ? new Set([state.selected]) : new Set()}
onSelectionChange={(_, lead) => useGhActions.setState({ selected: lead ? key(lead) : null })}
empty={
state.loading ? (
<T>Loading…</T>
) : state.branchOnly && state.branch ? (
tx`No workflow runs for ${state.branch}`
) : (
<T>No workflow runs</T>
)
}
speedSearch
/>
</SplitPanel>
<SplitHandle aria-label={tx("Resize workflow runs and log")} />
<SplitPanel id="log" minSize="25%">
{job ? (
<JobLog job={job} />
) : (
<div className={styles.hint}>
<T>Select a job to see its log</T>
</div>
)}
</SplitPanel>
</SplitGroup>
</div>
);
}