Skip to content

Commit

Permalink
feat(rr): config parsing (#662)
Browse files Browse the repository at this point in the history
* refactor(parsepagename): add guard for markdownpage

* refactor(types/utils): update type def and shift util function otu

* feat(config): add parsing

* feat(pageservice): refactor to have stronger guarantee on method

* chore(errors): add new error types

* chore(configService): add precondition for pathinfo

* refactor(types): rejig

* refactor(rr service): more elaborate parsing n defaults

* ref(review): update call sites and add new method

* chore(services): init service

* fix(rr): change andThen to map

* chore(rr): ref to fiot new api

* chore(error): add status code

* fix(sites):  update typing and refactor for result

* chore(review): update review dto

* ref(types): add more github types

* chore(pages): add docs

* fix(tests): fixed tests and services

* chore(notifications spec): add missing imports

* test(tests): fixed all tests

* chore(types): update types and add default return
  • Loading branch information
seaerchin authored May 25, 2023
1 parent b61f048 commit b6fe4b7
Show file tree
Hide file tree
Showing 26 changed files with 673 additions and 391 deletions.
7 changes: 7 additions & 0 deletions src/errors/ConfigParseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseIsomerError } from "@root/errors/BaseError"

export default class ConfigParseError extends BaseIsomerError {
constructor(fileName: string) {
super(500, `The given file: ${fileName} was not a config file!`)
}
}
9 changes: 9 additions & 0 deletions src/errors/NetworkError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseIsomerError } from "@root/errors/BaseError"

export default class NetworkError extends BaseIsomerError {
constructor(
message = "An error occurred with the network whilst processing your request. Please try again later."
) {
super(500, message)
}
}
7 changes: 7 additions & 0 deletions src/errors/PageParseError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseIsomerError } from "@root/errors/BaseError"

