-
Notifications
You must be signed in to change notification settings - Fork 593
/
bitbucket-service.ts
119 lines (103 loc) · 3.69 KB
/
bitbucket-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as ParseBitbucketUrl from 'parse-bitbucket-url';
import {
GitSource,
SecretType,
RepoMetadata,
BranchList,
RepoLanguageList,
RepoFileList,
} from '../types';
import { BaseService } from './base-service';
import { coFetchJSON } from '@console/internal/co-fetch';
export class BitbucketService extends BaseService {
private readonly metadata: RepoMetadata;
private readonly baseURL = 'https://api.bitbucket.org/2.0';
constructor(gitsource: GitSource) {
super(gitsource);
this.metadata = this.getRepoMetadata();
}
protected getAuthProvider = (): any => {
switch (this.gitsource.secretType) {
case SecretType.BASIC_AUTH: {
const { username, password } = this.gitsource.secretContent;
return { type: 'basic', username, password };
}
case SecretType.NO_AUTH:
return null;
default:
return null;
}
};
getRepoMetadata = (): RepoMetadata => {
const { name, owner, host, branch } = ParseBitbucketUrl(this.gitsource.url);
const contextDir = this.gitsource.contextDir?.replace(/\/$/, '') || '';
return {
repoName: name,
owner,
host,
defaultBranch: this.gitsource.ref || branch,
contextDir,
};
};
isRepoReachable = async (): Promise<boolean> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}`;
try {
const data = await coFetchJSON(url);
return data.slug === this.metadata.repoName;
} catch (e) {
return false;
}
};
getRepoBranchList = async (): Promise<BranchList> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/refs/branches`;
try {
const data = await coFetchJSON(url);
const list = data.values.map((b) => b.name);
return { branches: list };
} catch (e) {
return { branches: [] };
}
};
getRepoFileList = async (): Promise<RepoFileList> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${this.metadata.contextDir}?pagelen=50`;
try {
const data = await coFetchJSON(url);
const files = data.values?.map((f) => f.path) || [];
return { files };
} catch (e) {
return { files: [] };
}
};
getRepoLanguageList = async (): Promise<RepoLanguageList> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}`;
try {
const data = await coFetchJSON(url);
return { languages: [data.language] };
} catch (e) {
return { languages: [] };
}
};
isFilePresent = async (path: string): Promise<boolean> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${path}`;
try {
await coFetchJSON(url);
return true;
} catch (e) {
return false;
}
};
getFileContent = async (path: string): Promise<string | null> => {
const url = `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${path}`;
try {
const data = await coFetchJSON(url);
return data as string;
} catch (e) {
return null;
}
};
isDockerfilePresent = () => this.isFilePresent(`${this.metadata.contextDir}/Dockerfile`);
getDockerfileContent = () => this.getFileContent(`${this.metadata.contextDir}/Dockerfile`);
isDevfilePresent = () => this.isFilePresent(`${this.metadata.contextDir}/devfile.yaml`);
getDevfileContent = () => this.getFileContent(`${this.metadata.contextDir}/devfile.yaml`);
getPackageJsonContent = () => this.getFileContent(`${this.metadata.contextDir}/package.json`);
}