Skip to content

Commit

Permalink
feat(gitProvider.ts): add support for more Git repository providers a…
Browse files Browse the repository at this point in the history
…nd refactor composeFileUrl function to use the new fields in the Project model
  • Loading branch information
masterkain committed May 29, 2023
1 parent 5ec403b commit 9029584
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 35 deletions.
11 changes: 0 additions & 11 deletions __tests__/factories/prismaFactories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,13 @@ const chance = new Chance();
export const createProject = async (prismaInstance: PrismaClient, overrides?: Partial<Prisma.ProjectUncheckedCreateInput>): Promise<Project> => {
const defaultProject: Prisma.ProjectCreateInput = {
name: chance.company(),
api_key: chance.guid(),
organization: chance.company(),
};

const projectData = { ...defaultProject, ...overrides };
return await prismaInstance.project.create({ data: projectData });
};

export const projectAttributes = (overrides?: Partial<Prisma.ProjectUncheckedCreateInput>): Prisma.ProjectUncheckedCreateInput => {
const defaultProject: Prisma.ProjectUncheckedCreateInput = {
name: chance.company(),
api_key: chance.guid(),
organization: chance.company(),
};

return { ...defaultProject, ...overrides };
};

export const createNotice = async (prismaInstance: PrismaClient, project_id: string, overrides?: Partial<Prisma.NoticeUncheckedCreateInput>): Promise<Notice> => {
const defaultNotice: Prisma.NoticeUncheckedCreateInput = {
project_id: project_id,
Expand Down
82 changes: 82 additions & 0 deletions __tests__/lib/gitProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { prisma } from '@/lib/db';
import { composeFileUrl } from '@/lib/gitProvider';
import { describe, expect, test } from '@jest/globals';
import { Project } from '@prisma/client';

import { createProject } from '../factories/prismaFactories';

describe('composeFileUrl', () => {
test('should return the correct URL for GitHub', async () => {
const project: Project = await createProject(prisma, {
repo_provider: 'github',
repo_url: 'https://github.com/org/repo',
repo_branch: 'main',
});
const filePath = 'path/to/file';
const lineNumber = 5;

const expectedUrl = 'https://github.com/org/repo/blob/main/path/to/file#L5';
const url = composeFileUrl(project, filePath, lineNumber);

expect(url).toBe(expectedUrl);
});

test('should return the correct URL for Bitbucket', async () => {
const project: Project = await createProject(prisma, {
repo_provider: 'bitbucket',
repo_url: 'https://bitbucket.org/org/repo',
repo_branch: 'branch',
});
const filePath = 'path/to/file';
const lineNumber = 5;

const expectedUrl = 'https://bitbucket.org/org/repo/src/branch/path/to/file#lines-5';
const url = composeFileUrl(project, filePath, lineNumber);

expect(url).toBe(expectedUrl);
});

test('should return the correct URL for GitLab', async () => {
const project: Project = await createProject(prisma, {
repo_provider: 'gitlab',
repo_url: 'https://gitlab.com/org/repo',
repo_branch: 'branch',
});
const filePath = 'path/to/file';
const lineNumber = 5;

const expectedUrl = 'https://gitlab.com/org/repo/-/blob/branch/path/to/file#L5';
const url = composeFileUrl(project, filePath, lineNumber);

expect(url).toBe(expectedUrl);
});

test('should return the correct URL without line number', async () => {
const project: Project = await createProject(prisma, {
repo_provider: 'github',
repo_url: 'https://github.com/org/repo',
repo_branch: 'main',
});
const filePath = 'path/to/file';

const expectedUrl = 'https://github.com/org/repo/blob/main/path/to/file';
const url = composeFileUrl(project, filePath);

expect(url).toBe(expectedUrl);
});

test('should return an empty string for unknown repository provider', async () => {
const project: Project = await createProject(prisma, {
repo_provider: 'unknown',
repo_url: 'https://example.com/repo',
repo_branch: 'main',
});
const filePath = 'path/to/file';
const lineNumber = 5;

const expectedUrl = '#';
const url = composeFileUrl(project, filePath, lineNumber);

expect(url).toBe(expectedUrl);
});
});
74 changes: 50 additions & 24 deletions lib/gitProvider.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
import { Project } from "@prisma/client";

export function composeRepoUrl(project: Project): string {
let baseUrl: string;

switch (project.repo_provider) {
case 'github':
baseUrl = 'https://github.com';
break;
case 'bitbucket':
baseUrl = 'https://bitbucket.org';
break;
default:
return '#';
}

return `${baseUrl}/${project.organization}/${project.name}`;
}

export function composeFileUrl(
project: Project,
filePath: string,
lineNumber?: number
): string {
const repoFullUrl = composeRepoUrl(project);
if (repoFullUrl === '#') {
return '#';
}
const repoUrl = project.repo_url?.toLowerCase() || '';
const repoBranch = project.repo_branch?.toLowerCase() || '';
const filePathLower = filePath.toLowerCase();

const url = `${repoFullUrl}/blob/${project.repo_branch}/${filePath}`;
let url: string;

if (lineNumber) {
return `${url}#L${lineNumber}`;
switch (project.repo_provider?.toLowerCase()) {
case 'github':
url = `${repoUrl}/blob/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
case 'bitbucket':
url = `${repoUrl}/src/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#lines-${lineNumber}`;
}
break;
case 'gitlab':
url = `${repoUrl}/-/blob/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
case 'gitkraken':
url = `${repoUrl}/tree/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
case 'gitea':
url = `${repoUrl}/src/branch/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
case 'gogs':
url = `${repoUrl}/blob/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
case 'gitter':
url = `${repoUrl}/blob/${repoBranch}/${filePathLower}`;
if (lineNumber) {
url += `#L${lineNumber}`;
}
break;
default:
url = '#';
break;
}

return url;
Expand Down

0 comments on commit 9029584

Please sign in to comment.