refactor: reorganize task management imports and update task handling tools
- Updated import paths for task management providers in task-comment, task-create, and task-update tools. - Removed deprecated task-complete and task-pickup tools, replacing them with work-finish and work-start tools for improved task handling. - Enhanced work-finish and work-start tools to streamline task completion and pickup processes, including context-aware detection and auto-scheduling features. - Updated package.json to include build scripts and main entry point. - Modified tsconfig.json to enable output directory, declaration files, and source maps for better TypeScript support.
This commit is contained in:
123
lib/providers/github.ts
Normal file
123
lib/providers/github.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* GitHubProvider — IssueProvider implementation using gh CLI.
|
||||
*/
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { writeFile, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import {
|
||||
type IssueProvider,
|
||||
type Issue,
|
||||
type StateLabel,
|
||||
STATE_LABELS,
|
||||
LABEL_COLORS,
|
||||
} from "./provider.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
type GhIssue = {
|
||||
number: number;
|
||||
title: string;
|
||||
body: string;
|
||||
labels: Array<{ name: string }>;
|
||||
state: string;
|
||||
url: string;
|
||||
};
|
||||
|
||||
function toIssue(gh: GhIssue): Issue {
|
||||
return {
|
||||
iid: gh.number, title: gh.title, description: gh.body ?? "",
|
||||
labels: gh.labels.map((l) => l.name), state: gh.state, web_url: gh.url,
|
||||
};
|
||||
}
|
||||
|
||||
export class GitHubProvider implements IssueProvider {
|
||||
private repoPath: string;
|
||||
constructor(opts: { repoPath: string }) { this.repoPath = opts.repoPath; }
|
||||
|
||||
private async gh(args: string[]): Promise<string> {
|
||||
const { stdout } = await execFileAsync("gh", args, { cwd: this.repoPath, timeout: 30_000 });
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
async ensureLabel(name: string, color: string): Promise<void> {
|
||||
try { await this.gh(["label", "create", name, "--color", color.replace(/^#/, "")]); }
|
||||
catch (err) { if (!(err as Error).message?.includes("already exists")) throw err; }
|
||||
}
|
||||
|
||||
async ensureAllStateLabels(): Promise<void> {
|
||||
for (const label of STATE_LABELS) await this.ensureLabel(label, LABEL_COLORS[label]);
|
||||
}
|
||||
|
||||
async createIssue(title: string, description: string, label: StateLabel, assignees?: string[]): Promise<Issue> {
|
||||
const tempFile = join(tmpdir(), `devclaw-issue-${Date.now()}.md`);
|
||||
await writeFile(tempFile, description, "utf-8");
|
||||
try {
|
||||
const args = ["issue", "create", "--title", title, "--body-file", tempFile, "--label", label];
|
||||
if (assignees?.length) args.push("--assignee", assignees.join(","));
|
||||
const url = await this.gh(args);
|
||||
const match = url.match(/\/issues\/(\d+)$/);
|
||||
if (!match) throw new Error(`Failed to parse issue URL: ${url}`);
|
||||
return this.getIssue(parseInt(match[1], 10));
|
||||
} finally { try { await unlink(tempFile); } catch { /* ignore */ } }
|
||||
}
|
||||
|
||||
async listIssuesByLabel(label: StateLabel): Promise<Issue[]> {
|
||||
try {
|
||||
const raw = await this.gh(["issue", "list", "--label", label, "--state", "open", "--json", "number,title,body,labels,state,url"]);
|
||||
return (JSON.parse(raw) as GhIssue[]).map(toIssue);
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
async getIssue(issueId: number): Promise<Issue> {
|
||||
const raw = await this.gh(["issue", "view", String(issueId), "--json", "number,title,body,labels,state,url"]);
|
||||
return toIssue(JSON.parse(raw) as GhIssue);
|
||||
}
|
||||
|
||||
async transitionLabel(issueId: number, from: StateLabel, to: StateLabel): Promise<void> {
|
||||
const issue = await this.getIssue(issueId);
|
||||
const stateLabels = issue.labels.filter((l) => STATE_LABELS.includes(l as StateLabel));
|
||||
const args = ["issue", "edit", String(issueId)];
|
||||
for (const l of stateLabels) args.push("--remove-label", l);
|
||||
args.push("--add-label", to);
|
||||
await this.gh(args);
|
||||
}
|
||||
|
||||
async closeIssue(issueId: number): Promise<void> { await this.gh(["issue", "close", String(issueId)]); }
|
||||
async reopenIssue(issueId: number): Promise<void> { await this.gh(["issue", "reopen", String(issueId)]); }
|
||||
|
||||
hasStateLabel(issue: Issue, expected: StateLabel): boolean { return issue.labels.includes(expected); }
|
||||
getCurrentStateLabel(issue: Issue): StateLabel | null {
|
||||
return STATE_LABELS.find((l) => issue.labels.includes(l)) ?? null;
|
||||
}
|
||||
|
||||
async hasMergedMR(issueId: number): Promise<boolean> {
|
||||
try {
|
||||
const raw = await this.gh(["pr", "list", "--state", "merged", "--json", "title,body"]);
|
||||
const prs = JSON.parse(raw) as Array<{ title: string; body: string }>;
|
||||
const pat = `#${issueId}`;
|
||||
return prs.some((pr) => pr.title.includes(pat) || (pr.body ?? "").includes(pat));
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
async getMergedMRUrl(issueId: number): Promise<string | null> {
|
||||
try {
|
||||
const raw = await this.gh(["pr", "list", "--state", "merged", "--json", "number,title,body,url,mergedAt", "--limit", "20"]);
|
||||
const prs = JSON.parse(raw) as Array<{ number: number; title: string; body: string; url: string; mergedAt: string }>;
|
||||
const pat = `#${issueId}`;
|
||||
return prs.find((pr) => pr.title.includes(pat) || (pr.body ?? "").includes(pat))?.url ?? null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
async addComment(issueId: number, body: string): Promise<void> {
|
||||
const tempFile = join(tmpdir(), `devclaw-comment-${Date.now()}.md`);
|
||||
await writeFile(tempFile, body, "utf-8");
|
||||
try { await this.gh(["issue", "comment", String(issueId), "--body-file", tempFile]); }
|
||||
finally { try { await unlink(tempFile); } catch { /* ignore */ } }
|
||||
}
|
||||
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try { await this.gh(["auth", "status"]); return true; } catch { return false; }
|
||||
}
|
||||
}
|
||||
116
lib/providers/gitlab.ts
Normal file
116
lib/providers/gitlab.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* GitLabProvider — IssueProvider implementation using glab CLI.
|
||||
*/
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { writeFile, unlink } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { tmpdir } from "node:os";
|
||||
import {
|
||||
type IssueProvider,
|
||||
type Issue,
|
||||
type StateLabel,
|
||||
STATE_LABELS,
|
||||
LABEL_COLORS,
|
||||
} from "./provider.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export class GitLabProvider implements IssueProvider {
|
||||
private repoPath: string;
|
||||
constructor(opts: { repoPath: string }) { this.repoPath = opts.repoPath; }
|
||||
|
||||
private async glab(args: string[]): Promise<string> {
|
||||
const { stdout } = await execFileAsync("glab", args, { cwd: this.repoPath, timeout: 30_000 });
|
||||
return stdout.trim();
|
||||
}
|
||||
|
||||
async ensureLabel(name: string, color: string): Promise<void> {
|
||||
try { await this.glab(["label", "create", "--name", name, "--color", color]); }
|
||||
catch (err) { const msg = (err as Error).message ?? ""; if (!msg.includes("already exists") && !msg.includes("409")) throw err; }
|
||||
}
|
||||
|
||||
async ensureAllStateLabels(): Promise<void> {
|
||||
for (const label of STATE_LABELS) await this.ensureLabel(label, LABEL_COLORS[label]);
|
||||
}
|
||||
|
||||
async createIssue(title: string, description: string, label: StateLabel, assignees?: string[]): Promise<Issue> {
|
||||
const tempFile = join(tmpdir(), `devclaw-issue-${Date.now()}.md`);
|
||||
await writeFile(tempFile, description, "utf-8");
|
||||
try {
|
||||
const { exec } = await import("node:child_process");
|
||||
const execAsync = promisify(exec);
|
||||
let cmd = `glab issue create --title "${title.replace(/"/g, '\\"')}" --description "$(cat ${tempFile})" --label "${label}"`;
|
||||
if (assignees?.length) cmd += ` --assignee "${assignees.join(",")}"`;
|
||||
const { stdout } = await execAsync(cmd, { cwd: this.repoPath, timeout: 30_000 });
|
||||
// glab issue create returns the issue URL
|
||||
const match = stdout.trim().match(/\/issues\/(\d+)/);
|
||||
if (!match) throw new Error(`Failed to parse issue URL: ${stdout.trim()}`);
|
||||
return this.getIssue(parseInt(match[1], 10));
|
||||
} finally { try { await unlink(tempFile); } catch { /* ignore */ } }
|
||||
}
|
||||
|
||||
async listIssuesByLabel(label: StateLabel): Promise<Issue[]> {
|
||||
try {
|
||||
const raw = await this.glab(["issue", "list", "--label", label, "--output", "json"]);
|
||||
return JSON.parse(raw) as Issue[];
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
async getIssue(issueId: number): Promise<Issue> {
|
||||
const raw = await this.glab(["issue", "view", String(issueId), "--output", "json"]);
|
||||
return JSON.parse(raw) as Issue;
|
||||
}
|
||||
|
||||
async transitionLabel(issueId: number, from: StateLabel, to: StateLabel): Promise<void> {
|
||||
const issue = await this.getIssue(issueId);
|
||||
const stateLabels = issue.labels.filter((l) => STATE_LABELS.includes(l as StateLabel));
|
||||
const args = ["issue", "update", String(issueId)];
|
||||
for (const l of stateLabels) args.push("--unlabel", l);
|
||||
args.push("--label", to);
|
||||
await this.glab(args);
|
||||
}
|
||||
|
||||
async closeIssue(issueId: number): Promise<void> { await this.glab(["issue", "close", String(issueId)]); }
|
||||
async reopenIssue(issueId: number): Promise<void> { await this.glab(["issue", "reopen", String(issueId)]); }
|
||||
|
||||
hasStateLabel(issue: Issue, expected: StateLabel): boolean { return issue.labels.includes(expected); }
|
||||
getCurrentStateLabel(issue: Issue): StateLabel | null {
|
||||
return STATE_LABELS.find((l) => issue.labels.includes(l)) ?? null;
|
||||
}
|
||||
|
||||
async hasMergedMR(issueId: number): Promise<boolean> {
|
||||
try {
|
||||
const raw = await this.glab(["mr", "list", "--output", "json", "--state", "merged"]);
|
||||
const mrs = JSON.parse(raw) as Array<{ title: string; description: string }>;
|
||||
const pat = `#${issueId}`;
|
||||
return mrs.some((mr) => mr.title.includes(pat) || (mr.description ?? "").includes(pat));
|
||||
} catch { return false; }
|
||||
}
|
||||
|
||||
async getMergedMRUrl(issueId: number): Promise<string | null> {
|
||||
try {
|
||||
const raw = await this.glab(["mr", "list", "--output", "json", "--state", "merged"]);
|
||||
const mrs = JSON.parse(raw) as Array<{ iid: number; title: string; description: string; web_url: string; merged_at: string }>;
|
||||
const pat = `#${issueId}`;
|
||||
const mr = mrs
|
||||
.filter((mr) => mr.title.includes(pat) || (mr.description ?? "").includes(pat))
|
||||
.sort((a, b) => new Date(b.merged_at).getTime() - new Date(a.merged_at).getTime())[0];
|
||||
return mr?.web_url ?? null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
async addComment(issueId: number, body: string): Promise<void> {
|
||||
const tempFile = join(tmpdir(), `devclaw-comment-${Date.now()}.md`);
|
||||
await writeFile(tempFile, body, "utf-8");
|
||||
try {
|
||||
const { exec } = await import("node:child_process");
|
||||
const execAsync = promisify(exec);
|
||||
await execAsync(`glab issue note ${issueId} --message "$(cat ${tempFile})"`, { cwd: this.repoPath, timeout: 30_000 });
|
||||
} finally { try { await unlink(tempFile); } catch { /* ignore */ } }
|
||||
}
|
||||
|
||||
async healthCheck(): Promise<boolean> {
|
||||
try { await this.glab(["auth", "status"]); return true; } catch { return false; }
|
||||
}
|
||||
}
|
||||
36
lib/providers/index.ts
Normal file
36
lib/providers/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Provider factory — auto-detects GitHub vs GitLab from git remote.
|
||||
*/
|
||||
import { execFileSync } from "node:child_process";
|
||||
import type { IssueProvider } from "./provider.js";
|
||||
import { GitLabProvider } from "./gitlab.js";
|
||||
import { GitHubProvider } from "./github.js";
|
||||
import { resolveRepoPath } from "../projects.js";
|
||||
|
||||
export type ProviderOptions = {
|
||||
provider?: "gitlab" | "github";
|
||||
repo?: string;
|
||||
repoPath?: string;
|
||||
};
|
||||
|
||||
export type ProviderWithType = {
|
||||
provider: IssueProvider;
|
||||
type: "github" | "gitlab";
|
||||
};
|
||||
|
||||
function detectProvider(repoPath: string): "gitlab" | "github" {
|
||||
try {
|
||||
const url = execFileSync("git", ["remote", "get-url", "origin"], { cwd: repoPath, timeout: 5_000 }).toString().trim();
|
||||
return url.includes("github.com") ? "github" : "gitlab";
|
||||
} catch {
|
||||
return "gitlab";
|
||||
}
|
||||
}
|
||||
|
||||
export function createProvider(opts: ProviderOptions): ProviderWithType {
|
||||
const repoPath = opts.repoPath ?? (opts.repo ? resolveRepoPath(opts.repo) : null);
|
||||
if (!repoPath) throw new Error("Either repoPath or repo must be provided");
|
||||
const type = opts.provider ?? detectProvider(repoPath);
|
||||
const provider = type === "github" ? new GitHubProvider({ repoPath }) : new GitLabProvider({ repoPath });
|
||||
return { provider, type };
|
||||
}
|
||||
45
lib/providers/provider.ts
Normal file
45
lib/providers/provider.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* IssueProvider — Abstract interface for issue tracker operations.
|
||||
*
|
||||
* Implementations: GitHub (gh CLI), GitLab (glab CLI).
|
||||
*/
|
||||
|
||||
export const STATE_LABELS = [
|
||||
"Planning", "To Do", "Doing", "To Test", "Testing", "Done", "To Improve", "Refining",
|
||||
] as const;
|
||||
|
||||
export type StateLabel = (typeof STATE_LABELS)[number];
|
||||
|
||||
export const LABEL_COLORS: Record<StateLabel, string> = {
|
||||
Planning: "#6699cc", "To Do": "#428bca", Doing: "#f0ad4e", "To Test": "#5bc0de",
|
||||
Testing: "#9b59b6", Done: "#5cb85c", "To Improve": "#d9534f", Refining: "#f39c12",
|
||||
};
|
||||
|
||||
export type Issue = {
|
||||
iid: number;
|
||||
title: string;
|
||||
description: string;
|
||||
labels: string[];
|
||||
state: string;
|
||||
web_url: string;
|
||||
};
|
||||
|
||||
export interface IssueProvider {
|
||||
ensureLabel(name: string, color: string): Promise<void>;
|
||||
ensureAllStateLabels(): Promise<void>;
|
||||
createIssue(title: string, description: string, label: StateLabel, assignees?: string[]): Promise<Issue>;
|
||||
listIssuesByLabel(label: StateLabel): Promise<Issue[]>;
|
||||
getIssue(issueId: number): Promise<Issue>;
|
||||
transitionLabel(issueId: number, from: StateLabel, to: StateLabel): Promise<void>;
|
||||
closeIssue(issueId: number): Promise<void>;
|
||||
reopenIssue(issueId: number): Promise<void>;
|
||||
hasStateLabel(issue: Issue, expected: StateLabel): boolean;
|
||||
getCurrentStateLabel(issue: Issue): StateLabel | null;
|
||||
hasMergedMR(issueId: number): Promise<boolean>;
|
||||
getMergedMRUrl(issueId: number): Promise<string | null>;
|
||||
addComment(issueId: number, body: string): Promise<void>;
|
||||
healthCheck(): Promise<boolean>;
|
||||
}
|
||||
|
||||
/** @deprecated Use IssueProvider */
|
||||
export type TaskManager = IssueProvider;
|
||||
Reference in New Issue
Block a user