Skip to content

Conversation

github-actions[bot]
Copy link

Closes #428

Applied changes based on Issue #428.

I’ve added a runtime check that validates the configured OPENAI_API_KEY against the selected model before proceeding with the action, and updated the main‐unit test to mock out that new call. Specifically:

1. src/main.ts

  • Imported getOpenAIClient from the OpenAI wrapper.
  • Inserted a pre‑flight check that calls client.models.retrieve(config.openaiModel).
  • On failure, calls core.setFailed(...) and bails out early.
import { getOpenAIClient } from './api/openai.js';
// …
const config = getConfig();

// Verify OpenAI API key and access to the configured model
core.info(`Verifying OpenAI API key and access to model ${config.openaiModel}`);
try {
  const client = getOpenAIClient(config);
  await client.models.retrieve(config.openaiModel);
} catch (error) {
  core.setFailed(
    `OPENAI_API_KEY invalid or no access to model "${config.openaiModel}": ${
      error instanceof Error ? error.message : String(error)
    }`,
  );
  return;
}

// Process Event
const processedEvent = await processEvent(config);
// …

【F:src/main.ts†L16-L22】【F:src/main.ts†L27-L38】

2. tests/unit/main.test.ts

  • Mocked out getOpenAIClient so tests don’t perform a real API call.
  • In beforeEach, ensured the stubbed client has a models.retrieve() that resolves successfully.
// Mock OpenAI client to prevent real API calls during tests
jest.mock('../../../src/api/openai.js', () => ({ getOpenAIClient: jest.fn() }));

import { getOpenAIClient } from '../../../src/api/openai.js';
// …
beforeEach(() => {
  jest.clearAllMocks();
  // Ensure OpenAI client mock returns a valid retrieve() method
  (getOpenAIClient as jest.Mock).mockReturnValue({
    models: { retrieve: jest.fn().mockResolvedValue({}) },
  });
});

【F:tests/unit/main.test.ts†L9-L15】【F:tests/unit/main.test.ts†L25-L33】


These changes implement the requested “pre‑flight” key‑and‑model validation and ensure our existing unit tests continue to pass (by mocking out the new API call).

@YiweiShen YiweiShen merged commit 763986a into main Jul 14, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-428-apply-changes-for-issue-428 branch July 14, 2025 03:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a check to determine whether the OPENAI_API_KEY is still valid for the current model, and exit if it is invalid.

1 participant