// Jenkins CI for Archive/rping
//
// Add this webhook in Gitea:
// http://192.168.1.37:8080/gitea-webhook/post
//
// rping is a Rust ICMP ping CLI. CI validates compilation and tests only;
// it deliberately does not execute a live ping because raw ICMP sockets may
// require elevated privileges/capabilities on the Jenkins worker.

pipeline {
    agent { label 'rust_nodocker' }

    options {
        skipDefaultCheckout(true)
        timestamps()
        disableConcurrentBuilds()
    }

    environment {
        CARGO_TERM_COLOR = 'never'
        RUST_BACKTRACE = '1'
    }

    stages {
        stage('Checkout') {
            steps {
                checkout scm

                sh '''
                    set -eu

                    git submodule sync --recursive
                    git submodule update --init --recursive
                '''
            }
        }

        stage('Setup Rust') {
            steps {
                sh '''
                    set -eu

                    if ! command -v rustup >/dev/null 2>&1; then
                        echo "ERROR: rustup is not installed on this worker." >&2
                        exit 1
                    fi

                    # rustup installs the version and components from rust-toolchain.toml.
                    # Do not change the shared worker's default toolchain.
                    rustup show active-toolchain

                    rustc --version
                    cargo --version
                '''
            }
        }

        stage('Format') {
            steps {
                sh '''
                    set -eu
                    cargo fmt --all -- --check
                '''
            }
        }

        stage('Check') {
            steps {
                sh '''
                    set -eu
                    cargo check --locked --all-targets --all-features
                '''
            }
        }

        stage('Tests') {
            steps {
                sh '''
                    set -eu
                    cargo test --locked --all-targets --all-features
                '''
            }
        }

        stage('Clippy') {
            steps {
                sh '''
                    set -eu
                    cargo clippy --locked --all-targets --all-features -- -D warnings
                '''
            }
        }

        stage('Build Release') {
            steps {
                sh '''
                    set -eu
                    cargo build --locked --release --all-features
                    test -x target/release/rping
                '''
            }
        }
    }

    post {
        success {
            publishChecks(
                conclusion: 'SUCCESS',
                name: 'rping CI',
                summary: 'rping passed CI.',
                text: 'Formatting, compilation, tests, Clippy, and the release build completed successfully.',
                title: 'Build Successful'
            )
        }

        failure {
            publishChecks(
                conclusion: 'FAILURE',
                name: 'rping CI',
                summary: 'rping failed CI.',
                text: 'One or more Rust checks failed. See the Jenkins log.',
                title: 'Build Failed'
            )
        }

        unstable {
            publishChecks(
                conclusion: 'ACTION_REQUIRED',
                name: 'rping CI',
                summary: 'rping CI is unstable.',
                text: 'The Jenkins build completed with an unstable status.',
                title: 'Build Unstable'
            )
        }
    }
}
