default:
  image: gitup.uni-potsdam.de:4567/maxschro/cofe_up/cofe_up

variables:
  GIT_STRATEGY: clone
  AUFGABE: "1.5"
  PARAMETER: ""

stages:
  - clean
  - build
  - lint
  - publish

Clean:
  tags:
    - lint
  stage: clean
  script:
    - rm -rf data
    - mkdir data
  artifacts:
    when: always
    paths: 
      - results/output.make
      - data

Build:
  tags:
    - lint
  stage: build
  script:
    - echo "Try building"
    - rm -rf results
    - mkdir results
    - make clean
    - make 2> results/output.make
    - if [ -f results/output.make ]; then if grep -i warning results/output.make > /dev/null; then exit 1; else exit 0;fi;fi
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/make.pkl
  allow_failure:
    exit_codes:
      - 1
  artifacts:
    when: always
    paths: 
      - results/output.make
      - data
    reports:
      junit: output.xml

Build-Vorlage:
  tags:
    - lint
  stage: build
  script:
    - echo "Try building"
    - rm -rf results
    - mkdir results
    # remove students Makefile
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile.bak" Makefile
    - make clean
    - make 2> results/output.make
    - if [ -f results/output.make ]; then if grep -i warning results/output.make > /dev/null; then exit 4; else exit 0;fi;fi
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/make-template.pkl
  allow_failure:
    exit_codes:
      - 4
  artifacts:
    when: always
    paths: 
      - results/output.make
      - data
    reports:
      junit: output.xml

Run:
  tags:
    - lint
  stage: lint
  script:
    - echo "Try building"
    - rm -rf results
    - mkdir results
    # remove students Makefile
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-analyzer.bak" Makefile
    - make clean
    - make
    - echo "-------------------------------------------------"; echo "YOUR PROGRAM IS BEING EXECUTED"; echo "-------------------------------------------------"
    - "./prog ${PARAMETER}"
  allow_failure: true
  artifacts:
    when: always
    paths: 
      - prog
      - core

StaticAnalyze:
  tags:
    - lint
  stage: lint
  script:
    - echo "Static analyze with 'SimpleErrorChecker'"
    - rm -rf results
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-analyzer.bak" Makefile
    - make clean
    # need the directory even if the analyzer don't find an error
    - mkdir results
    - '( scan-build -o ./results -plist-html -load-plugin /root/llvm-static-analyzers/build/SimpleErrorChecker.so -enable-checker alpha.unix.ErrorChecker2 -disable-checker unix.Malloc make 2> /dev/null || true ) | grep "scan-build: No bugs found."'
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/scan-build.pkl
  allow_failure:
    exit_codes:
      - 1
  artifacts:
    when: always    
    expose_as: 'StaticAnalyze Report'
    paths:
      - results
      - data
    reports:
      junit: output.xml

StaticAnalyze2-Header:
  tags:
    - lint
  stage: lint
  script:
    - echo "Static analyze with 'DefinitionInHeaders'"
    - rm -rf results
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-analyzer.bak" Makefile
    - make clean
    - bear -- make 
    - mkdir results
    - clang-tidy -checks=-*,misc-definitions-in-headers,bugprone-suspicious-include -header-filter=.* *.c  >> results/clangCheck.tidy
    - clang-tidy -checks=-*,misc-definitions-in-headers,bugprone-suspicious-include -header-filter=.* --export-fixes=rawoutput.txt *.c 
    - if [ -s rawoutput.txt ]; then exit 2; fi;
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/tidy.pkl
  allow_failure: 
    exit_codes:
      - 1
      - 2
  artifacts:
    when: always    
    expose_as: 'StaticAnalyze2-Header Report'
    paths: 
      - results
      - data
    reports: 
      junit: output.xml

UnitTest:
  tags:
    - lint
  stage: lint
  variables:
    CMOCKA_XML_FILE: "results/%g.cmocka"
  script:
    - echo "Unittest with CMocka"
    - rm -rf results
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-test.bak" Makefile
    - cp "/opt/exercises/gbr/$AUFGABE"/test-* ./
    - make clean
    - mkdir results
    - make 2> results/output.makecm
    # If make fails, the job stops here. Otherwise we proceed with Unit-Tests
    - rm -f /results/output.makecm
    - unset ERROR
    - |
      for i in test*.out; do 
        if "./$i" > /dev/null 2> "results/$i.make"; then 
          rm -f "results/$i.make"; 
        else 
          if [ $? -ge 125 ] && [ $? -lt 255 ]; then 
            echo "Fehler beim ausfuehren der Unit-Tests, möglicher Segmentation Fault" > results/output.make; 
          else 
            rm -f "results/$i.make"; 
          fi; 
          ERROR=1;
        fi; 
      done
    - if [[ -v ERROR ]]; then false; fi
    - rm -f results/output.make
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/unit.pkl
  allow_failure: true
  artifacts:
    when: always
    paths: 
      - results
      - core
      - test*.out
      - data
    reports:
      junit: output.xml

MemoryCheck:
  tags:
    - lint
  stage: lint
  script:
    - echo "Thread Checks with TSAN"
    - rm -rf results
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-asan.bak" Makefile
    - make
    - echo "Analyze"
    - mkdir results
    - ASAN_OPTIONS="log_path=results/log.asan" ./prog $PARAMETER
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/asan.pkl
  allow_failure:
    exit_codes:
      - 1
  artifacts:
    when: always
    paths:
      - results
      - data
    reports:
      junit: output.xml

Threadanalyzer:
  tags:
    - lint
  stage: lint
  script:
    - echo "Thread Checks with TSAN"
    - rm -rf results
    - find . -iname "makefile" -exec rm {} \;
    - cp "/opt/exercises/gbr/$AUFGABE/Makefile-tsan.bak" Makefile
    - make
    - echo "Analyze"
    - mkdir results
    - TSAN_OPTIONS="log_path=results/log.tsan" ./prog $PARAMETER
  after_script:
    - python3 /opt/parser/run.py --input-dir ./results --output-junit output.xml --cf_local "/opt/exercises/gbr/$AUFGABE/messages.xml" --cf_global /opt/exercises/gbr/messages.xml --srcdir ./ --export-errors ./data/tsan.pkl
  allow_failure:
    exit_codes:
      - 1
  artifacts:
    when: always
    paths:
      - results
      - data
    reports:
      junit: output.xml

pages:
  stage: publish
  tags:
    - lint
  script:
    - mkdir -p public
    - python3 /opt/parser/run.py --print --output-html ./public --template-dir /opt/templates --srcdir ./ --load-errors ./data --create-report --with-gitlab --download-report --export-report ./public/cofee.pkl
  allow_failure:
    exit_codes:
      - 1  
  artifacts:
    paths:
    - public
    - data
  when: always
