UNPKG

@techmynder/gitlab-ci-file-lint

Version:

Validate a GitLab pipeline YAML file using the official API.

3 lines (2 loc) 1.8 kB
import*as r from"fs";import n from"node-fetch";import e from"chalk";function t(){return t=Object.assign||function(r){for(var n=1;n<arguments.length;n++){var e=arguments[n];for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(r[t]=e[t])}return r},t.apply(this,arguments)}var o=function(e,o,i,s){try{return Promise.resolve(n(s+"/api/v4/projects/"+o+"/ci/lint",{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json","Private-Token":e},body:JSON.stringify({content:r.readFileSync(i).toString()})})).then(function(r){if(200!==r.status)throw new Error('Unexpected response status code "'+r.status+'" given');return Promise.resolve(r.json()).then(function(r){return t({file:i},r)})})}catch(r){return Promise.reject(r)}},i=function(){try{var n=process.env.GITLAB_LINT_TOKEN||"",e=parseInt(process.env.GITLAB_LINT_PROJECT_ID||"",10),t=process.env.GITLAB_LINT_FILE||".gitlab-ci.yml",i=process.env.GITLAB_LINT_BASE_URL||"https://gitlab.com";if(!n||isNaN(e))throw new Error("Configuration missing. Define at least the `GITLAB_LINT_TOKEN` and `GITLAB_LINT_PROJECT_ID` environment variables.");if(!r.existsSync(t))throw new Error('File "'+t+'" does not exist');return o(n,e,t,i)}catch(r){return Promise.reject(r)}},s=function(r){return r.map(function(r,n){return e.bold("#"+(n+1)+".")+r}).join("\n")};function a(r){var n=e.bold("Validated "+r.file+": ")+function(r){return"valid"===r.status&&r.warnings.length?e.bold.hex("#ffa500")("OK (with warnings)"):"invalid"===r.status||r.errors.length?e.bold.red("FAIL"):e.bold.green("OK")}(r);return r.errors.length&&(n+=e.red("\n\nErrors:\n")+s(r.errors)),r.warnings.length&&(n+=e.hex("#ffa500")("\n\nWarnings:\n")+s(r.warnings)),n}export{a as formatResult,o as lintFile,i as run}; //# sourceMappingURL=gitlab-ci-file-lint.module.js.map