diff --git a/.changeset/soft-beans-tease.md b/.changeset/soft-beans-tease.md new file mode 100644 index 0000000000..881900e773 --- /dev/null +++ b/.changeset/soft-beans-tease.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-cloudbuild': patch +--- + +Add telemetry HTTP header Google Cloud Platform diff --git a/plugins/cloudbuild/src/api/CloudbuildClient.ts b/plugins/cloudbuild/src/api/CloudbuildClient.ts index 3717e3d1db..256e702cda 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 { @@ -30,19 +31,13 @@ export class CloudbuildClient implements CloudbuildApi { location: string; runId: string; }): Promise { - 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 { - 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 { - 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 { - 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 { + 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, + }); + } }