Skip to content

Commit

Permalink
DOCSP-8385: Render commit-based builds (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophstad authored Jan 23, 2020
1 parent 6648732 commit e6dad2b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v0.2] - 2020-01-23

### Added

- Support commit-based builds

## [v0.1] - 2020-01-23

### Added

- Initial release
17 changes: 14 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ include .env.production

.PHONY: stage static

stage:
# To stage a specific build, include the commit hash as environment variable when staging
# example: COMMIT_HASH=123456 make stage
# Here, generate path prefix according to environment variables
prefix:
ifdef COMMIT_HASH
PREFIX = $(COMMIT_HASH)/$(GATSBY_PARSER_BRANCH)/$(GATSBY_SITE)
else
PREFIX = $(GATSBY_PARSER_BRANCH)/$(GATSBY_SITE)
endif


stage: prefix
@if [ -z "${GATSBY_SNOOTY_DEV}" ]; then \
echo "To stage changes to the Snooty frontend, ensure that GATSBY_SNOOTY_DEV=true in your production environment."; exit 1; \
else \
mut-publish public ${STAGING_BUCKET} --prefix=${GATSBY_PARSER_BRANCH}/${GATSBY_SITE} --stage ${ARGS}; \
echo "Hosted at ${STAGING_URL}/${GATSBY_PARSER_BRANCH}/${GATSBY_SITE}/${USER}/${GIT_BRANCH}/"; \
mut-publish public ${STAGING_BUCKET} --prefix=${PREFIX} --stage ${ARGS}; \
echo "Hosted at ${STAGING_URL}/${PREFIX}/${USER}/${GIT_BRANCH}/"; \
fi

static:
Expand Down
31 changes: 24 additions & 7 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ const { getGuideMetadata } = require('./src/utils/get-guide-metadata');
const { getPageSlug } = require('./src/utils/get-page-slug');

// Atlas DB config
const DB = 'snooty';
let DB = 'snooty_dev';
if (process.env.SNOOTY_ENV === 'staging') {
DB = 'snooty_stage';
} else if (process.env.SNOOTY_ENV === 'production') {
DB = 'snooty_prod';
}

const DOCUMENTS_COLLECTION = 'documents';
const ASSETS_COLLECTION = 'assets';
const METADATA_COLLECTION = 'metadata';

const SNOOTY_STITCH_ID = 'snooty-koueq';
let ID_PREFIX;
let PAGE_ID_PREFIX;

// test data properties
const USE_TEST_DATA = process.env.USE_TEST_DATA;
Expand Down Expand Up @@ -71,6 +78,11 @@ const saveAssetFiles = async assets => {
return Promise.all(promises);
};

const constructDbFilter = () => ({
page_id: { $regex: new RegExp(`^${PAGE_ID_PREFIX}/*`) },
commit_hash: process.env.COMMIT_HASH || { $exists: false },
});

exports.sourceNodes = async () => {
// setup env variables
const envResults = validateEnvVariables();
Expand All @@ -95,13 +107,18 @@ exports.sourceNodes = async () => {
}
} else {
// start from index document
ID_PREFIX = `${process.env.GATSBY_SITE}/${process.env.GATSBY_PARSER_USER}/${process.env.GATSBY_PARSER_BRANCH}`;
const query = { _id: { $regex: new RegExp(`^${ID_PREFIX}/*`) } };
PAGE_ID_PREFIX = `${process.env.GATSBY_SITE}/${process.env.GATSBY_PARSER_USER}/${process.env.GATSBY_PARSER_BRANCH}`;
const query = constructDbFilter();
const documents = await stitchClient.callFunction('fetchDocuments', [DB, DOCUMENTS_COLLECTION, query]);

if (documents.length === 0) {
console.error('No documents matched your query.');
process.exit(1);
}

documents.forEach(doc => {
const { _id, ...rest } = doc;
RESOLVED_REF_DOC_MAPPING[_id.replace(`${ID_PREFIX}/`, '')] = rest;
const { page_id, ...rest } = doc;
RESOLVED_REF_DOC_MAPPING[page_id.replace(`${PAGE_ID_PREFIX}/`, '')] = rest;
});
}

Expand Down Expand Up @@ -137,7 +154,7 @@ exports.sourceNodes = async () => {

exports.createPages = async ({ actions }) => {
const { createPage } = actions;
const metadata = await stitchClient.callFunction('fetchDocument', [DB, METADATA_COLLECTION, { _id: ID_PREFIX }]);
const metadata = await stitchClient.callFunction('fetchDocument', [DB, METADATA_COLLECTION, constructDbFilter()]);

return new Promise((resolve, reject) => {
PAGES.forEach(page => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"develop": "gatsby develop",
"format": "npm run prettier -- --check",
"format:fix": "npm run prettier -- --write",
"lint": "eslint --ignore-path .gitignore --ignore-path .prettierignore --ignore-pattern 'docs-tools/*' --ext .js,.jsx .",
"lint": "eslint --ignore-path .gitignore --ignore-path .prettierignore --ignore-pattern 'docs-tools/*' --ignore-pattern 'preview/*' --ext .js,.jsx .",
"lint:fix": "npm run lint -- --fix",
"prettier": "prettier \"**/*.{js,jsx,json,md}\"",
"preview": "webpack-dev-server --open --config ./webpack.config.js --env.PREVIEW_MODE='cli'",
Expand Down
17 changes: 15 additions & 2 deletions src/utils/generate-path-prefix.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const normalizePath = path => path.replace(/\/+/g, `/`);

const generatePathPrefix = ({ parserBranch, project, snootyBranch, user }) => {
const base = `/${project}/${user}/${parserBranch}`;
return process.env.GATSBY_SNOOTY_DEV ? `/${parserBranch}/${project}/${user}/${snootyBranch}` : base;
let prefix = '';
if (process.env.COMMIT_HASH) prefix = `${process.env.COMMIT_HASH}`;

// Include the Snooty branch in pathPrefix for Snooty developers. mut automatically
// includes the git branch of the repo where it is called, so this parameter must
// be present in the URL's path prefix in order to be mut-compatible.
//
// TODO: Simplify this logic when Snooty development is staged in integration environment
const base = `${project}/${user}`;
const path = process.env.GATSBY_SNOOTY_DEV
? `/${prefix}/${parserBranch}/${base}/${snootyBranch}`
: `/${prefix}/${base}/${parserBranch}`;
return normalizePath(path);
};

// TODO: switch to ES6 export syntax if Gatsby implements support for ES6 module imports
Expand Down

0 comments on commit e6dad2b

Please sign in to comment.