Skip to content

Commit 7a37d5b

Browse files
committed
fix(lambda): Prevent scale-up lambda from starting runner for user repo if org level runners is enabled
1 parent fd7681c commit 7a37d5b

File tree

7 files changed

+34
-0
lines changed

7 files changed

+34
-0
lines changed

lambdas/functions/control-plane/src/lambda.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const body: ActionRequestMessage = {
1515
installationId: 1,
1616
repositoryName: 'name',
1717
repositoryOwner: 'owner',
18+
repoOwnerType: "Organization",
1819
};
1920

2021
const sqsRecord: SQSRecord = {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ const TEST_DATA: scaleUpModule.ActionRequestMessage = {
5353
repositoryName: 'hello-world',
5454
repositoryOwner: 'Codertocat',
5555
installationId: 2,
56+
repoOwnerType: "Organization",
57+
};
58+
59+
const TEST_DATA_USER_REPO: scaleUpModule.ActionRequestMessage = {
60+
id: 1,
61+
eventType: 'workflow_job',
62+
repositoryName: 'hello-world',
63+
repositoryOwner: 'Octocat',
64+
installationId: 2,
65+
repoOwnerType: "User",
5666
};
5767

5868
// installationId 0 means no installationId is set.
@@ -62,6 +72,7 @@ const TEST_DATA_WITH_ZERO_INSTALL_ID: scaleUpModule.ActionRequestMessage = {
6272
repositoryName: 'hello-world',
6373
repositoryOwner: 'Codertocat',
6474
installationId: 0,
75+
repoOwnerType: "Organization",
6576
};
6677

6778
const cleanEnv = process.env;
@@ -305,6 +316,11 @@ describe('scaleUp with GHES', () => {
305316
expect(mockOctokit.paginate).toHaveBeenCalledTimes(1);
306317
});
307318

319+
it('Throws error if it is a User repo and org level runners is enabled', () => {
320+
process.env.ENABLE_ORGANIZATION_RUNNERS = 'true';
321+
expect(() => scaleUpModule.scaleUp('aws:sqs', TEST_DATA_USER_REPO)).rejects.toBeInstanceOf(Error);
322+
});
323+
308324
it('create SSM parameter for runner group id if it doesnt exist', async () => {
309325
mockSSMClient.on(GetParameterCommand).rejects();
310326
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export interface ActionRequestMessage {
2727
repositoryName: string;
2828
repositoryOwner: string;
2929
installationId: number;
30+
repoOwnerType: string;
3031
}
3132

3233
interface CreateGitHubRunnerConfig {
@@ -250,6 +251,16 @@ export async function scaleUp(eventSource: string, payload: ActionRequestMessage
250251
`Please ensure you have enabled workflow_job events.`,
251252
);
252253
}
254+
255+
if (enableOrgLevel && payload.repoOwnerType !== 'Organization') {
256+
logger.warn(`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
257+
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.`);
258+
throw Error(
259+
`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
260+
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.`,
261+
);
262+
}
263+
253264
const ephemeral = ephemeralEnabled && payload.eventType === 'workflow_job';
254265
const runnerType = enableOrgLevel ? 'Org' : 'Repo';
255266
const runnerOwner = enableOrgLevel ? payload.repositoryOwner : `${payload.repositoryOwner}/${payload.repositoryName}`;

lambdas/functions/webhook/src/sqs/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('Test sending message to SQS.', () => {
2626
repositoryOwner: 'owner',
2727
queueId: queueUrl,
2828
queueFifo: false,
29+
repoOwnerType: 'Organization',
2930
};
3031

3132
afterEach(() => {

lambdas/functions/webhook/src/sqs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface ActionRequestMessage {
1313
installationId: number;
1414
queueId: string;
1515
queueFifo: boolean;
16+
repoOwnerType: string;
1617
}
1718

1819
export interface MatcherConfig {

lambdas/functions/webhook/src/webhook/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ describe('handler', () => {
450450
installationId: 0,
451451
queueId: 'ubuntu-queue-id',
452452
queueFifo: false,
453+
repoOwnerType: "Organization",
453454
});
454455
});
455456
it('Check webhook will accept jobs for latest labels if workflow labels are not specific', async () => {
@@ -492,6 +493,7 @@ describe('handler', () => {
492493
installationId: 0,
493494
queueId: 'ubuntu-queue-id',
494495
queueFifo: false,
496+
repoOwnerType: "Organization",
495497
});
496498
});
497499
});
@@ -531,6 +533,7 @@ describe('handler', () => {
531533
installationId: 0,
532534
queueId: 'ubuntu-queue-id',
533535
queueFifo: false,
536+
repoOwnerType: "Organization",
534537
});
535538
});
536539

lambdas/functions/webhook/src/webhook/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ async function handleWorkflowJob(
5353
installationId: installationId,
5454
queueId: queue.id,
5555
queueFifo: queue.fifo,
56+
repoOwnerType: body.repository.owner.type,
5657
});
5758
logger.info(`Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`);
5859
return { statusCode: 201 };

0 commit comments

Comments
 (0)