Merge pull request #22343 from mondragonda/gcp-ua-header

[Google Cloud Build] Added X-Goog-Api-Client request header
This commit is contained in:
Patrik Oldsberg
2024-01-22 01:13:05 +01:00
committed by GitHub
2 changed files with 30 additions and 29 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-cloudbuild': patch
---
Add telemetry HTTP header Google Cloud Platform
+25 -29
View File
@@ -20,6 +20,7 @@ import {
ActionsGetWorkflowResponseData,
} from '../api/types';
import { OAuthApi } from '@backstage/core-plugin-api';
import packageinfo from '../../package.json';
/** @public */
export class CloudbuildClient implements CloudbuildApi {
@@ -30,19 +31,13 @@ export class CloudbuildClient implements CloudbuildApi {
location: string;
runId: string;
}): Promise<void> {
await fetch(
await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/locations/${encodeURIComponent(
options.location,
)}/builds/${encodeURIComponent(options.runId)}:retry`,
{
method: 'POST',
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
'POST',
);
}
@@ -51,18 +46,12 @@ export class CloudbuildClient implements CloudbuildApi {
location: string;
cloudBuildFilter: string;
}): Promise<ActionsListWorkflowRunsForRepoResponseData> {
const workflowRuns = await fetch(
const workflowRuns = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/locations/${encodeURIComponent(
options.location,
)}/builds?filter=${encodeURIComponent(options.cloudBuildFilter)}`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const builds: ActionsListWorkflowRunsForRepoResponseData =
@@ -76,18 +65,12 @@ export class CloudbuildClient implements CloudbuildApi {
location: string;
id: string;
}): Promise<ActionsGetWorkflowResponseData> {
const workflow = await fetch(
const workflow = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/locations/${encodeURIComponent(
options.location,
)}/builds/${encodeURIComponent(options.id)}`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const build: ActionsGetWorkflowResponseData = await workflow.json();
@@ -100,18 +83,12 @@ export class CloudbuildClient implements CloudbuildApi {
location: string;
id: string;
}): Promise<ActionsGetWorkflowResponseData> {
const workflow = await fetch(
const workflow = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/locations/${encodeURIComponent(
options.location,
)}/builds/${encodeURIComponent(options.id)}`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const build: ActionsGetWorkflowResponseData = await workflow.json();
@@ -127,4 +104,23 @@ export class CloudbuildClient implements CloudbuildApi {
'https://www.googleapis.com/auth/cloud-platform',
);
}
private async request(
url: string,
method: string = 'GET',
): Promise<Response> {
const requestHeaders = {
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
...(process.env.NODE_ENV === 'production'
? {
'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`,
}
: {}),
};
return fetch(url, {
method,
headers: requestHeaders,
});
}
}