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

DOP-4248: Create action that uploads directory content to S3 #79

Merged
merged 16 commits into from
Feb 20, 2024

Conversation

seungpark
Copy link
Contributor

@seungpark seungpark commented Feb 7, 2024

This PR exposes a github action that uploads a directory (and its contents) to a specified AWS S3 bucket (and path).

Front end workflow that uses this action is found here

Test Deploy of this action

@@ -2,9 +2,8 @@
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"module": "CommonJS",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was changed in order to work with this mime package for node

src/publish-build/index.ts Outdated Show resolved Hide resolved
src/publish-build/index.ts Outdated Show resolved Hide resolved
src/publish-build/index.ts Show resolved Hide resolved
src/publish-build/index.ts Show resolved Hide resolved
@seungpark seungpark requested a review from branberry February 8, 2024 22:51
Copy link
Contributor

@mmeigs mmeigs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. I just put annoying comments 🤭

core.error(errMsg);
throw new Error(errMsg);
}
res[requiredVar] = process.env[requiredVar] ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I wonder if this nullish coalescer is necessary because it would have thrown an error?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a limitation of TypeScript where it doesn't know if process.env[requiredVar] is defined or not even if we check because requiredVar is a variable we use to index it.

image

Copy link
Collaborator

@branberry branberry Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a way that this can be done without running into TS errors. There are a few more moving parts, but this should make TypeScript happy. I added some explanations, but if I can clarify something more let me know!!

const REQUIRED_ENV_VARS = [
  'AWS_BUCKET',
  'PROJECT',
  'SOURCE_DIR',
  'DESTINATION_DIR',
  'COMMIT_HASH',
  'GITHUB_WORKSPACE',
] as const;

// translates the array of values in REQUIRED_ENV_VARS into a literal type i.e. RequiredVar = 'AWS_BUCKET' | 'PROJECT' ....
type RequiredVar = (typeof REQUIRED_ENV_VARS)[number];

// We can then use this literal type to create an object definition. Have to use a type here instead of an interface
// as interfaces don't support mapped types very well :/
// This boils down to the same intreface, but with the added benefit of being able to assign these string values to the object in
// the getEnvVars function. If this is not done, TypeScript sees a string versus a key within the `envParams` type. Because of this,
// an error will occur if you try assigning a value to the envParams object.
type envParams = {
  [V in RequiredVar]: string;
};

function getEnvVars(): envParams {
  const res: Partial<envParams> = {};
  for (const requiredVar of REQUIRED_ENV_VARS) {
    const envVar = process.env[requiredVar];
    if (!envVar) {
      const errMsg = `Required env variable missing: ${requiredVar}`;
      core.error(errMsg);
      throw new Error(errMsg);
    }
    res[requiredVar] = envVar;
  }
  return res as envParams;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

omg grosssss typescript!! 😭 @branberry you right, you always right!

Copy link
Collaborator

@branberry branberry Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, I wish I was always right... I've just had to do this so many times already hahaha. I've been wallowing in the murky TypeScript underbelly for far too long...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part was throwing an error so i updated it to be a type Record<RequiredVar, string>;

type envParams = {
  [var in RequiredVar]: string;
};

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the issue is that var is a JavaScript keyword.. If you use something like V instead of var the error goes away.

But, I prefer the Record as that is more readable

}

function getEnvVars(): envParams {
const res: { [key: string]: string } = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this type be Partial<envParams>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

'GITHUB_WORKSPACE',
];

interface envParams {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit of nits: I believe the convention is to capitalize interface names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@seungpark seungpark requested a review from mmeigs February 20, 2024 15:44
Copy link
Contributor

@mmeigs mmeigs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@seungpark seungpark merged commit 5f493d6 into main Feb 20, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants