name: "check-version-consistency"
description: "Check consistency between given version and given file (install, update, .version...)."
inputs:
  file:
    description: The file to check for version consistency
    required: true
  version:
    description: The version to check against the file
    required: true

runs:
  using: "composite"
  steps:
    - name: Check version consistency
      uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
      env:
        VERSION: ${{ inputs.version }}
        FILE: ${{ inputs.file }}
      with:
        script: |
          const fs = require('fs');
          const filePath = process.env.FILE;
          const version = process.env.VERSION;

          if (!fs.existsSync(filePath)) {
            core.setFailed(`File ${filePath} does not exist.`);
            return;
          }

          if (!fs.existsSync('.version')) {
            core.setFailed('No .version file found in the repository.');
            return;
          }

          const majorVersionContent = fs.readFileSync('.version', { encoding: 'utf8' });
          const majorVersionMatch = majorVersionContent.match(/MAJOR="?(\d+\.\d+)"?/);
          if (!majorVersionMatch || majorVersionMatch.length < 2) {
            core.setFailed('No valid MAJOR version found in .version.');
            return;
          }
          const majorVersion = majorVersionMatch[1];

          if (filePath.match(/CMakeLists\.txt$/)) {
            const content = fs.readFileSync(filePath, { encoding: 'utf8' });
            let majorVarName = 'COLLECT_MAJOR';
            let minorVarName = 'COLLECT_MINOR';
            let patchVarName = 'COLLECT_PATCH';
            if (context.workflow === 'monitoring-agent') {
              patchVarName = 'AGENT_PATCH';
            }

            const matches = content.matchAll(new RegExp(`^\\s*set\\(\\s*(${majorVarName}|${minorVarName}|${patchVarName})\\s+(\\d+)\\s*\\)`, 'gm'));
            console.log(matches);
            if (matches) {
              let majorVersion = null;
              let minorVersion = null;
              let patchVersion = null;
              for (const match of matches) {
              console.log(match);
                if (match && match.length > 2) {
                  if (match[1] === majorVarName) {
                    majorVersion = match[2];
                  } else if (match[1] === minorVarName) {
                    minorVersion = match[2];
                  } else if (match[1] === patchVarName) {
                    patchVersion = match[2];
                  }
                }
              }
              if (majorVersion && minorVersion && patchVersion) {
                if (version !== `${majorVersion}.${minorVersion}.${patchVersion}`) {
                  core.setFailed(`Version mismatch: expected ${version}, found ${majorVersion}.${minorVersion}.${patchVersion} in ${filePath}.`);
                  return;
                } else {
                  core.info(`Version consistency check passed for ${filePath} ${majorVersion}.${minorVersion}.${patchVersion}.`);
                  return;
                }
              }
            }
            core.setFailed(`Version not found in ${filePath}.`);
            return;
          }

          if (filePath.match(/\.version.(\w|-)+$/)) {
            const minorVersionContent = fs.readFileSync(filePath, { encoding: 'utf8' });
            const minorVersionMatch = minorVersionContent.match(/MINOR="?(\d+)"?/);
            if (!minorVersionMatch || minorVersionMatch.length < 2) {
              core.setFailed(`No valid MINOR version found in ${filePath}.`);
              return;
            }
            const minorVersion = minorVersionMatch[1];

            if (version !== `${majorVersion}.${minorVersion}`) {
              core.setFailed(`Version mismatch: expected ${version}, found ${majorVersion}.${minorVersion} in ${filePath}.`);
              return;
            }

            core.info(`Version consistency check passed for ${filePath} ${majorVersion}.${minorVersion}.`);
            return;
          }

          core.setFailed(`Unsupported file: ${filePath}`);
          return;
