-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from zanerock/work-liquid-labs/github-toolkit/19
Add getGitHubOrgAndProject helper
- Loading branch information
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) |