This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from newrelic/tabatha/nr4041-parse-quickstart…
…-files NR-4041: Parse quickstart PR files
- Loading branch information
Showing
9 changed files
with
1,995 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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 [quickstart, setQuickstart] = 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 () => { | ||
try { | ||
const rawFileContent = await getQuickstartFilesFromPR( | ||
prNumber, | ||
quickstartPath | ||
); | ||
|
||
const parsedQuickstart = parseQuickstartFilesFromPR(rawFileContent); | ||
|
||
setQuickstart(parsedQuickstart); | ||
} catch (error) { | ||
console.log('Error:', error.message); | ||
navigate('/'); | ||
return; | ||
} | ||
}; | ||
|
||
fetchFiles(); | ||
}, []); | ||
return quickstart; | ||
}; | ||
|
||
export default usePullRequestQuickstart; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const parseHelpers = require('../preview/parseHelpers'); | ||
const { | ||
expectedConfigOutput, | ||
missingConfigOutput, | ||
configContentMissingFields, | ||
configContent, | ||
installPlansInput, | ||
baseFiles, | ||
dashboardFiles, | ||
dashboardContent, | ||
expectedDashboardOutput, | ||
installPlansOutput, | ||
documentationInput, | ||
documentationOutput, | ||
} = require('../mock_data/content'); | ||
|
||
console.log(baseFiles); | ||
|
||
describe('parseHelpers', () => { | ||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('parseDocs for a documentation with multiuple newlines at the end', () => { | ||
const output = parseHelpers.parseDocs(documentationInput); | ||
expect(output).toEqual(documentationOutput); | ||
}); | ||
|
||
test('parseDocs for empty documentation list', () => { | ||
expect(() => { | ||
parseHelpers.parseDocs([]); | ||
}).not.toThrowError(); | ||
}); | ||
|
||
test('parseDocs for a documentation without description field', () => { | ||
const input = [ | ||
{ | ||
name: 'Kamon installation docs', | ||
url: | ||
'https://docs.newrelic.com/docs/integrations/open-source-telemetry-integrations/open-source-telemetry-integration-list/kamon-reporter', | ||
}, | ||
]; | ||
|
||
const output = parseHelpers.parseDocs(input); | ||
expect(output).toEqual(input); | ||
}); | ||
|
||
test('parseInstallPlans for a single install plan', () => { | ||
const output = parseHelpers.parseInstallPlans(installPlansInput); | ||
expect(output).toEqual(installPlansOutput); | ||
}); | ||
|
||
test('parseInstallPlans for multiple install plans', () => { | ||
const multipleInputs = ['install-plan', 'guided-install', 'battlesnake']; | ||
const output = parseHelpers.parseInstallPlans(multipleInputs); | ||
|
||
expect(output).toEqual([ | ||
{ name: '', id: 'install-plan' }, | ||
{ name: '', id: 'guided-install' }, | ||
{ name: '', id: 'battlesnake' }, | ||
]); | ||
}); | ||
|
||
test('parseInstallPlans for no install plan', () => { | ||
const input = []; | ||
const output = parseHelpers.parseInstallPlans(input); | ||
|
||
expect(output).toEqual([]); | ||
}); | ||
|
||
test('parseInstallPlans for no install plan not to throw', () => { | ||
const input = []; | ||
const output = parseHelpers.parseInstallPlans(input); | ||
|
||
expect(() => { | ||
parseHelpers.parseInstallPlans(input); | ||
}).not.toThrowError(); | ||
}); | ||
|
||
test('parseQuickstartFiles for valid quickstart', () => { | ||
const input = baseFiles(configContent); | ||
const output = parseHelpers.parseQuickstartFiles(input); | ||
|
||
expect(output).toEqual(expectedConfigOutput); | ||
}); | ||
|
||
test('parseQuickstartFiles for invalid quickstart', () => { | ||
const input = baseFiles(configContentMissingFields); | ||
const output = parseHelpers.parseQuickstartFiles(input); | ||
|
||
expect({ ...output, ...missingConfigOutput }).toEqual(expectedConfigOutput); | ||
}); | ||
|
||
test('parseQuickstartFiles for invalid quickstart not to throw', () => { | ||
const input = baseFiles(configContentMissingFields); | ||
expect(() => { | ||
parseHelpers.parseQuickstartFiles(input); | ||
}).not.toThrow(); | ||
}); | ||
|
||
test('parseDashboardFiles for valid quickstart', () => { | ||
const input = dashboardFiles(dashboardContent); | ||
const output = parseHelpers.parseDashboardFiles(input); | ||
|
||
expect(output).toEqual(expectedDashboardOutput); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
const installPlansContent = ` | ||
installPlans: | ||
- guided-install | ||
`; | ||
|
||
const configContent = ` | ||
id: generic-quickstart-id-1 | ||
slug: quickstart-slug | ||
description: example description | ||
summary: example summary | ||
icon: logo.png | ||
level: New Relic | ||
authors: | ||
- New Relic | ||
title: Generic Quickstart | ||
keywords: | ||
- os | ||
- operating system | ||
${installPlansContent} | ||
`; | ||
|
||
const configContentMissingFields = ` | ||
id: generic-quickstart-id-1 | ||
slug: quickstart-slug | ||
description: example description | ||
icon: logo.png | ||
level: New Relic | ||
authors: | ||
- New Relic | ||
keywords: | ||
- os | ||
- operating system | ||
${installPlansContent} | ||
`; | ||
|
||
const dashboardContent = { | ||
name: 'mock dashboard name', | ||
description: 'mock dashboard description', | ||
}; | ||
|
||
const missingConfigOutput = { | ||
title: 'Generic Quickstart', | ||
summary: 'example summary', | ||
}; | ||
|
||
const installPlansInput = ['guided-install']; | ||
|
||
const documentationInput = [ | ||
{ | ||
name: 'Kamon installation docs', | ||
description: | ||
'Kamon is used to automatically instrument, monitor and debug distributed\nsystems.\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n', | ||
url: | ||
'https://docs.newrelic.com/docs/integrations/open-source-telemetry-integrations/open-source-telemetry-integration-list/kamon-reporter', | ||
}, | ||
]; | ||
|
||
const documentationOutput = [ | ||
{ | ||
name: 'Kamon installation docs', | ||
description: | ||
'Kamon is used to automatically instrument, monitor and debug distributed\nsystems.', | ||
url: | ||
'https://docs.newrelic.com/docs/integrations/open-source-telemetry-integrations/open-source-telemetry-integration-list/kamon-reporter', | ||
}, | ||
]; | ||
|
||
const installPlansOutput = [ | ||
{ | ||
name: '', | ||
id: 'guided-install', | ||
}, | ||
]; | ||
|
||
const expectedConfigOutput = { | ||
title: 'Generic Quickstart', | ||
name: 'quickstart-slug', | ||
description: 'example description', | ||
packUrl: | ||
'https://github.com/newrelic/newrelic-quickstarts/tree/main/quickstarts/mock_quickstart_1', | ||
|
||
id: 'generic-quickstart-id-1', | ||
level: 'New Relic', | ||
logoUrl: 'fake/path/to/logo.png', | ||
summary: 'example summary', | ||
keywords: ['os', 'operating system'], | ||
authors: ['New Relic'], | ||
relatedResources: [], | ||
documentation: [], | ||
installPlans: installPlansOutput, | ||
}; | ||
|
||
const baseFiles = (content) => [ | ||
{ | ||
type: 'yaml', | ||
filePath: 'mock_quickstart_1/config.yml', | ||
fileName: 'config.yml', | ||
content: content, | ||
}, | ||
{ | ||
type: 'image', | ||
filepath: 'mock_quickstarts_1/logo.png', | ||
fileName: 'logo.png', | ||
content: 'fake/path/to/logo.png', | ||
}, | ||
]; | ||
|
||
const dashboardFiles = (content) => [ | ||
{ | ||
type: 'json', | ||
filePath: | ||
'mock_quickstart_1/dashboards/custom_dashboard/mock_dashboard.json', | ||
fileName: 'mock_dashboard.json', | ||
content: JSON.stringify(content), | ||
}, | ||
{ | ||
type: 'image', | ||
filePath: | ||
'mock_quickstart_1/dashboards/custom_dashboard/mock_dashboard01.png', | ||
fileName: 'mock_dashboard01.png', | ||
content: 'mock/url/for/mock_dashboard01.png', | ||
}, | ||
{ | ||
type: 'image', | ||
filePath: | ||
'mock_quickstart_1/dashboards/custom_dashboard/mock_dashboard02.png', | ||
fileName: 'mock_dashboard02.png', | ||
content: 'mock/url/for/mock_dashboard02.png', | ||
}, | ||
]; | ||
|
||
const expectedDashboardOutput = [ | ||
{ | ||
name: 'mock dashboard name', | ||
description: 'mock dashboard description', | ||
screenshots: [ | ||
'mock/url/for/mock_dashboard01.png', | ||
'mock/url/for/mock_dashboard02.png', | ||
], | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
installPlansContent, | ||
configContent, | ||
configContentMissingFields, | ||
dashboardContent, | ||
expectedConfigOutput, | ||
missingConfigOutput, | ||
baseFiles, | ||
dashboardFiles, | ||
expectedDashboardOutput, | ||
installPlansInput, | ||
installPlansOutput, | ||
documentationInput, | ||
documentationOutput, | ||
}; |
Oops, something went wrong.