From 4571a60b33732f519d45f2856fd94cdbe34548ab Mon Sep 17 00:00:00 2001 From: Adam Rensel Date: Sat, 8 May 2021 16:55:00 -0400 Subject: [PATCH] Add basic file path and content validations --- .github/workflows/src/app/file-validator.js | 75 ++++++++++++++++++++- .github/workflows/src/index.js | 31 ++++++--- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/.github/workflows/src/app/file-validator.js b/.github/workflows/src/app/file-validator.js index 451f6018..edf1298a 100644 --- a/.github/workflows/src/app/file-validator.js +++ b/.github/workflows/src/app/file-validator.js @@ -1,10 +1,83 @@ +const actionEvent = require('./action-event.js'); +const path = require('path') +const yaml = require('yaml') +const md = require('markdown-it') + +const pullAuthor = actionEvent.pull.user.login +const expectedPath = `_data/${pullAuthor}` + class FileVaidator { constructor() { } - isValidPaths(files=[]) { + async isMarkdownValid(markdown) { + // hack to import a module TOO Enable modules and swap all the requires + const metadataBlock = (await import("markdown-it-metadata-block")).default; + const meta = {} + const errors = [] + let metadata + + const mdParser = md({ + html: true, + linkify: true, + typographer: true + }).use(metadataBlock,{ + parseMetadata: yaml.parse, + meta: meta + }); + + try { + metadata = mdParser.render(markdown) + } catch(err) { + errors.push(`The markdown content in \`${expectedPath}/${pullAuthor}.md\` has syntax errors`) + } + + if(metadata) { + if(metadata.github_user !== pullAuthor) { + errors.push(`The yaml content in \`${expectedPath}/${pullAuthor}.md\` must contain your github username`) + } + + for(const key of [ "name", "institution", "quote" ]) { + if(!metadata[key]) { + errors.push(`The attribute \`${key}\` is required in \`${expectedPath}/${pullAuthor}.md\``) + } + } + } else { + errors.push(`\`${expectedPath}/${pullAuthor}.md\` does not contain any yaml metadata`) + } + + return { isValid: !errors.length, errors } + } + + isValidPaths(filePaths=[]) { + const errors = [] + let invalidDirectory = false + let InvalidMarkdownFile = true + let isValid = true + let pathData + + for(let filePath of filePaths) { + pathData = path.parse(filePath) + + if(pathData.dir !== expectedPath) { + invalidDirectory = true + } + + if(pathData.base === `${pullAuthor}.md`) { + InvalidMarkdownFile = false + } + } + + if(InvalidMarkdownFile) { + errors.push(`The required markdown file does not exist, please ensure the file \`${expectedPath}/${pullAuthor}.md\` exists`) + } + + if(invalidDirectory) { + errors.push(`Invalid file path. Please ensure all changes are contained within the \`${expectedPath}/\` directory`) + } + return { isValid: !errors.length, errors } } } diff --git a/.github/workflows/src/index.js b/.github/workflows/src/index.js index 13291b7d..f1babc66 100644 --- a/.github/workflows/src/index.js +++ b/.github/workflows/src/index.js @@ -35,9 +35,9 @@ const fileValidator = require('./app/file-validator.js'); ;(async ()=>{ const results = await Promise.all([ octokit.fetchPr(actionEvent.pullNumber), - airtable.userParticipated2020(actionEvent.pullRepoAuthor), - educationWeb.hasPack(actionEvent.pull.user.login), - airtable.fetch2021Graduate(actionEvent.pullRepoAuthor) + airtable.userParticipated2020(actionEvent.pullAuthor), + educationWeb.hasPack(actionEvent.pullAuthor), + airtable.fetch2021Graduate(actionEvent.pullAuthor) ]) const pull = results[0] @@ -58,14 +58,23 @@ const fileValidator = require('./app/file-validator.js'); return file.node.path }) - console.log(fileNames) + let isMarkdownValid + const isFilePathValid = fileValidator.isValidPaths(fileNames) + const content = isFilePathValid && await octokit.getContent(`_data/${actionEvent.pullAuthor}/${actionEvent.pullAuthor}.md`) - const pathIsCorrect = fileValidator.isValidPaths(fileNames, actionEvent.pull.user.login) + if(content) { + isMarkdownValid = fileValidator.isMarkdownValid(content) + } - // pull.body + console.log(content) - const content = await octokit.getContent("_data/juanpflores/juanpflores.md") - // const markdownValid = TODO + // I have read the instructions on the README file before submitting my application. + // I made my submission by creating a folder on the _data folder and followed the naming convention mentioned in the instructions () and markdown file. + // I have submitted a swag shipping form. + // I have used the Markdown file template to add my information to the Year Book. + // I understand that a reviewer will merge my pull request after examining it or ask for changes in case needed. + // I understand I should not tag or add a reviewer to this Pull Request. + // I have added the event to my Calendar. // #################### TODO CACHE AIR TABLE SOMEHOW ######################## // * cache the entire base in a json file with actions @@ -80,9 +89,9 @@ const fileValidator = require('./app/file-validator.js'); console.log("User has not applied for SDP") } else if(!completedShippingForm) { console.log("user has not completed the shipping form") - } else if(!pathIsCorrect) { - console.log("user file path is incorrect") - } else if(!markdownValid) { + } else if(!isFilePathValid.isValid) { + console.log('Files have errors: \n' + isFilePathValid.errors.join('\n')) + } else if(!isMarkdownValid.isValid) { console.log("markdown is invalid") } else if(!userAgreesCoc) { console.log("User has not agreed to COC") -- knotmirror 0.0.1