Skip to content

Commit

Permalink
circle-ci integration on progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish-Upadhyay-101 committed Feb 7, 2023
1 parent b0ffac2 commit 80d219c
Show file tree
Hide file tree
Showing 11 changed files with 385 additions and 341 deletions.
281 changes: 139 additions & 142 deletions backend/src/controllers/v1/integrationAuthController.ts
Original file line number Diff line number Diff line change
@@ -1,124 +1,120 @@
import { Request, Response } from 'express';
import { Types } from 'mongoose';
import * as Sentry from '@sentry/node';
import {
Integration,
IntegrationAuth,
Bot
} from '../../models';
import { INTEGRATION_SET, INTEGRATION_OPTIONS } from '../../variables';
import { IntegrationService } from '../../services';
import { getApps, revokeAccess } from '../../integrations';

export const getIntegrationOptions = async (
req: Request,
res: Response
) => {
return res.status(200).send({
integrationOptions: INTEGRATION_OPTIONS
});
}
import { Request, Response } from "express";
import { Types } from "mongoose";
import * as Sentry from "@sentry/node";
import { Integration, IntegrationAuth, Bot } from "../../models";
import { INTEGRATION_SET, INTEGRATION_OPTIONS } from "../../variables";
import { IntegrationService } from "../../services";
import { getApps, revokeAccess } from "../../integrations";

export const getIntegrationOptions = async (req: Request, res: Response) => {
return res.status(200).send({
integrationOptions: INTEGRATION_OPTIONS,
});
};

/**
* Perform OAuth2 code-token exchange as part of integration [integration] for workspace with id [workspaceId]
* @param req
* @param res
* @returns
*/
export const oAuthExchange = async (
req: Request,
res: Response
) => {
try {
const { workspaceId, code, integration } = req.body;

if (!INTEGRATION_SET.has(integration))
throw new Error('Failed to validate integration');

const environments = req.membership.workspace?.environments || [];
if(environments.length === 0){
throw new Error("Failed to get environments")
}

await IntegrationService.handleOAuthExchange({
workspaceId,
integration,
code,
environment: environments[0].slug,
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to get OAuth2 code-token exchange'
});
}

return res.status(200).send({
message: 'Successfully enabled integration authorization'
});
export const oAuthExchange = async (req: Request, res: Response) => {
try {
const { workspaceId, code, integration } = req.body;

if (!INTEGRATION_SET.has(integration))
throw new Error("Failed to validate integration");

const environments = req.membership.workspace?.environments || [];
if (environments.length === 0) {
throw new Error("Failed to get environments");
}

await IntegrationService.handleOAuthExchange({
workspaceId,
integration,
code,
environment: environments[0].slug,
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to get OAuth2 code-token exchange",
});
}

return res.status(200).send({
message: "Successfully enabled integration authorization",
});
};

/**
* Save integration access token as part of integration [integration] for workspace with id [workspaceId]
* @param req
* @param res
* @param req
* @param res
*/
export const saveIntegrationAccessToken = async (
req: Request,
res: Response
req: Request,
res: Response
) => {
// TODO: refactor
let integrationAuth;
try {
const {
workspaceId,
accessToken,
integration
}: {
workspaceId: string;
accessToken: string;
integration: string;
} = req.body;

integrationAuth = await IntegrationAuth.findOneAndUpdate({
workspace: new Types.ObjectId(workspaceId),
integration
}, {
workspace: new Types.ObjectId(workspaceId),
integration
}, {
new: true,
upsert: true
});

const bot = await Bot.findOne({
workspace: new Types.ObjectId(workspaceId),
isActive: true
});

if (!bot) throw new Error('Bot must be enabled to save integration access token');

// encrypt and save integration access token
integrationAuth = await IntegrationService.setIntegrationAuthAccess({
integrationAuthId: integrationAuth._id.toString(),
accessToken,
accessExpiresAt: undefined
});

if (!integrationAuth) throw new Error('Failed to save integration access token');
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to save access token for integration'
});
}

return res.status(200).send({
integrationAuth
});
}
// TODO: refactor
let integrationAuth;
try {
const {
workspaceId,
accessToken,
integration,
}: {
workspaceId: string;
accessToken: string;
integration: string;
} = req.body;

integrationAuth = await IntegrationAuth.findOneAndUpdate(
{
workspace: new Types.ObjectId(workspaceId),
integration,
},
{
workspace: new Types.ObjectId(workspaceId),
integration,
},
{
new: true,
upsert: true,
}
);

const bot = await Bot.findOne({
workspace: new Types.ObjectId(workspaceId),
isActive: true,
});

if (!bot)
throw new Error("Bot must be enabled to save integration access token");

// encrypt and save integration access token
integrationAuth = await IntegrationService.setIntegrationAuthAccess({
integrationAuthId: integrationAuth._id.toString(),
accessToken,
accessExpiresAt: undefined,
});

if (!integrationAuth)
throw new Error("Failed to save integration access token");
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to save access token for integration",
});
}

return res.status(200).send({
integrationAuth,
});
};

/**
* Return list of applications allowed for integration with integration authorization id [integrationAuthId]
Expand All @@ -127,23 +123,24 @@ export const saveIntegrationAccessToken = async (
* @returns
*/
export const getIntegrationAuthApps = async (req: Request, res: Response) => {
let apps;
try {
apps = await getApps({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to get integration authorization applications'
});
}

return res.status(200).send({
apps
});
let apps;
try {
apps = await getApps({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken,
});
} catch (err) {
console.log(err); // testing
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to get integration authorization applications",
});
}

return res.status(200).send({
apps,
});
};

/**
Expand All @@ -153,21 +150,21 @@ export const getIntegrationAuthApps = async (req: Request, res: Response) => {
* @returns
*/
export const deleteIntegrationAuth = async (req: Request, res: Response) => {
let integrationAuth;
try {
integrationAuth = await revokeAccess({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: 'Failed to delete integration authorization'
});
}
return res.status(200).send({
integrationAuth
});
}
let integrationAuth;
try {
integrationAuth = await revokeAccess({
integrationAuth: req.integrationAuth,
accessToken: req.accessToken,
});
} catch (err) {
Sentry.setUser({ email: req.user.email });
Sentry.captureException(err);
return res.status(400).send({
message: "Failed to delete integration authorization",
});
}

return res.status(200).send({
integrationAuth,
});
};
Loading

0 comments on commit 80d219c

Please sign in to comment.