import type { ServiceMethodFactory } from "@ethicdevs/react-monolith";
import { Repository, ResourceVisibility } from "@prisma/client";
import { Env } from "../../env";
import { getEnv } from "../../utils/server";
import type { RepositoryServiceDeps } from "./types";
const makeGetRepositoryHTTPCloneUrl: ServiceMethodFactory<
RepositoryServiceDeps,
[Repository],
Promise<string>
> = ({ request }) => {
const env = getEnv();
return async (repo) => {
const parentOrg = await request.prisma.organization.findUnique({
where: {
id: repo.organizationId,
},
});
if (parentOrg == null) {
throw new Error(
`Could not find the parent organization for project "${repo.slug}".`
);
}
const authCredentials =
repo.visibility === ResourceVisibility.PRIVATE
? `${parentOrg.slug}:secret@`
: "";
const baseUrl =
env === "development"
? `${Env.DEPLOYMENT_SCHEME}://${authCredentials}${Env.DEPLOYMENT_DOMAIN}:${Env.PORT}`
: `${Env.DEPLOYMENT_SCHEME}://${authCredentials}${Env.DEPLOYMENT_DOMAIN}`;
return `${baseUrl}/${parentOrg.slug}/${repo.slug}.git`;
};
};
export default makeGetRepositoryHTTPCloneUrl;