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/site creation form email #679

Merged
merged 8 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
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: 9 additions & 3 deletions src/routes/formsgSiteCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const SITE_CREATE_FORM_KEY = config.get("formSg.siteCreateFormKey")
const REQUESTER_EMAIL_FIELD = "Government E-mail"
const SITE_NAME_FIELD = "Site Name"
const REPO_NAME_FIELD = "Repository Name"
const OWNER_NAME_FIELD = "Site Owner E-mail"

export interface FormsgRouterProps {
usersService: UsersService
Expand Down Expand Up @@ -51,6 +52,7 @@ export class FormsgRouter {
const requesterEmail = getField(responses, REQUESTER_EMAIL_FIELD)
const siteName = getField(responses, SITE_NAME_FIELD)
const repoName = getField(responses, REPO_NAME_FIELD)
const ownerEmail = getField(responses, OWNER_NAME_FIELD)?.toLowerCase()
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could we do a strip on the string?
worried if ops put space then the string equality operation fails and it registers as new email
(maybe this error case is captured downstream)

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 catch, added in 80cfbbd


logger.info(
`Create site form submission [${submissionId}] (repoName '${repoName}', siteName '${siteName}') requested by <${requesterEmail}>`
Expand All @@ -75,16 +77,20 @@ export class FormsgRouter {
await this.sendCreateError(requesterEmail, repoName, submissionId, err)
return res.sendStatus(200)
}
const foundUser = await this.usersService.findByEmail(requesterEmail)
if (!foundUser) {
const foundIsomerRequester = await this.usersService.findByEmail(
requesterEmail
)
if (!foundIsomerRequester) {
const err = `Form submitter ${requesterEmail} is not an Isomer user. Register an account for this user and try again.`
await this.sendCreateError(requesterEmail, repoName, submissionId, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry a bit confused, this is an error flow right? Why are we returning 200 res after sendCreateError?

Copy link
Contributor Author

@alexanderleegs alexanderleegs Apr 6, 2023

Choose a reason for hiding this comment

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

This endpoint is only triggered by forms, which expects a 200 response, otherwise it could trigger a retry! The error handling is done separately via email

return res.sendStatus(200)
}
const foundOwner = await this.usersService.findOrCreateByEmail(ownerEmail)
kishore03109 marked this conversation as resolved.
Show resolved Hide resolved

// 3. Use service to create site
const { deployment } = await this.infraService.createSite(
foundUser,
foundIsomerRequester,
foundOwner,
siteName,
repoName
)
Expand Down
21 changes: 11 additions & 10 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,6 @@ const launchesService = new LaunchesService({
launchClient,
})
const queueService = new QueueService()
const infraService = new InfraService({
sitesService,
reposService,
deploymentsService,
launchesService,
queueService,
})

// poller for incoming queue
infraService.pollQueue()

const identityAuthService = getIdentityAuthService(gitHubService)
const collaboratorsService = new CollaboratorsService({
Expand All @@ -179,6 +169,17 @@ const collaboratorsService = new CollaboratorsService({
whitelist: Whitelist,
})

const infraService = new InfraService({
sitesService,
reposService,
deploymentsService,
launchesService,
queueService,
collaboratorsService,
})
// poller for incoming queue
infraService.pollQueue()

const authenticationMiddleware = getAuthenticationMiddleware()
const authorizationMiddleware = getAuthorizationMiddleware({
identityAuthService,
Expand Down
10 changes: 0 additions & 10 deletions src/services/identity/ReposService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export default class ReposService {
const repoUrl = `https://github.com/isomerpages/${repoName}`

await this.createRepoOnGithub(repoName)
await this.createTeamOnGitHub(repoName)
await this.generateRepoAndPublishToGitHub(repoName, repoUrl)
return this.create({
name: repoName,
Expand Down Expand Up @@ -148,15 +147,6 @@ export default class ReposService {
private: false,
})

createTeamOnGitHub = (
repoName: string
): Promise<octokitCreateTeamResponseType> =>
octokit.teams.create({
org: ISOMER_GITHUB_ORGANIZATION_NAME,
name: repoName,
privacy: "closed",
})

setRepoAndTeamPermissions = async (repoName: string): Promise<void> => {
await octokit.repos.updateBranchProtection({
owner: ISOMER_GITHUB_ORGANIZATION_NAME,
Expand Down
7 changes: 7 additions & 0 deletions src/services/identity/UsersService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ class UsersService {
return user
}

async findOrCreateByEmail(email: string | undefined) {
const [user] = await this.repository.findOrCreate({
where: { email },
})
return user
}

async login(githubId: string): Promise<User> {
return this.sequelize.transaction<User>(async (transaction) => {
// NOTE: The service's findOrCreate is not being used here as this requires an explicit transaction
Expand Down
25 changes: 23 additions & 2 deletions src/services/infra/InfraService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ReposService from "@services/identity/ReposService"
import SitesService from "@services/identity/SitesService"
import { mailer } from "@services/utilServices/MailClient"

import CollaboratorsService from "../identity/CollaboratorsService"
import QueueService from "../identity/QueueService"

const SITE_LAUNCH_UPDATE_INTERVAL = 30000
Expand All @@ -31,6 +32,7 @@ interface InfraServiceProps {
deploymentsService: DeploymentsService
launchesService: LaunchesService
queueService: QueueService
collaboratorsService: CollaboratorsService
}

interface dnsRecordDto {
Expand All @@ -49,27 +51,45 @@ export default class InfraService {

private readonly queueService: InfraServiceProps["queueService"]

private readonly collaboratorsService: InfraServiceProps["collaboratorsService"]

constructor({
sitesService,
reposService,
deploymentsService,
launchesService,
queueService,
collaboratorsService,
}: InfraServiceProps) {
this.sitesService = sitesService
this.reposService = reposService
this.deploymentsService = deploymentsService
this.launchesService = launchesService
this.queueService = queueService
this.collaboratorsService = collaboratorsService
}

createSite = async (creator: User, siteName: string, repoName: string) => {
createSite = async (
creator: User,
member: User,
siteName: string,
repoName: string
) => {
let site: Site | undefined // For error handling
const memberEmail = member.email
if (!memberEmail) {
logger.error(
`createSite: initial member for ${siteName} does not have associated email`
)
throw new Error(
`createSite: initial member for ${siteName} does not have associated email`
)
}
try {
// 1. Create a new site record in the Sites table
const newSiteParams = {
name: siteName,
apiTokenName: "", // TODO: figure this out
apiTokenName: "", // TODO (IS-76): Remove once DB has removed this param
creator,
creatorId: creator.id,
}
Expand All @@ -96,6 +116,7 @@ export default class InfraService {

// 5. Set up permissions
await this.reposService.setRepoAndTeamPermissions(repoName)
await this.collaboratorsService.create(repoName, memberEmail, true)
kishore03109 marked this conversation as resolved.
Show resolved Hide resolved

// 6. Update status
const updateSuccessSiteInitParams = {
Expand Down