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

ref(services): migrate SitesService to TypeScript #512

Merged
merged 10 commits into from
Oct 12, 2022
24 changes: 24 additions & 0 deletions src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,27 @@ export enum CollaboratorRoles {
export const E2E_ISOMER_ID = "-1"
export const E2E_TEST_EMAIL = "test@e2e"
export const E2E_TEST_CONTACT = "12345678"

export const GH_MAX_REPO_COUNT = 100
export const ISOMERPAGES_REPO_PAGE_COUNT =
(process.env.ISOMERPAGES_REPO_PAGE_COUNT &&
parseInt(process.env.ISOMERPAGES_REPO_PAGE_COUNT, 10)) ||
3
export const ISOMER_GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
export const ISOMER_ADMIN_REPOS = [
"isomercms-backend",
"isomercms-frontend",
"isomer-redirection",
"isomerpages-template",
"isomer-conversion-scripts",
"isomer-wysiwyg",
"isomer-slackbot",
"isomer-tooling",
"generate-site",
"travisci-scripts",
"recommender-train",
"editor",
"ci-test",
"infra",
"markdown-helper",
]
17 changes: 6 additions & 11 deletions src/fixtures/repoInfo.js → src/fixtures/repoInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const repoInfo = {
import { GitHubRepositoryData } from "@root/types/repoInfo"

export const repoInfo: GitHubRepositoryData = {
name: "repo",
private: false,
description:
Expand All @@ -13,7 +15,7 @@ const repoInfo = {
},
}

const repoInfo2 = {
export const repoInfo2: GitHubRepositoryData = {
name: "repo2",
private: false,
description:
Expand All @@ -28,7 +30,7 @@ const repoInfo2 = {
},
}

const adminRepo = {
export const adminRepo: GitHubRepositoryData = {
name: "isomercms-backend",
private: false,
description:
Expand All @@ -43,7 +45,7 @@ const adminRepo = {
},
}

const noAccessRepo = {
export const noAccessRepo: GitHubRepositoryData = {
name: "noaccess",
private: false,
description:
Expand All @@ -57,10 +59,3 @@ const noAccessRepo = {
pull: true,
},
}

module.exports = {
repoInfo,
repoInfo2,
adminRepo,
noAccessRepo,
}
6 changes: 5 additions & 1 deletion src/integration/Sites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { SitesRouter as _SitesRouter } from "@root/routes/v2/authenticated/sites
import { GitHubService } from "@root/services/db/GitHubService"
import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/ConfigYmlService"
import IsomerAdminsService from "@root/services/identity/IsomerAdminsService"
import { SitesService } from "@root/services/utilServices/SitesService"
import SitesService from "@root/services/identity/SitesService"
import TokenStore from "@root/services/identity/TokenStore"
import { getUsersService } from "@services/identity"
import { sequelize } from "@tests/database"

Expand All @@ -25,11 +26,14 @@ const gitHubService = new GitHubService({ axiosInstance: mockAxios.create() })
const configYmlService = new ConfigYmlService({ gitHubService })
const usersService = getUsersService(sequelize)
const isomerAdminsService = new IsomerAdminsService({ repository: IsomerAdmin })
const tokenStore = new TokenStore()
const sitesService = new SitesService({
siteRepository: Site,
gitHubService,
configYmlService,
usersService,
isomerAdminsService,
tokenStore,
})

const SitesRouter = new _SitesRouter({ sitesService })
Expand Down
11 changes: 1 addition & 10 deletions src/routes/v2/authenticated/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const express = require("express")
const {
NetlifyTomlService,
} = require("@services/configServices/NetlifyTomlService")
const { SitesService } = require("@services/utilServices/SitesService")

const { CollaboratorsRouter } = require("./collaborators")
const { NetlifyTomlRouter } = require("./netlifyToml")
Expand All @@ -12,19 +11,11 @@ const { UsersRouter } = require("./users")

const getAuthenticatedSubrouter = ({
authenticationMiddleware,
gitHubService,
configYmlService,
sitesService,
usersService,
isomerAdminsService,
collaboratorsService,
authorizationMiddleware,
}) => {
const sitesService = new SitesService({
gitHubService,
configYmlService,
usersService,
isomerAdminsService,
})
const netlifyTomlService = new NetlifyTomlService()

const sitesV2Router = new SitesRouter({ sitesService })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
const autoBind = require("auto-bind")
const express = require("express")
import autoBind from "auto-bind"
import express from "express"

// Import middleware
const { attachReadRouteHandlerWrapper } = require("@middleware/routeHandler")
import { attachReadRouteHandlerWrapper } from "@middleware/routeHandler"

const {
default: UserWithSiteSessionData,
} = require("@classes/UserWithSiteSessionData")
import UserWithSiteSessionData from "@classes/UserWithSiteSessionData"

const { attachSiteHandler } = require("@root/middleware")
import type UserSessionData from "@root/classes/UserSessionData"
import { attachSiteHandler } from "@root/middleware"
import type { RequestHandler } from "@root/types"
import type SitesService from "@services/identity/SitesService"

class SitesRouter {
constructor({ sitesService }) {
type SitesRouterProps = {
sitesService: SitesService
}

export class SitesRouter {
private readonly sitesService

constructor({ sitesService }: SitesRouterProps) {
this.sitesService = sitesService
// We need to bind all methods because we don't invoke them from the class directly
autoBind(this)
}

addSiteNameToSessionData(userSessionData, siteName) {
addSiteNameToSessionData(userSessionData: UserSessionData, siteName: string) {
const { githubId, accessToken, isomerUserId, email } = userSessionData
return new UserWithSiteSessionData({
githubId,
Expand All @@ -28,13 +34,25 @@ class SitesRouter {
})
}

async getSites(req, res) {
getSites: RequestHandler<
never,
unknown,
never,
never,
{ userSessionData: UserWithSiteSessionData }
> = async (req, res) => {
const { userSessionData } = res.locals
const siteNames = await this.sitesService.getSites(userSessionData)
return res.status(200).json({ siteNames })
}

async getLastUpdated(req, res) {
getLastUpdated: RequestHandler<
{ siteName: string },
unknown,
never,
never,
{ userSessionData: UserWithSiteSessionData }
> = async (req, res) => {
const { userSessionData } = res.locals
const { siteName } = req.params
dcshzj marked this conversation as resolved.
Show resolved Hide resolved
const userWithSiteSessionData = this.addSiteNameToSessionData(
Expand All @@ -47,9 +65,14 @@ class SitesRouter {
return res.status(200).json({ lastUpdated })
}

async getStagingUrl(req, res) {
getStagingUrl: RequestHandler<
{ siteName: string },
unknown,
never,
never,
{ userSessionData: UserWithSiteSessionData }
> = async (req, res) => {
const { userSessionData } = res.locals

const { siteName } = req.params
dcshzj marked this conversation as resolved.
Show resolved Hide resolved
const userWithSiteSessionData = this.addSiteNameToSessionData(
userSessionData,
Expand Down Expand Up @@ -79,5 +102,3 @@ class SitesRouter {
return router
}
}

module.exports = { SitesRouter }
37 changes: 23 additions & 14 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ import { isomerRepoAxiosInstance } from "@services/api/AxiosInstance"
import {
getIdentityAuthService,
getUsersService,
sitesService,
isomerAdminsService,
} from "@services/identity"
import DeploymentsService from "@services/identity/DeploymentsService"
import ReposService from "@services/identity/ReposService"
import SitesService from "@services/identity/SitesService"
import InfraService from "@services/infra/InfraService"

import { AuthorizationMiddleware } from "./middleware/authorization"
Expand Down Expand Up @@ -58,7 +58,14 @@ const helmet = require("helmet")
const createError = require("http-errors")

// Env vars
const { FRONTEND_URL } = process.env
const { FRONTEND_URL, NODE_ENV, LOCAL_SITE_ACCESS_TOKEN } = process.env
const IS_LOCAL_DEV = NODE_ENV === "LOCAL_DEV"

const tokenStore = IS_LOCAL_DEV
? {
getToken: (_apiTokenName) => LOCAL_SITE_ACCESS_TOKEN,
}
: new TokenStore()

// Import middleware

Expand All @@ -78,6 +85,19 @@ const { AuthService } = require("@services/utilServices/AuthService")
const authService = new AuthService({ usersService })
const reposService = new ReposService({ repository: Repo })
const deploymentsService = new DeploymentsService({ repository: Deployment })
const gitHubService = new GitHubService({
axiosInstance: isomerRepoAxiosInstance,
})
const identityAuthService = getIdentityAuthService(gitHubService)
const configYmlService = new ConfigYmlService({ gitHubService })
const sitesService = new SitesService({
siteRepository: Site,
gitHubService,
configYmlService,
usersService,
isomerAdminsService,
tokenStore,
})
const infraService = new InfraService({
sitesService,
reposService,
Expand All @@ -91,13 +111,6 @@ const collaboratorsService = new CollaboratorsService({
whitelist: Whitelist,
})

const gitHubService = new GitHubService({
axiosInstance: isomerRepoAxiosInstance,
})
const identityAuthService = getIdentityAuthService(gitHubService)

const configYmlService = new ConfigYmlService({ gitHubService })

const authenticationMiddleware = getAuthenticationMiddleware()
const authorizationMiddleware = getAuthorizationMiddleware({
identityAuthService,
Expand All @@ -117,12 +130,8 @@ const authenticatedSitesSubrouterV1 = getAuthenticatedSitesSubrouterV1({

const authenticatedSubrouterV2 = getAuthenticatedSubrouter({
authenticationMiddleware,
gitHubService,
configYmlService,
sitesService,
usersService,
reposService,
deploymentsService,
isomerAdminsService,
collaboratorsService,
authorizationMiddleware,
})
Expand Down
Loading