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