@techmynder/gitlab-ci-file-lint
Version:
Validate a GitLab pipeline YAML file using the official API.
1 lines • 4.21 kB
Source Map (JSON)
{"version":3,"file":"gitlab-ci-file-lint.modern.mjs","sources":["../src/index.ts"],"sourcesContent":["import * as fs from \"fs\"\nimport fetch from \"node-fetch\"\nimport chalk from \"chalk\"\nimport { GitLabLintResult, LintResult } from \"./types\"\n\nexport async function run(): Promise<LintResult> {\n const token = process.env.GITLAB_LINT_TOKEN || \"\"\n const projectId = parseInt(process.env.GITLAB_LINT_PROJECT_ID || \"\", 10)\n const file = process.env.GITLAB_LINT_FILE || \".gitlab-ci.yml\"\n const baseUrl = process.env.GITLAB_LINT_BASE_URL || \"https://gitlab.com\"\n\n if (!token || isNaN(projectId)) {\n throw new Error(\n \"Configuration missing. Define at least the `GITLAB_LINT_TOKEN` and `GITLAB_LINT_PROJECT_ID` environment variables.\"\n )\n }\n\n if (!fs.existsSync(file)) {\n throw new Error(`File \"${file}\" does not exist`)\n }\n\n return lintFile(token, projectId, file, baseUrl)\n}\n\n/**\n * @see https://docs.gitlab.com/ee/api/lint.html\n */\nexport async function lintFile(\n token: string,\n projectId: number,\n file: string,\n baseUrl: string\n): Promise<LintResult> {\n const response = await fetch(\n `${baseUrl}/api/v4/projects/${projectId}/ci/lint`,\n {\n method: \"POST\",\n headers: {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n \"Private-Token\": token,\n },\n body: JSON.stringify({ content: fs.readFileSync(file).toString() }),\n }\n )\n\n if (response.status !== 200) {\n throw new Error(\n `Unexpected response status code \"${response.status}\" given`\n )\n }\n\n return {\n file,\n ...((await response.json()) as GitLabLintResult),\n }\n}\n\nconst orange = \"#ffa500\"\n\nconst formatLintIssues = (lines: string[]): string =>\n lines.map((line, i) => chalk.bold(`#${i + 1}.`) + line).join(\"\\n\")\n\nconst formatShortResult = (result: LintResult): string => {\n if (result.status === \"valid\" && result.warnings.length) {\n return chalk.bold.hex(orange)(\"OK (with warnings)\")\n }\n\n if (result.status === \"invalid\" || result.errors.length) {\n return chalk.bold.red(\"FAIL\")\n }\n\n return chalk.bold.green(\"OK\")\n}\n\nexport function formatResult(result: LintResult): string {\n let output =\n chalk.bold(`Validated ${result.file}: `) + formatShortResult(result)\n\n if (result.errors.length) {\n output += chalk.red(\"\\n\\nErrors:\\n\") + formatLintIssues(result.errors)\n }\n\n if (result.warnings.length) {\n output +=\n chalk.hex(orange)(\"\\n\\nWarnings:\\n\") + formatLintIssues(result.warnings)\n }\n\n return output\n}\n"],"names":["run","token","process","env","GITLAB_LINT_TOKEN","projectId","parseInt","GITLAB_LINT_PROJECT_ID","file","GITLAB_LINT_FILE","baseUrl","GITLAB_LINT_BASE_URL","isNaN","Error","fs","existsSync","lintFile","response","fetch","method","headers","Accept","body","JSON","stringify","content","readFileSync","toString","status","_extends","json","formatLintIssues","lines","map","line","i","chalk","bold","join","formatResult","result","output","warnings","length","hex","errors","red","green","formatShortResult"],"mappings":"mSAKsBA,IACpB,MAAMC,EAAQC,QAAQC,IAAIC,mBAAqB,GACzCC,EAAYC,SAASJ,QAAQC,IAAII,wBAA0B,GAAI,IAC/DC,EAAON,QAAQC,IAAIM,kBAAoB,iBACvCC,EAAUR,QAAQC,IAAIQ,sBAAwB,qBAEpD,IAAKV,GAASW,MAAMP,GAClB,UAAUQ,MACR,sHAIJ,IAAKC,EAAGC,WAAWP,GACjB,UAAUK,eAAeL,qBAG3B,OAAOQ,EAASf,EAAOI,EAAWG,EAAME,kBAMpBM,EACpBf,EACAI,EACAG,EACAE,GAEA,MAAMO,QAAiBC,KAClBR,qBAA2BL,YAC9B,CACEc,OAAQ,OACRC,QAAS,CACPC,OAAQ,mBACR,eAAgB,mBAChB,gBAAiBpB,GAEnBqB,KAAMC,KAAKC,UAAU,CAAEC,QAASX,EAAGY,aAAalB,GAAMmB,eAI1D,GAAwB,MAApBV,EAASW,OACX,UAAUf,0CAC4BI,EAASW,iBAIjD,OAAAC,GACErB,KAAAA,SACWS,EAASa,QAIxB,MAEMC,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