| 201 | } |
| 202 | |
| 203 | export default class API { |
| 204 | apiRoot: string; |
| 205 | branch: string; |
| 206 | repo: string; |
| 207 | requestFunction: (req: ApiRequest) => Promise<Response>; |
| 208 | repoURL: string; |
| 209 | commitAuthor?: CommitAuthor; |
| 210 | mergeStrategy: string; |
| 211 | initialWorkflowStatus: string; |
| 212 | cmsLabelPrefix: string; |
| 213 | |
| 214 | constructor(config: Config) { |
| 215 | this.apiRoot = config.apiRoot || 'https://api.bitbucket.org/2.0'; |
| 216 | this.branch = config.branch || 'master'; |
| 217 | this.repo = config.repo || ''; |
| 218 | this.requestFunction = config.requestFunction || unsentRequest.performRequest; |
| 219 | // Allow overriding this.hasWriteAccess |
| 220 | this.hasWriteAccess = config.hasWriteAccess || this.hasWriteAccess; |
| 221 | this.repoURL = this.repo ? `/repositories/${this.repo}` : ''; |
| 222 | this.mergeStrategy = config.squashMerges ? 'squash' : 'merge_commit'; |
| 223 | this.initialWorkflowStatus = config.initialWorkflowStatus; |
| 224 | this.cmsLabelPrefix = config.cmsLabelPrefix; |
| 225 | } |
| 226 | |
| 227 | buildRequest = (req: ApiRequest) => { |
| 228 | const withRoot = unsentRequest.withRoot(this.apiRoot)(req); |
| 229 | if (withRoot.has('cache')) { |
| 230 | return withRoot; |
| 231 | } else { |
| 232 | const withNoCache = unsentRequest.withNoCache(withRoot); |
| 233 | return withNoCache; |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | request = (req: ApiRequest): Promise<Response> => { |
| 238 | try { |
| 239 | return requestWithBackoff(this, req); |
| 240 | } catch (err) { |
| 241 | throw new APIError(err.message, null, API_NAME); |
| 242 | } |
| 243 | }; |
| 244 | |
| 245 | responseToJSON = responseParser({ format: 'json', apiName: API_NAME }); |
| 246 | responseToBlob = responseParser({ format: 'blob', apiName: API_NAME }); |
| 247 | responseToText = responseParser({ format: 'text', apiName: API_NAME }); |
| 248 | |
| 249 | // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 250 | requestJSON = (req: ApiRequest) => this.request(req).then(this.responseToJSON) as Promise<any>; |
| 251 | requestText = (req: ApiRequest) => this.request(req).then(this.responseToText) as Promise<string>; |
| 252 | |
| 253 | user = () => this.requestJSON('/user') as Promise<BitBucketUser>; |
| 254 | |
| 255 | hasWriteAccess = async () => { |
| 256 | const response = await this.request(this.repoURL); |
| 257 | if (response.status === 404) { |
| 258 | throw Error('Repo not found'); |
| 259 | } |
| 260 | return response.ok; |
nothing calls this directly
no test coverage detected