Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
feat: parse github PR files
Browse files Browse the repository at this point in the history
  • Loading branch information
tabathadelane committed May 17, 2022
1 parent 9357a35 commit 1d2c810
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 48 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
"gatsby-source-filesystem": "^3.8.0",
"gatsby-transformer-json": "^3.3.0",
"gatsby-transformer-sharp": "^3.13.0",
"sass": "^1.49.9",
"js-yaml": "^4.1.0",
"pluralize": "^8.0.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-feather": "^2.0.9",
"react-helmet": "^6.1.0",
"react-markdown": "^6.0.0",
"react-slick": "^0.28.1",
"sass": "^1.49.9",
"sharp": "^0.29.1",
"slick-carousel": "^1.8.1",
"use-mobile-detect-hook": "^1.0.5",
Expand Down
51 changes: 51 additions & 0 deletions src/hooks/usePullRequestQuickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect, useState } from 'react';

import { getQuickstartFilesFromPR } from '../utils/preview/fetchHelpers';
import { parseQuickstartFilesFromPR } from '../utils/preview/parseHelpers';
import { navigate } from 'gatsby';

const usePullRequestQuickstart = (location) => {
const [contentFiles, setContentFiles] = useState([]);

useEffect(() => {
// grab query parameters to determine if it is a local preview or
// PR preview
const urlParams = new URLSearchParams(location.search);
const prNumber = urlParams.get('pr');
const quickstartPath = urlParams.get('quickstart');

// check to make sure query parameters are set
// otherwise, return home
if (!prNumber || !quickstartPath) {
navigate('/');
return;
}

/*
* Async function to walk the file system in Github
* and set the content to a stateful variable.
**/
const fetchFiles = async () => {
const rawFileContent = await getQuickstartFilesFromPR(
prNumber,
quickstartPath
);

// Error handling in the chance Github returns
// a non 200 status
if (rawFileContent === null) {
navigate('/');
return;
}

const quickstart = await parseQuickstartFilesFromPR(rawFileContent);

setContentFiles(quickstart);
};

fetchFiles();
}, []);
return contentFiles;
};

export default usePullRequestQuickstart;
52 changes: 5 additions & 47 deletions src/pages/preview.jsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,15 @@
import React, { useEffect, useState } from 'react';
import React from 'react';

import usePullRequestQuickstart from '../hooks/usePullRequestQuickstart';

import PropTypes from 'prop-types';
import { getQuickstartFilesFromPR } from '../utils/preview/fetchHelpers';
import { navigate } from 'gatsby';

const PreviewPage = ({ location }) => {
const [contentFiles, setContentFiles] = useState([]);

// TODO: Make this into a custom hook to reduce useEffect usage
useEffect(() => {
// grab query parameters to determine if it is a local preview or
// PR preview
const urlParams = new URLSearchParams(location.search);
const prNumber = urlParams.get('pr');
const quickstartPath = urlParams.get('quickstart');

// check to make sure query parameters are set
// otherwise, return home
if (!prNumber || !quickstartPath) {
navigate('/');
return;
}

/*
* Async function to walk the file system in Github
* and set the content to a stateful variable.
**/
const fetchRawFiles = async () => {
const fileContent = await getQuickstartFilesFromPR(
prNumber,
quickstartPath
);

// Error handling in the chance Github returns
// a non 200 status
if (fileContent === null) {
navigate('/');
return;
}
setContentFiles(fileContent);
};
fetchRawFiles();
}, []);
const contentFiles = usePullRequestQuickstart(location);

// To console log the results as part of AC
// TODO: Remove/refactor this in parsing implementation
useEffect(() => {
if (contentFiles.length < 1) {
return;
}

console.log(contentFiles);
}, [contentFiles]);
console.log('Parsed quickstart content:', contentFiles);

return <span>oh hai</span>;
};
Expand Down
99 changes: 99 additions & 0 deletions src/utils/preview/parseHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import yaml from 'js-yaml';

