#include tcwg/default.yaml.inc

- job:
    name: tcwg-report-stale-rr-jobs-#{REPORT_PROJECTS}
    project-type: freestyle
    defaults: global
    properties:
      - default-properties
    parameters:
      - string:
          name: days
          default: 8
          description: 'List jobs with no successful run in DAYS'
      - string:
          name: days_per_project_extra_params
          default: ==days_per_project[tcwg_flang] 1 ==days_per_project[tcwg_chromium] 360
          description: 'For the specified projects, use this DAYS value'
    disabled: false
    node: tcwg-coordinator
    concurrent: false
    display-name: 'TCWG CCC Report Stale RR Jobs (#{REPORT_PROJECTS})'
    scm:
      - jenkins-scripts-master
    triggers:
      - timed: '@daily'
    wrappers:
      - timeout:
          timeout: 300
      - timestamps
      - ssh-agent-credentials:
          # tcwg-buildslave user id
          users:
            - 'e0958a95-204f-4c14-a66c-5e2be6c5d50a'
    builders:
      - shell: |
          #!/bin/bash
          set -euf -o pipefail
          set -x

          rm -rf artifacts
          mkdir artifacts
          bash -x ./jenkins-scripts/tcwg-report-stale-rr-jobs.sh --days "$days" \
              --report_projects #{REPORT_PROJECTS} \
              $days_per_project_extra_params \
              --output artifacts/stale-jobs.log
          bash -x ./jenkins-scripts/tcwg-report-stale-rr-jobs.sh \
              --classify artifacts/stale-jobs.log \
              --output artifacts/stale-jobs-analysis.log || true
          # Count the number non-empty nor non-comments lines
          n_lines=$(grep -c -v -E '^#|^$' artifacts/stale-jobs.log || true)
          ./jenkins-scripts/tcwg-report-ci-status.sh > artifacts/ci-status.log
          if [ x"$n_lines" != x"0" ]; then
            (
              echo "Jobs with no new results in $days days ($n_lines entries):"
              cat artifacts/ci-status.log
              cat artifacts/stale-jobs-analysis.log
              cat artifacts/stale-jobs.log
            ) > artifacts/email-body.txt
            exit 1
          fi
    publishers:
        - archive:
            artifacts: 'artifacts/**'
            latest-only: false
        - email-ext:
            recipients: 'maxim.kuvyrkov@linaro.org, laurent.alfonsi@linaro.org'
            content-type: text
            body: |
              ${FILE,path="artifacts/email-body.txt"}
            failure: true
            aborted: true
