Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.5 KB
Raw Blame
import { GitMetadataParser } from './gitMetadataParser'

describe('GitMetadataParser', () => {
    describe('getCommitLink', () => {
        it.each([
            {
                description: 'should create commit link from SSH URL',
                remote_url: '[email protected]:user/repo.git',
                expected: 'https://github.com/user/repo/commit/commit-sha',
            },
            {
                description: 'should create commit link from HTTPS URL',
                remote_url: 'https://github.com/user/repo.git',
                expected: 'https://github.com/user/repo/commit/commit-sha',
            },
            {
                description: 'should create commit link for gitlab URL',
                remote_url: '[email protected]:posthog-bot-group/posthog-bot-project.git',
                expected: 'https://gitlab.com/posthog-bot-group/posthog-bot-project/-/commit/commit-sha',
            },
            {
                description: 'should create commit link for gitlab URL',
                remote_url: 'invalid_url',
                expected: undefined,
            },
            {
                description: 'should create commit link for gitlab URL',
                remote_url: '[email protected]:user/repo.git',
                expected: undefined,
            },
        ])('$description', ({ remote_url, expected }) => {
            const result = GitMetadataParser.getCommitLink(remote_url, 'commit-sha')
            expect(result).toBe(expected)
        })
    })

    describe('getBranchLink', () => {
        it.each([
            {
                description: 'should create commit link from SSH URL',
                remote_url: '[email protected]:user/repo.git',
                expected: 'https://github.com/user/repo/tree/branch-name',
            },
            {
                description: 'should create commit link from HTTPS URL',
                remote_url: 'https://github.com/user/repo.git',
                expected: 'https://github.com/user/repo/tree/branch-name',
            },
            {
                description: 'should create commit link for gitlab URL',
                remote_url: '[email protected]:posthog-bot-group/posthog-bot-project.git',
                expected: 'https://gitlab.com/posthog-bot-group/posthog-bot-project/-/tree/branch-name',
            },
        ])('$description', ({ remote_url, expected }) => {
            const result = GitMetadataParser.getBranchLink(remote_url, 'branch-name')
            expect(result).toBe(expected)
        })
    })

    describe('getGitHubRepositorySlug', () => {
        it.each([
            {
                description: 'SSH GitHub remote',
                remote_url: '[email protected]:PostHog/posthog.git',
                expected: 'PostHog/posthog',
            },
            {
                description: 'HTTPS GitHub remote',
                remote_url: 'https://github.com/PostHog/posthog.git',
                expected: 'PostHog/posthog',
            },
            { description: 'GitLab remote', remote_url: '[email protected]:group/project.git', expected: undefined },
            {
                description: 'remote with a credential',
                remote_url: 'https://x-access-token:[email protected]/PostHog/posthog.git',
                expected: undefined,
            },
            { description: 'missing remote', remote_url: undefined, expected: undefined },
        ])('returns the slug for a $description', ({ remote_url, expected }) => {
            expect(GitMetadataParser.getGitHubRepositorySlug(remote_url)).toBe(expected)
        })
    })
})