Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

3.7 KB
Raw Blame
import type { ProjectDetail } from '@/lib/api/endpoints/projects';
import type { Issue } from '@/lib/api/endpoints/issues';
import { issuePath } from '@/utils/paths';

// A git-branch-safe handle from the current user: "[email protected]" ->
// "poluosmaka". Empty when there is no user, which drops the branch prefix.
function userHandle(
  transliterate: (text: string) => string,
  name?: string | null,
  email?: string | null,
): string {
  const base = email ? email.split('@')[0] : (name ?? '');
  return transliterate(base)
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '');
}

function titleSlug(transliterate: (text: string) => string, title: string): string {
  return transliterate(title)
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
    .slice(0, 50)
    .replace(/-+$/, '');
}

export async function buildIssueBranchName(
  issue: Pick<Issue, 'identifier' | 'title'>,
  user?: { name?: string | null; email?: string | null },
): Promise<string> {
  // Every project page renders the copy actions, so the large transliteration tables
  // load on the first copy instead of with the page.
  const { transliterate } = await import('@/utils/transliterate');
  const handle = userHandle(transliterate, user?.name, user?.email);
  const slug = titleSlug(transliterate, issue.title);
  const id = issue.identifier.toLowerCase();
  return `${handle ? `${handle}/` : ''}${slug ? `${id}-${slug}` : id}`;
}

// Builds the prompt copied by "Copy Prompt", matching Linear's format: a lead
// instruction, a suggested branch name, then the issue as XML-like tags with the
// full Markdown description inside <description>. IDs (status, assignee, labels,
// ...) are resolved to human-readable names via the project. Called in the
// browser, so window.location.origin gives the absolute issue URL.
export async function buildIssuePrompt(
  issue: Issue,
  project: ProjectDetail,
  user?: { name?: string | null; email?: string | null },
): Promise<string> {
  const status = project.columns.find((c) => c.id === issue.columnId)?.name;
  const type =
    issue.typeId != null ? project.issueTypes.find((t) => t.id === issue.typeId)?.name : undefined;
  const assignee = issue.assigneeUserId
    ? project.assignees.find((a) => a.userId === issue.assigneeUserId)?.name
    : undefined;
  const delegate = issue.delegateUserId
    ? project.assignees.find((a) => a.userId === issue.delegateUserId)?.name
    : undefined;
  const labels = issue.labelIds
    .map((id) => project.labels.find((l) => l.id === id)?.name)
    .filter((name): name is string => Boolean(name));
  const url = `${window.location.origin}${issuePath(project.project.ref, issue.sequenceNumber)}`;

  const branch = await buildIssueBranchName(issue, user);

  const tags: string[] = [
    `<issue identifier="${issue.identifier}">`,
    `<title>${issue.title}</title>`,
  ];
  tags.push('<description>', issue.description.trim(), '</description>');
  if (status) tags.push(`<status>${status}</status>`);
  if (issue.priority) tags.push(`<priority>${issue.priority}</priority>`);
  if (type) tags.push(`<type>${type}</type>`);
  if (assignee) tags.push(`<assignee>${assignee}</assignee>`);
  if (delegate) tags.push(`<delegate>${delegate}</delegate>`);
  if (issue.startDate) tags.push(`<startDate>${issue.startDate}</startDate>`);
  if (issue.dueDate) tags.push(`<dueDate>${issue.dueDate}</dueDate>`);
  for (const label of labels) tags.push(`<label>${label}</label>`);
  tags.push(`<project name="${project.project.name}"/>`);
  tags.push(`<url>${url}</url>`);
  tags.push('</issue>');

  return [
    `Work on Itsaplan issue ${issue.identifier}:`,
    '',
    `Suggested branch name: ${branch}`,
    '',
    tags.join('\n'),
  ].join('\n');
}