import {z} from 'zod' export type GetApiReposOwnerRepoSecurityAdvisoriesGhsaIdArgs = {owner: string, repo: string, ghsa_id: string}; export type GetApiReposOwnerRepoSecurityAdvisoriesGhsaIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const simpleUser = z.object({name: z.string().nullable().optional(), email: z.string().nullable().optional(), login: z.string(), id: z.number().int(), node_id: z.string(), avatar_url: z.string(), gravatar_id: z.string().nullable(), url: z.string(), html_url: z.string(), followers_url: z.string(), following_url: z.string(), gists_url: z.string(), starred_url: z.string(), subscriptions_url: z.string(), organizations_url: z.string(), repos_url: z.string(), events_url: z.string(), received_events_url: z.string(), type: z.string(), site_admin: z.boolean(), starred_at: z.string().optional(), user_view_type: z.string().optional()}); export const securityAdvisoryEcosystems = z.enum(["rubygems", "npm", "pip", "maven", "nuget", "composer", "go", "rust", "erlang", "actions", "pub", "other", "swift"]); export const repositoryAdvisoryVulnerability = z.object({package: z.object({ecosystem: securityAdvisoryEcosystems, name: z.string().nullable()}).nullable(), vulnerable_version_range: z.string().nullable(), patched_versions: z.string().nullable(), vulnerable_functions: z.array(z.string()).nullable()}); export const cvssSeverities = z.object({cvss_v3: z.object({vector_string: z.string().nullable(), score: z.number().gte(0).lte(10).nullable()}).nullable().optional(), cvss_v4: z.object({vector_string: z.string().nullable(), score: z.number().gte(0).lte(10).nullable()}).nullable().optional()}).nullable(); export const securityAdvisoryCreditTypes = z.enum(["analyst", "finder", "reporter", "coordinator", "remediation_developer", "remediation_reviewer", "remediation_verifier", "tool", "sponsor", "other"]); export const repositoryAdvisoryCredit = z.object({user: simpleUser, type: securityAdvisoryCreditTypes, state: z.enum(["accepted", "declined", "pending"])}); export const nullableTeamSimple = z.object({id: z.number().int(), node_id: z.string(), url: z.string(), members_url: z.string(), name: z.string(), description: z.string().nullable(), permission: z.string(), privacy: z.string().optional(), notification_setting: z.string().optional(), html_url: z.string(), repositories_url: z.string(), slug: z.string(), ldap_dn: z.string().optional(), type: z.enum(["enterprise", "organization"]), organization_id: z.number().int().optional(), enterprise_id: z.number().int().optional()}).nullable(); export const team = z.object({id: z.number().int(), node_id: z.string(), name: z.string(), slug: z.string(), description: z.string().nullable(), privacy: z.string().optional(), notification_setting: z.string().optional(), permission: z.string(), permissions: z.object({pull: z.boolean(), triage: z.boolean(), push: z.boolean(), maintain: z.boolean(), admin: z.boolean()}).optional(), url: z.string(), html_url: z.string(), members_url: z.string(), repositories_url: z.string(), type: z.enum(["enterprise", "organization"]), access_source: z.enum(["direct", "organization", "enterprise"]).optional(), organization_id: z.number().int().optional(), enterprise_id: z.number().int().optional(), parent: nullableTeamSimple}); export const simpleRepository = z.object({id: z.number().int(), node_id: z.string(), name: z.string(), full_name: z.string(), owner: simpleUser, private: z.boolean(), html_url: z.string(), description: z.string().nullable(), fork: z.boolean(), url: z.string(), archive_url: z.string(), assignees_url: z.string(), blobs_url: z.string(), branches_url: z.string(), collaborators_url: z.string(), comments_url: z.string(), commits_url: z.string(), compare_url: z.string(), contents_url: z.string(), contributors_url: z.string(), deployments_url: z.string(), downloads_url: z.string(), events_url: z.string(), forks_url: z.string(), git_commits_url: z.string(), git_refs_url: z.string(), git_tags_url: z.string(), issue_comment_url: z.string(), issue_events_url: z.string(), issues_url: z.string(), keys_url: z.string(), labels_url: z.string(), languages_url: z.string(), merges_url: z.string(), milestones_url: z.string(), notifications_url: z.string(), pulls_url: z.string(), releases_url: z.string(), stargazers_url: z.string(), statuses_url: z.string(), subscribers_url: z.string(), subscription_url: z.string(), tags_url: z.string(), teams_url: z.string(), trees_url: z.string(), hooks_url: z.string()}); export const repositoryAdvisory = z.object({ghsa_id: z.string(), cve_id: z.string().nullable(), url: z.string(), html_url: z.string(), summary: z.string().max(1024), description: z.string().max(65535).nullable(), severity: z.enum(["critical", "high", "medium", "low"]).nullable(), author: simpleUser.nullable(), publisher: simpleUser.nullable(), identifiers: z.array(z.object({type: z.enum(["CVE", "GHSA"]), value: z.string()})), state: z.enum(["published", "closed", "withdrawn", "draft", "triage"]), created_at: z.string().nullable(), updated_at: z.string().nullable(), published_at: z.string().nullable(), closed_at: z.string().nullable(), withdrawn_at: z.string().nullable(), submission: z.object({accepted: z.boolean()}).nullable(), vulnerabilities: z.array(repositoryAdvisoryVulnerability).nullable(), cvss: z.object({vector_string: z.string().nullable(), score: z.number().gte(0).lte(10).nullable()}).nullable(), cvss_severities: cvssSeverities.optional(), cwes: z.array(z.object({cwe_id: z.string(), name: z.string()})).nullable(), cwe_ids: z.array(z.string()).nullable(), credits: z.array(z.object({login: z.string().optional(), type: securityAdvisoryCreditTypes.optional()})).nullable(), credits_detailed: z.array(repositoryAdvisoryCredit).nullable(), collaborating_users: z.array(simpleUser).nullable(), collaborating_teams: z.array(team).nullable(), private_fork: simpleRepository.nullable()}); 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 }; /** * Get a repository security advisory * * GET /repos/{owner}/{repo}/security-advisories/{ghsa_id} */ export const getApiReposOwnerRepoSecurityAdvisoriesGhsaId = async (args: GetApiReposOwnerRepoSecurityAdvisoriesGhsaIdArgs, options?: GetApiReposOwnerRepoSecurityAdvisoriesGhsaIdOptions) => { const {baseUrl = "https://api.github.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/repos/${encodeURIComponent(String(args.owner))}/${encodeURIComponent(String(args.repo))}/security-advisories/${encodeURIComponent(String(args.ghsa_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(repositoryAdvisory, await res.json()) };