From 4f966269215eb118869744d095a29c581fad1790 Mon Sep 17 00:00:00 2001 From: Krishna Date: Thu, 10 Oct 2019 10:38:52 -0400 Subject: [PATCH 1/2] throw error if no pipeline --- plugin.js | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/plugin.js b/plugin.js index 84bbe67..d5fe657 100644 --- a/plugin.js +++ b/plugin.js @@ -45,30 +45,41 @@ app.post('/', bodyParser.json({limit: '50mb'}), async (req, res) => { return res.sendStatus(500) } - const finalYamlDocs = parsedYaml.map((py, index) => { - if (py.kind !== 'pipeline') return yaml.stringify(py) - if (py.trigger && py.trigger.changeset && py.trigger.changeset.includes) { - const requiredFiles = py.trigger.changeset.includes - const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true }) - console.log('Matched files for pipeline:', matchedFiles.length, 'Allowed matches:', requiredFiles) - if (!matchedFiles.length) { - py.trigger = { event: { exclude: ['*'] } } - return yaml.stringify(py) + try { + const finalYamlDocs = parsedYaml.map((py, index) => { + if (py.kind !== 'pipeline') { + let error = new Error("noPipelineFound"); + error.code = 404; + throw error; + } + if (py.trigger && py.trigger.changeset && py.trigger.changeset.includes) { + const requiredFiles = py.trigger.changeset.includes + const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true }) + console.log('Matched files for pipeline:', matchedFiles.length, 'Allowed matches:', requiredFiles) + if (!matchedFiles.length) { + py.trigger = { event: { exclude: ['*'] } } + return yaml.stringify(py) + } } - } - const trimmedSteps = py.steps.filter(s => { - if (!s.when || !s.when.changeset || !s.when.changeset.includes) return true - const requiredFiles = s.when.changeset.includes - const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true }) - console.log('Matched files for step:', matchedFiles.length, 'Allowed matches:', requiredFiles) - return matchedFiles.length + const trimmedSteps = py.steps.filter(s => { + if (!s.when || !s.when.changeset || !s.when.changeset.includes) return true + const requiredFiles = s.when.changeset.includes + const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true }) + console.log('Matched files for step:', matchedFiles.length, 'Allowed matches:', requiredFiles) + return matchedFiles.length + }) + + return trimmedSteps.length ? yaml.stringify({ ...py, steps: trimmedSteps }) : nullYaml(index) }) - return trimmedSteps.length ? yaml.stringify({ ...py, steps: trimmedSteps }) : nullYaml(index) - }) + res.json({ Data: finalYamlDocs.join('\n---\n') }) + } catch(e) { + console.log(e) + if (e.code === 404) return res.sendStatus(204) + return res.sendStatus(500) + } - res.json({ Data: finalYamlDocs.join('\n---\n') }) }) app.listen(3000) From f47fd5359d5a93e2e90dedc033e4a25b4870191c Mon Sep 17 00:00:00 2001 From: Jim Sheldon Date: Mon, 14 Oct 2019 09:31:44 -0400 Subject: [PATCH 2/2] check if 'kind' is undefined if 'kind' is undefined, the config could be 0.8 syntax, so return a 204 and let the drone server retrieve it if 'kind' is anything other than 'pipeline', return it --- plugin.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugin.js b/plugin.js index d5fe657..f3ee273 100644 --- a/plugin.js +++ b/plugin.js @@ -47,11 +47,12 @@ app.post('/', bodyParser.json({limit: '50mb'}), async (req, res) => { try { const finalYamlDocs = parsedYaml.map((py, index) => { - if (py.kind !== 'pipeline') { - let error = new Error("noPipelineFound"); + if (py.kind == undefined) { + let error = new Error("Missing kind"); error.code = 404; throw error; } + if (py.kind !== 'pipeline') return yaml.stringify(py) if (py.trigger && py.trigger.changeset && py.trigger.changeset.includes) { const requiredFiles = py.trigger.changeset.includes const matchedFiles = glob.match(requiredFiles, filesChanged, { dot: true })