UNPKG

@halospv3/hce.shared-config

Version:

Automate commit message quality, changelogs, and CI/CD releases. Its `main` entry point is a Semantic Release config. Functions and classes are exposed for customization. An ESLint config, a Commitlint config, and addl. resources for .NET projects are als

128 lines (119 loc) 5.42 kB
import { getEnvVarValue as getEnvironmentVariableValue } from '../utils/env.ts'; import { isError } from '../utils/isError.ts'; import { NugetRegistryInfo, NRIOpts as NRIOptions, NRIOptsBase as NRIOptionsBase, } from './NugetRegistryInfo.ts'; import type { Out, Type } from 'arktype'; import type { Default } from 'arktype/internal/attributes.ts'; import type { MSBuildProject, MSBuildEvaluationOutput } from './MSBuildProject.ts'; import type { NugetProjectProperties } from './NugetProjectProperties.ts'; // https://docs.gitlab.com/ee/user/packages/nuget_repository/ export class GitlabNugetRegistryInfo extends NugetRegistryInfo { static readonly DefaultGitlabTokenEnvVars: readonly ['GL_TOKEN', 'GITLAB_TOKEN', 'CI_JOB_TOKEN'] = Object.freeze([ 'GL_TOKEN', 'GITLAB_TOKEN', 'CI_JOB_TOKEN', ] as const); /** * The GitLab API v4 root URL. * @returns The value of the environment variable `CI_API_V4_URL`. * If that's `undefined`, 'https://gitlab.com/api/v4' is returned, instead. */ static get CI_API_V4_URL(): string { return getEnvironmentVariableValue('CI_API_V4_URL') ?? 'https://gitlab.com/api/v4'; } /** * CI_PROJECT_ID - If you want to publish to your GitLab server, this needs to be set to the Id of the project you want to publish to. When running in GitLab CI this is already set to the project the pipeline runs in by GitLab. * This method checks the contents of your `.env` file, if present. * @returns The value of the environment variable `CI_PROJECT_ID` or `undefined`. * @todo add URI encoded project pathname as alternative e.g. 'halospv3%2FHCE.Shared' in 'https://gitlab.com/api/v4/projects/halospv3%2FHCE.Shared' */ static get projectId(): string | undefined { return getEnvironmentVariableValue('CI_PROJECT_ID'); } /** * CI_PROJECT_NAMESPACE_ID * This method checks the contents of your `.env` file, if present. * @returns The value of the environment variable 'CI_PROJECT_NAMESPACE_ID' or `undefined`. */ static get ownerId(): string | undefined { return getEnvironmentVariableValue('CI_PROJECT_NAMESPACE_ID'); } /** * Get the GitLab Nuget API for your project url as seen in https://docs.gitlab.com/ee/user/packages/nuget_repository/index.html#publish-a-nuget-package-by-using-cicd * ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/nuget/index.json * @returns If {@link this.projectId} is a string, a string formatted like * `${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/nuget/index.json`. * Else, {@link Error}. */ static get projectUrl(): string | Error { return this.projectId ? `${this.CI_API_V4_URL}/projects/${this.projectId}/packages/nuget/index.json` : new Error('The project-type URL was specified, but one or more of the required environment variables (CI_API_V4_URL, CI_PROJECT_ID) were undefined.'); } /** * ${CI_API_V4_URL}/groups/${CI_PROJECT_NAMESPACE_ID}/-/packages/nuget/index.json * @returns If {@link ownerId} is a string, then a string formatted like * `${CI_API_V4_URL}/groups/${CI_PROJECT_NAMESPACE_ID}/-/packages/nuget/index.json`. * Else, {@link Error}. */ static get groupUrl(): string | Error { return this.ownerId ? `${this.CI_API_V4_URL}/groups/${this.ownerId}/-/packages/nuget/index.json` : new Error('env.CI_PROJECT_NAMESPACE_ID must be defined to use its GitLab API endpoint!'); } /** * Creates an instance of GitlabNugetRegistryInfo. * @param opts The input type of {@link GLNRIOpts.from} */ // eslint-disable-next-line unicorn/name-replacements constructor(opts: typeof GLNRIOpts.inferIn) { const optionsOut = GLNRIOpts.from(opts); if (isError(optionsOut.source)) throw optionsOut.source; super(optionsOut as typeof optionsOut & { source: string }); } } const GLNRI = GitlabNugetRegistryInfo; /** * The Arktype definition for {@link GitlabNugetRegistryInfo}'s constructor parameter. Construct an object of this type by calling {@link GLNRIOpts.from} */ // eslint-disable-next-line unicorn/name-replacements export const GLNRIOpts: Type<{ project: MSBuildProject | { readonly Items: Readonly<Required<MSBuildEvaluationOutput>['Items']>; readonly Properties: Readonly<NugetProjectProperties>; readonly Targets: readonly string[]; readonly TargetResults: Required<MSBuildEvaluationOutput>['TargetResults'][]; }; tokenEnvVars: Default<readonly string[], readonly ['GL_TOKEN', 'GITLAB_TOKEN', 'CI_JOB_TOKEN']>; source: (In: Default<string | Error, string | Error>) => Out<string | Error>; }> = NRIOptions.merge({ tokenEnvVars: NRIOptionsBase.get('tokenEnvVars').default( () => GLNRI.DefaultGitlabTokenEnvVars, ), /** * The GitLab Nuget API URL to push packages to -OR- a keyword such as "group" * or "project" used to determine URL. * @default GLNRI.projectUrl * @see {@link GLNRI.projectUrl}, {@link GLNRI.groupUrl} */ // todo: change '"group" | "project"' to '"GITLAB:PROJECT" | "GITLAB:GROUP"' source: NRIOptionsBase.get('source') .or('"group" | "project" | Error') .pipe((source: string | Error): string | Error => { switch (source) { case 'group': { return GLNRI.groupUrl; } case 'project': { return GLNRI.projectUrl; } default: { return source; } } }).default(() => GLNRI.projectUrl), });