From a4563ffc608dd9d6ef12fc84c82eec77f5d8310b Mon Sep 17 00:00:00 2001 From: Benjamin Janssens Date: Fri, 31 Jan 2025 13:32:19 +0100 Subject: [PATCH] refactor(scaffolder): split getOctokitOptions repoUrl parameter into host, owner and repo parameters Signed-off-by: Benjamin Janssens --- .../report.api.md | 4 +++- .../src/actions/github.ts | 8 ++++--- .../src/actions/githubActionsDispatch.ts | 6 ++++-- .../src/actions/githubAutolinks.ts | 6 ++++-- .../src/actions/githubBranchProtection.ts | 8 ++++--- .../src/actions/githubDeployKey.ts | 8 ++++--- .../src/actions/githubEnvironment.ts | 8 ++++--- .../src/actions/githubIssuesLabel.ts | 6 ++++-- .../src/actions/githubPagesEnable.ts | 8 ++++--- .../src/actions/githubPullRequest.ts | 8 +++---- .../src/actions/githubRepoCreate.ts | 8 ++++--- .../src/actions/githubRepoPush.ts | 6 ++++-- .../src/actions/githubWebhook.ts | 6 ++++-- .../src/actions/helpers.ts | 21 ++++++++++--------- 14 files changed, 67 insertions(+), 44 deletions(-) diff --git a/plugins/scaffolder-backend-module-github/report.api.md b/plugins/scaffolder-backend-module-github/report.api.md index 935886bf14..782846dec7 100644 --- a/plugins/scaffolder-backend-module-github/report.api.md +++ b/plugins/scaffolder-backend-module-github/report.api.md @@ -477,8 +477,10 @@ export const createPublishGithubPullRequestAction: ( export function getOctokitOptions(options: { integrations: ScmIntegrationRegistry; credentialsProvider?: GithubCredentialsProvider; + host: string; + owner?: string; + repo?: string; token?: string; - repoUrl: string; }): Promise; // @public diff --git a/plugins/scaffolder-backend-module-github/src/actions/github.ts b/plugins/scaffolder-backend-module-github/src/actions/github.ts index e3a9ff3352..da54b7c8cb 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/github.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/github.ts @@ -225,16 +225,18 @@ export function createPublishGithubAction(options: { requiredLinearHistory = false, } = ctx.input; + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); const client = new Octokit(octokitOptions); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts index 0d24b74927..4c83f538ce 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubActionsDispatch.ts @@ -96,7 +96,7 @@ export function createGithubActionsDispatchAction(options: { `Dispatching workflow ${workflowId} for repo ${repoUrl} on ${branchOrTagName}`, ); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); @@ -105,7 +105,9 @@ export function createGithubActionsDispatchAction(options: { const client = new Octokit( await getOctokitOptions({ integrations, - repoUrl, + host, + owner, + repo, credentialsProvider: githubCredentialsProvider, token: providedToken, }), diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts index 4b0d0e1e1f..2037287fad 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubAutolinks.ts @@ -89,7 +89,7 @@ export function createGithubAutolinksAction(options: { ctx.logger.info(`Creating autolink reference for repo ${repoUrl}`); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); @@ -98,7 +98,9 @@ export function createGithubAutolinksAction(options: { const client = new Octokit( await getOctokitOptions({ integrations, - repoUrl, + host, + owner, + repo, credentialsProvider: githubCredentialsProvider, token, }), diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts index a7555d3af1..b8716a3f8f 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubBranchProtection.ts @@ -115,15 +115,17 @@ export function createGithubBranchProtectionAction(options: { token: providedToken, } = ctx.input; + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); const client = new Octokit(octokitOptions); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError(`No owner provided for repo ${repoUrl}`); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts index ba55b6a1fb..c9a881c6b1 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubDeployKey.ts @@ -107,14 +107,16 @@ export function createGithubDeployKeyAction(options: { token: providedToken, } = ctx.input; + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError(`No owner provided for repo ${repoUrl}`); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts index ef59b611fe..736b562d54 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubEnvironment.ts @@ -175,14 +175,16 @@ export function createGithubEnvironmentAction(options: { // Add a 2-second delay before initiating the steps in this action. await new Promise(resolve => setTimeout(resolve, 2000)); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError(`No owner provided for repo ${repoUrl}`); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts index 90f5a89dc7..7814429bd7 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubIssuesLabel.ts @@ -80,7 +80,7 @@ export function createGithubIssuesLabelAction(options: { async handler(ctx) { const { repoUrl, number, labels, token: providedToken } = ctx.input; - const { owner, repo } = parseRepoUrl(repoUrl, integrations); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); ctx.logger.info(`Adding labels to ${number} issue on repo ${repo}`); if (!owner) { @@ -91,7 +91,9 @@ export function createGithubIssuesLabelAction(options: { await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, - repoUrl: repoUrl, + host, + owner, + repo, token: providedToken, }), ); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts index 42b9c47030..f8df207638 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPagesEnable.ts @@ -92,16 +92,18 @@ export function createGithubPagesEnableAction(options: { token: providedToken, } = ctx.input; + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); const client = new Octokit(octokitOptions); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts index 7fe9d8e401..63496133f1 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubPullRequest.ts @@ -49,14 +49,12 @@ export const defaultClientFactory: CreateGithubPullRequestActionOptions['clientF host = 'github.com', token: providedToken, }) => { - const [encodedHost, encodedOwner, encodedRepo] = [host, owner, repo].map( - encodeURIComponent, - ); - const octokitOptions = await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, - repoUrl: `${encodedHost}?owner=${encodedOwner}&repo=${encodedRepo}`, + host, + owner, + repo, token: providedToken, }); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts index b4608e57de..a3a758e005 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoCreate.ts @@ -182,16 +182,18 @@ export function createGithubRepoCreateAction(options: { token: providedToken, } = ctx.input; + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); + const octokitOptions = await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, token: providedToken, - repoUrl: repoUrl, + host, + owner, + repo, }); const client = new Octokit(octokitOptions); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); - if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); } diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts index 62624057c8..b11c4b8f5c 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubRepoPush.ts @@ -142,7 +142,7 @@ export function createGithubRepoPushAction(options: { requiredLinearHistory = false, } = ctx.input; - const { owner, repo } = parseRepoUrl(repoUrl, integrations); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); @@ -152,7 +152,9 @@ export function createGithubRepoPushAction(options: { integrations, credentialsProvider: githubCredentialsProvider, token: providedToken, - repoUrl, + host, + owner, + repo, }); const client = new Octokit(octokitOptions); diff --git a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts index e188cdf0cb..0289d3c69b 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/githubWebhook.ts @@ -134,7 +134,7 @@ export function createGithubWebhookAction(options: { } = ctx.input; ctx.logger.info(`Creating webhook ${webhookUrl} for repo ${repoUrl}`); - const { owner, repo } = parseRepoUrl(repoUrl, integrations); + const { host, owner, repo } = parseRepoUrl(repoUrl, integrations); if (!owner) { throw new InputError('Invalid repository owner provided in repoUrl'); @@ -144,7 +144,9 @@ export function createGithubWebhookAction(options: { await getOctokitOptions({ integrations, credentialsProvider: githubCredentialsProvider, - repoUrl: repoUrl, + host, + owner, + repo, token: providedToken, }), ); diff --git a/plugins/scaffolder-backend-module-github/src/actions/helpers.ts b/plugins/scaffolder-backend-module-github/src/actions/helpers.ts index 242f95ce34..45e4229e73 100644 --- a/plugins/scaffolder-backend-module-github/src/actions/helpers.ts +++ b/plugins/scaffolder-backend-module-github/src/actions/helpers.ts @@ -27,7 +27,6 @@ import { Octokit } from 'octokit'; import { getRepoSourceDirectory, initRepoAndPush, - parseRepoUrl, } from '@backstage/plugin-scaffolder-node'; import Sodium from 'libsodium-wrappers'; @@ -47,21 +46,19 @@ const DEFAULT_TIMEOUT_MS = 60_000; export async function getOctokitOptions(options: { integrations: ScmIntegrationRegistry; credentialsProvider?: GithubCredentialsProvider; + host: string; + owner?: string; + repo?: string; token?: string; - repoUrl: string; }): Promise { - const { integrations, credentialsProvider, repoUrl, token } = options; - const { owner, repo, host } = parseRepoUrl(repoUrl, integrations); + const { integrations, credentialsProvider, host, owner, repo, token } = + options; const requestOptions = { // set timeout to 60 seconds timeout: DEFAULT_TIMEOUT_MS, }; - if (!owner) { - throw new InputError(`No owner provided for repo ${repoUrl}`); - } - const integrationConfig = integrations.github.byHost(host)?.config; if (!integrationConfig) { @@ -78,12 +75,16 @@ export async function getOctokitOptions(options: { }; } + if (!owner || !repo) { + throw new InputError( + `No owner and/or owner provided, which is required if a token is not provided`, + ); + } + const githubCredentialsProvider = credentialsProvider ?? DefaultGithubCredentialsProvider.fromIntegrations(integrations); - // TODO(blam): Consider changing this API to take host and repo instead of repoUrl, as we end up parsing in this function - // and then parsing in the `getCredentials` function too the other side const { token: credentialProviderToken } = await githubCredentialsProvider.getCredentials({ url: `https://${host}/${encodeURIComponent(owner)}/${encodeURIComponent(