From 809b93c03ff0431e25650df665d4c782b4df7a32 Mon Sep 17 00:00:00 2001 From: Thawan Keane Date: Mon, 27 Feb 2023 13:03:52 -0300 Subject: [PATCH 1/5] feat(platforms): add support for bitbucket repository access tokens --- .../bitbucket_cloud/BitBucketCloudAPI.ts | 21 +++++++++++++++++-- source/platforms/platform.ts | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts b/source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts index 2b74e0a43..10b208950 100644 --- a/source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts +++ b/source/platforms/bitbucket_cloud/BitBucketCloudAPI.ts @@ -21,7 +21,7 @@ import { RepoMetaData } from "../../dsl/RepoMetaData" export type BitBucketCloudCredentials = { /** Unique ID for this user, must be wrapped with brackets */ uuid?: string -} & (BitBucketCloudCredentialsOAuth | BitBucketCloudCredentialsPassword) +} & (BitBucketCloudCredentialsOAuth | BitBucketCloudCredentialsPassword | BitBucketCloudCredentialsRepoAccessToken) interface BitBucketCloudCredentialsOAuth { type: "OAUTH" @@ -35,6 +35,11 @@ interface BitBucketCloudCredentialsPassword { password: string } +interface BitBucketCloudCredentialsRepoAccessToken { + type: "REPO_ACCESS_TOKEN" + accessToken: string +} + export function bitbucketCloudCredentialsFromEnv(env: Env): BitBucketCloudCredentials { const uuid: string | undefined = env["DANGER_BITBUCKETCLOUD_UUID"] if (uuid != null && uuid.length > 0) { @@ -64,7 +69,17 @@ export function bitbucketCloudCredentialsFromEnv(env: Env): BitBucketCloudCreden uuid, } } - throw new Error(`Either DANGER_BITBUCKETCLOUD_OAUTH_KEY or DANGER_BITBUCKETCLOUD_USERNAME is not set`) + + if (env["DANGER_BITBUCKETCLOUD_REPOSITORY_ACCESSTOKEN"]) { + return { + type: "REPO_ACCESS_TOKEN", + accessToken: env["DANGER_BITBUCKETCLOUD_REPOSITORY_ACCESSTOKEN"], + } + } + + throw new Error( + `Either DANGER_BITBUCKETCLOUD_OAUTH_KEY, DANGER_BITBUCKETCLOUD_USERNAME or DANGER_BITBUCKETCLOUD_REPOSITORY_ACCESSTOKEN is not set` + ) } export class BitBucketCloudAPI implements BitBucketCloudAPIDSL { @@ -316,6 +331,8 @@ export class BitBucketCloudAPI implements BitBucketCloudAPIDSL { headers["Authorization"] = `Basic ${Buffer.from( this.credentials.username + ":" + this.credentials.password ).toString("base64")}` + } else if (this.credentials.type === "REPO_ACCESS_TOKEN") { + headers["Authorization"] = `Bearer ${this.credentials.accessToken}` } else { if (this.accessToken == null) { this.d(`accessToken not found, trying to get from ${this.oauthURL}.`) diff --git a/source/platforms/platform.ts b/source/platforms/platform.ts index 9d776b822..180937fcc 100644 --- a/source/platforms/platform.ts +++ b/source/platforms/platform.ts @@ -120,6 +120,7 @@ export function getPlatformForEnv(env: Env, source: CISource): Platform { if ( env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || env["DANGER_BITBUCKETCLOUD_USERNAME"] || + env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] || env["DANGER_PR_PLATFORM"] === BitBucketCloud.name ) { const api = new BitBucketCloudAPI( From f1593a1deae8c1fd0374ec73db3f38f306eda820 Mon Sep 17 00:00:00 2001 From: Thawan Keane Date: Mon, 27 Feb 2023 13:05:32 -0300 Subject: [PATCH 2/5] feat(ci_source): add support for bitbucket repository access tokens --- source/ci_source/ci_source_helpers.ts | 6 +++++- source/ci_source/providers/BitbucketPipelines.ts | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/source/ci_source/ci_source_helpers.ts b/source/ci_source/ci_source_helpers.ts index 1a0ee7d95..7c177007c 100644 --- a/source/ci_source/ci_source_helpers.ts +++ b/source/ci_source/ci_source_helpers.ts @@ -45,7 +45,11 @@ export async function getPullRequestIDForBranch(metadata: RepoMetaData, env: Env } return 0 } - if (process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || process.env["DANGER_BITBUCKETCLOUD_USERNAME"]) { + if ( + process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || + process.env["DANGER_BITBUCKETCLOUD_USERNAME"] || + process.env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] + ) { const api = new BitBucketCloudAPI(metadata, bitbucketCloudCredentialsFromEnv(env)) const prs = await api.getPullRequestsFromBranch(branch) if (prs.length) { diff --git a/source/ci_source/providers/BitbucketPipelines.ts b/source/ci_source/providers/BitbucketPipelines.ts index 891661328..9ef5e1153 100644 --- a/source/ci_source/providers/BitbucketPipelines.ts +++ b/source/ci_source/providers/BitbucketPipelines.ts @@ -26,8 +26,9 @@ import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers" * * ### Token Setup * - * You can either add `DANGER_BITBUCKETCLOUD_USERNAME`, `DANGER_BITBUCKETCLOUD_PASSWORD` - * or add `DANGER_BITBUCKETCLOUD_OAUTH_KEY`, `DANGER_BITBUCKETCLOUD_OAUTH_SECRET` + * You can add `DANGER_BITBUCKETCLOUD_USERNAME` and `DANGER_BITBUCKETCLOUD_PASSWORD` + * or add `DANGER_BITBUCKETCLOUD_OAUTH_KEY` and `DANGER_BITBUCKETCLOUD_OAUTH_SECRET` + * or add `DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN` * - */ From 53b527631af9dd55b742eac2b4e17d11d8718282 Mon Sep 17 00:00:00 2001 From: Thawan Keane Date: Mon, 27 Feb 2023 13:06:51 -0300 Subject: [PATCH 3/5] feat(runner): add support for bitbucket repository access tokens --- source/commands/danger-pr.ts | 3 ++- source/runner/Executor.ts | 6 +++++- source/runner/jsonToDSL.ts | 12 ++++++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/source/commands/danger-pr.ts b/source/commands/danger-pr.ts index c9dd8089e..bfc3d4f5a 100644 --- a/source/commands/danger-pr.ts +++ b/source/commands/danger-pr.ts @@ -43,12 +43,13 @@ program !process.env["DANGER_BITBUCKETSERVER_HOST"] && !process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] && !process.env["DANGER_BITBUCKETCLOUD_USERNAME"] && + !process.env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] && !gitLabApiCredentials.token && !gitLabApiCredentials.oauthToken ) { log("") log( - " You don't have a DANGER_GITHUB_API_TOKEN/DANGER_GITLAB_API_TOKEN/DANGER_GITLAB_API_OAUTH_TOKEN/DANGER_BITBUCKETCLOUD_OAUTH_KEY/DANGER_BITBUCKETCLOUD_USERNAME set up, this is optional, but TBH, you want to do this." + " You don't have a DANGER_GITHUB_API_TOKEN/DANGER_GITLAB_API_TOKEN/DANGER_GITLAB_API_OAUTH_TOKEN/DANGER_BITBUCKETCLOUD_OAUTH_KEY/DANGER_BITBUCKETCLOUD_USERNAME/DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN set up, this is optional, but TBH, you want to do this." ) log(" Check out: http://danger.systems/js/guides/the_dangerfile.html#working-on-your-dangerfile") log("") diff --git a/source/runner/Executor.ts b/source/runner/Executor.ts index bcb922f6c..8f3f1c56c 100644 --- a/source/runner/Executor.ts +++ b/source/runner/Executor.ts @@ -315,7 +315,11 @@ export class Executor { let comment if (process.env["DANGER_BITBUCKETSERVER_HOST"]) { comment = bitbucketServerTemplate(dangerID, mergedResults, commitID) - } else if (process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || process.env["DANGER_BITBUCKETCLOUD_USERNAME"]) { + } else if ( + process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || + process.env["DANGER_BITBUCKETCLOUD_USERNAME"] || + process.env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] + ) { comment = bitbucketCloudTemplate(dangerID, mergedResults, commitID) } else { comment = githubResultsTemplate(dangerID, mergedResults, commitID) diff --git a/source/runner/jsonToDSL.ts b/source/runner/jsonToDSL.ts index e7f86d748..d7ea91f12 100644 --- a/source/runner/jsonToDSL.ts +++ b/source/runner/jsonToDSL.ts @@ -45,7 +45,11 @@ export const jsonToDSL = async (dsl: DangerDSLJSONType, source: CISource): Promi git = await localPlatform.getPlatformGitRepresentation() } else if (process.env["DANGER_BITBUCKETSERVER_HOST"]) { git = bitBucketServerGitDSL(bitbucket_server!, dsl.git, api as BitBucketServerAPI) - } else if (process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || process.env["DANGER_BITBUCKETCLOUD_USERNAME"]) { + } else if ( + process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || + process.env["DANGER_BITBUCKETCLOUD_USERNAME"] || + process.env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] + ) { git = bitBucketCloudGitDSL(bitbucket_cloud!, dsl.git, api as BitBucketCloudAPI) } else if (process.env["DANGER_GITLAB_API_TOKEN"] || process.env["DANGER_GITLAB_API_OAUTH_TOKEN"]) { git = gitLabGitDSL(gitlab!, dsl.git, api as GitLabAPI) @@ -74,7 +78,11 @@ const apiForDSL = (dsl: DangerDSLJSONType): Octokit | BitBucketServerAPI | GitLa return new BitBucketServerAPI(dsl.bitbucket_server!.metadata, bitbucketServerRepoCredentialsFromEnv(process.env)) } - if (process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || process.env["DANGER_BITBUCKETCLOUD_USERNAME"]) { + if ( + process.env["DANGER_BITBUCKETCLOUD_OAUTH_KEY"] || + process.env["DANGER_BITBUCKETCLOUD_USERNAME"] || + process.env["DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN"] + ) { return new BitBucketCloudAPI(dsl.bitbucket_cloud!.metadata, bitbucketCloudCredentialsFromEnv(process.env)) } From 3ce2eae0c58311b72ae787c3af0501c70fd610af Mon Sep 17 00:00:00 2001 From: Thawan Keane Date: Mon, 27 Feb 2023 13:07:08 -0300 Subject: [PATCH 4/5] docs: add support for bitbucket repository access tokens --- CHANGELOG.md | 1 + docs/guides/the_dangerfile.html.md | 4 ++++ docs/usage/bitbucket_cloud.html.md | 9 ++++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60976a91b..74bf22832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ +- Add support for BitBucket Cloud Repository Access Token - [@thawankeane] diff --git a/docs/guides/the_dangerfile.html.md b/docs/guides/the_dangerfile.html.md index b2ce9dc55..98115aaa6 100644 --- a/docs/guides/the_dangerfile.html.md +++ b/docs/guides/the_dangerfile.html.md @@ -39,6 +39,10 @@ export DANGER_BITBUCKETCLOUD_PASSWORD='yyyy' # You can get OAuth key from Settings > OAuth > Add consumer, put `https://bitbucket.org/site/oauth2/authorize` for `Callback URL`, and enable Read Pull requests, and Read Account Permissions. export DANGER_BITBUCKETCLOUD_OAUTH_KEY='xxxx' export DANGER_BITBUCKETCLOUD_OAUTH_SECRET='yyyy' + +# or for BitBucket Cloud by Repository Access Token +# You can get a Repository Access Token from Repo Settings > Security > Acesss Tokens and set Pull requests write scope. +export DANGER_BITBUCKETCLOUD_REPO_ACCESSTOKEN='xxxx' ``` Then the danger CLI will use authenticated API calls, which don't get this by API limits. diff --git a/docs/usage/bitbucket_cloud.html.md b/docs/usage/bitbucket_cloud.html.md index da06bf584..dfc8fa791 100644 --- a/docs/usage/bitbucket_cloud.html.md +++ b/docs/usage/bitbucket_cloud.html.md @@ -9,7 +9,7 @@ blurb: An overview of using Danger with BitBucket Cloud, and some examples To use Danger JS with BitBucket Cloud: you'll need to create a new account for Danger to use, then set the following environment variables on your CI: -You could use either username with password or OAuth key with OAuth secret. +You could use either username with password, OAuth key with OAuth secret or repository access token. For username and password, you need to set. @@ -30,6 +30,13 @@ For OAuth key and OAuth secret, you can get them from. - `DANGER_BITBUCKETCLOUD_OAUTH_SECRET` = The consumer secret for the account used to comment, as show as `Secret` on the website. +For [repository access token](https://support.atlassian.com/bitbucket-cloud/docs/repository-access-tokens/), what you +need to create one is: + +- Open your repository URL +- Navigate to Settings > Security > Access Tokens > Create Repository Access Token +- Give it a name and set Pull requests write scope + Then in your Dangerfiles you will have a fully fleshed out `danger.bitbucket_cloud` object to work with. For example: ```ts From 95bdac8fa1a6e4e5b0774915b02247d460d3df9d Mon Sep 17 00:00:00 2001 From: Thawan Keane Date: Wed, 1 Mar 2023 14:37:17 -0300 Subject: [PATCH 5/5] fix(changelog): add missing anchor to thawankeane collaborator --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74bf22832..2039da5ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2039,6 +2039,7 @@ Not usable for others, only stubs of classes etc. - [@orta] [@thii]: https://github.com/thii [@tibdex]: https://github.com/tibdex [@tim3trick]: https://github.com/tim3trick +[@thawankeane]: https://github.com/thawankeane [@tychota]: https://github.com/tychota [@urkle]: https://github.com/urkle [@valscion]: https://github.com/valscion