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: Restructure with site branch config relation #4192

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ This repository is home to "pages-core" - a Node.js app that allows government u

## Examples

Partner agencies across the federal government use cloud.gov Pages to host websites. A few examples include:

- [College Scorecard](https://collegescorecard.ed.gov/)
- [Natural Resources Revenue Data](https://revenuedata.doi.gov/)
- [NSF Small Business Innovation Research program](https://seedfund.nsf.gov)

More examples can be found at [https://cloud.gov/pages/success-stories/](https://cloud.gov/pages/success-stories/).

## Development
Expand Down
265 changes: 265 additions & 0 deletions api/controllers/site-branch-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
const _ = require('underscore');
const { wrapHandlers } = require('../utils');
const {
serialize,
serializeMany,
} = require('../serializers/site-branch-config');
const EventCreator = require('../services/EventCreator');
const {
branchRegex,
ValidationError,
parseSiteConfig,
} = require('../utils/validators');
const {
Build, Site, SiteBranchConfig, Event,
} = require('../models');

function generateS3Key(site, context, branch) {
if (context === 'site' || context === 'demo') {
return `/${context}/${site.owner}/${site.repository}`;
}

if (context === 'preview') {
return null;
}

return `preview/${site.owner}/${site.repository}/${branch}`;
}

function validate({ branch, config = {}, context } = {}) {
const parsedConfig = parseSiteConfig(config);

if (context && typeof context !== 'string') {
throw new ValidationError('Context must be a valid string.');
}

if (!branchRegex.test(branch)) {
throw new ValidationError('Branch name must be valid.');
}

if (
parsedConfig instanceof Error
|| typeof parsedConfig === 'number'
|| typeof parsedConfig === 'string'
) {
throw new ValidationError('Config must be valid JSON or YAML.');
}

return { branch, config: parsedConfig, context };
}

module.exports = wrapHandlers({
async find(req, res) {
const { params, user } = req;
const { site_id: siteId } = params;

const site = await Site.forUser(user).findByPk(siteId);

if (!site) {
return res.notFound();
}

const siteBranchConfigs = await SiteBranchConfig.findAll({
where: { siteId: site.id },
});

const json = serializeMany(siteBranchConfigs);

return res.ok(json);
},

async create(req, res) {
const { body, params, user } = req;
const { site_id: siteId } = params;

const site = await Site.forUser(user).findByPk(siteId);

if (!site) {
return res.notFound();
}

try {
const { branch, config, context } = validate(body);
const s3Key = generateS3Key(site, context, branch);

const sbc = await SiteBranchConfig.create({
siteId: site.id,
branch,
config,
context,
s3Key,
});

EventCreator.audit(
Event.labels.USER_ACTION,
req.user,
'SiteBranchConfig Created',
{
siteBranchConfig: { id: sbc.id, siteId },
}
);

if (context && context !== 'preview' && branch) {
const build = await Build.create({
user: req.user.id,
site: sbc.siteId,
branch,
username: req.user.username,
});

await build.enqueue();

EventCreator.audit(
Event.labels.USER_ACTION,
req.user,
'Build started by SiteBranchConfig creation',
{
siteBranchConfig: {
id: sbc.id,
siteId,
buildId: build.id,
},
}
);
}

const json = serialize(sbc);

return res.ok(json);
} catch (error) {
if (error.errors) {
const duplicateBranchError = error.errors.find(
err => err.type === 'unique violation'
);

if (duplicateBranchError) {
return res.badRequest({
message:
'An error occurred creating the site branch config: Branch names must be unique per site.',
});
}
}
return res.badRequest({
message: `An error occurred creating the site branch config: ${error.message}`,
});
}
},

async destroy(req, res) {
const { params, user } = req;
const { id, site_id: siteId } = params;

const site = await Site.forUser(user)
.findByPk(siteId)
.catch(() => null);

if (!site) {
return res.notFound();
}

const siteBranchConfig = await SiteBranchConfig.findOne({
where: {
id,
siteId,
},
});

if (!siteBranchConfig) {
return res.notFound();
}

await siteBranchConfig.destroy();
EventCreator.audit(
Event.labels.USER_ACTION,
req.user,
'SiteBranchConfig Destroyed',
{
siteBranchConfig: {
id,
siteId,
branch: siteBranchConfig.branch,
context: siteBranchConfig.context,
},
}
);

return res.ok({});
},

async update(req, res) {
const { body, params, user } = req;
const { site_id: siteId, id } = params;

const site = await Site.forUser(user).findByPk(siteId);

if (!site) {
return res.notFound();
}

try {
const { branch, config, context } = validate(body);
const sbc = await SiteBranchConfig.findByPk(id);

if (!sbc) {
return res.notFound();
}

const payload = _.omit(
{
branch,
config,
context,
},
x => !x
);

const sbcUpdated = await sbc.update(payload, {
where: {
id,
siteId,
},
});

EventCreator.audit(
Event.labels.USER_ACTION,
req.user,
'SiteBranchConfig Updated',
{
siteBranchConfig: { id: sbcUpdated.id, siteId },
}
);

if (context && context !== 'preview' && branch) {
const build = await Build.create({
user: req.user.id,
site: sbcUpdated.siteId,
branch,
username: req.user.username,
});

await build.enqueue();

EventCreator.audit(
Event.labels.USER_ACTION,
req.user,
'Build started by SiteBranchConfig update',
{
siteBranchConfig: {
id: sbcUpdated.id,
siteId,
buildId: build.id,
},
}
);
}

const json = serialize(sbcUpdated);

return res.ok(json);
} catch (error) {
return res.badRequest({
message: `An error occurred updating the site branch config: ${error.message}`,
});
}
},
});
Loading
Loading