From 893b24389948ff7bc3716af7362e95345ecd1eb3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stevan=20Radakovi=C4=87?= <stevan.radakovic@linaro.org>
Date: Mon, 6 Jun 2022 14:35:22 +0200
Subject: [PATCH] test-generic.yaml: Utilize SQUAD API in CI jobs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Use SQUAD callback to trigger the lava-report pipeline job and
simplify lava-badge calculation by using SQUAD API.
SQUAD build ID is now implemented to be the same as pipeline ID.

Signed-off-by: Stevan Radaković <stevan.radakovic@linaro.org>
---
 .oniro-ci/test-generic.yaml | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/.oniro-ci/test-generic.yaml b/.oniro-ci/test-generic.yaml
index dedabddf..81e314d8 100644
--- a/.oniro-ci/test-generic.yaml
+++ b/.oniro-ci/test-generic.yaml
@@ -44,8 +44,7 @@
       -e 's/@ci_project_id@/'"$CI_PROJECT_ID"'/'
       -e 's/@ci_pipeline_id@/'"$CI_PIPELINE_ID"'/'
       -e 's,@ci_pipeline_url@,'"$CI_PIPELINE_URL"','
-      -e 's/@build_job_id@/'"$build_job_id"'/'
-      -e 's,@callback_url@,'"$CALLBACK_URL"',' job_def.yaml
+      -e 's/@build_job_id@/'"$build_job_id"'/' job_def.yaml
     # Generate test jobs.
     - |
       set -x
@@ -71,8 +70,10 @@
     # Submit the jobs to SQUAD.
     - |
       for job_def in $(find lava_jobs/ -name "*.yaml"); do
-        echo $CI_SQUAD_INSTANCE/api/submitjob/oniro-core/$CI_PROJECT_NAME/$CI_COMMIT_SHORT_SHA/$MACHINE --header "Auth-Token: $CI_SQUAD_TOKEN"  --form "backend=oniro_lava" --form "definition=@${job_def}"
-        curl $CI_SQUAD_INSTANCE/api/submitjob/oniro-core/$CI_PROJECT_NAME/$CI_COMMIT_SHORT_SHA/$MACHINE --header "Auth-Token: $CI_SQUAD_TOKEN"  --form "backend=oniro_lava" --form "definition=@${job_def}"
+        echo $CI_SQUAD_INSTANCE/api/submitjob/oniro-core/$CI_PROJECT_NAME/$CI_PIPELINE_ID/$MACHINE --header "Auth-Token: $CI_SQUAD_TOKEN"  --form "backend=oniro_lava" --form "definition=@${job_def}"
+        curl $CI_SQUAD_INSTANCE/api/submitjob/oniro-core/$CI_PROJECT_NAME/$CI_PIPELINE_ID/$MACHINE --header "Auth-Token: $CI_SQUAD_TOKEN"  --form "backend=oniro_lava" --form "definition=@${job_def}"
+      # Attach a callback to this build to trigger the lava-report job
+      curl -X POST /api/build/$CI_PIPELINE_ID/callbacks/ -F "callback_url=$CALLBACK_URL"
       done
   artifacts:
     paths:
@@ -136,10 +137,8 @@
   script:
     - |
       set -x
-      curl --silent "$CI_API_V4_URL/projects/$CI_PROJECT_ID/pipelines/$CI_PIPELINE_ID/test_report" > test-report.json
-      total_count="$(jq '.total_count' test-report.json)"
-      success_count="$(jq '.success_count' test-report.json)"
-      success_rate=$(( 100 * success_count / total_count ))
+      pass_percentage="$(curl --silent https://squadp.svc.ostc-eu.dev/api/builds/$build_id/status/ | jq -r '.pass_percentage')"
+      success_rate=`printf "%.2f" $pass_percentage`
       anybadge --label=lava-test --value=${success_rate} --suffix='%' --file=lava-test.svg 50=red 60=orange 80=yellow 100=green
       set +x
   artifacts:
-- 
GitLab