From e784ba9de0afe79530e5db5677dd827c39692c30 Mon Sep 17 00:00:00 2001 From: Kevin Snyder Date: Fri, 4 Apr 2025 11:49:22 -0700 Subject: [PATCH] Use action context logger in Octokit client This will surface Octokit logs in the `OngoingTask` log stream. For example, Octokit will now surface logs when GitHub API rate limits are hit. Previously these were only visible in the backend logs and did not use the structured Backstage logger. Signed-off-by: Kevin Snyder --- .../src/actions/github.ts | 5 ++++- .../src/actions/githubActionsDispatch.ts | 22 ++++++++++--------- .../src/actions/githubAutolinks.ts | 22 ++++++++++--------- .../src/actions/githubBranchProtection.ts | 5 ++++- .../src/actions/githubDeployKey.ts | 5 ++++- .../src/actions/githubEnvironment.ts | 6 +++-- .../src/actions/githubIssuesLabel.ts | 22 ++++++++++--------- .../src/actions/githubPagesEnable.ts | 5 ++++- .../src/actions/githubRepoCreate.ts | 5 ++++- .../src/actions/githubRepoPush.ts | 5 ++++- .../src/actions/githubWebhook.ts | 22 ++++++++++--------- 11 files changed, 76 insertions(+), 48 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index fd858f0830..62e0497dae 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -242,7 +242,10 @@ export function createPublishGithubAction(options: { owner, repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); const { remoteUrl, repoContentsUrl } = await ctx.checkpoint({ key: `create.github.repo.${owner}.${repo}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts index 4faa53e6b8..40e48bb902 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts @@ -104,16 +104,18 @@ export function createGithubActionsDispatchAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - const client = new Octokit( - await getOctokitOptions({ - integrations, - host, - owner, - repo, - credentialsProvider: githubCredentialsProvider, - token: providedToken, - }), - ); + const octokitOptions = await getOctokitOptions({ + integrations, + host, + owner, + repo, + credentialsProvider: githubCredentialsProvider, + token: providedToken, + }); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); await ctx.checkpoint({ key: `create.workflow.dispatch.${owner}.${repo}.${workflowId}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts index 396d85708f..50ed052458 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts @@ -97,16 +97,18 @@ export function createGithubAutolinksAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - const client = new Octokit( - await getOctokitOptions({ - integrations, - host, - owner, - repo, - credentialsProvider: githubCredentialsProvider, - token, - }), - ); + const octokitOptions = await getOctokitOptions({ + integrations, + host, + owner, + repo, + credentialsProvider: githubCredentialsProvider, + token, + }); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); await ctx.checkpoint({ key: `create.auto.link.${owner}.${repo}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts index ae8965ef9a..79eebb4096 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts @@ -128,7 +128,10 @@ export function createGithubBranchProtectionAction(options: { owner, repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); const defaultBranch = await ctx.checkpoint({ key: `read.default.branch.${owner}.${repo}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts index 7e674a3e52..a820050a87 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts @@ -124,7 +124,10 @@ export function createGithubDeployKeyAction(options: { repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); await client.rest.repos.createDeployKey({ owner: owner, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts index f93cb49946..522b6ed8b5 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts @@ -192,8 +192,10 @@ Wildcard characters will not match \`/\`. For example, to match tags that begin owner, repo, }); - - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); const repositoryId = await ctx.checkpoint({ key: `get.repo.${owner}.${repo}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts index 7dd116be79..f91bd01a73 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts @@ -89,16 +89,18 @@ export function createGithubIssuesLabelAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - const client = new Octokit( - await getOctokitOptions({ - integrations, - credentialsProvider: githubCredentialsProvider, - host, - owner, - repo, - token: providedToken, - }), - ); + const octokitOptions = await getOctokitOptions({ + integrations, + credentialsProvider: githubCredentialsProvider, + host, + owner, + repo, + token: providedToken, + }); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); try { await ctx.checkpoint({ diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts index 7efee89a5d..01830a252d 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts @@ -112,7 +112,10 @@ export function createGithubPagesEnableAction(options: { owner, repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); ctx.logger.info( `Attempting to enable GitHub Pages for ${owner}/${repo} with "${buildType}" build type, on source branch "${sourceBranch}" and source path "${sourcePath}"`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index 6899b3dff8..442af4057c 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -197,7 +197,10 @@ export function createGithubRepoCreateAction(options: { owner, repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); const remoteUrl = await ctx.checkpoint({ key: `create.repo.and.topics.${owner}.${repo}`, diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts index 1923a0bd35..14c12b84f9 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts @@ -158,7 +158,10 @@ export function createGithubRepoPushAction(options: { repo, }); - const client = new Octokit(octokitOptions); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); const targetRepo = await client.rest.repos.get({ owner, repo }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index 39a659cd0a..7345f7dd9f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -149,16 +149,18 @@ export function createGithubWebhookAction(options: { throw new InputError('Invalid repository owner provided in repoUrl'); } - const client = new Octokit( - await getOctokitOptions({ - integrations, - credentialsProvider: githubCredentialsProvider, - host, - owner, - repo, - token: providedToken, - }), - ); + const octokitOptions = await getOctokitOptions({ + integrations, + credentialsProvider: githubCredentialsProvider, + host, + owner, + repo, + token: providedToken, + }); + const client = new Octokit({ + ...octokitOptions, + log: ctx.logger, + }); // If this is a dry run, log and return if (ctx.isDryRun) {