refactor(scaffolder): move getOctokitOptions to separate util file

Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
Benjamin Janssens
2025-01-31 13:44:33 +01:00
committed by blam
parent fda96db681
commit 95439bbdad
18 changed files with 110 additions and 95 deletions
@@ -27,9 +27,9 @@ import {
} from '@backstage/plugin-scaffolder-node';
import {
createGithubRepoWithCollaboratorsAndTopics,
getOctokitOptions,
initRepoPushAndProtect,
} from './helpers';
import { getOctokitOptions } from '../util';
import * as inputProps from './inputProperties';
import * as outputProps from './outputProperties';
import { examples } from './github.examples';
@@ -24,7 +24,7 @@ import {
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import { Octokit } from 'octokit';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { examples } from './githubActionsDispatch.examples';
/**
@@ -25,7 +25,7 @@ import {
} from '@backstage/plugin-scaffolder-node';
import { Octokit } from 'octokit';
import { examples } from './githubAutolinks.examples';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
/**
* Create an autolink reference for a repository
@@ -22,7 +22,7 @@ import {
import { ScmIntegrationRegistry } from '@backstage/integration';
import { examples } from './githubBranchProtection.examples';
import * as inputProps from './inputProperties';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { Octokit } from 'octokit';
import { enableBranchProtectionOnDefaultRepoBranch } from './gitHelpers';
@@ -20,7 +20,7 @@ import {
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import { ScmIntegrationRegistry } from '@backstage/integration';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { Octokit } from 'octokit';
import Sodium from 'libsodium-wrappers';
import { examples } from './githubDeployKey.examples';
@@ -20,7 +20,7 @@ import {
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import { ScmIntegrationRegistry } from '@backstage/integration';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { Octokit } from 'octokit';
import Sodium from 'libsodium-wrappers';
import { examples } from './gitHubEnvironment.examples';
@@ -25,7 +25,7 @@ import {
import { createGithubIssuesLabelAction } from './githubIssuesLabel';
import yaml from 'yaml';
import { examples } from './githubIssuesLabel.examples';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
jest.mock('./helpers', () => {
return {
@@ -23,7 +23,7 @@ import {
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
import { ConfigReader } from '@backstage/config';
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
jest.mock('./helpers', () => {
return {
@@ -24,7 +24,7 @@ import {
} from '@backstage/plugin-scaffolder-node';
import { assertError, InputError } from '@backstage/errors';
import { Octokit } from 'octokit';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { examples } from './githubIssuesLabel.examples';
/**
@@ -25,7 +25,7 @@ import {
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import { examples } from './githubPagesEnable.examples';
import { getOctokitOptions } from '@backstage/plugin-scaffolder-backend-module-github';
import { getOctokitOptions } from '../util';
/**
* Creates a new action that enables GitHub Pages for a repository.
@@ -28,7 +28,7 @@ import {
import { Octokit } from 'octokit';
import { CustomErrorBase, InputError } from '@backstage/errors';
import { createPullRequest } from 'octokit-plugin-create-pull-request';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { examples } from './githubPullRequest.examples';
import {
LoggerService,
@@ -24,10 +24,8 @@ import {
createTemplateAction,
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import {
createGithubRepoWithCollaboratorsAndTopics,
getOctokitOptions,
} from './helpers';
import { createGithubRepoWithCollaboratorsAndTopics } from './helpers';
import { getOctokitOptions } from '../util';
import * as inputProps from './inputProperties';
import * as outputProps from './outputProperties';
import { examples } from './githubRepoCreate.examples';
@@ -25,7 +25,8 @@ import {
createTemplateAction,
parseRepoUrl,
} from '@backstage/plugin-scaffolder-node';
import { getOctokitOptions, initRepoPushAndProtect } from './helpers';
import { initRepoPushAndProtect } from './helpers';
import { getOctokitOptions } from '../util';
import * as inputProps from './inputProperties';
import * as outputProps from './outputProperties';
import { examples } from './githubRepoPush.examples';
@@ -25,7 +25,7 @@ import {
import { emitterEventNames } from '@octokit/webhooks';
import { assertError, InputError } from '@backstage/errors';
import { Octokit } from 'octokit';
import { getOctokitOptions } from './helpers';
import { getOctokitOptions } from '../util';
import { examples } from './githubWebhook.examples';
/**
@@ -15,13 +15,7 @@
*/
import { Config } from '@backstage/config';
import { assertError, InputError, NotFoundError } from '@backstage/errors';
import {
DefaultGithubCredentialsProvider,
GithubCredentialsProvider,
ScmIntegrationRegistry,
} from '@backstage/integration';
import { OctokitOptions } from '@octokit/core/dist-types/types';
import { assertError, NotFoundError } from '@backstage/errors';
import { Octokit } from 'octokit';
import {
@@ -36,75 +30,6 @@ import {
} from './gitHelpers';
import { LoggerService } from '@backstage/backend-plugin-api';
const DEFAULT_TIMEOUT_MS = 60_000;
/**
* Helper for generating octokit configuration options for given repoUrl.
* If no token is provided, it will attempt to get a token from the credentials provider.
* @public
*/
export async function getOctokitOptions(options: {
integrations: ScmIntegrationRegistry;
credentialsProvider?: GithubCredentialsProvider;
host: string;
owner?: string;
repo?: string;
token?: string;
}): Promise<OctokitOptions> {
const { integrations, credentialsProvider, host, owner, repo, token } =
options;
const requestOptions = {
// set timeout to 60 seconds
timeout: DEFAULT_TIMEOUT_MS,
};
const integrationConfig = integrations.github.byHost(host)?.config;
if (!integrationConfig) {
throw new InputError(`No integration for host ${host}`);
}
// short circuit the `githubCredentialsProvider` if there is a token provided by the caller already
if (token) {
return {
auth: token,
baseUrl: integrationConfig.apiBaseUrl,
previews: ['nebula-preview'],
request: requestOptions,
};
}
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);
const { token: credentialProviderToken } =
await githubCredentialsProvider.getCredentials({
url: `https://${host}/${encodeURIComponent(owner)}/${encodeURIComponent(
repo,
)}`,
});
if (!credentialProviderToken) {
throw new InputError(
`No token available for host: ${host}, with owner ${owner}, and repo ${repo}. Make sure GitHub auth is configured correctly. See https://backstage.io/docs/auth/github/provider for more details.`,
);
}
return {
auth: credentialProviderToken,
baseUrl: integrationConfig.apiBaseUrl,
previews: ['nebula-preview'],
};
}
export async function createGithubRepoWithCollaboratorsAndTopics(
client: Octokit,
repo: string,
@@ -29,5 +29,3 @@ export { createPublishGithubAction } from './github';
export { createGithubAutolinksAction } from './githubAutolinks';
export { createGithubPagesEnableAction } from './githubPagesEnable';
export { createGithubBranchProtectionAction } from './githubBranchProtection';
export { getOctokitOptions } from './helpers';
@@ -22,3 +22,4 @@
export * from './actions';
export { githubModule as default } from './module';
export { getOctokitOptions } from './util';
@@ -0,0 +1,92 @@
/*
* Copyright 2025 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { InputError } from '@backstage/errors';
import {
DefaultGithubCredentialsProvider,
GithubCredentialsProvider,
ScmIntegrationRegistry,
} from '@backstage/integration';
import { OctokitOptions } from '@octokit/core/dist-types/types';
const DEFAULT_TIMEOUT_MS = 60_000;
/**
* Helper for generating octokit configuration options for given repoUrl.
* If no token is provided, it will attempt to get a token from the credentials provider.
* @public
*/
export async function getOctokitOptions(options: {
integrations: ScmIntegrationRegistry;
credentialsProvider?: GithubCredentialsProvider;
host: string;
owner?: string;
repo?: string;
token?: string;
}): Promise<OctokitOptions> {
const { integrations, credentialsProvider, host, owner, repo, token } =
options;
const requestOptions = {
// set timeout to 60 seconds
timeout: DEFAULT_TIMEOUT_MS,
};
const integrationConfig = integrations.github.byHost(host)?.config;
if (!integrationConfig) {
throw new InputError(`No integration for host ${host}`);
}
// short circuit the `githubCredentialsProvider` if there is a token provided by the caller already
if (token) {
return {
auth: token,
baseUrl: integrationConfig.apiBaseUrl,
previews: ['nebula-preview'],
request: requestOptions,
};
}
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);
const { token: credentialProviderToken } =
await githubCredentialsProvider.getCredentials({
url: `https://${host}/${encodeURIComponent(owner)}/${encodeURIComponent(
repo,
)}`,
});
if (!credentialProviderToken) {
throw new InputError(
`No token available for host: ${host}, with owner ${owner}, and repo ${repo}. Make sure GitHub auth is configured correctly. See https://backstage.io/docs/auth/github/provider for more details.`,
);
}
return {
auth: credentialProviderToken,
baseUrl: integrationConfig.apiBaseUrl,
previews: ['nebula-preview'],
};
}