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

refactor: env-helpers #249

Merged
merged 2 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions services/core/functions/demo-review-lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import {
LANGCHAIN_API_KEY_PARAM_NAME,
OPENAI_API_KEY_PARAM_NAME,
} from "../../constants";
import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import { reviewLambdaEnvironment } from "../helpers/reviewLambdaEnvironment";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";
import { reviewLambdaEnvironment } from "../utils/reviewLambdaEnvironment";

export type DemoReviewLambdaProps = {
table: Table;
bucket: Bucket;
}
};

export class DemoReviewLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: DemoReviewLambdaProps) {
Expand Down
6 changes: 3 additions & 3 deletions services/core/functions/demo-review-lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { S3Client } from "@aws-sdk/client-s3";
import { APIGatewayProxyEvent } from "aws-lambda";
import { v4 as uuidv4 } from "uuid";

import { saveInputAndResponseToS3 } from "./saveInputAndResponseToS3";
import { getMaxPromptLength } from "../../../../src/common/model/getMaxPromptLength";
import { logger } from "../../../../src/common/utils/logger";
import { askAI } from "../../../../src/review/llm/askAI";
import { demoPrompt } from "../../../../src/review/prompt/prompts";
import { ReviewDemoCounterEntity } from "../../entities";
import { getEnvVariable, getVariableFromSSM } from "../helpers/getVariable";
import { getEnvVariable, getVariableFromSSM } from "../utils/getVariable";
import { saveInputAndResponseToS3 } from "./saveInputAndResponseToS3";

const DEFAULT_DEMO_MODEL = "gpt-3.5-turbo";

Expand All @@ -27,7 +27,7 @@ type DemoReviewLambdaResponse = {

type ReviewLambdaInput = {
code: string;
}
};

const isReviewLambdaInput = (input: unknown): input is ReviewLambdaInput =>
typeof input === "object" && input !== null && "code" in input;
Expand Down
10 changes: 6 additions & 4 deletions services/core/functions/get-user/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import { join } from "path";

import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";

type GetUserLambdaProps = {
table: Table;
}
};

export class GetUserLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: GetUserLambdaProps) {
Expand Down
6 changes: 0 additions & 6 deletions services/core/functions/helpers/commonLambdaEnvironment.ts

This file was deleted.

17 changes: 0 additions & 17 deletions services/core/functions/helpers/sharedCdkEsbuildconfig.ts

This file was deleted.

6 changes: 3 additions & 3 deletions services/core/functions/review-lambda/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { subtle } from "crypto";

import { getVariableFromSSM } from "../helpers/getVariable";
import { getVariableFromSSM } from "../utils/getVariable";

export const authenticate = async (
header: string,
Expand Down Expand Up @@ -30,7 +30,7 @@ export const authenticate = async (
return equal;
};

function hexToBytes(hex: string) {
const hexToBytes = (hex: string) => {
const len = hex.length / 2;
const bytes = new Uint8Array(len);

Expand All @@ -43,4 +43,4 @@ function hexToBytes(hex: string) {
}

return bytes;
}
};
6 changes: 3 additions & 3 deletions services/core/functions/review-lambda/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
OPENAI_API_KEY_PARAM_NAME,
} from "../../constants";
import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import { reviewLambdaEnvironment } from "../helpers/reviewLambdaEnvironment";
import { commonLambdaEnvironment } from "../utils/commonLambdaEnvironment";
import { commonLambdaProps } from "../utils/commonLambdaProps";
import { reviewLambdaEnvironment } from "../utils/reviewLambdaEnvironment";

export class ReviewLambda extends NodejsFunction {
constructor(scope: Construct, id: string) {
Expand Down
2 changes: 1 addition & 1 deletion services/core/functions/review-lambda/decryptKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DecryptCommand, KMSClient } from "@aws-sdk/client-kms";

import { getEnvVariable } from "../helpers/getVariable";
import { getEnvVariable } from "../utils/getVariable";

const kmsClient = new KMSClient({});

Expand Down
4 changes: 2 additions & 2 deletions services/core/functions/review-lambda/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { ReviewArgs, ReviewFile } from "../../../../src/common/types";
import { logger } from "../../../../src/common/utils/logger";
import { review } from "../../../../src/review/index";
import { GITHUB_SIGNATURE_HEADER_KEY } from "../../constants";
import { getVariableFromSSM } from "../helpers/getVariable";
import { getVariableFromSSM } from "../utils/getVariable";

type ReviewLambdasBody = {
args: ReviewArgs;
files: ReviewFile[];
}
};

logger.settings.minLevel = 4;

Expand Down
10 changes: 6 additions & 4 deletions services/core/functions/update-user/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Construct } from "constructs";
import { join } from "path";

