From ef3cad4d00a77111fcfb973f0f0d970574b84b03 Mon Sep 17 00:00:00 2001 From: Diego Mondragon Date: Fri, 8 Dec 2023 09:27:59 -0800 Subject: [PATCH 1/4] 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, + }); + } } From f8cb33d4a4b376c489dbb35dd26619165ceaa520 Mon Sep 17 00:00:00 2001 From: Diego Mondragon Date: Tue, 12 Dec 2023 14:36:12 -0800 Subject: [PATCH 2/4] Change request method access modifier Signed-off-by: Diego Mondragon --- plugins/cloudbuild/src/api/CloudbuildClient.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/cloudbuild/src/api/CloudbuildClient.ts b/plugins/cloudbuild/src/api/CloudbuildClient.ts index e5dfc47f60..4aa080dbcb 100644 --- a/plugins/cloudbuild/src/api/CloudbuildClient.ts +++ b/plugins/cloudbuild/src/api/CloudbuildClient.ts @@ -92,7 +92,10 @@ export class CloudbuildClient implements CloudbuildApi { ); } - async request(url: string, method: string = 'GET'): Promise { + private async request( + url: string, + method: string = 'GET', + ): Promise { const requestHeaders = { Accept: '*/*', Authorization: `Bearer ${await this.getToken()}`, From 18afd35ef7c8db71dff250c7e10da54ce3b505a1 Mon Sep 17 00:00:00 2001 From: Diego Mondragon Date: Thu, 18 Jan 2024 12:51:44 -0800 Subject: [PATCH 3/4] GCP CloudBuild plugin: change to use custom http header Signed-off-by: Diego Mondragon --- plugins/cloudbuild/src/api/CloudbuildClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/cloudbuild/src/api/CloudbuildClient.ts b/plugins/cloudbuild/src/api/CloudbuildClient.ts index 4aa080dbcb..06f393b433 100644 --- a/plugins/cloudbuild/src/api/CloudbuildClient.ts +++ b/plugins/cloudbuild/src/api/CloudbuildClient.ts @@ -101,7 +101,7 @@ export class CloudbuildClient implements CloudbuildApi { Authorization: `Bearer ${await this.getToken()}`, ...(process.env.NODE_ENV === 'production' ? { - 'User-Agent': `backstage/cloudbuild/${packageinfo.version}`, + 'X-Goog-Api-Client': `backstage/cloudbuild/${packageinfo.version}`, } : {}), }; From 4a2691e3ef8446f3a479f399788506d645ed9670 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 22 Jan 2024 01:02:17 +0100 Subject: [PATCH 4/4] Update .changeset/soft-beans-tease.md Signed-off-by: Patrik Oldsberg --- .changeset/soft-beans-tease.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/soft-beans-tease.md b/.changeset/soft-beans-tease.md index 3f3210a8ce..881900e773 100644 --- a/.changeset/soft-beans-tease.md +++ b/.changeset/soft-beans-tease.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-cloudbuild': minor +'@backstage/plugin-cloudbuild': patch --- Add telemetry HTTP header Google Cloud Platform