This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bitbucket and Bitbucket Server support (#752)
- Loading branch information
Showing
23 changed files
with
756 additions
and
52 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
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
21 changes: 21 additions & 0 deletions
21
tools/devworkspace-generator/src/bitbucket-server/bitbucket-server-module.ts
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,21 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
import { ContainerModule, interfaces } from 'inversify'; | ||
|
||
import { BitbucketServerResolver } from './bitbucket-server-resolver'; | ||
import { TYPES } from '../types'; | ||
|
||
const { Resolver } = TYPES; | ||
|
||
const bitbucketServerModule = new ContainerModule((bind: interfaces.Bind) => { | ||
bind(Resolver).to(BitbucketServerResolver).inSingletonScope(); | ||
}); | ||
|
||
export { bitbucketServerModule }; |
53 changes: 53 additions & 0 deletions
53
tools/devworkspace-generator/src/bitbucket-server/bitbucket-server-resolver.ts
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 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { BitbucketServerUrl } from './bitbucket-server-url'; | ||
import { injectable } from 'inversify'; | ||
import { Url } from '../resolve/url'; | ||
import { Resolver } from '../resolve/resolver'; | ||
|
||
@injectable() | ||
export class BitbucketServerResolver implements Resolver { | ||
// eslint-disable-next-line max-len | ||
static readonly BITBUCKET_URL_PATTERNS: RegExp[] = [ | ||
/^(?<scheme>https?):\/\/(?<host>.*)\/scm\/~(?<user>[^\/]+)\/(?<repo>.*).git$/, | ||
/^(?<scheme>https?):\/\/(?<host>.*)\/users\/(?<user>[^\/]+)\/repos\/(?<repo>[^\/]+)\/browse(\?at=(?<branch>.*))?$/, | ||
/^(?<scheme>https?):\/\/(?<host>.*)\/scm\/(?<project>[^\/~]+)\/(?<repo>[^\/]+).git$/, | ||
/^(?<scheme>https?):\/\/(?<host>.*)\/projects\/(?<project>[^\/]+)\/repos\/(?<repo>[^\/]+)\/browse(\?at=(?<branch>.*))?$/, | ||
]; | ||
|
||
isValid(url: string): boolean { | ||
return BitbucketServerResolver.BITBUCKET_URL_PATTERNS.some(p => p.test(url)); | ||
} | ||
|
||
resolve(url: string): Url { | ||
const regExp = BitbucketServerResolver.BITBUCKET_URL_PATTERNS.find(p => p.test(url)); | ||
if (!regExp) { | ||
throw new Error(`Invalid bitbucket-server URL: ${url}`); | ||
} | ||
const match = regExp.exec(url); | ||
const scheme = this.getGroup(match, 'scheme'); | ||
const hostName = this.getGroup(match, 'host'); | ||
const user = this.getGroup(match, 'user'); | ||
const project = this.getGroup(match, 'project'); | ||
const repo = this.getGroup(match, 'repo'); | ||
let branch = this.getGroup(match, 'branch'); | ||
if (branch !== undefined && branch.startsWith('refs%2Fheads%2F')) { | ||
branch = branch.substring(15); | ||
} | ||
return new BitbucketServerUrl(scheme, hostName, user, project, repo, branch); | ||
} | ||
|
||
private getGroup(match: RegExpExecArray, groupName: string): string | undefined { | ||
if (match.groups && match.groups[groupName]) { | ||
return match.groups[groupName]; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
tools/devworkspace-generator/src/bitbucket-server/bitbucket-server-url.ts
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,51 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { Url } from '../resolve/url'; | ||
|
||
export class BitbucketServerUrl implements Url { | ||
constructor( | ||
private readonly scheme: string, | ||
private readonly hostName: string, | ||
private readonly user: string | undefined, | ||
private readonly project: string | undefined, | ||
private readonly repo: string, | ||
private readonly branch: string | undefined | ||
) {} | ||
|
||
getContentUrl(path: string): string { | ||
const isUser = this.user !== undefined; | ||
return `${this.scheme}://${this.hostName}/${isUser ? 'users' : 'projects'}/${ | ||
isUser ? this.user : this.project | ||
}/repos/${this.repo}/raw/${path}${this.branch !== undefined ? '?/at=' + this.branch : ''}`; | ||
} | ||
|
||
getUrl(): string { | ||
const isUser = this.user !== undefined; | ||
return `${this.scheme}://${this.hostName}/${isUser ? 'users' : 'projects'}/${ | ||
isUser ? this.user : this.project | ||
}/repos/${this.repo}${this.branch !== undefined ? '/browse?at=' + this.branch : ''}`; | ||
} | ||
|
||
getCloneUrl(): string { | ||
const isUser = this.user !== undefined; | ||
return `${this.scheme}://${this.hostName}/scm/${isUser ? '~' + this.user : this.project.toLowerCase()}/${ | ||
this.repo | ||
}.git`; | ||
} | ||
|
||
getRepoName(): string { | ||
return this.repo; | ||
} | ||
|
||
getBranchName(): string { | ||
return this.branch; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tools/devworkspace-generator/src/bitbucket/bitbucket-module.ts
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,21 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
import { ContainerModule, interfaces } from 'inversify'; | ||
|
||
import { BitbucketResolver } from './bitbucket-resolver'; | ||
import { TYPES } from '../types'; | ||
|
||
const { Resolver } = TYPES; | ||
|
||
const bitbucketModule = new ContainerModule((bind: interfaces.Bind) => { | ||
bind(Resolver).to(BitbucketResolver).inSingletonScope(); | ||
}); | ||
|
||
export { bitbucketModule }; |
46 changes: 46 additions & 0 deletions
46
tools/devworkspace-generator/src/bitbucket/bitbucket-resolver.ts
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,46 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { BitbucketUrl } from './bitbucket-url'; | ||
import { injectable } from 'inversify'; | ||
import { Url } from '../resolve/url'; | ||
import { Resolver } from '../resolve/resolver'; | ||
|
||
@injectable() | ||
export class BitbucketResolver implements Resolver { | ||
// eslint-disable-next-line max-len | ||
static readonly BITBUCKET_URL_PATTERN: RegExp = | ||
/^https:\/\/.*@?bitbucket\.org\/(?<workspaceId>[^\/]+)\/(?<repoName>[^\/]+)(\/(src|branch)\/(?<branchName>[^\/]+))?\/?$/; | ||
|
||
isValid(url: string): boolean { | ||
return BitbucketResolver.BITBUCKET_URL_PATTERN.test(url); | ||
} | ||
|
||
resolve(url: string): Url { | ||
const match = BitbucketResolver.BITBUCKET_URL_PATTERN.exec(url); | ||
if (!match) { | ||
throw new Error(`Invalid bitbucket URL: ${url}`); | ||
} | ||
const workspaceId = this.getGroup(match, 'workspaceId'); | ||
let repoName = this.getGroup(match, 'repoName'); | ||
if (/^[\w-][\w.-]*?\.git$/.test(repoName)) { | ||
repoName = repoName.substring(0, repoName.length - 4); | ||
} | ||
const branchName = this.getGroup(match, 'branchName', 'HEAD'); | ||
return new BitbucketUrl(workspaceId, repoName, branchName); | ||
} | ||
|
||
private getGroup(match: RegExpExecArray, groupName: string, defaultValue?: string) { | ||
if (match.groups && match.groups[groupName]) { | ||
return match.groups[groupName]; | ||
} | ||
return defaultValue; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
tools/devworkspace-generator/src/bitbucket/bitbucket-url.ts
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,41 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
import { Url } from '../resolve/url'; | ||
|
||
export class BitbucketUrl implements Url { | ||
private static readonly BITBUCKET_URL = 'https://bitbucket.org'; | ||
|
||
constructor( | ||
private readonly workspaceId: string, | ||
private readonly repoName: string, | ||
private readonly branchName: string | ||
) {} | ||
|
||
getContentUrl(path: string): string { | ||
return `${BitbucketUrl.BITBUCKET_URL}/${this.workspaceId}/${this.repoName}/raw/${this.branchName}/${path}`; | ||
} | ||
|
||
getUrl(): string { | ||
return `${BitbucketUrl.BITBUCKET_URL}/${this.workspaceId}/${this.repoName}/src/${this.branchName}`; | ||
} | ||
|
||
getCloneUrl(): string { | ||
return `${BitbucketUrl.BITBUCKET_URL}/${this.workspaceId}/${this.repoName}.git`; | ||
} | ||
|
||
getRepoName(): string { | ||
return this.repoName; | ||
} | ||
|
||
getBranchName(): string { | ||
return this.branchName; | ||
} | ||
} |
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
Oops, something went wrong.