let dashboardDirs = {};
let alertsDir = [];

let parsedQuickstart = {
alerts: alertsDir ?? [],
dashboards: dashboardDirs ?? [],
};

const parseQuickstartDir = (file) => {
if (file.fileName.includes('logo') && file.type === 'image') {
parsedQuickstart.logoUrl = file.content;
}
if (file.type === 'yaml') {
//if config, parse the yaml
const loadYaml = yaml.load(file.content);

//itterate through the array of documentation objects to trim new lines
const trimmedDocumentation = loadYaml.documentation.map((doc) => {
doc.description = doc.description?.trim();
return doc;
});
//build the packUrl since it is not part of the raw github file contents
const packUrl =
'https://github.com/newrelic/newrelic-quickstarts/tree/main/quickstarts/' +
file.filePath.split('/config')[0];
//assumes the pathName is always directly under 'quickstarts/'

parsedQuickstart.authors = loadYaml.authors ?? [];
parsedQuickstart.description = loadYaml.description?.trim() ?? '';
parsedQuickstart.documentation = trimmedDocumentation ?? [];
parsedQuickstart.id = loadYaml.id ?? '';
parsedQuickstart.installPlans = loadYaml.installPlans ?? [];
parsedQuickstart.keywords = loadYaml.keywords ?? [];
parsedQuickstart.level = loadYaml.level ?? '';
parsedQuickstart.name = loadYaml.slug ?? '';
parsedQuickstart.packUrl = packUrl ?? '';
parsedQuickstart.relatedResources = [];
parsedQuickstart.summary = loadYaml.summary?.trim() ?? '';
parsedQuickstart.title = loadYaml.title ?? '';
}
};

const parseDashboardDir = (file) => {
//split each filepath to get its dashboard dir
const getDir = file.filePath.split('/dashboards/')[1].split('/')[0];
//add it to the dashboard dirs object as a key if new
//or update it if not
if (!dashboardDirs[getDir]) {
dashboardDirs[getDir] = { name: '', description: '', screenshots: [] };
}
if (file.type === 'json') {
const dashboardContent = JSON.parse(file.content);
dashboardDirs[getDir]['name'] = dashboardContent.name ?? '';
dashboardDirs[getDir]['description'] = dashboardContent.description ?? '';
}
if (file.type === 'image') {
dashboardDirs[getDir]['screenshots'].push(file.content);
}
};

const parseAlertsDir = (file) => {
//add each alert to the array, parsing out its relevent data into an object
const loadYaml = yaml.load(file.content);

const alert = {
details: loadYaml.description?.trim() ?? '',
name: loadYaml.name?.trim() ?? '',
type: loadYaml.type?.trim() ?? '',
};

alertsDir.push(alert);
};

const checkFileType = (rawFile) => {
for (const each in rawFile) {
const file = rawFile[each];
if (file.filePath.includes('/dashboards/')) {
parseDashboardDir(file);
} else if (file.filePath.includes('/alerts/')) {
parseAlertsDir(file);
} else {
parseQuickstartDir(file);
}
}
};

/**
* Async function handles parsing fetched files from pull request
* Transforms them into shape needed for QuickstartDetails.js
**/

export const parseQuickstartFilesFromPR = async (rawFileContent) => {
checkFileType(rawFileContent);
dashboardDirs = Object.values(dashboardDirs);

return parsedQuickstart;
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4145,6 +4145,11 @@ argparse@^1.0.7:
dependencies:
sprintf-js "~1.0.2"

argparse@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==

aria-query@^4.2.2:
version "4.2.2"
resolved "https://registry.yarnpkg.com/aria-query/-/aria-query-4.2.2.tgz#0d2ca6c9aceb56b8977e9fed6aed7e15bbd2f83b"
Expand Down Expand Up @@ -9583,6 +9588,13 @@ js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"

jsesc@^2.5.1:
version "2.5.2"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
Expand Down

0 comments on commit 1d2c810

Please sign in to comment.