diff --git a/.changeset/hungry-frogs-flow.md b/.changeset/hungry-frogs-flow.md new file mode 100644 index 0000000000..91a6ab0892 --- /dev/null +++ b/.changeset/hungry-frogs-flow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Consider both authentication methods for both `onprem` and `cloud` BitBucket diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts index 373920c005..102a3a7d02 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts @@ -98,10 +98,6 @@ export class BitbucketPublisher implements PublisherBase { const { project, name, description } = opts; let response: Response; - const buffer = Buffer.from( - `${this.config.username}:${this.config.appPassword}`, - 'utf8', - ); const options: RequestInit = { method: 'POST', @@ -111,7 +107,7 @@ export class BitbucketPublisher implements PublisherBase { is_private: this.config.repoVisibility === 'private', }), headers: { - Authorization: `Basic ${buffer.toString('base64')}`, + Authorization: this.getAuthorizationHeader(), 'Content-Type': 'application/json', }, }; @@ -132,13 +128,32 @@ export class BitbucketPublisher implements PublisherBase { } } - // TODO use the urlReader to get the defautl branch + // TODO use the urlReader to get the default branch const catalogInfoUrl = `${r.links.html.href}/src/master/catalog-info.yaml`; return { remoteUrl, catalogInfoUrl }; } throw new Error(`Not a valid response code ${await response.text()}`); } + private getAuthorizationHeader(): string { + if (this.config.username && this.config.appPassword) { + const buffer = Buffer.from( + `${this.config.username}:${this.config.appPassword}`, + 'utf8', + ); + + return `Basic ${buffer.toString('base64')}`; + } + + if (this.config.token) { + return `Bearer ${this.config.token}`; + } + + throw new Error( + `Authorization has not been provided for Bitbucket. Please add either username + appPassword or token to the Integrations config`, + ); + } + private async createBitbucketServerRepository(opts: { project: string; name: string; @@ -155,10 +170,11 @@ export class BitbucketPublisher implements PublisherBase { is_private: this.config.repoVisibility === 'private', }), headers: { - Authorization: `Bearer ${this.config.token}`, + Authorization: this.getAuthorizationHeader(), 'Content-Type': 'application/json', }, }; + try { response = await fetch( `https://${this.config.host}/rest/api/1.0/projects/${project}/repos`,