refactor(scaffolder): split getOctokitOptions repoUrl parameter into host, owner and repo parameters

Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
Benjamin Janssens
2025-01-31 13:32:19 +01:00
committed by blam
parent 8e5f283244
commit fda96db681
14 changed files with 67 additions and 44 deletions
@@ -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<OctokitOptions>;
// @public
@@ -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');
}
@@ -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,
}),
@@ -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,
}),
@@ -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}`);
}
@@ -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}`);
}
@@ -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}`);
}
@@ -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,
}),
);
@@ -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');
}
@@ -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,
});
@@ -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');
}
@@ -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);
@@ -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,
}),
);
@@ -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<OctokitOptions> {
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(