sailfork / t3code-github / packages/ source-control-forgejo/ src/ server/ forgejoPullRequestJson.ts

import { toIsoUtc } from "@t3tools/source-control-core/server/pullRequestUtils";
import * as Schema from "effect/Schema";
import type {
  PullRequestActor,
  PullRequestCheck,
  PullRequestCheckStatus,
  PullRequestComment,
  PullRequestCommit,
  PullRequestReaction,
  PullRequestReactionContent,
  PullRequestReviewThread,
} from "@t3tools/contracts";
import type { ProviderChangeRequest } from "@t3tools/source-control-core/server/PullRequestProvider";
import { dedupeChecks } from "@t3tools/source-control-core/server/pullRequestChecks";

export const ForgejoUser = Schema.Struct({
  login: Schema.String,
  full_name: Schema.optional(Schema.NullOr(Schema.String)),
  avatar_url: Schema.optional(Schema.NullOr(Schema.String)),
});
export const ForgejoLabel = Schema.Struct({
  id: Schema.Int,
  name: Schema.String,
  color: Schema.optional(Schema.NullOr(Schema.String)),
  description: Schema.optional(Schema.NullOr(Schema.String)),
});
export const ForgejoRepository = Schema.Struct({
  full_name: Schema.String,
  permissions: Schema.optional(Schema.Struct({ push: Schema.Boolean, admin: Schema.Boolean })),
  archived: Schema.optional(Schema.Boolean),
  allow_merge_commits: Schema.optional(Schema.Boolean),
  allow_squash_merge: Schema.optional(Schema.Boolean),
  allow_rebase: Schema.optional(Schema.Boolean),
  allow_rebase_update: Schema.optional(Schema.Boolean),
  default_delete_branch_after_merge: Schema.optional(Schema.Boolean),
});
const Branch = Schema.Struct({
  ref: Schema.String,
  sha: Schema.String,
  repo: Schema.NullOr(ForgejoRepository),
});
export const ForgejoPullRequest = Schema.Struct({
  number: Schema.Int,
  title: Schema.String,
  body: Schema.NullOr(Schema.String),
  html_url: Schema.String,
  user: Schema.NullOr(ForgejoUser),
  state: Schema.String,
  draft: Schema.optional(Schema.Boolean),
  merged: Schema.Boolean,
  mergeable: Schema.optional(Schema.Boolean),
  is_locked: Schema.optional(Schema.Boolean),
  head: Branch,
  base: Branch,
  merge_base: Schema.optional(Schema.String),
  created_at: Schema.String,
  updated_at: Schema.String,
  closed_at: Schema.NullOr(Schema.String),
  merged_at: Schema.NullOr(Schema.String),
  additions: Schema.optional(Schema.NullOr(Schema.Int)),
  deletions: Schema.optional(Schema.NullOr(Schema.Int)),
  changed_files: Schema.optional(Schema.NullOr(Schema.Int)),
  comments: Schema.optional(Schema.Int),
  labels: Schema.NullOr(Schema.Array(ForgejoLabel)),
  requested_reviewers: Schema.optional(Schema.NullOr(Schema.Array(ForgejoUser))),
});
export const ForgejoComment = Schema.Struct({
  id: Schema.Int,
  body: Schema.String,
  user: Schema.NullOr(ForgejoUser),
  created_at: Schema.String,
  updated_at: Schema.optional(Schema.NullOr(Schema.String)),
  html_url: Schema.optional(Schema.String),
});
export const ForgejoReview = Schema.Struct({
  id: Schema.Int,
  body: Schema.String,
  user: Schema.NullOr(ForgejoUser),
  state: Schema.String,
  dismissed: Schema.optional(Schema.Boolean),
  submitted_at: Schema.String,
  html_url: Schema.optional(Schema.String),
  comments_count: Schema.Int,
});
export const ForgejoReviewComment = Schema.Struct({
  ...ForgejoComment.fields,
  path: Schema.String,
  position: Schema.Int,
  original_position: Schema.Int,
  commit_id: Schema.String,
  original_commit_id: Schema.String,
  resolver: Schema.NullOr(ForgejoUser),
});
export const ForgejoCommit = Schema.Struct({
  sha: Schema.String,
  author: Schema.NullOr(ForgejoUser),
  commit: Schema.Struct({
    message: Schema.String,
    committer: Schema.Struct({ date: Schema.String }),
  }),
  parents: Schema.Array(Schema.Struct({ sha: Schema.String })),
  stats: Schema.optional(
    Schema.NullOr(Schema.Struct({ additions: Schema.Int, deletions: Schema.Int })),
  ),
});
export const ForgejoStatus = Schema.Struct({
  id: Schema.optional(Schema.Int),
  context: Schema.String,
  status: Schema.String,
  description: Schema.NullOr(Schema.String),
  target_url: Schema.NullOr(Schema.String),
  updated_at: Schema.String,
});
export const ForgejoReaction = Schema.Struct({
  content: Schema.String,
  user: Schema.NullOr(ForgejoUser),
});

export function forgejoActor(
  user: typeof ForgejoUser.Type | null | undefined,
): PullRequestActor | null {
  return user?.login
    ? { login: user.login, name: user.full_name || null, avatarUrl: user.avatar_url || null }
    : null;
}

