Skip to content

Commit

Permalink
Merge pull request #20 from zanerock/work-liquid-labs/github-toolkit/19
Browse files Browse the repository at this point in the history
Add getGitHubOrgAndProject helper
  • Loading branch information
zanerock authored Oct 4, 2023
2 parents 9c50749 + 5ba1ab4 commit 0d84b8c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/get-github-org-and-project.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const urlRegEx = /github.com\/([^/]+)\/([^.]+)/

const getGitHubOrgAndProject = ({ packageJSON, require }) => {
const { repository } = packageJSON

const url = typeof repository === 'string' ? repository : repository.url

const [, org, project] = url.match(urlRegEx) || []

if (org === undefined && require === true) {
throw new Error(`Could not extract GitHub org and project from url '${url}'; this may not be a GitHub project.`)
}

return { org, project }
}

export { getGitHubOrgAndProject }
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './access-lib'
export * from './branch-lib'
export * from './determine-current-release'
export * from './get-github-org-and-project'
export * from './issues-lib'
export * from './labels-lib'
export * from './milestones-lib'
26 changes: 26 additions & 0 deletions src/test/get-github-org-and-project.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* global describe expect test */

import { getGitHubOrgAndProject } from '../get-github-org-and-project'

// requires user to have API access
describe('getGitHubOrgAndProject', () => {
test('works with string repositories', () => {
const packageJSON = { repository : 'git+ssh://[email protected]/acme/foo-bar.git' }
expect(getGitHubOrgAndProject({ packageJSON })).toEqual({ org : 'acme', project : 'foo-bar' })
})

test('works with object repositories', () => {
const packageJSON = { repository : { url : 'git+ssh://[email protected]/acme/foo-bar.git' } }
expect(getGitHubOrgAndProject({ packageJSON })).toEqual({ org : 'acme', project : 'foo-bar' })
})

test('returns undefined values when no match', () => {
const packageJSON = { repository : 'git+ssh://[email protected]/acme/foo-bar.git' }
expect(getGitHubOrgAndProject({ packageJSON })).toEqual({ org : undefined, project : undefined })
})

test('throws error when no match and required', () => {
const packageJSON = { repository : 'git+ssh://[email protected]/acme/foo-bar.git' }
expect(() => getGitHubOrgAndProject({ packageJSON, require : true })).toThrow()
})
})

0 comments on commit 0d84b8c

Please sign in to comment.