diff --git a/__tests__/required.test.ts b/__tests__/required.test.ts index 0c6f447..05ba94d 100644 --- a/__tests__/required.test.ts +++ b/__tests__/required.test.ts @@ -9,6 +9,7 @@ describe('required', () => { let event: WorkflowRunCompletedEvent beforeEach(() => { + process.env.GITHUB_REPOSITORY = 'owner/repo' // Setting GITHUB_REPOSITORY environment variable mockOctokit = { rest: { repos: { @@ -16,7 +17,9 @@ describe('required', () => { }, actions: { getWorkflowRun: jest.fn(), - listWorkflowRunsForRepo: jest.fn() + listWorkflowRunsForRepo: jest.fn().mockImplementation(() => { + return Promise.resolve({ data: undefined }) // Simulating undefined data scenario + }) } } } diff --git a/src/required.ts b/src/required.ts index 2eb11b3..d504244 100644 --- a/src/required.ts +++ b/src/required.ts @@ -23,6 +23,11 @@ export async function required({ run_id: event.workflow_run.id }) + // Validate workflow data for undefined before accessing properties + if (!workflow.data) { + throw new Error('Workflow data is undefined.') + } + const head_sha = workflow.data.head_sha console.log(`Processing ${workflow.data.name} ${workflow.data.html_url}`) @@ -41,6 +46,11 @@ export async function required({ per_page: 100 }) + // Validate workflowRuns data for undefined before accessing properties + if (!workflowRuns.data || !workflowRuns.data.workflow_runs) { + throw new Error('Workflow runs data is undefined.') + } + const selectedWorkflows = workflowRuns.data.workflow_runs.filter( w => w.name !== undefined && w.name !== null && workflows.includes(w.name) )