|
| 1 | +const YAML = require('yaml') |
| 2 | +const Ajv = require('ajv'); |
| 3 | +const fetch = require('node-fetch'); |
| 4 | +const fs = require('fs'); |
| 5 | +const chalk = require('chalk'); |
| 6 | + |
| 7 | +const ghToken = process.env.GITHUB_TOKEN; |
| 8 | +const ghHeaders = {'User-Agent': 'sdk-bot', 'Authorization': 'token ' + ghToken,'Accept': 'application/vnd.github.v3.raw'}; |
| 9 | + |
| 10 | +const sdkReposJSONBranch = "develop"; |
| 11 | +let sdkReposJSONPath = "http://api.github.com/repos/pubnub/documentation-resources/contents/website-common/tools/build/sdk-repos.json?ref=" + sdkReposJSONBranch; |
| 12 | +startExecution(sdkReposJSONPath); |
| 13 | + |
| 14 | +async function startExecution(sdkReposJSONPath){ |
| 15 | + var sdkRepos = await requestGetFromGithub(sdkReposJSONPath); |
| 16 | + var sdkReposAndFeatureMappingArray = parseReposAndFeatureMapping(sdkRepos); |
| 17 | + var schemaText = await requestGetFromGithub(sdkReposAndFeatureMappingArray[2]); |
| 18 | + |
| 19 | + schema = JSON.parse(schemaText); |
| 20 | + var yaml = fs.readFileSync(".pubnub.yml", 'utf8'); |
| 21 | + |
| 22 | + if(yaml != null){ |
| 23 | + yml = YAML.parse(yaml); |
| 24 | + var ajv = new Ajv({schemaId: 'id', "verbose":true, "allErrors": true}); |
| 25 | + const validate = ajv.compile(schema); |
| 26 | + const valid = validate(yml); |
| 27 | + if (validate.errors!= null) { |
| 28 | + console.log(chalk.cyan("===================================")); |
| 29 | + console.log(chalk.red(yml["version"] + " validation errors...")); |
| 30 | + console.log(chalk.cyan("===================================")); |
| 31 | + console.log(validate.errors); |
| 32 | + console.log(chalk.cyan("===================================")); |
| 33 | + var result = {code:1, repo: yml["version"], msg: "validation errors"}; |
| 34 | + printResult(result); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + else { |
| 38 | + var result = {code: 0, repo: yml["version"], msg: "validation pass"}; |
| 39 | + printResult(result); |
| 40 | + } |
| 41 | + } else { |
| 42 | + var result = {code:1, repo: "yml null", msg: "validation errors"}; |
| 43 | + printResult(result); |
| 44 | + process.exit(1); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function printResult(result){ |
| 49 | + var str = result.repo + ", " + result.msg; |
| 50 | + if(result.code === 0){ |
| 51 | + console.log(chalk.green(str) + ", Code: " + result.code); |
| 52 | + } else { |
| 53 | + console.log(chalk.red(str) + ", Code: " + result.code); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +async function requestGetFromGithub(url){ |
| 58 | + try { |
| 59 | + const response = await fetch(url, { |
| 60 | + headers: ghHeaders, |
| 61 | + method: 'get', |
| 62 | + }); |
| 63 | + if(response.status == 200){ |
| 64 | + const json = await response.text(); |
| 65 | + return json; |
| 66 | + } else { |
| 67 | + console.error(chalk.red("res.status: " + response.status + "\n URL: " + url)); |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + } catch (error) { |
| 72 | + console.error(chalk.red("requestGetFromGithub: " + error + "\n URL: " + url)); |
| 73 | + return null; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function parseReposAndFeatureMapping(body){ |
| 78 | + if(body != null){ |
| 79 | + var sdkRepos = JSON.parse(body); |
| 80 | + var locations = sdkRepos["locations"]; |
| 81 | + if(locations!=null){ |
| 82 | + var sdkURLs = locations["sdks"]; |
| 83 | + var featureMappingURL = locations["featureMapping"]; |
| 84 | + var pubnubYAMLSchemaURL = locations["pubnubYAMLSchema"]; |
| 85 | + return [sdkURLs, featureMappingURL, pubnubYAMLSchemaURL]; |
| 86 | + } else { |
| 87 | + console.log(chalk.red("response locations null")); |
| 88 | + return null; |
| 89 | + } |
| 90 | + } else { |
| 91 | + console.log(chalk.red("response body null")); |
| 92 | + return null; |
| 93 | + } |
| 94 | +} |
0 commit comments