export default class PageParseError extends BaseIsomerError {
constructor(fileName: string) {
super(500, `The given file: ${fileName} was not a page!`)
}
}
5 changes: 4 additions & 1 deletion src/integration/NotificationOnEditHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { ResourcePageService } from "@root/services/fileServices/MdPageServices/
import { SubcollectionPageService } from "@root/services/fileServices/MdPageServices/SubcollectionPageService"
import { UnlinkedPageService } from "@root/services/fileServices/MdPageServices/UnlinkedPageService"
import { CollectionYmlService } from "@root/services/fileServices/YmlFileServices/CollectionYmlService"
import { ConfigService } from "@root/services/fileServices/YmlFileServices/ConfigService"
import { FooterYmlService } from "@root/services/fileServices/YmlFileServices/FooterYmlService"
import { GitHubService } from "@services/db/GitHubService"
import * as ReviewApi from "@services/db/review"
Expand Down Expand Up @@ -100,14 +101,16 @@ const pageService = new PageService({
unlinkedPageService,
resourceRoomDirectoryService,
})
const configService = new ConfigService()
const reviewRequestService = new ReviewRequestService(
(mockGithubService as unknown) as typeof ReviewApi,
User,
ReviewRequest,
Reviewer,
ReviewMeta,
ReviewRequestView,
pageService
pageService,
configService
)
const sitesService = new SitesService({
siteRepository: Site,
Expand Down
6 changes: 4 additions & 2 deletions src/integration/Notifications.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
} from "@root/fixtures/sessionData"
import { getAuthorizationMiddleware } from "@root/middleware"
import { NotificationsRouter as _NotificationsRouter } from "@root/routes/v2/authenticated/notifications"
import { SitesRouter as _SitesRouter } from "@root/routes/v2/authenticated/sites"
import { genericGitHubAxiosInstance } from "@root/services/api/AxiosInstance"
import { GitHubService } from "@root/services/db/GitHubService"
import { BaseDirectoryService } from "@root/services/directoryServices/BaseDirectoryService"
Expand All @@ -44,6 +43,7 @@ import { ResourcePageService } from "@root/services/fileServices/MdPageServices/
import { SubcollectionPageService } from "@root/services/fileServices/MdPageServices/SubcollectionPageService"
import { UnlinkedPageService } from "@root/services/fileServices/MdPageServices/UnlinkedPageService"
import { CollectionYmlService } from "@root/services/fileServices/YmlFileServices/CollectionYmlService"
import { ConfigService } from "@root/services/fileServices/YmlFileServices/ConfigService"
import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/ConfigYmlService"
import { FooterYmlService } from "@root/services/fileServices/YmlFileServices/FooterYmlService"
import CollaboratorsService from "@root/services/identity/CollaboratorsService"
Expand Down Expand Up @@ -101,14 +101,16 @@ const pageService = new PageService({
unlinkedPageService,
resourceRoomDirectoryService,
})
const configService = new ConfigService()
const reviewRequestService = new ReviewRequestService(
(gitHubService as unknown) as typeof ReviewApi,
User,
ReviewRequest,
Reviewer,
ReviewMeta,
ReviewRequestView,
pageService
pageService,
configService
)
const sitesService = new SitesService({
siteRepository: Site,
Expand Down
31 changes: 16 additions & 15 deletions src/integration/Reviews.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import { ResourcePageService } from "@root/services/fileServices/MdPageServices/
import { SubcollectionPageService } from "@root/services/fileServices/MdPageServices/SubcollectionPageService"
import { UnlinkedPageService } from "@root/services/fileServices/MdPageServices/UnlinkedPageService"
import { CollectionYmlService } from "@root/services/fileServices/YmlFileServices/CollectionYmlService"
import { ConfigService } from "@root/services/fileServices/YmlFileServices/ConfigService"
import { FooterYmlService } from "@root/services/fileServices/YmlFileServices/FooterYmlService"
import { ReviewRequestDto } from "@root/types/dto/review"
import { GitHubService } from "@services/db/GitHubService"
Expand Down Expand Up @@ -132,14 +133,16 @@ const pageService = new PageService({
unlinkedPageService,
resourceRoomDirectoryService,
})
const configService = new ConfigService()
const reviewRequestService = new ReviewRequestService(
(gitHubService as unknown) as typeof ReviewApi,
User,
ReviewRequest,
Reviewer,
ReviewMeta,
ReviewRequestView,
pageService
pageService,
configService
)
const sitesService = new SitesService({
siteRepository: Site,
Expand Down Expand Up @@ -263,20 +266,20 @@ describe("Review Requests Integration Tests", () => {
const expected = {
items: [
{
type: ["page"],
type: "page",
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
cmsFileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/homepage`,
lastEditedBy: MOCK_USER_EMAIL_TWO,
lastEditedTime: new Date(MOCK_GITHUB_COMMIT_DATE_THREE).getTime(),
},
{
type: ["page"],
type: "page",
name: MOCK_GITHUB_FILENAME_ALPHA_TWO,
path: MOCK_GITHUB_FILEPATH_ALPHA_TWO.split("/").filter((x) => x),
stagingUrl: `${MOCK_DEPLOYMENT_DBENTRY_ONE.stagingUrl}${MOCK_PAGE_PERMALINK}`,
fileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/editPage/${MOCK_GITHUB_FILENAME_ALPHA_TWO}`,
cmsFileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/editPage/${MOCK_GITHUB_FILENAME_ALPHA_TWO}`,
lastEditedBy: MOCK_USER_EMAIL_TWO,
lastEditedTime: new Date(MOCK_GITHUB_COMMIT_DATE_THREE).getTime(),
},
Expand Down Expand Up @@ -750,22 +753,22 @@ describe("Review Requests Integration Tests", () => {
reviewRequestedTime: new Date(MOCK_GITHUB_DATE_ONE).getTime(),
changedItems: [
{
type: ["page"],
type: "page",
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
lastEditedBy: MOCK_USER_EMAIL_TWO,
lastEditedTime: new Date(MOCK_GITHUB_COMMIT_DATE_THREE).getTime(),
stagingUrl: `${MOCK_DEPLOYMENT_DBENTRY_ONE.stagingUrl}${MOCK_PAGE_PERMALINK}`,
cmsFileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/homepage`,
},
{
type: ["page"],
type: "page",
name: MOCK_GITHUB_FILENAME_ALPHA_TWO,
path: MOCK_GITHUB_FILEPATH_ALPHA_TWO.split("/").filter((x) => x),
stagingUrl: `${MOCK_DEPLOYMENT_DBENTRY_ONE.stagingUrl}${MOCK_PAGE_PERMALINK}`,
fileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/editPage/${MOCK_GITHUB_FILENAME_ALPHA_TWO}`,
lastEditedBy: MOCK_USER_EMAIL_TWO,
lastEditedTime: new Date(MOCK_GITHUB_COMMIT_DATE_THREE).getTime(),
stagingUrl: `${MOCK_DEPLOYMENT_DBENTRY_ONE.stagingUrl}${MOCK_PAGE_PERMALINK}`,
cmsFileUrl: `${FRONTEND_URL}/sites/${MOCK_REPO_NAME_ONE}/editPage/${MOCK_GITHUB_FILENAME_ALPHA_TWO}`,
},
],
}
Expand Down Expand Up @@ -1480,7 +1483,6 @@ describe("Review Requests Integration Tests", () => {
const actual = await request(app).post(
`/${MOCK_REPO_NAME_ONE}/${MOCK_GITHUB_PULL_REQUEST_NUMBER}/approve`
)
console.log(actual.error)

// Assert
expect(actual.statusCode).toEqual(403)
Expand All @@ -1498,7 +1500,6 @@ describe("Review Requests Integration Tests", () => {
const actual = await request(app).post(
`/${MOCK_REPO_NAME_ONE}/${MOCK_GITHUB_PULL_REQUEST_NUMBER}/approve`
)
console.log(actual.error)

// Assert
expect(actual.statusCode).toEqual(403)
Expand Down
5 changes: 4 additions & 1 deletion src/integration/Sites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ResourcePageService } from "@root/services/fileServices/MdPageServices/
import { SubcollectionPageService } from "@root/services/fileServices/MdPageServices/SubcollectionPageService"
import { UnlinkedPageService } from "@root/services/fileServices/MdPageServices/UnlinkedPageService"
import { CollectionYmlService } from "@root/services/fileServices/YmlFileServices/CollectionYmlService"
import { ConfigService } from "@root/services/fileServices/YmlFileServices/ConfigService"
import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/ConfigYmlService"
import { FooterYmlService } from "@root/services/fileServices/YmlFileServices/FooterYmlService"
import IsomerAdminsService from "@root/services/identity/IsomerAdminsService"
Expand Down Expand Up @@ -89,14 +90,16 @@ const pageService = new PageService({
unlinkedPageService,
resourceRoomDirectoryService,
})
const configService = new ConfigService()
const reviewRequestService = new ReviewRequestService(
gitHubService,
User,
ReviewRequest,
Reviewer,
ReviewMeta,
ReviewRequestView,
pageService
pageService,
configService
)
const sitesService = new SitesService({
siteRepository: Site,
Expand Down
6 changes: 3 additions & 3 deletions src/routes/v2/authenticated/__tests__/Sites.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express"
import { ok, okAsync } from "neverthrow"
import { okAsync } from "neverthrow"
import request from "supertest"

import type { AuthorizationMiddleware } from "@middleware/authorization"
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("Sites Router", () => {
describe("getStagingUrl", () => {
it("returns the site's staging URL", async () => {
const stagingUrl = "staging-url"
mockSitesService.getStagingUrl.mockResolvedValueOnce(ok(stagingUrl))
mockSitesService.getStagingUrl.mockReturnValueOnce(okAsync(stagingUrl))

const resp = await request(app)
.get(`/${mockSiteName}/stagingUrl`)
Expand Down Expand Up @@ -138,7 +138,7 @@ describe("Sites Router", () => {
stagingUrl: "staging-url",
siteUrl: "prod-url",
}
mockSitesService.getSiteInfo.mockResolvedValueOnce(ok(siteInfo))
mockSitesService.getSiteInfo.mockReturnValueOnce(okAsync(siteInfo))

const resp = await request(app).get(`/${mockSiteName}/info`).expect(200)

Expand Down
6 changes: 4 additions & 2 deletions src/routes/v2/authenticated/__tests__/review.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ describe("Review Requests Router", () => {
// Arrange
const mockFilesChanged = ["file1", "file2"]
mockIdentityUsersService.getSiteMember.mockResolvedValueOnce("user")
mockReviewRequestService.compareDiff.mockResolvedValueOnce(
mockFilesChanged
mockReviewRequestService.compareDiff.mockReturnValueOnce(
okAsync(mockFilesChanged)
)
mockSitesService.getBySiteName.mockResolvedValueOnce(ok(true))
mockSitesService.getStagingUrl.mockReturnValueOnce(okAsync("staging-url"))
mockGithubService.getRepoInfo.mockResolvedValueOnce(true)

// Act
const response = await request(app).get("/mockSite/review/compare")
Expand Down
19 changes: 12 additions & 7 deletions src/routes/v2/authenticated/review.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import autoBind from "auto-bind"
import express from "express"
import _ from "lodash"
import { ResultAsync } from "neverthrow"

import logger from "@logger/logger"

Expand All @@ -15,6 +14,8 @@ import UserWithSiteSessionData from "@classes/UserWithSiteSessionData"

import { CollaboratorRoles, ReviewRequestStatus } from "@root/constants"
import { SiteMember, User } from "@root/database/models"
import MissingSiteError from "@root/errors/MissingSiteError"
import { NotFoundError } from "@root/errors/NotFoundError"
import { GitHubService } from "@root/services/db/GitHubService"
import CollaboratorsService from "@root/services/identity/CollaboratorsService"
import NotificationsService from "@root/services/identity/NotificationsService"
Expand All @@ -25,10 +26,10 @@ import { ResponseErrorBody } from "@root/types/dto/error"
import {
CommentItem,
DashboardReviewRequestDto,
EditedItemDto,
UpdateReviewRequestDto,
ReviewRequestDto,
BlobDiffDto,
EditedItemDto,
} from "@root/types/dto/review"
import ReviewRequestService from "@services/review/ReviewRequestService"
// eslint-disable-next-line import/prefer-default-export
Expand Down Expand Up @@ -125,14 +126,18 @@ export class ReviewsRouter {
return this.sitesService
.getStagingUrl(userWithSiteSessionData)
.andThen((stagingLink) =>
ResultAsync.fromSafePromise(
this.reviewRequestService.compareDiff(
userWithSiteSessionData,
stagingLink
)
this.reviewRequestService.compareDiff(
userWithSiteSessionData,
stagingLink
)
)
.map((items) => res.status(200).json({ items }))
.mapErr((err) => {
if (err instanceof MissingSiteError || err instanceof NotFoundError) {
return res.status(404).json({ message: err.message })
}
return res.status(500).json({ message: err.message })
})
}

createReviewRequest: RequestHandler<
Expand Down
Loading

0 comments on commit b6fe4b7

Please sign in to comment.