Skip to content

Commit 27d8a65

Browse files
committed
feat(utils): support GitLab report links
1 parent 9ef1887 commit 27d8a65

File tree

5 files changed

+123
-26
lines changed

5 files changed

+123
-26
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { type EnvironmentType, SUPPORTED_ENVIRONMENTS } from './types';
2+
3+
const environmentChecks: Record<EnvironmentType, () => boolean> = {
4+
vscode: () => process.env['TERM_PROGRAM'] === 'vscode',
5+
github: () => process.env['GITHUB_ACTIONS'] === 'true',
6+
gitlab: () => process.env['GITLAB_CI'] === 'true',
7+
other: () => true,
8+
};
9+
10+
export function getEnvironmentType(): EnvironmentType {
11+
return (
12+
SUPPORTED_ENVIRONMENTS.find(env => environmentChecks[env]()) ?? 'other'
13+
);
14+
}
15+
16+
export function getGitHubBaseUrl(): string {
17+
return `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHUB_REPOSITORY']}/blob/${process.env['GITHUB_SHA']}`;
18+
}
19+
20+
export function getGitLabBaseUrl(): string {
21+
return `${process.env['CI_SERVER_URL']}/${process.env['CI_PROJECT_PATH']}/-/blob/${process.env['CI_COMMIT_SHA']}`;
22+
}

packages/utils/src/lib/reports/formatting.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ import {
1616
getColumnAlignments,
1717
rowToStringArray,
1818
} from '../text-formats/table';
19-
import { getEnvironmentType, getGitHubBaseUrl } from './ide-environment';
19+
import {
20+
getEnvironmentType,
21+
getGitHubBaseUrl,
22+
getGitLabBaseUrl,
23+
} from './environment-type';
2024
import type { MdReportOptions } from './types';
2125

