/
bitbucket-service.ts
163 lines (145 loc) · 5.6 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Base64 } from 'js-base64';
import * as ParseBitbucketUrl from 'parse-bitbucket-url';
import 'whatwg-fetch';
import {
GitSource,
SecretType,
RepoMetadata,
BranchList,
RepoLanguageList,
RepoFileList,
RepoStatus,
} from '../types';
import { BaseService } from './base-service';
export class BitbucketService extends BaseService {
private readonly metadata: RepoMetadata;
private baseURL = 'https://api.bitbucket.org/2.0';
private isServer = false;
constructor(gitsource: GitSource) {
super(gitsource);
this.metadata = this.getRepoMetadata();
if (this.metadata.host !== 'bitbucket.org') {
this.baseURL = `https://${this.metadata.host}/rest/api/1.0`;
this.isServer = true;
}
}
protected getAuthProvider = (): any => {
switch (this.gitsource.secretType) {
case SecretType.BASIC_AUTH: {
const { username, password } = this.gitsource.secretContent;
const encodedAuth = Base64.encode(`${Base64.decode(username)}:${Base64.decode(password)}`);
return { Authorization: `Basic ${encodedAuth}` };
}
default:
return null;
}
};
protected fetchJson = async (url: string) => {
const authHeaders = this.getAuthProvider();
const response = await fetch(url, { headers: { Accept: 'application/json', ...authHeaders } });
if (!response.ok) {
throw response;
}
if (response.headers.get('Content-Type') === 'text/plain') {
return response.text();
}
return response.json();
};
getRepoMetadata = (): RepoMetadata => {
const { name, owner, host } = ParseBitbucketUrl(this.gitsource.url);
const contextDir = this.gitsource.contextDir?.replace(/\/$/, '') || '';
return {
repoName: name,
owner,
host,
defaultBranch: this.gitsource.ref || 'HEAD',
contextDir,
devfilePath: this.gitsource.devfilePath,
dockerfilePath: this.gitsource.dockerfilePath,
};
};
isRepoReachable = async (): Promise<RepoStatus> => {
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}`;
try {
const data = await this.fetchJson(url);
if (data.slug === this.metadata.repoName) {
return RepoStatus.Reachable;
}
} catch (e) {
if (e.status === 429) {
return RepoStatus.RateLimitExceeded;
}
}
return RepoStatus.Unreachable;
};
getRepoBranchList = async (): Promise<BranchList> => {
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}/branches`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/refs/branches`;
try {
const data = await this.fetchJson(url);
const list = data.values.map((b) => b.name);
return { branches: list };
} catch (e) {
return { branches: [] };
}
};
getRepoFileList = async (): Promise<RepoFileList> => {
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}/files/${this.metadata.contextDir}?limit=50&at=${this.metadata.defaultBranch}`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${this.metadata.contextDir}?pagelen=50`;
try {
const data = await this.fetchJson(url);
const files = this.isServer ? data.values : data.values?.map((f) => f.path) || [];
return { files };
} catch (e) {
return { files: [] };
}
};
getRepoLanguageList = async (): Promise<RepoLanguageList> => {
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}`;
try {
const data = await this.fetchJson(url);
return { languages: [data.language] };
} catch (e) {
return { languages: [] };
}
};
isFilePresent = async (path: string): Promise<boolean> => {
const filePath = path.replace(/^\//, '');
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}/raw/${filePath}?at=${this.metadata.defaultBranch}`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${filePath}`;
try {
await this.fetchJson(url);
return true;
} catch (e) {
return false;
}
};
getFileContent = async (path: string): Promise<string | null> => {
const filePath = path.replace(/^\//, '');
const url = this.isServer
? `${this.baseURL}/projects/${this.metadata.owner}/repos/${this.metadata.repoName}/raw/${filePath}?at=${this.metadata.defaultBranch}`
: `${this.baseURL}/repositories/${this.metadata.owner}/${this.metadata.repoName}/src/${this.metadata.defaultBranch}/${filePath}`;
try {
const data = await this.fetchJson(url);
return data as string;
} catch (e) {
return null;
}
};
isDockerfilePresent = () =>
this.isFilePresent(`${this.metadata.contextDir}/${this.metadata.dockerfilePath}`);
getDockerfileContent = () =>
this.getFileContent(`${this.metadata.contextDir}/${this.metadata.dockerfilePath}`);
isDevfilePresent = () =>
this.isFilePresent(`${this.metadata.contextDir}/${this.metadata.devfilePath}`);
getDevfileContent = () =>
this.getFileContent(`${this.metadata.contextDir}/${this.metadata.devfilePath}`);
getPackageJsonContent = () => this.getFileContent(`${this.metadata.contextDir}/package.json`);
}