Skip to content

Commit

Permalink
Merge pull request #96 from deinsoftware/dev
Browse files Browse the repository at this point in the history
fix: 🐛 fix open repo with ssh
  • Loading branch information
equiman authored Oct 19, 2023
2 parents 64a080c + 6def3dd commit ccd69c4
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 12 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ Fixed for any bug fixes.
Security to invite users to upgrade in case of vulnerabilities.
-->

## 2.5.1 - 2023/10/19

### Fixed

- get repository url when using ssh uri

### Added

- repository helpers unit test

## 2.5.0 - 2023/10/19

### Changed
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swpm",
"version": "2.5.0",
"version": "2.5.1",
"engines": {
"node": ">=16"
},
Expand Down
135 changes: 135 additions & 0 deletions src/helpers/repos.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { afterEach, it, describe, expect, vi } from 'vitest'
import { pathExists } from 'find-up'
import { getCommandResult } from './cmds.js'
import { getReposStatus, hasRepository } from './repos.js'

describe('getReposStatus()', () => {
vi.mock('./cmds.ts', async () => {
const mod = await vi.importActual<typeof import('./cmds.ts')>('./cmds.ts')
return {
...mod,
getCommandResult: vi.fn()
}
})

afterEach(() => {
vi.clearAllMocks()
vi.resetAllMocks()
})

it('should return ssh repository object', async () => {
// Mock the dependencies
vi.mocked(getCommandResult)
.mockReturnValueOnce('[email protected]:username/repo.git')
.mockReturnValueOnce('main')

// Call the function
const result = await getReposStatus()

// Assertions
expect(getCommandResult).toHaveBeenCalledTimes(2)
expect(result).toEqual({
url: 'https://github.com/username/repo',
provider: 'github',
current: 'main'
})

vi.mocked(getCommandResult).mockRestore()
})

it('should return ssh repository object without .git extension', async () => {
// Mock the dependencies
vi.mocked(getCommandResult)
.mockReturnValueOnce('[email protected]:username/repo')
.mockReturnValueOnce('main')

// Call the function
const result = await getReposStatus()

// Assertions
expect(getCommandResult).toHaveBeenCalledTimes(2)
expect(result).toEqual({
url: 'https://github.com/username/repo',
provider: 'github',
current: 'main'
})

vi.mocked(getCommandResult).mockRestore()
})

it('should return http repository object', async () => {
// Mock the dependencies
vi.mocked(getCommandResult)
.mockReturnValueOnce('https://github.com/username/repo.git')
.mockReturnValueOnce('main')

// Call the function
const result = await getReposStatus()

// Assertions
expect(getCommandResult).toHaveBeenCalledTimes(2)
expect(result).toEqual({
url: 'https://github.com/username/repo',
provider: 'github',
current: 'main'
})

vi.mocked(getCommandResult).mockRestore()
})

it('should return http repository object without .git extension', async () => {
// Mock the dependencies
vi.mocked(getCommandResult)
.mockReturnValueOnce('https://github.com/username/repo')
.mockReturnValueOnce('main')

// Call the function
const result = await getReposStatus()

// Assertions
expect(getCommandResult).toHaveBeenCalledTimes(2)
expect(result).toEqual({
url: 'https://github.com/username/repo',
provider: 'github',
current: 'main'
})

vi.mocked(getCommandResult).mockRestore()
})
})

describe('hasRepository()', () => {
vi.mock('find-up', async () => {
const mod = await vi.importActual<typeof import('find-up')>('find-up')

return {
...mod,
pathExists: await vi.fn()
}
})

const pathMock = vi.mocked(pathExists)

afterEach(() => {
vi.clearAllMocks()
vi.resetAllMocks()
})

it('should found a repository in current path', async () => {
vi.mocked(pathExists).mockResolvedValue(true)
const result = await hasRepository()
expect(pathMock).toHaveBeenCalledTimes(1)
expect(result).toBeTruthy()

vi.mocked(pathExists).mockRestore()
})

it('should found not found a repository in current path', async () => {
vi.mocked(pathExists).mockResolvedValue(false)
const result = await hasRepository()
expect(pathMock).toHaveBeenCalledTimes(1)
expect(result).toBeFalsy()

vi.mocked(pathExists).mockRestore()
})
})
21 changes: 14 additions & 7 deletions src/helpers/repos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,26 @@ const gitCurrentBranch = () => {
return getCommandResult({ command: 'git branch --show-current' })
}

export const getReposStatus = async () => {
const origin = getCommandResult({ command: 'git config remote.origin.url' })
const host = new URL(origin)?.hostname
const provider = host?.split('.').at(0) as Providers
const gitOrigin = () => {
return getCommandResult({ command: 'git config remote.origin.url' })
}

const config = await getProviderConfiguration({ id: provider })
export const getReposStatus = async () => {
const origin = gitOrigin()
const current = gitCurrentBranch()

let url = origin.replace(`git@${origin}`, `https://${origin}/`)
let url = origin
if (url.startsWith('git@')) {
url = url.split(':').join('/')
url = url.replace('git@', 'https://')
}
if (url.endsWith('.git')) {
url = url.slice(0, -4)
}

const current = gitCurrentBranch()
const { hostname } = new URL(url)
const provider = hostname?.split('.').at(0) as Providers
const config = await getProviderConfiguration({ id: provider })

const repository: Repository = {
...config,
Expand Down
8 changes: 4 additions & 4 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ export default defineConfig({
reporter: ['text', 'html', 'lcov'],
include: [...include],
exclude: [...exclude],
statements: 48,
branches: 75,
functions: 51,
lines: 48
statements: 52,
branches: 77,
functions: 55,
lines: 52
}
}
})

0 comments on commit ccd69c4

Please sign in to comment.