Skip to content

Commit 387eb99

Browse files
committed
test: enhance error handling for GitHub API responses in scale-down logic
1 parent d3356d6 commit 387eb99

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

lambdas/functions/control-plane/src/scale-runners/scale-down.test.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Octokit } from '@octokit/rest';
2+
import { RequestError } from '@octokit/request-error';
23
import moment from 'moment';
34
import nock from 'nock';
45

@@ -421,8 +422,13 @@ describe('Scale down runners', () => {
421422
mockAwsRunners(runners);
422423

423424
// Mock 404 error response
424-
const error404 = new Error('Runner not found');
425-
(error404 as any).status = 404;
425+
const error404 = new RequestError('Runner not found', 404, {
426+
request: {
427+
method: 'GET',
428+
url: 'https://api.github.com/test',
429+
headers: {},
430+
},
431+
});
426432

427433
if (type === 'Repo') {
428434
mockOctokit.actions.getSelfHostedRunnerForRepo.mockRejectedValueOnce(error404);
@@ -453,8 +459,13 @@ describe('Scale down runners', () => {
453459
mockAwsRunners(runners);
454460

455461
// Mock 404 error response for busy state check
456-
const error404 = new Error('Runner not found');
457-
(error404 as any).status = 404;
462+
const error404 = new RequestError('Runner not found', 404, {
463+
request: {
464+
method: 'GET',
465+
url: 'https://api.github.com/test',
466+
headers: {},
467+
},
468+
});
458469

459470
if (type === 'Repo') {
460471
mockOctokit.actions.getSelfHostedRunnerForRepo.mockRejectedValueOnce(error404);
@@ -487,8 +498,13 @@ describe('Scale down runners', () => {
487498
mockAwsRunners(runners);
488499

489500
// Mock non-404 error response
490-
const error500 = new Error('Internal server error');
491-
(error500 as any).status = 500;
501+
const error500 = new RequestError('Internal server error', 500, {
502+
request: {
503+
method: 'GET',
504+
url: 'https://api.github.com/test',
505+
headers: {},
506+
},
507+
});
492508

493509
if (type === 'Repo') {
494510
mockOctokit.actions.getSelfHostedRunnerForRepo.mockRejectedValueOnce(error500);

lambdas/functions/control-plane/src/scale-runners/scale-down.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Octokit } from '@octokit/rest';
22
import { Endpoints } from '@octokit/types';
3+
import { RequestError } from '@octokit/request-error';
34
import { createChildLogger } from '@aws-github-runner/aws-powertools-util';
45
import moment from 'moment';
56

@@ -71,8 +72,8 @@ async function getGitHubSelfHostedRunnerState(
7172
metricGitHubAppRateLimit(state.headers);
7273

7374
return state.data;
74-
} catch (error: any) {
75-
if (error.status === 404) {
75+
} catch (error) {
76+
if (error instanceof RequestError && error.status === 404) {
7677
logger.info(`Runner '${ec2runner.instanceId}' with GitHub Runner ID '${runnerId}' not found on GitHub (404)`);
7778
return null;
7879
}

0 commit comments

Comments
 (0)