/**
 * GitLab CI/CD Runner
 *
 * Integrates Code Buddy with GitLab CI/CD pipelines for MR review and pipeline automation.
 */
import { logger } from '../utils/logger.js';
// ============================================================================
// GitLabCIRunner
// ============================================================================
export class GitLabCIRunner {
    config = null;
    /**
     * Parse environment from CI_* env vars
     */
    parseEnvironment() {
        const token = process.env.CI_JOB_TOKEN || process.env.GITLAB_TOKEN || '';
        const projectId = process.env.CI_PROJECT_ID || '';
        const pipelineSource = process.env.CI_PIPELINE_SOURCE || 'push';
        const mrIid = process.env.CI_MERGE_REQUEST_IID;
        this.config = {
            token,
            projectId,
            pipelineSource,
            mergeRequestIid: mrIid ? parseInt(mrIid, 10) : undefined,
        };
        logger.debug(`Parsed GitLab CI env: project=${projectId}, source=${pipelineSource}`);
        return this.config;
    }
    /**
     * Generate a review comment for a merge request
     */
    generateMRComment(diff, files) {
        const fileList = files.map(f => `- \`${f}\``).join('\n');
        const diffLines = diff.split('\n');
        const additions = diffLines.filter(l => l.startsWith('+')).length;
        const deletions = diffLines.filter(l => l.startsWith('-')).length;
        const comment = [
            '## Code Buddy AI Review',
            '',
            `**Files changed (${files.length}):**`,
            fileList,
            '',
            `**Changes:** +${additions} / -${deletions} lines`,
            '',
            '### Summary',
            '',
            'AI review of the changes in this merge request.',
            '',
            '### Suggestions',
            '',
            'No critical issues found.',
            '',
            '---',
            '*Generated by Code Buddy GitLab CI*',
        ].join('\n');
        return comment;
    }
    /**
     * Format result for CI pipeline artifacts
     */
    formatPipelineOutput(result) {
        return JSON.stringify({
            codebuddy_analysis: {
                timestamp: new Date().toISOString(),
                ...result,
            },
        }, null, 2);
    }
    /**
     * Generate .gitlab-ci.yml template content
     */
    createGitLabTemplate() {
        return `# Code Buddy AI Review for GitLab CI/CD
#
# Add this to your .gitlab-ci.yml or include it as a template.

stages:
  - review

code-buddy-review:
  stage: review
  image: node:20-slim
  only:
    - merge_requests
  script:
    - npm install -g code-buddy
    - code-buddy --non-interactive "Review the changes in this MR and provide feedback"
  variables:
    GROK_API_KEY: $GROK_API_KEY
  artifacts:
    paths:
      - codebuddy-report.json
    when: always
  allow_failure: true
`;
    }
    /**
     * Get current config
     */
    getConfig() {
        return this.config;
    }
}
//# sourceMappingURL=gitlab-ci-runner.js.map