export function forgejoChangeRequest(pr: typeof ForgejoPullRequest.Type) {
  return {
    number: pr.number,
    title: pr.title,
    url: pr.html_url,
    author: forgejoActor(pr.user),
    headBranch: pr.head.ref,
    baseBranch: pr.base.ref,
    headRepositoryNameWithOwner: pr.head.repo?.full_name ?? null,
    state: pr.merged ? "merged" : pr.state === "closed" ? "closed" : "open",
    isDraft: pr.draft ?? /^(?:\[WIP\]|WIP:)/i.test(pr.title),
    // A false value also covers drafts and unfinished or failed conflict checks.
    mergeability: pr.mergeable === true ? "mergeable" : "unknown",
    additions: pr.additions ?? 0,
    deletions: pr.deletions ?? 0,
    createdAt: toIsoUtc(pr.created_at),
    updatedAt: toIsoUtc(pr.updated_at),
    closedAt: pr.closed_at === null ? null : toIsoUtc(pr.closed_at),
    mergedAt: pr.merged_at === null ? null : toIsoUtc(pr.merged_at),
    reviewRequestLogins: (pr.requested_reviewers ?? []).map((user) => user.login),
    labels: (pr.labels ?? []).map((label) => ({ name: label.name, color: label.color ?? null })),
  } satisfies ProviderChangeRequest;
}

export function forgejoComment(comment: typeof ForgejoComment.Type): PullRequestComment {
  const createdAt = toIsoUtc(comment.created_at);
  const editedAt = comment.updated_at == null ? undefined : toIsoUtc(comment.updated_at);
  return {
    id: String(comment.id),
    kind: "issue-comment",
    author: forgejoActor(comment.user),
    body: comment.body,
    createdAt,
    ...(editedAt === undefined || editedAt === createdAt ? {} : { editedAt }),
    url: comment.html_url || null,
    path: null,
    reviewState: null,
  };
}

export function forgejoReview(review: typeof ForgejoReview.Type): PullRequestComment {
  return {
    id: `review:${review.id}`,
    kind: "review",
    author: forgejoActor(review.user),
    body: review.body,
    createdAt: toIsoUtc(review.submitted_at),
    url: review.html_url || null,
    path: null,
    reviewState:
      review.dismissed === true
        ? "DISMISSED"
        : review.state === "REQUEST_CHANGES"
          ? "CHANGES_REQUESTED"
          : review.state === "COMMENT"
            ? "COMMENTED"
            : review.state,
  };
}

export function forgejoReviewThread(
  comment: typeof ForgejoReviewComment.Type,
): PullRequestReviewThread {
  const oldSide = comment.position === 0 && comment.original_position > 0;
  const line = oldSide ? comment.original_position : comment.position;
  return {
    id: String(comment.id),
    path: comment.path,
    line: line > 0 ? line : null,
    side: oldSide ? "left" : "right",
    isResolved: comment.resolver !== null,
    isOutdated: false,
    comments: [forgejoComment(comment)],
  };
}

export function forgejoCommit(commit: typeof ForgejoCommit.Type): PullRequestCommit {
  const author = forgejoActor(commit.author);
  return {
    oid: commit.sha,
    messageHeadline: commit.commit.message.split("\n")[0] ?? "",
    committedDate: toIsoUtc(commit.commit.committer.date),
    authors: author ? [author] : [],
    ...(commit.stats
      ? { additions: commit.stats.additions, deletions: commit.stats.deletions }
      : {}),
  };
}

// Forgejo Actions reports a job that did not run as `skipped` and a cancelled one as `failure`,
// and `warning` is finished without being a pass. Only `pending` and states we do not know may
// hold a watch open.
function forgejoCheckStatus(state: string): PullRequestCheckStatus {
  switch (state) {
    case "success":
      return "success";
    case "failure":
    case "error":
      return "failure";
    case "skipped":
      return "skipped";
    case "warning":
      return "neutral";
    default:
      return "pending";
  }
}

export function forgejoChecks(
  statuses: ReadonlyArray<typeof ForgejoStatus.Type>,
  pullRequestUrl: string,
): ReadonlyArray<PullRequestCheck> {
  return dedupeChecks(
    statuses.map((status) => ({
      workflowName: null,
      at: status.updated_at,
      ...(status.id === undefined ? {} : { sequence: status.id }),
      check: {
        name: status.context || "check",
        description: status.description || null,
        url: status.target_url
          ? (URL.parse(status.target_url, pullRequestUrl)?.href ?? null)
          : null,
        status: forgejoCheckStatus(status.status),
      },
    })),
  );
}

export const FORGEJO_REACTIONS: Record<PullRequestReactionContent, string> = {
  "thumbs-up": "+1",
  "thumbs-down": "-1",
  laugh: "laugh",
  hooray: "hooray",
  confused: "confused",
  heart: "heart",
  rocket: "rocket",
  eyes: "eyes",
};
export function forgejoReactions(
  reactions: ReadonlyArray<typeof ForgejoReaction.Type>,
  viewer: string,
): ReadonlyArray<PullRequestReaction> {
  return Object.entries(FORGEJO_REACTIONS).flatMap(([content, emoji]) => {
    const matching = reactions.filter((reaction) => reaction.content === emoji);
    return matching.length === 0
      ? []
      : [
          {
            content: content as PullRequestReactionContent,
            count: matching.length,
            actors: matching.flatMap((reaction) =>
              reaction.user?.login && reaction.user.login !== viewer ? [reaction.user.login] : [],
            ),
            viewerHasReacted: matching.some((reaction) => reaction.user?.login === viewer),
          },
        ];
  });
}