From 43722213ce25dcb209879998cdd9d665bbdb5ed0 Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Mon, 27 Mar 2023 19:21:31 +0200
Subject: [PATCH 1/2] Added codecov token to solve upload issues

---
 .github/workflows/test-go-task.yml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/.github/workflows/test-go-task.yml b/.github/workflows/test-go-task.yml
index 7aef9b0da86..4503d228cad 100644
--- a/.github/workflows/test-go-task.yml
+++ b/.github/workflows/test-go-task.yml
@@ -197,6 +197,7 @@ jobs:
       - name: Send unit tests coverage to Codecov
         uses: codecov/codecov-action@v3
         with:
+          token: ${{ secrets.CODECOV_TOKEN }}
           files: ./coverage.txt
           flags: unit
           fail_ci_if_error: ${{ github.repository == 'arduino/arduino-cli' }}

From bc7014bc604cb8266457fc441a8c0dadcf98a2da Mon Sep 17 00:00:00 2001
From: Cristian Maglie <c.maglie@arduino.cc>
Date: Wed, 29 Mar 2023 09:48:18 +0200
Subject: [PATCH 2/2] Use CODECOV_TOKEN also on pull-request runs.

It requires to expose the token, this is intentional.
https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
---
 .github/workflows/test-go-task.yml | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/test-go-task.yml b/.github/workflows/test-go-task.yml
index 4503d228cad..d4707ce46d1 100644
--- a/.github/workflows/test-go-task.yml
+++ b/.github/workflows/test-go-task.yml
@@ -194,10 +194,24 @@ jobs:
       - name: Merge all code coverage artifacts
         run: gocovmerge coverage*.txt > coverage.txt
 
+      # A token is used to avoid intermittent spurious job failures caused by rate limiting.
+      - name: Set up Codecov upload token
+        run: |
+          if [[ "${{ github.repository }}" == "arduino/arduino-cli" ]]; then
+            # In order to avoid uploads of data from forks, only use the token for runs in the arduino/arduino-ide repo.
+            # Token is intentionally exposed.
+            # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954
+            CODECOV_TOKEN="18b9885b-cbf2-4166-bfbb-39617323daf0"
+          else
+            # codecov/codecov-action does unauthenticated upload if empty string is passed via the `token` input.
+            CODECOV_TOKEN=""
+          fi
+          echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV"
+
       - name: Send unit tests coverage to Codecov
         uses: codecov/codecov-action@v3
         with:
-          token: ${{ secrets.CODECOV_TOKEN }}
+          token: ${{ env.CODECOV_TOKEN }}
           files: ./coverage.txt
           flags: unit
           fail_ci_if_error: ${{ github.repository == 'arduino/arduino-cli' }}