- job:
    name: schneider-openembedded-test-notify
    project-type: freestyle
    defaults: global
    properties:
        - authorization:
            anonymous:
                - job-read
                - job-extended-read
                - job-workspace
            everyone-flat:
                - job-build
                - job-cancel
        - build-discarder:
            days-to-keep: 30
            num-to-keep: 30
    disabled: false
    node: master
    concurrent: false
    display-name: 'Schneider OpenEmbedded Test Notify'
    wrappers:
        - timeout:
            timeout: 60
        - timestamps
        - ssh-agent-credentials:
            users:
                - '768c1ba1-6052-4b97-a531-7e4fabda6979'
        - credentials-binding:
            - text:
                credential-id: QA_REPORTS_TOKEN
                variable: QA_REPORTS_TOKEN
    builders:
        - shell: |
            #!/bin/bash

            set -ex
            pwd
            echo "Get previous"
            wget -nv -O last.cve https://snapshots.linaro.org//openembedded/schneider/linaro-warrior-4.19/rzn1d/latest/dip/dip-image-rzn1-snarc.rootfs.cve
            echo "Get baseline"
            wget -nv -O base.cve https://releases.linaro.org/members/schneider/openembedded/2019.09-warrior.2/rzn1d-4.19/dip-image-rzn1-snarc-linaro-rel-2019.09-warrior.2-internal-70.rootfs.cve
    publishers:
        - postbuildscript:
            builders:
                - role: MASTER
                  build-on:
                    - SUCCESS
                  build-steps:
                    - shell: |
                        echo "Combining CVEs"
                        #sort -u ${JENKINS_HOME}/jobs/${JOB_NAME}/configurations/axis-DISTRO/dip/axis-MACHINE/*/axis-label/docker-stretch-amd64/builds/${BUILD_NUMBER}/archive/cve-*.txt > cve.txt
                        sort -u ${JENKINS_HOME}/jobs/schneider-openembedded-warrior-4.19-rfs/configurations/axis-DISTRO/dip/axis-MACHINE/*/axis-label/docker-stretch-amd64/builds/31/archive/cve-*.txt > cve.txt
                        cat cve.txt
                        echo "HTMLify CVEs"
                        perl -F'\t' -lane '($type, $cve, $pkg, $url, @rest) = @F; push @{$foo{$type}}, ("<a href=\"$url\">$cve</a> $pkg" . list(@rest)); sub list { return @_ ? join("", "<ul>\n", map("<li>$_</li>\n", @_), "</ul>") : ""} ; END { foreach (reverse sort keys %foo) { print "<b>$_ CVEs</b>", list(@{$foo{$_}}) if @{$foo{$_}} }}' cve.txt > cve.html
                        echo "Summarize CVEs"
                        printf "NEW=%d CHANGED=%d FIXED=%d\n" `grep NEW cve.txt|wc -l` >cve.sum

        - groovy-postbuild:
            script:
                !include-raw:
                  - schneider-openembedded/postbuild.groovy
        - email-ext:
            # Send custom email generated in jenkins job
            recipients: 'ralph.siemsen@linaro.org'
            subject: 'Schneider OpenEmbedded Test Notify'
            body: |
                $DEFAULT_CONTENT

                CVE summary: ${FILE,path="cve.sum"}
            attachments: "cve.html"
            success: true
        - email:
             recipients: 'ralph.siemsen@linaro.org'
