.ts
TypeScript
(application/typescript)
// 1st-party
import type { ReqHandler } from "@ethicdevs/react-monolith";
// app
import type { RepositoryCountersDTO } from "../../types";
import { AppRoute, AppRouteParams } from "../../routes.defs";

const getRepositoryCounters: ReqHandler<
  AppRouteParams,
  AppRoute.REPOSITORY_COUNTERS_API
> = async (request, reply) => {
  const { orgSlug, repoSlug } = request.params;

  // total pulls in repository (PRs targeting this repo)
  const totalPulls = await request.prisma.pullRequest.count({
    where: {
      targetRepository: {
        organization: { slug: orgSlug },
        slug: repoSlug,
      },
    },
  });

  // for now provide pulls and zeros
  const counters: RepositoryCountersDTO = {
    pulls: totalPulls,
    tests: 0,
    builds: 0,
    issues: 0,
    apiRefSymbols: 0,
    helpCenterNotifs: 0,
  };

  reply.send(JSON.stringify(counters) + "\n");
};

export default getRepositoryCounters;

export const APIControllers = {
  getRepositoryCounters,
};