Added telemetry header GCP Cloud build

Signed-off-by: Diego Mondragon <mdiegoarmando@google.com>
This commit is contained in:
Diego Mondragon
2023-12-08 09:27:59 -08:00
parent 09d8d18e49
commit ef3cad4d00
2 changed files with 27 additions and 29 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-cloudbuild': minor
---
Add telemetry HTTP header Google Cloud Platform
+22 -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 {
@@ -29,33 +30,21 @@ export class CloudbuildClient implements CloudbuildApi {
projectId: string;
runId: string;
}): Promise<void> {
await fetch(
await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/builds/${encodeURIComponent(options.runId)}:retry`,
{
method: 'POST',
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
'POST',
);
}
async listWorkflowRuns(options: {
projectId: string;
}): Promise<ActionsListWorkflowRunsForRepoResponseData> {
const workflowRuns = await fetch(
const workflowRuns = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/builds`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const builds: ActionsListWorkflowRunsForRepoResponseData =
@@ -68,16 +57,10 @@ export class CloudbuildClient implements CloudbuildApi {
projectId: string;
id: string;
}): Promise<ActionsGetWorkflowResponseData> {
const workflow = await fetch(
const workflow = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/builds/${encodeURIComponent(options.id)}`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const build: ActionsGetWorkflowResponseData = await workflow.json();
@@ -89,16 +72,10 @@ export class CloudbuildClient implements CloudbuildApi {
projectId: string;
id: string;
}): Promise<ActionsGetWorkflowResponseData> {
const workflow = await fetch(
const workflow = await this.request(
`https://cloudbuild.googleapis.com/v1/projects/${encodeURIComponent(
options.projectId,
)}/builds/${encodeURIComponent(options.id)}`,
{
headers: new Headers({
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
}),
},
);
const build: ActionsGetWorkflowResponseData = await workflow.json();
@@ -114,4 +91,20 @@ export class CloudbuildClient implements CloudbuildApi {
'https://www.googleapis.com/auth/cloud-platform',
);
}
async request(url: string, method: string = 'GET'): Promise<Response> {
const requestHeaders = {
Accept: '*/*',
Authorization: `Bearer ${await this.getToken()}`,
...(process.env.NODE_ENV === 'production'
? {
'User-Agent': `backstage/cloudbuild/${packageinfo.version}`,
}
: {}),
};
return fetch(url, {
method,
headers: requestHeaders,
});
}
}