2226
export function tableSection(
@@ -120,6 +124,20 @@ export function formatGitHubLink(
120124
return `${baseUrl}/${file}#${lineRange}`;
121125
}
122126

127+
export function formatGitLabLink(
128+
file: string,
129+
position: SourceFileLocation['position'],
130+
): string {
131+
const baseUrl = getGitLabBaseUrl();
132+
if (!position) {
133+
return `${baseUrl}/${file}`;
134+
}
135+
const { startLine, endLine } = position;
136+
const lineRange =
137+
endLine && startLine !== endLine ? `${startLine}-${endLine}` : startLine;
138+
return `${baseUrl}/${file}#L${lineRange}`;
139+
}
140+
123141
export function formatFileLink(
124142
file: string,
125143
position: SourceFileLocation['position'],
@@ -133,6 +151,8 @@ export function formatFileLink(
133151
return position ? `${relativePath}#L${position.startLine}` : relativePath;
134152
case 'github':
135153
return formatGitHubLink(file, position);
154+
case 'gitlab':
155+
return formatGitLabLink(file, position);
136156
default:
137157
return relativePath;
138158
}

packages/utils/src/lib/reports/formatting.unit.test.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { toUnixPath } from '../transform';
33
import {
44
formatFileLink,
55
formatGitHubLink,
6+
formatGitLabLink,
67
formatSourceLine,
78
linkToLocalSourceForIde,
89
metaDescription,
@@ -226,6 +227,57 @@ describe('formatGitHubLink', () => {
226227
});
227228
});
228229

230+
describe('formatGitLabLink', () => {
231+
beforeEach(() => {
232+
vi.stubEnv('TERM_PROGRAM', '');
233+
vi.stubEnv('GITHUB_ACTIONS', 'false');
234+
vi.stubEnv('GITLAB_CI', 'true');
235+
vi.stubEnv('CI_SERVER_URL', 'https://gitlab.com');
236+
vi.stubEnv('CI_PROJECT_PATH', 'user/repo');
237+
vi.stubEnv('CI_COMMIT_SHA', '1234567890abcdef');
238+
});
239+
240+
it.each([
241+
[
242+
{ startLine: 2 },
243+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2',
244+
],
245+
[
246+
{ startLine: 2, endLine: 5 },
247+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2-5',
248+
],
249+
[
250+
{ startLine: 2, startColumn: 1 },
251+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2',
252+
],
253+
[
254+
{ startLine: 2, endLine: 2, startColumn: 1, endColumn: 5 },
255+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2',
256+
],
257+
[
258+
{ startLine: 2, endLine: 5, startColumn: 1, endColumn: 6 },
259+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2-5',
260+
],
261+
[
262+
{ startLine: 2, endLine: 2, startColumn: 1, endColumn: 1 },
263+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2',
264+
],
265+
])(
266+
'should generate a GitLab repository link for the file with position %o',
267+
(position, expected) => {
268+
expect(formatGitLabLink(toUnixPath('src/index.ts'), position)).toBe(
269+
expected,
270+
);
271+
},
272+
);
273+
274+
it('should generate a GitLab repository link for the file when the position is undefined', () => {
275+
expect(formatGitLabLink(toUnixPath('src/index.ts'), undefined)).toBe(
276+
'https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts',
277+
);
278+
});
279+
});
280+
229281
describe('formatFileLink', () => {
230282
it('should return a GitHub repository link when running in GitHub Actions', () => {
231283
vi.stubEnv('TERM_PROGRAM', '');
@@ -245,6 +297,25 @@ describe('formatFileLink', () => {
245297
);
246298
});
247299

300+
it('should return a GitLab repository link when running in GitLab CI/CD', () => {
301+
vi.stubEnv('TERM_PROGRAM', '');
302+
vi.stubEnv('GITHUB_ACTIONS', 'false');
303+
vi.stubEnv('GITLAB_CI', 'true');
304+
vi.stubEnv('CI_SERVER_URL', 'https://gitlab.com');
305+
vi.stubEnv('CI_PROJECT_PATH', 'user/repo');
306+
vi.stubEnv('CI_COMMIT_SHA', '1234567890abcdef');
307+
308+
expect(
309+
formatFileLink(
310+
toUnixPath('src/index.ts'),
311+
{ startLine: 2 },
312+
toUnixPath('.code-pushup'),
313+
),
314+
).toBe(
315+
`https://gitlab.com/user/repo/-/blob/1234567890abcdef/src/index.ts#L2`,
316+
);
317+
});
318+
248319
it.each([
249320
[{ startLine: 2 }, '../src/index.ts#L2'],
250321
[{ startLine: 2, endLine: 5 }, '../src/index.ts#L2'],
@@ -276,9 +347,10 @@ describe('formatFileLink', () => {
276347
).toBe('../src/index.ts');
277348
});
278349

279-
it('should return a relative file path when the environment is neither VS Code nor GitHub', () => {
350+
it('should return a relative file path when the environment is neither VS Code nor GitHub, nor GitLab', () => {
280351
vi.stubEnv('TERM_PROGRAM', '');
281352
vi.stubEnv('GITHUB_ACTIONS', 'false');
353+
vi.stubEnv('GITLAB_CI', 'false');
282354
expect(
283355
formatFileLink(
284356
toUnixPath('src/index.ts'),

packages/utils/src/lib/reports/ide-environment.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/utils/src/lib/reports/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ export type DiffOutcome = 'positive' | 'negative' | 'mixed' | 'unchanged';
3434

3535
export type MdReportOptions = Pick<PersistConfig, 'outputDir'>;
3636

37-
export type IdeEnvironment = 'vscode' | 'github' | 'other';
37+
export const SUPPORTED_ENVIRONMENTS = [
38+
'vscode',
39+
'github',
40+
'gitlab',
41+
'other',
42+
] as const;
43+
export type EnvironmentType = (typeof SUPPORTED_ENVIRONMENTS)[number];

0 commit comments

Comments
 (0)