import { buildResourceName } from "../../helpers";
import { commonLambdaEnvironment } from "../helpers/commonLambdaEnvironment";
import { commonLambdaProps } from "../helpers/commonLambdaProps";
import {
buildResourceName,
commonLambdaEnvironment,
commonLambdaProps,
} from "../../helpers";

type UpdateUserLambdaProps = {
table: Table;
kmsKey: Key;
}
};

export class UpdateUserLambda extends NodejsFunction {
constructor(scope: Construct, id: string, props: UpdateUserLambdaProps) {
Expand Down
2 changes: 1 addition & 1 deletion services/core/functions/update-user/encryptKey.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EncryptCommand, KMSClient } from "@aws-sdk/client-kms";

import { getEnvVariable } from "../helpers/getVariable";
import { getEnvVariable } from "../utils/getVariable";

const kmsClient = new KMSClient({});

Expand Down
2 changes: 1 addition & 1 deletion services/core/helpers/certificates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { StringParameter } from "aws-cdk-lib/aws-ssm";
import { Construct } from "constructs";

import { getStage } from "./env-helpers";
import { isDev } from "./environment";
import { isDev } from "./env-helpers/environment";

export const getCertificateArn = (
scope: Construct,
Expand Down
77 changes: 0 additions & 77 deletions services/core/helpers/env-helpers.ts

This file was deleted.

4 changes: 4 additions & 0 deletions services/core/helpers/env-helpers/buildResourceName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { getStage } from "./getStage";

export const buildResourceName = (resourceName: string): string =>
`${getStage()}-${resourceName}`;
14 changes: 14 additions & 0 deletions services/core/helpers/env-helpers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const defaultStage = "dev";
export const defaultRegion = "eu-west-2";
export const projectName = "crgpt";

export const sharedCdkEsbuildConfig = {
minify: false,
keepNames: true,
sourceMap: true,
externalModules: ["aws-sdk"],
target: "node16",
platform: "node",
metafile: true,
mainFields: ["module", "main"],
};
mattzcarey marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getStage } from "./env-helpers";
import { getStage } from "./getStage";

export const isProduction = (): boolean => {
const stage = getStage();
Expand Down
47 changes: 47 additions & 0 deletions services/core/helpers/env-helpers/getArg.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export type GetArgProps = {
cliArg: string;
processEnvName: string;
defaultValue?: string;
};

export const getArg = ({
cliArg,
processEnvName,
defaultValue,
}: GetArgProps): string => {
const isNonEmptyString = (arg: unknown): arg is string =>
typeof arg === "string" && arg !== "";

const getCdkContext = (): Record<string, unknown> | undefined => {
const cdkContextEnv = process.env.CDK_CONTEXT_JSON;

return cdkContextEnv != null
? (JSON.parse(cdkContextEnv) as Record<string, unknown>)
: undefined;
};

const findCliArg = (key: string): string | undefined => {
const index = process.argv.findIndex((arg) => arg === `--${key}`);

return index !== -1 && index + 1 < process.argv.length
? process.argv[index + 1]
: undefined;
};

const argSources = [
findCliArg(cliArg),
getCdkContext()?.[cliArg] as string | undefined,
process.env[processEnvName],
defaultValue,
];

for (const arg of argSources) {
if (isNonEmptyString(arg)) {
return arg;
}
}

throw new Error(
`--${cliArg} CLI argument or ${processEnvName} env var required.`
);
};
9 changes: 9 additions & 0 deletions services/core/helpers/env-helpers/getRegion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defaultRegion } from "./config";
import { getArg } from "./getArg";

export const getRegion = (): string =>
getArg({
cliArg: "region",
processEnvName: "REGION",
defaultValue: defaultRegion,
});
10 changes: 10 additions & 0 deletions services/core/helpers/env-helpers/getStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defaultStage } from "./config";
import { getArg } from "./getArg";

export const getStage = (): string => {
return getArg({
cliArg: "stage",
processEnvName: "STAGE",
defaultValue: defaultStage,
});
};
6 changes: 6 additions & 0 deletions services/core/helpers/env-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./buildResourceName";
export * from "./config";
export * from "./environment";
export * from "./getArg";
export * from "./getRegion";
export * from "./getStage";
3 changes: 2 additions & 1 deletion services/core/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./certificates";
export * from "./domains";
export * from "./env-helpers";
export * from "./env-helpers/environment";
export * from "./format-response";
export * from "./environment";
export * from "./lambda";
Loading