Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda): Prevent scale-up lambda from starting runner for user repo if org level runners is enabled #3909

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions lambdas/functions/control-plane/src/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const body: ActionRequestMessage = {
installationId: 1,
repositoryName: 'name',
repositoryOwner: 'owner',
repoOwnerType: "Organization",
};

const sqsRecord: SQSRecord = {
Expand Down
10 changes: 10 additions & 0 deletions lambdas/functions/control-plane/src/scale-runners/scale-up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const TEST_DATA: scaleUpModule.ActionRequestMessage = {
repositoryName: 'hello-world',
repositoryOwner: 'Codertocat',
installationId: 2,
repoOwnerType: "Organization",
};

// installationId 0 means no installationId is set.
Expand All @@ -62,6 +63,7 @@ const TEST_DATA_WITH_ZERO_INSTALL_ID: scaleUpModule.ActionRequestMessage = {
repositoryName: 'hello-world',
repositoryOwner: 'Codertocat',
installationId: 0,
repoOwnerType: "Organization",
};

const cleanEnv = process.env;
Expand Down Expand Up @@ -305,6 +307,14 @@ describe('scaleUp with GHES', () => {
expect(mockOctokit.paginate).toHaveBeenCalledTimes(1);
});

it('Discards event if it is a User repo and org level runners is enabled', async () => {
process.env.ENABLE_ORGANIZATION_RUNNERS = 'true';
const USER_REPO_TEST_DATA = { ...TEST_DATA };
USER_REPO_TEST_DATA.repoOwnerType = 'User';
await scaleUpModule.scaleUp('aws:sqs', USER_REPO_TEST_DATA);
expect(createRunner).not.toHaveBeenCalled();
});

it('create SSM parameter for runner group id if it doesnt exist', async () => {
mockSSMClient.on(GetParameterCommand).rejects();
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
Expand Down
13 changes: 13 additions & 0 deletions lambdas/functions/control-plane/src/scale-runners/scale-up.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Octokit } from '@octokit/rest';

Check notice on line 1 in lambdas/functions/control-plane/src/scale-runners/scale-up.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

✅ Getting better: Overall Code Complexity

The mean cyclomatic complexity decreases from 4.92 to 4.79, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
import { addPersistentContextToChildLogger, createChildLogger } from '@terraform-aws-github-runner/aws-powertools-util';
import { getParameter, putParameter } from '@terraform-aws-github-runner/aws-ssm-util';
import yn from 'yn';
Expand Down Expand Up @@ -27,6 +27,7 @@
repositoryName: string;
repositoryOwner: string;
installationId: number;
repoOwnerType: string;
}

interface CreateGitHubRunnerConfig {
Expand Down Expand Up @@ -250,6 +251,14 @@
`Please ensure you have enabled workflow_job events.`,
);
}

if (!isValidRepoOwnerTypeIfOrgLevelEnabled(payload, enableOrgLevel)) {
logger.warn(`Repository ${payload.repositoryOwner}/${payload.repositoryName} does not belong to a GitHub` +
`organization and organization runners are enabled. This is not supported. Not scaling up for this event.` +
`Not throwing error to prevent re-queueing and just ignoring the event.`);
return;
}

Check warning on line 261 in lambdas/functions/control-plane/src/scale-runners/scale-up.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

scaleUp increases in cyclomatic complexity from 23 to 24, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
const ephemeral = ephemeralEnabled && payload.eventType === 'workflow_job';
const runnerType = enableOrgLevel ? 'Org' : 'Repo';
const runnerOwner = enableOrgLevel ? payload.repositoryOwner : `${payload.repositoryOwner}/${payload.repositoryName}`;
Expand Down Expand Up @@ -341,6 +350,10 @@
}
}

function isValidRepoOwnerTypeIfOrgLevelEnabled(payload: ActionRequestMessage, enableOrgLevel: boolean) : boolean {
return !(enableOrgLevel && payload.repoOwnerType !== 'Organization');
}

function addDelay(instances: string[]) {
const delay = async (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const ssmParameterStoreMaxThroughput = 40;
Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/sqs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('Test sending message to SQS.', () => {
repositoryOwner: 'owner',
queueId: queueUrl,
queueFifo: false,
repoOwnerType: 'Organization',
};

afterEach(() => {
Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/sqs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ActionRequestMessage {
installationId: number;
queueId: string;
queueFifo: boolean;
repoOwnerType: string;
}

export interface MatcherConfig {
Expand Down
3 changes: 3 additions & 0 deletions lambdas/functions/webhook/src/webhook/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});
it('Check webhook will accept jobs for latest labels if workflow labels are not specific', async () => {
Expand Down Expand Up @@ -492,6 +493,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});
});
Expand Down Expand Up @@ -531,6 +533,7 @@ describe('handler', () => {
installationId: 0,
queueId: 'ubuntu-queue-id',
queueFifo: false,
repoOwnerType: "Organization",
});
});

Expand Down
1 change: 1 addition & 0 deletions lambdas/functions/webhook/src/webhook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function handleWorkflowJob(
installationId: installationId,
queueId: queue.id,
queueFifo: queue.fifo,
repoOwnerType: body.repository.owner.type,
});
logger.info(`Successfully queued job for ${body.repository.full_name} to the queue ${queue.id}`);
return { statusCode: 201 };
Expand Down
Loading