223 lines · 8.0 KB
import { env } from "cloudflare:workers";
import { batch, first, run, stmt } from "../db";
import { randomId } from "../crypto";
import { allocateIssueNumber, applyLabels, indexIssue } from "../issues";
import { loadRepoById } from "../repos";
import { githubApi, installationToken } from "./github-app";
import { isGithubRepoName } from "./github-sync";
import { loadIntegration, parseConfig } from "./store";

const PAGE_SIZE = 20;
const MAX_IMPORT_ITEMS = 2000;
const MAX_COMMENTS_PER_ITEM = 30;
const MAX_PAGE_BYTES = 2 * 1024 * 1024;
const MAX_BODY = 60_000;
const IMPORT_LABELS = [
  { name: "imported", color: "6e7681" },
  { name: "pull-request", color: "8957e5" },
];

export type ImportJobMessage = { kind: "integration.job"; jobId: string; page: number };

type GithubIssue = {
  number: number;
  title: string;
  body: string | null;
  state: "open" | "closed";
  html_url: string;
  comments: number;
  created_at: string;
  closed_at: string | null;
  user: { login: string } | null;
  pull_request?: { merged_at: string | null };
};

type GithubComment = { body: string | null; created_at: string; user: { login: string } | null; html_url: string };

type JobRow = { id: string; integration_id: string; status: string; progress: string };
type Progress = { imported: number; pages: number; done?: boolean };

function clipBody(text: string): string {