| 82 | } |
| 83 | |
| 84 | export class BitBucketCloudAPI implements BitBucketCloudAPIDSL { |
| 85 | fetch: typeof fetch |
| 86 | accessToken: string | undefined |
| 87 | uuid: string | undefined |
| 88 | |
| 89 | private readonly d = debug("BitBucketCloudAPI") |
| 90 | private pr: BitBucketCloudPRDSL | undefined |
| 91 | private commits: BitBucketCloudCommit[] | undefined |
| 92 | private baseURL = "https://api.bitbucket.org/2.0" |
| 93 | private oauthURL = "https://bitbucket.org/site/oauth2/access_token" |
| 94 | |
| 95 | constructor(public readonly repoMetadata: RepoMetaData, public readonly credentials: BitBucketCloudCredentials) { |
| 96 | // This allows Peril to DI in a new Fetch function |
| 97 | // which can handle unique API edge-cases around integrations |
| 98 | this.fetch = fetch |
| 99 | |
| 100 | // Backward compatible, |
| 101 | this.uuid = credentials.uuid |
| 102 | } |
| 103 | |
| 104 | getBaseRepoURL() { |
| 105 | const { repoSlug } = this.repoMetadata |
| 106 | return `${this.baseURL}/repositories/${repoSlug}` |
| 107 | } |
| 108 | |
| 109 | getPRURL() { |
| 110 | const { pullRequestID } = this.repoMetadata |
| 111 | return `${this.getBaseRepoURL()}/pullrequests/${pullRequestID}` |
| 112 | } |
| 113 | |
| 114 | getPullRequestsFromBranch = async (branch: string): Promise<BitBucketCloudPRDSL[]> => { |
| 115 | // Need to encode URI here because it used special characters in query params. |
| 116 | // https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pullrequests |
| 117 | let nextPageURL: string | undefined = encodeURI( |
| 118 | `${this.getBaseRepoURL()}/pullrequests?q=source.branch.name = "${branch}"` |
| 119 | ) |
| 120 | let values: BitBucketCloudPRDSL[] = [] |
| 121 | |
| 122 | do { |
| 123 | const res = await this.get(nextPageURL) |
| 124 | throwIfNotOk(res) |
| 125 | |
| 126 | const data = (await res.json()) as BitBucketCloudPagedResponse<BitBucketCloudPRDSL> |
| 127 | |
| 128 | values = values.concat(data.values) |
| 129 | |
| 130 | nextPageURL = data.next |
| 131 | } while (nextPageURL != null) |
| 132 | |
| 133 | return values |
| 134 | } |
| 135 | |
| 136 | getPullRequestInfo = async (): Promise<BitBucketCloudPRDSL> => { |
| 137 | if (this.pr) { |
| 138 | return this.pr |
| 139 | } |
| 140 | const res = await this.get(this.getPRURL()) |
| 141 | throwIfNotOk(res) |
nothing calls this directly
no test coverage detected