import {z} from 'zod' export type ProtosProjectsV1RepoFilters = {search?: string | undefined, codeAccess?: boolean | undefined, lastScanSince?: string | undefined, setup?: boolean | undefined, archived?: boolean | undefined, ids?: Array | undefined, hasSmsSetup?: boolean | undefined, isScmRepository?: boolean | undefined, invertSearch?: boolean | undefined, isLocalScanProject?: boolean | undefined, scmType?: ('SCM_TYPE_GITHUB' | 'SCM_TYPE_GITHUB_ENTERPRISE' | 'SCM_TYPE_GITLAB' | 'SCM_TYPE_GITLAB_SELFMANAGED' | 'SCM_TYPE_BITBUCKET' | 'SCM_TYPE_BITBUCKET_DATACENTER' | 'SCM_TYPE_AZURE_DEVOPS' | 'SCM_TYPE_UNKNOWN' | 'SCM_TYPE_HARNESS') | undefined, tagId?: number | undefined}; export type ProtosProjectsV1BulkAddRepoSecretRequest = {deploymentId?: string | undefined, filters?: ProtosProjectsV1RepoFilters | undefined, excludeIds?: Array | undefined}; export type CreateApiApiAgentDeploymentsDeploymentIdReposSecretArgs = {deploymentId: string, body: ProtosProjectsV1BulkAddRepoSecretRequest}; export type CreateApiApiAgentDeploymentsDeploymentIdReposSecretOptions = {baseUrl: string, SemgrepWebToken?: string | undefined, fetch?: typeof fetch | undefined}; export const protosCommonV1BulkEndpointError = z.object({code: z.enum(["UPDATE_ERROR_CODE_BAD_REQUEST", "UPDATE_ERROR_CODE_NOT_FOUND", "UPDATE_ERROR_CODE_CONFLICT", "UPDATE_ERROR_CODE_UNAUTHORIZED"]).optional(), message: z.string().optional(), ids: z.array(z.string()).optional()}); export const protosProjectsV1BulkAddRepoSecretResponse = z.object({repoTokenIds: z.record(z.string(), z.string()).optional(), errors: z.array(protosCommonV1BulkEndpointError).optional()}); 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 }; /** * Add Semgrep CI Secrets to GitHub Repositories * * POST /api/agent/deployments/{deploymentId}/repos/_secret * * Also accepts auth: SemgrepJWT */ export const createApiApiAgentDeploymentsDeploymentIdReposSecret = async (args: CreateApiApiAgentDeploymentsDeploymentIdReposSecretArgs, options: CreateApiApiAgentDeploymentsDeploymentIdReposSecretOptions) => { const {baseUrl, fetch: fetchFn = fetch, SemgrepWebToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (SemgrepWebToken !== undefined) headers.set('authorization', `Bearer ${SemgrepWebToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/agent/deployments/${encodeURIComponent(String(args.deploymentId))}/repos/_secret${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(protosProjectsV1BulkAddRepoSecretResponse, await res.json()) };