import {z} from 'zod' export type RepositoryID = string; export type RepositorySet = Array; export type IndexedGroups = Array>; export type DeveloperID = string; export type DeploymentWith = {pr_author?: Array | undefined, commit_author?: Array | undefined, releaser?: Array | undefined}; export type JIRAStatusCategory = 'To Do' | 'In Progress' | 'Done' | 'No Category'; export type JIRAFilter = {epics?: Array | undefined, labels_include?: Array | undefined, labels_exclude?: Array | undefined, issue_types?: Array | undefined, priorities?: Array | undefined, projects?: Array | undefined, status_categories?: Array | undefined, unmapped?: boolean | undefined}; export type ForSetDeployments = {repositories?: RepositorySet | undefined, repogroups?: IndexedGroups | undefined, with?: DeploymentWith | undefined, withgroups?: Array | undefined, pr_labels_include?: Array | undefined, pr_labels_exclude?: Array | undefined, jira?: JIRAFilter | undefined, with_labels?: Record | undefined, without_labels?: Record | undefined, environments?: Array | undefined, envgroups?: Array> | undefined}; export type DeploymentMetricID = 'dep-count' | 'dep-duration-all' | 'dep-duration-successful' | 'dep-duration-failed' | 'dep-success-count' | 'dep-failure-count' | 'dep-success-ratio' | 'dep-change-failure-count' | 'dep-change-failure-ratio' | 'dep-size-prs' | 'dep-size-releases' | 'dep-size-lines' | 'dep-size-commits' | 'dep-prs-count' | 'dep-releases-count' | 'dep-lines-count' | 'dep-commits-count' | 'dep-jira-issues-count' | 'dep-jira-bug-fixes-count'; export type TimeZone = number; export type Granularity = string; export type Granularities = Array; export type Quantiles = Array; export type DeploymentMetricsRequest = {for: Array, metrics: Array, date_from: string, date_to: string, timezone?: TimeZone | undefined, granularities: Granularities, quantiles?: Quantiles | undefined, account: number}; export type CreateApiMetricsDeploymentsArgs = {body: DeploymentMetricsRequest}; export type CreateApiMetricsDeploymentsOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const repositoryID = z.string(); export const repositorySet = z.array(repositoryID); export const indexedGroups = z.array(z.array(z.number().int().gte(0))); export const developerID = z.string(); export const deploymentWith = z.object({pr_author: z.array(developerID).optional(), commit_author: z.array(developerID).optional(), releaser: z.array(developerID).optional()}); export const jIRAStatusCategory = z.enum(["To Do", "In Progress", "Done", "No Category"]); export const jIRAFilter = z.object({epics: z.array(z.string()).optional(), labels_include: z.array(z.string()).optional(), labels_exclude: z.array(z.string()).optional(), issue_types: z.array(z.string()).optional(), priorities: z.array(z.string()).optional(), projects: z.array(z.string()).optional(), status_categories: z.array(jIRAStatusCategory).optional(), unmapped: z.boolean().optional()}); export const forSetDeployments = z.object({repositories: repositorySet.optional(), repogroups: indexedGroups.optional(), with: deploymentWith.optional(), withgroups: z.array(deploymentWith).optional(), pr_labels_include: z.array(z.string()).optional(), pr_labels_exclude: z.array(z.string()).optional(), jira: jIRAFilter.optional(), with_labels: z.object({}).optional(), without_labels: z.object({}).optional(), environments: z.array(z.string()).optional(), envgroups: z.array(z.array(z.string())).optional()}); export const deploymentMetricID = z.enum(["dep-count", "dep-duration-all", "dep-duration-successful", "dep-duration-failed", "dep-success-count", "dep-failure-count", "dep-success-ratio", "dep-change-failure-count", "dep-change-failure-ratio", "dep-size-prs", "dep-size-releases", "dep-size-lines", "dep-size-commits", "dep-prs-count", "dep-releases-count", "dep-lines-count", "dep-commits-count", "dep-jira-issues-count", "dep-jira-bug-fixes-count"]); export const granularity = z.string(); export const timeDuration = z.string(); export const calculatedLinearMetricValues = z.object({date: z.string(), values: z.array(z.union([z.number(), timeDuration, z.object({})])), confidence_mins: z.array(z.union([z.number().nullable(), timeDuration])).optional(), confidence_maxs: z.array(z.union([z.number().nullable(), timeDuration])).optional(), confidence_scores: z.array(z.number().int().nullable()).optional()}); export const calculatedDeploymentMetric = z.object({for: forSetDeployments, metrics: z.array(deploymentMetricID), granularity: granularity, values: z.array(calculatedLinearMetricValues)}); export const calculatedDeploymentMetrics = z.array(calculatedDeploymentMetric); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Calculate metrics on deployments submitted by `/events/deployments`. * * POST /metrics/deployments * * Also accepts auth: apiKeyAuth */ export const createApiMetricsDeployments = async (args: CreateApiMetricsDeploymentsArgs, options: CreateApiMetricsDeploymentsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/metrics/deployments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(calculatedDeploymentMetrics, await res.json()) };