json-parse-safe package used instead of JSON.parse

dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 7 years ago
parent 48de21871c
commit fa07bbd635
  1. 17
      BuildServer/lib/builder.ts
  2. 9
      BuildServer/lib/report-processor.ts
  3. 17
      BuildServer/lib/tasks/dotnetbuilderwrapper.ts
  4. 1
      BuildServer/package.json
  5. 11
      BuildServer/routes/postreceive.ts

@ -4,6 +4,7 @@ import { join } from "path";
import { exists, readFile, writeFileSync } from "fs"; import { exists, readFile, writeFileSync } from "fs";
import { mkdirsSync, remove } from "fs-extra"; import { mkdirsSync, remove } from "fs-extra";
import { parallel, queue } from "async"; import { parallel, queue } from "async";
import * as JSONParse from "json-parse-safe";
import { gitLoader } from "./git/loader"; import { gitLoader } from "./git/loader";
import { processTask } from "./task-processor"; import { processTask } from "./task-processor";
import { writeReport } from "./report-processor"; import { writeReport } from "./report-processor";
@ -62,14 +63,6 @@ const wrapGitLoader = (skipGitLoader) => {
return (gitLoaderOptions, gitLoaderCallback) => process.nextTick(gitLoaderCallback); return (gitLoaderOptions, gitLoaderCallback) => process.nextTick(gitLoaderCallback);
}; };
const safeParseJson = (data):any => {
try {
return { "parsed": JSON.parse(data) };
} catch (err) {
return { err };
}
};
export const build = (options, buildCallback) => { export const build = (options, buildCallback) => {
const url = options.url; const url = options.url;
const owner = options.owner; const owner = options.owner;
@ -187,15 +180,15 @@ export const build = (options, buildCallback) => {
return done(readErr, "MBSUnableToRead"); return done(readErr, "MBSUnableToRead");
} }
const { parsed, err } = safeParseJson(data); const { value, error } = JSONParse(data);
if (err) { if (error) {
console.log(`Malformed data: ${data}`); console.log(`Malformed data: ${data}`);
return done(err, "MBSMalformed"); return done(error, "MBSMalformed");
} }
return processTask(parsed, { return processTask(value, {
branch, branch,
exported, exported,
owner, owner,

@ -6,6 +6,7 @@ import { createGzip, createGunzip } from "zlib";
import * as glob from "glob"; import * as glob from "glob";
import { ReadableStreamBuffer, WritableStreamBuffer } from "stream-buffers"; import { ReadableStreamBuffer, WritableStreamBuffer } from "stream-buffers";
import * as _ from "underscore"; import * as _ from "underscore";
import * as JSONParse from "json-parse-safe";
const reportFilename = "report.json.gz"; const reportFilename = "report.json.gz";
const maxAttemptsNumber = 100; const maxAttemptsNumber = 100;
@ -62,12 +63,16 @@ export const readReport = (releaseDir, callback) => {
readStream.destroy(); readStream.destroy();
const data = writable.getContentsAsString(); const data = writable.getContentsAsString();
if (!data) { if (!data) {
return callback("ReportFileNotFound"); return callback("ReportFileNotFound");
} }
return callback(null, JSON.parse(data)); const { error, value } = JSONParse(data);
if (error) {
return callback("ReportFileMalformed");
}
return callback(null, value);
}); });
}; };

@ -2,6 +2,7 @@
import { spawn } from "child_process"; import { spawn } from "child_process";
import { WritableStreamBuffer } from "stream-buffers"; import { WritableStreamBuffer } from "stream-buffers";
import * as JSONParse from "json-parse-safe";
import settings from "../../settings"; import settings from "../../settings";
const wrapBuilder = (builder, input, onExit) => { const wrapBuilder = (builder, input, onExit) => {
@ -26,14 +27,6 @@ const wrapBuilder = (builder, input, onExit) => {
builder.stdin.end(); builder.stdin.end();
}; };
const safeParseJson = (data):any => {
try {
return { "parsed": JSON.parse(data) };
} catch (err) {
return { err };
}
};
export default (params, processor) => ({ export default (params, processor) => ({
"process": () => { "process": () => {
const input = JSON.stringify(params); const input = JSON.stringify(params);
@ -48,16 +41,16 @@ export default (params, processor) => ({
return processor.done(); return processor.done();
} }
const { parsed, err } = safeParseJson(result); const { value, error } = JSONParse(result);
if (err || !parsed || !parsed.Messages) { if (error || !value || !value.Messages) {
processor.onError(`Malformed JSON: ${err}`); processor.onError(`Malformed JSON: ${error}`);
processor.onInfo(result); processor.onInfo(result);
return processor.done(); return processor.done();
} }
const messages = parsed.Messages; const messages = value.Messages;
messages.forEach((message) => { messages.forEach((message) => {
if (!message) { if (!message) {

@ -19,6 +19,7 @@
"glob": "~7.1.1", "glob": "~7.1.1",
"graceful-fs": "^4.1.11", "graceful-fs": "^4.1.11",
"jade": "*", "jade": "*",
"json-parse-safe": "^1.0.3",
"method-override": "^2.3.7", "method-override": "^2.3.7",
"morgan": "^1.7.0", "morgan": "^1.7.0",
"mustache": "~2.3.0", "mustache": "~2.3.0",

@ -1,5 +1,6 @@
"use strict"; "use strict";
import * as JSONParse from "json-parse-safe";
import { build } from "../lib/builder"; import { build } from "../lib/builder";
import { commentOnPullRequest } from "../lib/commenter"; import { commentOnPullRequest } from "../lib/commenter";
@ -90,13 +91,21 @@ const processPullRequest = (req, res, payload) => {
); );
}; };
const getPayload = (body) => {
if (!body.payload) {
return body;
}
return JSONParse(body.payload).value;
}
export default (req, res) => { export default (req, res) => {
if (!req.body || (!req.body.payload && !req.body.repository)) { if (!req.body || (!req.body.payload && !req.body.repository)) {
return res.end(); return res.end();
} }
const eventType = req.header("x-github-event"); const eventType = req.header("x-github-event");
const payload = (req.body.payload && JSON.parse(req.body.payload)) || req.body; const payload = getPayload(req.body);
if (eventType === "push") { if (eventType === "push") {
return processPush(req, res, payload); return processPush(req, res, payload);

Loading…
Cancel
Save