@google-automations/bot-config-utils
Version:
Utilities for github bot config
158 lines (157 loc) • 6.8 kB
TypeScript
import { Octokit } from '@octokit/rest';
import { GCFLogger } from 'gcf-utils';
/**
* The return value of `validateConfig` function.
*
* @template ConfigType
* @prop {boolean} isValid - The result of the schema validation.
* @prop {string} errorText - The error message from the validation.
* @prop {ConfigType} config - The loaded config object,only available
* when the validation succeeds.
*/
export interface ValidateConfigResult<ConfigType> {
isValid: boolean;
errorText: string | undefined;
config?: ConfigType;
}
/**
* The optional arguments for `validateConfig`.
*
* @prop {Array<object>} additionalSchemas - Additional schemas for Ajv schema
* validator. When you have complex schema that used definition in different
* files, you need to give all the schema definitions to Ajv.
*/
export interface ValidateConfigOptions {
additionalSchemas?: Array<object>;
}
/**
* It loads the given string as yaml, then validates against the given schema.
*
* @template ConfigType
* @param {string} configYaml - The string representation of the config.
* @param {object} schema - The schema definition.
* @param {ValidateConfigOptions} options - Optional arguments for validation.
*
* @return {ValidateConfigResult<ConfigType>}
*/
export declare function validateConfig<ConfigType>(configYaml: string, schema: object, options: ValidateConfigOptions): ValidateConfigResult<ConfigType>;
/**
* A class for validating the config changes on pull requests.
*
* @template ConfigType
*/
export declare class ConfigChecker<ConfigType> {
private configPath;
private checker;
constructor(schema: object, configFileName: string, additionalSchemas?: Array<object>);
/**
* A function for getting the config object validated by Ajv.
*
* @return {ConfigType | null} When the validation fails, it returns null.
*/
getConfig(): ConfigType | null;
/**
* A function for validate the config file against given schema. It
* will create a failing Github Check on the commit when validation fails.
*
* @param {Octokit} octokit - Authenticated octokit object.
* @param {string} owner - The owner of the base repository of the PR.
* @param {string} repo - The name of the base repository of the PR.
* @param {string} commitSha - The commit hash of the tip of the PR head.
* @param {number} prNumber - The number of the PR.
*
* @return {Promise<boolean>} Returns 'true' if config is valid, 'false' if invalid.
*/
validateConfigChanges(octokit: Octokit, owner: string, repo: string, commitSha: string, prNumber: number, logger?: GCFLogger): Promise<boolean>;
}
/**
* A class for validating multiple config file changes on pull requests.
* It validates both the schema and common file extension mismatches (e.g.
* yaml <-> yml).
*/
export declare class MultiConfigChecker {
private schemasByFile;
private additionalSchemasByFile;
private configNamesByFile;
private badConfigFiles;
private parsedConfigs;
/**
* Instantiate a new MultiConfigChecker
*
* @param {Record<string, object>} schemasByFile JSON schemas indexed by filename
* @param {Record<string, object[]>} additionalSchemasByFile Additional JSON schemas indexed by filename
*/
constructor(schemasByFile: Record<string, object>, additionalSchemasByFile?: Record<string, object[]>);
/**
* Returns the parsed config for a given filename. Only available after
* the config file has been validated (and is valid).
* @param {string} filename The path of the config
*/
getConfig(filename: string): object | null;
/**
* A function for validate the config file against given schema. It
* will create a failing Github Check per config fiel on the commit
* when validation fails.
*
* @param {Octokit} octokit - Authenticated octokit object.
* @param {string} owner - The owner of the base repository of the PR.
* @param {string} repo - The name of the base repository of the PR.
* @param {string} commitSha - The commit hash of the tip of the PR head.
* @param {number} prNumber - The number of the PR.
* @param {GCFLogger} logger - Optional. Logger for debug output.
*
* @return {Promise<boolean>} Returns 'true' if config is valid, 'false' if invalid.
*/
validateConfigChanges(octokit: Octokit, owner: string, repo: string, commitSha: string, prNumber: number, logger?: GCFLogger): Promise<boolean>;
}
/**
* Optional arguments to `getConfig` and `getConfigWithDefault`.
*
* @param {boolean} fallbackToOrgConfig - If set to true, it will try to fetch
* the config from `.github` repo in the same org, defaults to true.
* @param {string} branch - The branch for getting the config.
* @param {object} schema - The json schema definition.
* @param {Array<object>} additionalSchemas - Additional schema definitions.
*/
export interface getConfigOptions {
fallbackToOrgConfig?: boolean;
branch?: string;
schema?: object;
additionalSchemas?: Array<object>;
}
export declare class InvalidConfigurationFormat extends Error {
readonly path: string;
constructor(path: string, validationMessage?: string);
}
/**
* A function for fetching config file from the repo. It falls back to
* `.github` repository by default.
*
* @template ConfigType
* @param {Octokit} octokit - Authenticated octokit object.
* @param {string} owner - The owner of the repository.
* @param {string} repo - The name of the repository.
* @param {string} fileName - The filename of the config file.
* @param {getConfigOptions} options - Optional arguments.
*
* @return {Promise<ConfigType | null>} - It returns null when config is not
* found.
*/
export declare function getConfig<ConfigType>(octokit: Octokit, owner: string, repo: string, fileName: string, options?: getConfigOptions): Promise<ConfigType | null>;
/**
* A function for fetching config file from the repo. It falls back to
* `.github` repository by default.
*
* @template ConfigType
* @param {Octokit} octokit - Authenticated octokit object.
* @param {string} owner - The owner of the repository.
* @param {string} repo - The name of the repository.
* @param {string} fileName - The filename of the config file.
* @param {ConfigType} defaultConfig - This can be used for filling the default
* value of the config.
* @param {getConfigOptions} options - Optional arguments.
*
* @return {Promise<ConfigType>} - It returns the given defaultConfig when
* config file is not found.
*/
export declare function getConfigWithDefault<ConfigType>(octokit: Octokit, owner: string, repo: string, fileName: string, defaultConfig: ConfigType, options?: getConfigOptions): Promise<ConfigType>;