From ef3cad4d00a77111fcfb973f0f0d970574b84b03 Mon Sep 17 00:00:00 2001 From: Diego Mondragon Date: Fri, 8 Dec 2023 09:27:59 -0800 Subject: [PATCH] Added telemetry header GCP Cloud build Signed-off-by: Diego Mondragon --- .changeset/soft-beans-tease.md | 5 ++ .../cloudbuild/src/api/CloudbuildClient.ts | 51 ++++++++----------- 2 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 .changeset/soft-beans-tease.md diff --git a/.changeset/soft-beans-tease.md b/.changeset/soft-beans-tease.md new file mode 100644 index 0000000000..3f3210a8ce --- /dev/null +++ b/.changeset/soft-beans-tease.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-cloudbuild': minor +--- + +Add telemetry HTTP header Google Cloud Platform diff --git a/plugins/cloudbuild/src/api/CloudbuildClient.ts b/plugins/cloudbuild/src/api/CloudbuildClient.ts index cb6747e803..e5dfc47f60 100644 --- a/plugins/cloudbuild/src/api/CloudbuildClient.ts +++ b/plugins/cloudbuild/src/api/CloudbuildClient.ts @@ -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 { - 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 { - 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 { - 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 { - 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 { + 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, + }); + } }