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

feat(rr): config parsing #662

Merged
merged 21 commits into from
May 25, 2023
Merged

feat(rr): config parsing #662

merged 21 commits into from
May 25, 2023

Conversation

seaerchin
Copy link
Contributor

@seaerchin seaerchin commented Mar 22, 2023

Problem

Our current model of parsing is simplified and only works for pages; this PR adds parsing for configuration files (nav + setting)

Closes IS-54

Solution

  1. add a config service that handles whether a file is a configuration file or not
  2. extend dto definitions to differentiate between the types
  3. in the event of failure, return the filename + folder path to the frontend to display

@seaerchin seaerchin requested a review from a team March 22, 2023 09:34
@seaerchin seaerchin requested review from alexanderleegs and a team March 27, 2023 04:00
src/errors/ConfigParseError.ts Show resolved Hide resolved
src/errors/PageParseError.ts Show resolved Hide resolved
return {
name,
path,
kind: "YmlConfig",
Copy link
Contributor

Choose a reason for hiding this comment

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

use enums

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i like unions more tbh, i've ran into type errors w/ enums in the past due to how they are implemented in TS and hence, i've avoided them like the plague ever since.

see here or here for more details but if you feel strongly about this, i can change it

Copy link
Contributor

@harishv7 harishv7 Mar 29, 2023

Choose a reason for hiding this comment

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

@seaerchin That's a good point! I think I used to do it like this:

enum ESomeEnum {
    ValOne: "1",
    ValTwo: "2"
}

type EFinalType = ESomeEnum.ValOne | ESomeEnum.ValTwo

Then we use the EFinalType for the actual assertion. Wdyt?

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 actually results in the same thing HAHA

Copy link
Contributor

Choose a reason for hiding this comment

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

@seaerchin same thing as in the same issue described in the links above? how so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh ps, i haven't had time to look at this due to being caught up in the release but this has the same effect because the discriminated union still only allows the same set of values.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes true, but felt it is cleaner with the use of discriminated unions + enums rather than strings

src/services/review/ReviewRequestService.ts Outdated Show resolved Hide resolved
src/services/review/ReviewRequestService.ts Show resolved Hide resolved
src/services/review/ReviewRequestService.ts Show resolved Hide resolved
src/services/review/ReviewRequestService.ts Show resolved Hide resolved
src/routes/v2/authenticated/review.ts Show resolved Hide resolved
}

export interface EditedConfigDto extends BaseEditedItemDto {
type: "nav" | "setting"
Copy link
Contributor

Choose a reason for hiding this comment

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

using enums will be cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

see above ;--;

import { Brand } from "@root/types/util"

// eslint-disable-next-line import/prefer-default-export
export class ConfigService {
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have a test file for this new service?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not at present, will add in a future PR

src/services/review/ReviewRequestService.ts Outdated Show resolved Hide resolved
src/utils/files.ts Show resolved Hide resolved
.orElse(() => this.parseCollectionPage(pathInfo))

isMarkdownPage = (pageName: string): Result<string, PageParseError> => {
if (pageName.endsWith(".md")) return ok(pageName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we just return a boolean value instead? Using neverthrow here feels too verbose for a function like this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

actually the problem here is that we want to distinguish MarkdownPages from other things (eg: YmlConfig or HtmlPages) - the problem with returning boolean is that we lose context wrt what the pageName is beyond its immediate containing function.

i can change this to string is MarkdownPage and brand this string to prevent the verbosity - wdyt

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm isnt that beyond the scope of this function, which is to just tell if this page is of type MarkdownPages?
if not should we then rename this to getPageType right?

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 is an example of a type guard and the naming is usually (by convention, at least) of the form isType

separately, telling if the page is of a specific type should assert that the page is that type, and not merely boolean otherwise downstream we would require calling again

) {
return ok({ name: Brand.fromString(name), kind: "ContactUsPage" })
}
return err(new NotFoundError())
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to return the specific file that wasnt found here? (ie the path?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point but actually i feel like we should extend NotFoundError to have a PageNotFoundError; the reason why idw to return just the pagename alone is cos we might have more errors in the future and no semantic meaning is tied to the path @__@

so like the page not found error would have a property page name, which can be used downstream to distinguish the type of page (if required) + provide a helpful message for displaying to FE.

didnt do here cos laze ._. (would have to do so for more errors to avoid inconsistency - maybe we can do this down the line)

@kishore03109
Copy link
Contributor

CICD tests are failing, may I trouble you you to take a look at them?

@seaerchin
Copy link
Contributor Author

CICD tests are failing, may I trouble you you to take a look at them?

kek i think the source branch is feat/buttons, which had tests fixed in #587 and #588; i'll probably rebase + merge those first and then rebase this on top once approved to see if the tests are still failing

Base automatically changed from feat/edit-button to develop April 17, 2023 04:29
@seaerchin seaerchin force-pushed the feat/config-parsing branch from 3ca54e8 to a496ee1 Compare May 17, 2023 10:05
@seaerchin seaerchin requested review from a team, kishore03109 and harishv7 May 17, 2023 10:07
@seaerchin seaerchin force-pushed the feat/config-parsing branch 3 times, most recently from 3cf6a41 to db69fa6 Compare May 23, 2023 03:53
Copy link
Contributor

@kishore03109 kishore03109 left a comment

Choose a reason for hiding this comment

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

[Tests] The full testing plan will be in the corresponding FE PR for buttons right? If not, please dismiss this review

fileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/homepage`,
lastEditedBy: MOCK_USER_EMAIL_TWO, // TODO: This should be MOCK_USER_EMAIL_ONE
cmsFileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/homepage`,
lastEditedBy: MOCK_USER_EMAIL_TWO,
Copy link
Contributor

Choose a reason for hiding this comment

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

Sanity check here: is this removal of todo intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can i clarify this - isn't the removed property added back below?

name: MOCK_GITHUB_FILENAME_ALPHA_ONE,
path: [],
stagingUrl: `${MOCK_DEPLOYMENT_DBENTRY_ONE.stagingUrl}${MOCK_PAGE_PERMALINK}`,
fileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/homepage`,
lastEditedBy: MOCK_USER_EMAIL_TWO, // TODO: This should be MOCK_USER_EMAIL_ONE
Copy link
Contributor

Choose a reason for hiding this comment

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

Sanity check again: checking in if this removal is intended

Copy link
Contributor Author

Choose a reason for hiding this comment

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

can i clarify this - isn't the removed property added back below?

if (err instanceof MissingSiteError || err instanceof NotFoundError) {
return res.status(404).json({ message: err.message })
}
return res.status(500).json({ message: err.message })
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we return a generic error instead? Worried of the existance of errors bubbling up and sensitive info being leaked to our frontend

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the NotFoundError was constructed either with no arguments (defaults to generic error messages of Something went wrong) or a default string (no sensitive info) or a formatted string containing the requested rawPath, similarly for MissingSiteError, it's just constructed with a default message of the requested site was not found in isomer, which i think is ok.

lmk if rawPath is a concern

src/types/pages.ts Show resolved Hide resolved
@seaerchin seaerchin force-pushed the feat/config-parsing branch from db69fa6 to ea8051b Compare May 24, 2023 10:17
@seaerchin seaerchin merged commit b6fe4b7 into develop May 25, 2023
@seaerchin seaerchin deleted the feat/config-parsing branch May 25, 2023 05:09
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.

4 participants