Skip to content

Commit

Permalink
feat: use repository info to build permalink (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche authored Sep 30, 2024
1 parent 0be0c38 commit 90314a2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
19 changes: 11 additions & 8 deletions src/repository.test.js → src/repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { describe, expect, it } from 'vitest'
import { buildPermalink, guessRepositoryInfo, guessRepositorySubdir } from './repository.js'
import { Host, PermalinkFn } from './types.js'

describe('buildPermalink', () => {
it('builds a permalink with a custom function', () => {
const permalink = ({ filePath, lineNumber }) =>
it('uses the custom permalink function if it is present', () => {
const permalink: PermalinkFn = ({ filePath, lineNumber }) =>
`https://gitlab.com/cherrypush/cherry-cli/blob/HEAD/${filePath}${lineNumber ? `#L${lineNumber}` : ''}`

const projectName = 'cherrypush/cherry-cli'
const filePath = 'src/permalink.js'
const lineNumber = 1

const result = buildPermalink(permalink, projectName, filePath, lineNumber)

const repositoryInfo = { host: Host.Github, owner: 'owner', name: 'name', subdir: '' }
const result = buildPermalink(permalink, repositoryInfo, 'src/permalink.js', 1)
expect(result).toBe('https://gitlab.com/cherrypush/cherry-cli/blob/HEAD/src/permalink.js#L1')
})

it('works for config files inside sub folders', () => {
const repositoryInfo = { host: Host.Github, owner: 'cherrypush', name: 'cherrypush.com', subdir: 'frontend' }
const result = buildPermalink(undefined, repositoryInfo, 'permalink.js', 1)
expect(result).toBe('https://github.com/cherrypush/cherrypush.com/blob/HEAD/frontend/permalink.js#L1')
})
})

describe('guessRepositoryInfo', () => {
Expand Down
23 changes: 18 additions & 5 deletions src/repository.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import path from 'path'
import { PermalinkFn, Repository } from './types.js'

export const buildRepoURL = (repository: Repository) =>
`https://${repository.host}/${repository.owner}/${repository.name}`

export const buildPermalink = (
/**
* Builds the permalink for a file in a remote repository.
*
* If a custom permalink function is provided, it will be used.
* Otherwise, we'll compose the permalink based on the repository information.
*/
export function buildPermalink(
permalink: PermalinkFn | undefined,
repository: Repository,
filePath: string,
lineNumber: number | undefined
) => {
if (!permalink) return `${buildRepoURL(repository)}/blob/HEAD/${filePath}${lineNumber ? `#L${lineNumber}` : ''}`

return permalink({ filePath, lineNumber })
) {
if (permalink) return permalink({ filePath, lineNumber })
return `${buildRepoURL(repository)}/blob/HEAD/${path.join(repository.subdir, filePath)}${lineNumber ? `#L${lineNumber}` : ''}`
}

/**
Expand Down Expand Up @@ -58,6 +64,13 @@ export async function guessRepositoryInfo({
throw new Error(`Could not guess repository info from remote URL: ${remoteUrl}`)
}

/**
* Guesses the subdirectory of the repository.
*
* We compare the config file path with the git project root to determine the subdirectory.
* For instance, if the config file is at /Users/fwuensche/projects/cherry-cli/another/subdir/config.js
* and the git project root is /Users/fwuensche/projects/cherry-cli, the subdirectory would be another/subdir.
*/
export function guessRepositorySubdir({
configFile,
projectRoot,
Expand Down

0 comments on commit 90314a2

Please sign in to comment.