Merge pull request #4651 from backstage/blam/bitbucket-authorisation

Both Authorization patterns are valid for both versions of Bitbucket
This commit is contained in:
Fredrik Adelöw
2021-02-23 22:06:36 +01:00
committed by GitHub
2 changed files with 28 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Consider both authentication methods for both `onprem` and `cloud` BitBucket
@@ -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`,