Skip to content
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
104 changes: 101 additions & 3 deletions src/__tests__/cli-commands.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-env jest */

import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks } from '../cli-commands.js'
import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows } from '../cli-commands.js'
import { getPackageJson } from '../utils.js'
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem } from '../types.js'
import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse } from './fixtures.js'
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIErrorResponse, APIChecklistItem, APICheckItem, APIWorkflowItem } from '../types.js'
import { mockApiHealthResponse, mockAPIProjectResponse, mockAPIGithubOrgResponse, mockAPIChecklistResponse, mockAPICheckResponse, mockAPIWorkflowResponse } from './fixtures.js'
import nock from 'nock'

const pkg = getPackageJson()
Expand Down Expand Up @@ -380,4 +380,102 @@ describe('CLI Commands', () => {
expect(result.messages[0]).toBe('No compliance checks found')
})
})

describe('printWorkflows', () => {
let mockWorkflows: APIWorkflowItem[]

beforeEach(() => {
nock.cleanAll()
mockWorkflows = [...mockAPIWorkflowResponse]
})

it('should retrieve and format workflow items successfully', async () => {
// Mock API call
nock('http://localhost:3000')
.get('/api/v1/workflow')
.reply(200, mockWorkflows)

// Execute the function
const result = await printWorkflows()

// Verify the result
expect(result.success).toBe(true)
expect(result.messages[0]).toBe('Compliance workflows available:')
expect(result.messages[1]).toContain(mockWorkflows[0].id)
expect(result.messages[1]).toContain(mockWorkflows[0].description)
expect(result.messages).toHaveLength(2) // Header + 1 workflow item
expect(nock.isDone()).toBe(true) // Verify all mocked endpoints were called
})

it('should handle multiple workflow items', async () => {
// Add a second workflow item
const secondWorkflow = {
...mockWorkflows[0],
id: 'create-stuff',
description: 'Another workflow description'
}
mockWorkflows.push(secondWorkflow)

// Mock API call
nock('http://localhost:3000')
.get('/api/v1/workflow')
.reply(200, mockWorkflows)

// Execute the function
const result = await printWorkflows()

// Verify the result
expect(result.success).toBe(true)
expect(result.messages[0]).toBe('Compliance workflows available:')
expect(result.messages[1]).toContain(mockWorkflows[0].id)
expect(result.messages[2]).toContain(mockWorkflows[1].id)
expect(result.messages).toHaveLength(3) // Header + 2 workflow items
})

it('should handle API errors gracefully', async () => {
// Mock API error
nock('http://localhost:3000')
.get('/api/v1/workflow')
.reply(500, { errors: [{ message: 'Internal server error' }] } as APIErrorResponse)

// Execute the function
const result = await printWorkflows()

// Verify the result
expect(result.success).toBe(false)
expect(result.messages[0]).toContain('❌ Failed to retrieve compliance workflow items')
expect(result.messages).toHaveLength(1)
})

it('should handle network errors gracefully', async () => {
// Mock network error
nock('http://localhost:3000')
.get('/api/v1/workflow')
.replyWithError('Network error')

// Execute the function
const result = await printWorkflows()

// Verify the result
expect(result.success).toBe(false)
expect(result.messages[0]).toContain('❌ Failed to retrieve compliance workflow items')
expect(result.messages[0]).toContain('Network error')
expect(result.messages).toHaveLength(1)
})

it('should handle empty workflow response', async () => {
// Mock empty response
nock('http://localhost:3000')
.get('/api/v1/workflow')
.reply(200, [])

// Execute the function
const result = await printWorkflows()

// Verify the result
expect(result.success).toBe(true)
expect(result.messages).toHaveLength(1) // Only the header message
expect(result.messages[0]).toBe('No compliance workflows found')
})
})
})
7 changes: 6 additions & 1 deletion src/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIChecklistItem, APICheckItem } from '../types.js'
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIChecklistItem, APICheckItem, APIWorkflowItem } from '../types.js'

export const mockApiHealthResponse: APIHealthResponse = {
status: 'ok',
Expand Down Expand Up @@ -121,3 +121,8 @@ export const mockAPICheckResponse: APICheckItem[] = [{
created_at: '2025-02-21T18:53:00.485Z',
updated_at: '2025-02-21T18:53:00.485Z'
}]

export const mockAPIWorkflowResponse: APIWorkflowItem[] = [{
id: 'update-stuff',
description: 'Test workflow description'
}]
11 changes: 10 additions & 1 deletion src/api-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getConfig } from './utils.js'
import { got } from 'got'
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIChecklistItem, APICheckItem } from './types.js'
import { APIHealthResponse, APIProjectDetails, APIGithubOrgDetails, APIChecklistItem, APICheckItem, APIWorkflowItem } from './types.js'

export const apiClient = () => {
const config = getConfig()
Expand Down Expand Up @@ -70,3 +70,12 @@ export const getAllChecks = async (): Promise<APICheckItem[]> => {
}
return response.body as APICheckItem[]
}

export const getAllWorkflows = async (): Promise<APIWorkflowItem[]> => {
const client = apiClient()
const response = await client.get('workflow', { responseType: 'json' })
if (response.statusCode !== 200) {
throw new Error(`Failed to get the data from the API: ${response.statusCode} ${response.body}`)
}
return response.body as APIWorkflowItem[]
}
29 changes: 28 additions & 1 deletion src/cli-commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandResult } from './types.js'
import { isApiAvailable, isApiCompatible, getPackageJson } from './utils.js'
import { getAPIDetails, createProject, addGithubOrgToProject, getAllChecklistItems, getAllChecks } from './api-client.js'
import { getAPIDetails, createProject, addGithubOrgToProject, getAllChecklistItems, getAllChecks, getAllWorkflows } from './api-client.js'

const pkg = getPackageJson()

Expand Down Expand Up @@ -110,3 +110,30 @@ export const printChecks = async (): Promise<CommandResult> => {
success
}
}

export const printWorkflows = async (): Promise<CommandResult> => {
const messages: string[] = []
let success = true
try {
const workflows = await getAllWorkflows()
if (workflows.length === 0) {
messages.push('No compliance workflows found')
return {
messages,
success
}
}
messages.push('Compliance workflows available:')
workflows.forEach((workflow) => {
messages.push(`- ${workflow.id}: ${workflow.description}`)
})
} catch (error) {
messages.push(`❌ Failed to retrieve compliance workflow items: ${error instanceof Error ? error.message : 'Unknown error'}`)
success = false
}

return {
messages,
success
}
}
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Command } from 'commander'
// @ts-ignore
import { stringToArray } from '@ulisesgascon/string-to-array'
import { handleCommandResult } from './utils.js'
import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks } from './cli-commands.js'
import { getVersion, runDoctor, addProjectWithGithubOrgs, printChecklists, printChecks, printWorkflows } from './cli-commands.js'

const program = new Command()

Expand All @@ -23,6 +23,18 @@ program
handleCommandResult(result)
})

const workflow = program
.command('workflow')
.description('Compliance workflow management')

workflow
.command('list')
.description('Print all available compliance workflows')
.action(async () => {
const result = await printWorkflows()
handleCommandResult(result)
})

const checklist = program
.command('compliance-checklist')
.description('Compliance checklist management')
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ export type APICheckItem = {
updated_at: string;
};

/**
* Workflow Schema
*/
export interface APIWorkflowItem {
id: string;
description: string;
}

/**
* Error object as defined in the OpenAPI schema
*/
Expand Down