63 lines · 1.9 KB
import { all, run } from "./db";
import { randomId } from "./crypto";
import { onCommitStatus } from "./merge-automation";
import type { StatusState } from "~/lib/types";

export type CommitStatusRow = {
  id: string;
  repo_id: string;
  sha: string;
  context: string;
  state: StatusState;
  description: string | null;
  target_url: string | null;
  created_at: number;
  updated_at: number;
};

export async function setCommitStatus(input: {
  repoId: string;
  sha: string;
  context: string;
  state: StatusState;
  description: string | null;
  targetUrl: string | null;
}): Promise<void> {
  const now = Date.now();
  await run(
    `INSERT INTO commit_statuses (id, repo_id, sha, context, state, description, target_url, created_at, updated_at)
     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
     ON CONFLICT (repo_id, sha, context) DO UPDATE SET
       state = excluded.state, description = excluded.description, target_url = excluded.target_url, updated_at = excluded.updated_at`,
    randomId(),
    input.repoId,
    input.sha,
    input.context,
    input.state,
    input.description,
    input.targetUrl,
    now,
    now,