feat(techdocs): add edit button support for bitbucketServer

Signed-off-by: Ömer Faruk Doğan <8117265+omerfarukdogan@users.noreply.github.com>
This commit is contained in:
Ömer Faruk Doğan
2022-07-27 18:31:13 +03:00
parent 593a5e57bb
commit ad35364e97
6 changed files with 77 additions and 28 deletions
+14 -2
View File
@@ -113,9 +113,21 @@ export class ScmIntegrations implements ScmIntegrationRegistry {
}
byUrl(url: string | URL): ScmIntegration | undefined {
return Object.values(this.byType)
let candidates = Object.values(this.byType)
.map(i => i.byUrl(url))
.find(Boolean);
.filter(Boolean);
// Do not return deprecated integrations if there are other options
if (candidates.length > 1) {
const filteredCandidates = candidates.filter(
x => !(x instanceof BitbucketIntegration),
);
if (filteredCandidates.length !== 0) {
candidates = filteredCandidates;
}
}
return candidates[0];
}
byHost(host: string): ScmIntegration | undefined {
@@ -44,31 +44,58 @@ describe('BitbucketServerIntegration', () => {
expect(integration.title).toBe('h.com');
});
it('resolves url line number correctly', () => {
it('resolves url', () => {
const integration = new BitbucketServerIntegration({
host: 'h.com',
} as any);
expect(
integration.resolveUrl({
url: './a.yaml',
base: 'https://h.com/my-owner/my-project/src/master/README.md',
lineNumber: 14,
url: './README.md',
base: 'https://h.com/projects/my-project/repos/my-repo/browse/?at=master',
}),
).toBe('https://h.com/my-owner/my-project/src/master/a.yaml#a.yaml-14');
).toBe(
'https://h.com/projects/my-project/repos/my-repo/browse/README.md?at=master',
);
});
it('resolve edit URL', () => {
it('resolves url with line number', () => {
const integration = new BitbucketServerIntegration({
host: 'h.com',
} as any);
expect(
integration.resolveUrl({
url: './README.md',
base: 'https://h.com/projects/my-project/repos/my-repo/browse/?at=master',
lineNumber: 14,
}),
).toBe(
'https://h.com/projects/my-project/repos/my-repo/browse/README.md?at=master#14',
);
});
it('resolves edit url', () => {
const integration = new BitbucketServerIntegration({
host: 'h.com',
} as any);
expect(
integration.resolveEditUrl(
'https://h.com/my-owner/my-project/src/master/README.md',
'https://h.com/projects/my-project/repos/my-repo/browse/README.md',
),
).toBe(
'https://h.com/my-owner/my-project/src/master/README.md?mode=edit&spa=0&at=master',
);
).toBe('https://h.com/projects/my-project/repos/my-repo/browse/README.md');
});
it('resolves edit url with query params', () => {
const integration = new BitbucketServerIntegration({
host: 'h.com',
} as any);
expect(
integration.resolveEditUrl(
'https://h.com/projects/my-project/repos/my-repo/browse/README.md?at=master',
),
).toBe('https://h.com/projects/my-project/repos/my-repo/browse/README.md');
});
});
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import parseGitUrl from 'git-url-parse';
import { basicIntegrations, defaultScmResolveUrl } from '../helpers';
import { ScmIntegration, ScmIntegrationsFactory } from '../types';
import {
@@ -63,12 +62,11 @@ export class BitbucketServerIntegration implements ScmIntegration {
}): string {
const resolved = defaultScmResolveUrl(options);
// Bitbucket Server line numbers use the syntax #example.txt-42, rather than #L42
// Bitbucket Server line numbers use the syntax #42, rather than #L42
if (options.lineNumber) {
const url = new URL(resolved);
const filename = url.pathname.split('/').slice(-1)[0];
url.hash = `${filename}-${options.lineNumber}`;
url.hash = options.lineNumber.toString();
return url.toString();
}
@@ -76,14 +74,11 @@ export class BitbucketServerIntegration implements ScmIntegration {
}
resolveEditUrl(url: string): string {
const urlData = parseGitUrl(url);
const editUrl = new URL(url);
editUrl.searchParams.set('mode', 'edit');
// TODO: Not sure what spa=0 does, at least bitbucket.org doesn't support it
// but this is taken over from the initial implementation.
editUrl.searchParams.set('spa', '0');
editUrl.searchParams.set('at', urlData.ref);
return editUrl.toString();
// Bitbucket Server doesn't support deep linking to edit mode, therefore there's nothing to do here.
// We just remove query parameters since they cause issues with TechDocs edit button.
if (url.includes('?')) {
return url.substring(0, url.indexOf('?'));
}
return url;
}
}