Web API for code.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

48 line
2.1 KiB

  1. import {constants} from 'http2';
  2. import * as codeCore from '@modal/code-core';
  3. import {HttpError} from '../../packages/fastify-compliant-http-errors';
  4. export interface GitService {
  5. createRepo(options: codeCore.git.CreateRepoData, user?: codeCore.common.User): Promise<codeCore.git.Repo>;
  6. deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void>
  7. }
  8. export class UnauthorizedToCreateRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unauthorized to Create Repo') {}
  9. export class UnauthorizedToDeleteRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unauthorized to Delete Repo') {}
  10. export class UnableToCreateRepoError extends HttpError(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, 'Unable to Create Repo') {}
  11. export class UnableToDeleteRepoError extends HttpError(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, 'Unable to Delete Repo') {}
  12. export class GitServiceImpl implements GitService {
  13. private readonly coreGitService: codeCore.git.GitService;
  14. constructor() {
  15. this.coreGitService = new codeCore.git.GitServiceImpl();
  16. }
  17. async createRepo(options: codeCore.git.CreateRepoData, user?: codeCore.common.User): Promise<codeCore.git.Repo> {
  18. if (user) {
  19. let newRepo: codeCore.git.Repo;
  20. try {
  21. newRepo = await this.coreGitService.create(options, user);
  22. } catch (causeRaw) {
  23. const cause = causeRaw as Error;
  24. throw new UnableToCreateRepoError('Something went wrong while creating the repo.', { cause, });
  25. }
  26. return newRepo;
  27. }
  28. throw new UnauthorizedToCreateRepoError('Could not create repo with insufficient authorization.');
  29. }
  30. async deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void> {
  31. if (user) {
  32. try {
  33. await this.coreGitService.delete(repoId, user);
  34. } catch (causeRaw) {
  35. const cause = causeRaw as Error;
  36. throw new UnableToDeleteRepoError('Something went wrong while deleting the repo.', { cause, });
  37. }
  38. }
  39. throw new UnauthorizedToDeleteRepoError('Could not delete repo with insufficient authorization.');
  40. }
  41. }