-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AXON-31] URI handler rework & unit test setup
* Add a new URI handler implemntation * Add necessary config for unit-testing vscode api * Decouple as much as possible * Fork the initialization to put the new router under a feature flag
- Loading branch information
1 parent
3ced5bc
commit 0375e02
Showing
31 changed files
with
980 additions
and
35 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,7 @@ | ||
/* eslint-disable @typescript-eslint/no-require-imports */ | ||
module.exports = { | ||
...require('jest-mock-vscode').createVSCodeMock(jest), | ||
env: { | ||
uriScheme: 'vscode' | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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,47 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { CheckoutBranchUriHandlerAction } from './checkoutBranch'; | ||
|
||
describe('CheckoutBranchUriHandlerAction', () => { | ||
const mockAnalyticsApi = { | ||
fireDeepLinkEvent: jest.fn(), | ||
}; | ||
const mockCheckoutHelper = { | ||
checkoutRef: jest.fn().mockResolvedValue(true), | ||
}; | ||
let action: CheckoutBranchUriHandlerAction; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
action = new CheckoutBranchUriHandlerAction(mockCheckoutHelper as any, mockAnalyticsApi as any); | ||
}); | ||
|
||
describe('handle', () => { | ||
it('throws if required query params are missing', async () => { | ||
await expect(action.handle(Uri.parse('https://some-uri/checkoutBranch'))).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=...&ref=...')), | ||
).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=...&refType=...')), | ||
).rejects.toThrow(); | ||
await expect( | ||
action.handle(Uri.parse('https://some-uri/checkoutBranch?ref=...&refType=...')), | ||
).rejects.toThrow(); | ||
}); | ||
|
||
it('checks out the branch and fires an event on success', async () => { | ||
mockCheckoutHelper.checkoutRef.mockResolvedValue(true); | ||
await action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=one&ref=two&refType=three')); | ||
|
||
expect(mockCheckoutHelper.checkoutRef).toHaveBeenCalledWith('one', 'two', 'three', ''); | ||
expect(mockAnalyticsApi.fireDeepLinkEvent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows an error message on failure', async () => { | ||
mockCheckoutHelper.checkoutRef.mockRejectedValue(new Error('oh no')); | ||
await action.handle(Uri.parse('https://some-uri/checkoutBranch?cloneUrl=one&ref=two&refType=three')); | ||
|
||
expect(window.showErrorMessage).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,53 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { isAcceptedBySuffix, UriHandlerAction } from '../uriHandlerAction'; | ||
import { CheckoutHelper } from '../../bitbucket/interfaces'; | ||
import { AnalyticsApi } from '../../lib/analyticsApi'; | ||
import { Logger } from '../../logger'; | ||
|
||
/** | ||
* Use a deep link to checkout a branch | ||
* | ||
* Expected link: | ||
* `vscode://atlassian.atlascode/checkoutBranch?cloneUrl=...&ref=...&refType=...` | ||
* | ||
* Query params: | ||
* - `cloneUrl`: the clone URL of the repository | ||
* - `ref`: the ref to check out | ||
* - `refType`: the type of ref to check out | ||
* - `sourceCloneUrl`: (optional) the clone URL of the source repository (for branches originating from a forked repo) | ||
*/ | ||
export class CheckoutBranchUriHandlerAction implements UriHandlerAction { | ||
constructor( | ||
private bitbucketHelper: CheckoutHelper, | ||
private analyticsApi: AnalyticsApi, | ||
) {} | ||
|
||
isAccepted(uri: Uri): boolean { | ||
return isAcceptedBySuffix(uri, 'checkoutBranch'); | ||
} | ||
|
||
async handle(uri: Uri) { | ||
const query = new URLSearchParams(uri.query); | ||
const cloneUrl = decodeURIComponent(query.get('cloneUrl') || ''); | ||
const sourceCloneUrl = decodeURIComponent(query.get('sourceCloneUrl') || ''); //For branches originating from a forked repo | ||
const ref = query.get('ref'); | ||
const refType = query.get('refType'); | ||
if (!ref || !cloneUrl || !refType) { | ||
throw new Error(`Query params are missing data: ${query}`); | ||
} | ||
|
||
try { | ||
const success = await this.bitbucketHelper.checkoutRef(cloneUrl, ref, refType, sourceCloneUrl); | ||
|
||
if (success) { | ||
this.analyticsApi.fireDeepLinkEvent( | ||
decodeURIComponent(query.get('source') || 'unknown'), | ||
'checkoutBranch', | ||
); | ||
} | ||
} catch (e) { | ||
Logger.debug('error checkout out branch:', e); | ||
window.showErrorMessage('Error checkout out branch (check log for details)'); | ||
} | ||
} | ||
} |
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,45 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { CloneRepositoryUriHandlerAction } from './cloneRepository'; | ||
|
||
describe('CloneRepositoryUriHandlerAction', () => { | ||
const mockAnalyticsApi = { | ||
fireDeepLinkEvent: jest.fn(), | ||
}; | ||
const mockCheckoutHelper = { | ||
cloneRepository: jest.fn(), | ||
}; | ||
let action: CloneRepositoryUriHandlerAction; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
action = new CloneRepositoryUriHandlerAction(mockCheckoutHelper as any, mockAnalyticsApi as any); | ||
}); | ||
|
||
describe('isAccepted', () => { | ||
it('only accepts URIs ending with cloneRepository', () => { | ||
expect(action.isAccepted(Uri.parse('https://some-uri/cloneRepository'))).toBe(true); | ||
expect(action.isAccepted(Uri.parse('https://some-uri/otherThing'))).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('handle', () => { | ||
it('throws if required query params are missing', async () => { | ||
await expect(action.handle(Uri.parse('https://some-uri/cloneRepository'))).rejects.toThrow(); | ||
}); | ||
|
||
it('clones the repo and fires an event on success', async () => { | ||
mockCheckoutHelper.cloneRepository.mockResolvedValue(null); | ||
await action.handle(Uri.parse('https://some-uri/cloneRepository?q=one')); | ||
|
||
expect(mockCheckoutHelper.cloneRepository).toHaveBeenCalledWith('one'); | ||
expect(mockAnalyticsApi.fireDeepLinkEvent).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows an error message on failure', async () => { | ||
mockCheckoutHelper.cloneRepository.mockRejectedValue(new Error('oh no')); | ||
await action.handle(Uri.parse('https://some-uri/cloneRepository?q=one')); | ||
|
||
expect(window.showErrorMessage).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
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,44 @@ | ||
import { Uri, window } from 'vscode'; | ||
import { isAcceptedBySuffix, UriHandlerAction } from '../uriHandlerAction'; | ||
import { CheckoutHelper } from '../../bitbucket/interfaces'; | ||
import { AnalyticsApi } from '../../lib/analyticsApi'; | ||
import { Logger } from '../../logger'; | ||
|
||
/** | ||
* Use a deep link to clone a repository | ||
* | ||
* Expected link: | ||
* `vscode://atlassian.atlascode/cloneRepository?q=...` | ||
* | ||
* Query params: | ||
* - `q`: the clone URL of the repository | ||
*/ | ||
export class CloneRepositoryUriHandlerAction implements UriHandlerAction { | ||
constructor( | ||
private bitbucketHelper: CheckoutHelper, | ||
private analyticsApi: AnalyticsApi, | ||
) {} | ||
|
||
isAccepted(uri: Uri): boolean { | ||
return isAcceptedBySuffix(uri, 'cloneRepository'); | ||
} | ||
|
||
async handle(uri: Uri) { | ||
const query = new URLSearchParams(uri.query); | ||
const repoUrl = decodeURIComponent(query.get('q') || ''); | ||
if (!repoUrl) { | ||
throw new Error(`Cannot parse clone URL from: ${query}`); | ||
} | ||
|
||
try { | ||
await this.bitbucketHelper.cloneRepository(repoUrl); | ||
this.analyticsApi.fireDeepLinkEvent( | ||
decodeURIComponent(query.get('source') || 'unknown'), | ||
'cloneRepository', | ||
); | ||
} catch (e) { | ||
Logger.debug('error cloning repository:', e); | ||
window.showErrorMessage('Error cloning repository (check log for details)'); | ||
} | ||
} | ||
} |
Oops, something went wrong.