-
Notifications
You must be signed in to change notification settings - Fork 39
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(merge-reports): ability to pass headers for databaseUrls.json files #527
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -5,10 +5,24 @@ const _ = require('lodash'); | |
const serverUtils = require('../server-utils'); | ||
const dbServerUtils = require('../db-utils/server'); | ||
|
||
module.exports = async (pluginConfig, hermione, srcPaths, {destination: destPath}) => { | ||
validateOpts(srcPaths, destPath); | ||
module.exports = async (pluginConfig, hermione, srcPaths, {destPath, headers}) => { | ||
validateOpts({srcPaths, destPath, headers}); | ||
|
||
const resolvedUrls = await tryResolveUrls(srcPaths); | ||
let headersFromEnv; | ||
|
||
try { | ||
headersFromEnv = JSON.parse(process.env.html_reporter_headers || '{}'); | ||
} catch (e) { | ||
throw new Error(`Couldn't parse headers from "html_reporter_headers" env variable: ${e.message}`); | ||
} | ||
|
||
const headersFromCli = headers.reduce((acc, header) => { | ||
const [key, ...values] = header.split('='); | ||
return _.set(acc, key, values.join('=')); | ||
}, {}); | ||
const parsedHeaders = {...headersFromCli, ...headersFromEnv}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Хедеры из переменной окружения имеют больший приоритет. |
||
|
||
const resolvedUrls = await tryResolveUrls(srcPaths, parsedHeaders); | ||
|
||
await Promise.all([ | ||
serverUtils.saveStaticFilesToReportDir(hermione.htmlReporter, pluginConfig, destPath), | ||
|
@@ -18,19 +32,25 @@ module.exports = async (pluginConfig, hermione, srcPaths, {destination: destPath | |
await hermione.htmlReporter.emitAsync(hermione.htmlReporter.events.REPORT_SAVED, {reportPath: destPath}); | ||
}; | ||
|
||
function validateOpts(srcPaths, destPath) { | ||
function validateOpts({srcPaths, destPath, headers}) { | ||
if (!srcPaths.length) { | ||
throw new Error('Nothing to merge, no source reports are passed'); | ||
} | ||
|
||
if (srcPaths.includes(destPath)) { | ||
throw new Error(`Destination report path: ${destPath}, exists in source report paths`); | ||
} | ||
|
||
for (const header of headers) { | ||
if (!header.includes('=')) { | ||
throw new Error(`Header must has key and value separated by "=" symbol, but got "${header}"`); | ||
} | ||
} | ||
} | ||
|
||
async function tryResolveUrls(urls) { | ||
async function tryResolveUrls(urls, headers) { | ||
const resolvedUrls = []; | ||
const results = await Promise.all(urls.map(tryResolveUrl)); | ||
const results = await Promise.all(urls.map(url => tryResolveUrl(url, headers))); | ||
|
||
results.forEach(({jsonUrls, dbUrls}) => { | ||
resolvedUrls.push(...jsonUrls.concat(dbUrls)); | ||
|
@@ -39,22 +59,22 @@ async function tryResolveUrls(urls) { | |
return resolvedUrls; | ||
} | ||
|
||
async function tryResolveUrl(url) { | ||
async function tryResolveUrl(url, headers) { | ||
const jsonUrls = []; | ||
const dbUrls = []; | ||
|
||
if (serverUtils.isDbUrl(url)) { | ||
dbUrls.push(url); | ||
} else if (serverUtils.isJsonUrl(url)) { | ||
try { | ||
const {data} = await axios.get(url); | ||
const {data} = await axios.get(url, {headers}); | ||
const currentDbUrls = _.get(data, 'dbUrls', []); | ||
const currentJsonUrls = _.get(data, 'jsonUrls', []); | ||
|
||
const preparedDbUrls = dbServerUtils.prepareUrls(currentDbUrls, url); | ||
const preparedJsonUrls = dbServerUtils.prepareUrls(currentJsonUrls, url); | ||
|
||
const responses = await Promise.all(preparedJsonUrls.map(tryResolveUrl)); | ||
const responses = await Promise.all(preparedJsonUrls.map(url => tryResolveUrl(url, headers))); | ||
|
||
dbUrls.push(...preparedDbUrls); | ||
|
||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Нужно докидывать понятное сообщение о ошибке иначе будет отображаться стандартная ошибка из
json.parse
(нефига не понятная)