Skip to content

Apply handle pattern to audit-logs #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/audit-log/cmd-audit-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'

import { getAuditLog } from './get-audit-log'
import { handleAuditLog } from './handle-audit-log'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -96,7 +96,7 @@ async function run(
return
}

await getAuditLog({
await handleAuditLog({
orgSlug,
outputKind: json ? 'json' : markdown ? 'markdown' : 'print',
page: Number(page || 0),
Expand Down
81 changes: 81 additions & 0 deletions src/commands/audit-log/fetch-audit-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { SocketSdkReturnType } from '@socketsecurity/sdk'

export async function fetchAuditLog({
logType,
orgSlug,
outputKind,
page,
perPage
}: {
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

return await fetchAuditLogWithToken(apiToken, {
logType,
orgSlug,
outputKind,
page,
perPage
})
}

export async function fetchAuditLogWithToken(
apiToken: string,
{
logType,
orgSlug,
outputKind,
page,
perPage
}: {
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}
): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
// Lazily access constants.spinner.
const { spinner } = constants

spinner.start(`Looking up audit log for ${orgSlug}`)

const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.getAuditLogEvents(orgSlug, {
// I'm not sure this is used at all.
outputJson: String(outputKind === 'json'),
// I'm not sure this is used at all.
outputMarkdown: String(outputKind === 'markdown'),
orgSlug,
type: logType,
page: String(page),
per_page: String(perPage)
}),
`Looking up audit log for ${orgSlug}\n`
)

if (!result.success) {
handleUnsuccessfulApiResponse('getAuditLogEvents', result)
return
}

spinner.stop()

return result.data
}
33 changes: 33 additions & 0 deletions src/commands/audit-log/handle-audit-log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { fetchAuditLog } from './fetch-audit-log'
import { outputAuditLog } from './output-audit-log'

export async function handleAuditLog({
logType,
orgSlug,
outputKind,
page,
perPage
}: {
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}): Promise<void> {
const auditLogs = await fetchAuditLog({
orgSlug,
outputKind,
page,
perPage,
logType
})
if (!auditLogs) return

await outputAuditLog(auditLogs, {
logType,
orgSlug,
outputKind,
page,
perPage
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { stripIndents } from 'common-tags'
import { logger } from '@socketsecurity/registry/lib/logger'
import { Separator, select } from '@socketsecurity/registry/lib/prompts'

import constants from '../../constants'
import { handleApiCall, handleUnsuccessfulApiResponse } from '../../utils/api'
import { AuthError } from '../../utils/errors'
import { mdTable } from '../../utils/markdown'
import { getDefaultToken, setupSdk } from '../../utils/sdk'

import type { Choice } from '@socketsecurity/registry/lib/prompts'
import type { SocketSdkReturnType } from '@socketsecurity/sdk'
Expand All @@ -16,41 +12,29 @@ type AuditChoice = Choice<string>

type AuditChoices = Array<Separator | AuditChoice>

export async function getAuditLog({
logType,
orgSlug,
outputKind,
page,
perPage
}: {
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}): Promise<void> {
const apiToken = getDefaultToken()
if (!apiToken) {
throw new AuthError(
'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
)
}

const auditLogs = await getAuditLogWithToken({
apiToken,
export async function outputAuditLog(
auditLogs: SocketSdkReturnType<'getAuditLogEvents'>['data'],
{
logType,
orgSlug,
outputKind,
page,
perPage,
logType
})
if (!auditLogs) return

if (outputKind === 'json')
perPage
}: {
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}
): Promise<void> {
if (outputKind === 'json') {
await outputAsJson(auditLogs.results, orgSlug, logType, page, perPage)
else if (outputKind === 'markdown')
} else if (outputKind === 'markdown') {
await outputAsMarkdown(auditLogs.results, orgSlug, logType, page, perPage)
else await outputAsPrint(auditLogs.results, orgSlug, logType)
} else {
await outputAsPrint(auditLogs.results, orgSlug, logType)
}
}

async function outputAsJson(
Expand Down Expand Up @@ -174,48 +158,3 @@ async function outputAsPrint(
]
)
}

async function getAuditLogWithToken({
apiToken,
logType,
orgSlug,
outputKind,
page,
perPage
}: {
apiToken: string
outputKind: 'json' | 'markdown' | 'print'
orgSlug: string
page: number
perPage: number
logType: string
}): Promise<SocketSdkReturnType<'getAuditLogEvents'>['data'] | void> {
// Lazily access constants.spinner.
const { spinner } = constants

spinner.start(`Looking up audit log for ${orgSlug}`)

const socketSdk = await setupSdk(apiToken)
const result = await handleApiCall(
socketSdk.getAuditLogEvents(orgSlug, {
// I'm not sure this is used at all.
outputJson: String(outputKind === 'json'),
// I'm not sure this is used at all.
outputMarkdown: String(outputKind === 'markdown'),
orgSlug,
type: logType,
page: String(page),
per_page: String(perPage)
}),
`Looking up audit log for ${orgSlug}\n`
)

if (!result.success) {
handleUnsuccessfulApiResponse('getAuditLogEvents', result)
return
}

spinner.stop()

return result.data
}
Loading