From e5b77404cdcd9711dcb7010b21fdd049f2cd0fbf Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 11:42:42 +0200 Subject: [PATCH 01/10] better tests --- .github/workflows/test.yaml | 19 +++++++----- check-junit-upload.js | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 check-junit-upload.js diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 5c31f3f..19a2bc2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,6 +5,9 @@ on: branches: - main - 'release/*' + - juan-fernandez/testing-new-version + schedule: + - cron: '0 0 * * *' # Runs at midnight UTC every day jobs: test: @@ -14,15 +17,17 @@ jobs: - uses: ./ with: api-key: ${{secrets.DD_API_KEY}} - datadog-site: datad0g.com logs: "true" - files: ./ci/fixtures/ - service: junit-upload-github-action + files: '**/fixtures/**' + service: junit-upload-github-action-tests env: ci tags: "foo:bar,alpha:bravo" - - name: Tag pipeline with ci version + - name: Check that test data can be queried run: | - datadog-ci tag --level pipeline --tags "datadogci.version:`npm list -g @datadog/datadog-ci | grep @datadog/datadog-ci | awk -F' ' '{print $2}' | awk -F'@' '{print $3}' | tr -d '\n'`" + npm install @datadog/datadog-api-client + node ./check-junit-upload.js env: - DATADOG_API_KEY: ${{secrets.DD_API_KEY}} - DATADOG_SITE: datad0g.com \ No newline at end of file + EXTRA_TAGS: "foo:bar alpha:bravo" + DD_API_KEY: ${{ secrets.DD_API_KEY_CI_VISIBILITY }} + DD_APP_KEY: ${{ secrets.DD_APP_KEY_CI_VISIBILITY }} + DD_SERVICE: junit-upload-github-action-tests diff --git a/check-junit-upload.js b/check-junit-upload.js new file mode 100644 index 0000000..42bf96e --- /dev/null +++ b/check-junit-upload.js @@ -0,0 +1,59 @@ +'use strict' + +const { client, v2 } = require("@datadog/datadog-api-client") + +const configuration = client.createConfiguration(); +const apiInstance = new v2.CIVisibilityTestsApi(configuration); + +const params = { + filterQuery: `@test.service:${process.env.DD_SERVICE} @git.commit.sha:${process.env.GITHUB_SHA}`, + filterFrom: new Date(new Date().getTime() + -300 * 1000), // Last 5 minutes + filterTo: new Date(), + pageLimit: 5, +}; + +const CHECK_INTERVAL_SECONDS = 10 +const MAX_NUM_ATTEMPTS = 10 + +function getTestData (extraFilter) { + const finalFilterQuery = `${params.filterQuery} ${extraFilter}` + console.log(`🔎 Querying CI Visibility tests with ${finalFilterQuery}.`) + return apiInstance + .listCIAppTestEvents({ + ...params, + filterQuery: `${finalFilterQuery}`, + }) + .then(data => data.data) + .catch(error => console.error(error)) +} + +function waitFor (waitSeconds) { + return new Promise(resolve => setTimeout(() => resolve(), waitSeconds * 1000)) +} + +async function checkJunitUpload (testLevel) { + let numAttempts = 0 + let isSuccess = false + let data = [] + while (numAttempts++ < MAX_NUM_ATTEMPTS && !isSuccess) { + data = await getTestData(`test_level:${testLevel} ${EXTRA_TAGS}`) + if (data.length > 0) { + isSuccess = true + } else { + const isLastAttempt = numAttempts === MAX_NUM_ATTEMPTS + if (!isLastAttempt) { + console.log(`🔁 Attempt number ${numAttempts} failed, retrying in ${CHECK_INTERVAL_SECONDS} seconds.`) + await waitFor(CHECK_INTERVAL_SECONDS) + } + } + } + if (isSuccess) { + console.log(`✅ Successful check: the API returned ${data.length} ${testLevel}s.`) + process.exit(0) + } else { + console.log(`❌ Failed check: the API did not return any ${testLevel}s for the given filter.`) + process.exit(1) + } +} + +checkJunitUpload("test") \ No newline at end of file From 1aa133538347914a9721d06cfc33cfa35326bc74 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 11:44:18 +0200 Subject: [PATCH 02/10] fix api key --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 19a2bc2..e6faf74 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,7 +16,7 @@ jobs: - uses: actions/checkout@v3 - uses: ./ with: - api-key: ${{secrets.DD_API_KEY}} + api-key: ${{secrets.DD_API_KEY_CI_VISIBILITY}} logs: "true" files: '**/fixtures/**' service: junit-upload-github-action-tests From b3d9ec07abc58407ecda34aa439e728a44e7fccb Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 11:46:02 +0200 Subject: [PATCH 03/10] fix usage of extra tags --- check-junit-upload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/check-junit-upload.js b/check-junit-upload.js index 42bf96e..d8802a0 100644 --- a/check-junit-upload.js +++ b/check-junit-upload.js @@ -36,7 +36,7 @@ async function checkJunitUpload (testLevel) { let isSuccess = false let data = [] while (numAttempts++ < MAX_NUM_ATTEMPTS && !isSuccess) { - data = await getTestData(`test_level:${testLevel} ${EXTRA_TAGS}`) + data = await getTestData(`test_level:${testLevel} ${process.env.EXTRA_TAGS}`) if (data.length > 0) { isSuccess = true } else { From d58eb64c3be3f58ed9b135f2bd1cea5ade855dd5 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:05:05 +0200 Subject: [PATCH 04/10] more clean up --- .github/workflows/test.yaml | 11 ++++++----- action.yaml | 17 +++++++---------- ci/fixtures/python-unittest.xml | 2 ++ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e6faf74..625a065 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,11 +10,12 @@ on: - cron: '0 0 * * *' # Runs at midnight UTC every day jobs: - test: + test-glob-pattern: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: ./ + - uses: actions/checkout@v4 + - name: Upload reports using a glob pattern + uses: ./ with: api-key: ${{secrets.DD_API_KEY_CI_VISIBILITY}} logs: "true" @@ -27,7 +28,7 @@ jobs: npm install @datadog/datadog-api-client node ./check-junit-upload.js env: - EXTRA_TAGS: "foo:bar alpha:bravo" + EXTRA_TAGS: "@foo:bar @alpha:bravo" DD_API_KEY: ${{ secrets.DD_API_KEY_CI_VISIBILITY }} DD_APP_KEY: ${{ secrets.DD_APP_KEY_CI_VISIBILITY }} - DD_SERVICE: junit-upload-github-action-tests + DD_SERVICE: junit-upload-github-action-tests \ No newline at end of file diff --git a/action.yaml b/action.yaml index a58346a..3c858cb 100644 --- a/action.yaml +++ b/action.yaml @@ -1,6 +1,6 @@ # Composite action to upload junit test result files to Datadog CI Visibility -name: 'Datadog JUnitXML Upload' -description: 'Upload JUnitXML reports files to Datadog CI Visibility' +name: "Datadog JUnitXML Upload" +description: "Upload JUnitXML reports files to Datadog CI Visibility" inputs: api-key: required: true @@ -23,7 +23,7 @@ inputs: node-version: required: true description: The node version used to install datadog-ci - default: "16" + default: "20" tags: required: false description: Datadog tags to associate with the uploaded test results. @@ -34,24 +34,21 @@ inputs: required: false description: Set to "true" to enable forwarding content from XML reports as logs. extra-args: - default: '' + default: "" description: Extra args to be passed to the datadog-ci cli. required: false runs: using: "composite" steps: - name: Install node - uses: actions/setup-node@v3 + uses: actions/setup-node@v4 with: node-version: ${{ inputs.node-version }} - - name: Get Datadog CLI - shell: bash - run: npm install -g @datadog/datadog-ci - name: Upload the JUnit files if: ${{ inputs.logs == 'true' }} shell: bash run: | - datadog-ci junit upload \ + npm exec @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --logs \ --max-concurrency ${{ inputs.concurrency }} \ @@ -66,7 +63,7 @@ runs: if: ${{ inputs.logs != 'true' }} shell: bash run: | - datadog-ci junit upload \ + npm exec @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --max-concurrency ${{ inputs.concurrency }} \ ${{ inputs.extra-args }} \ diff --git a/ci/fixtures/python-unittest.xml b/ci/fixtures/python-unittest.xml index 6fa9d5f..6ffaf4a 100644 --- a/ci/fixtures/python-unittest.xml +++ b/ci/fixtures/python-unittest.xml @@ -1,5 +1,6 @@ + @@ -41,3 +42,4 @@ AssertionError: True != False + \ No newline at end of file From 3c4bda2367e3d85af72cdb9def09ce05defb5b81 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:10:08 +0200 Subject: [PATCH 05/10] use npx --- README.md | 2 +- action.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ca4f544..8a1efaf 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The action has the following options: | `datadog-site` | The Datadog site to upload the files to. | True | `datadoghq.com` | | `files` | Path to file or folder containing XML files to upload | True | `.` | | `concurrency` | Controls the maximum number of concurrent file uploads | True | `20` | -| `node-version` | The node version to use to install the datadog-ci. It must be `>=10.24.1` | True | `16` | +| `node-version` | The node version to use to install the datadog-ci. It must be `>=10.24.1` | True | `20` | | `tags` | Optional extra tags to add to the tests | False | | | `env` | Optional environment to add to the tests | False | | | `logs` | When set to "true" enables forwarding content from the XML reports as Logs. The content inside ``, ``, and `` is collected as logs. Logs from elements inside a `` are automatically connected to the test. | False | | diff --git a/action.yaml b/action.yaml index 3c858cb..f8dac84 100644 --- a/action.yaml +++ b/action.yaml @@ -48,7 +48,7 @@ runs: if: ${{ inputs.logs == 'true' }} shell: bash run: | - npm exec @datadog/datadog-ci junit upload \ + npx @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --logs \ --max-concurrency ${{ inputs.concurrency }} \ @@ -63,7 +63,7 @@ runs: if: ${{ inputs.logs != 'true' }} shell: bash run: | - npm exec @datadog/datadog-ci junit upload \ + npx @datadog/datadog-ci junit upload \ --service ${{ inputs.service }} \ --max-concurrency ${{ inputs.concurrency }} \ ${{ inputs.extra-args }} \ From 5d103e06d1069c3a03ddeb1317f1673fb390d034 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:18:17 +0200 Subject: [PATCH 06/10] python fix --- .github/workflows/test.yaml | 1 - ci/fixtures/python-unittest.xml | 34 ++++++--------------------------- 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 625a065..4a64c23 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -5,7 +5,6 @@ on: branches: - main - 'release/*' - - juan-fernandez/testing-new-version schedule: - cron: '0 0 * * *' # Runs at midnight UTC every day diff --git a/ci/fixtures/python-unittest.xml b/ci/fixtures/python-unittest.xml index 6ffaf4a..3e87958 100644 --- a/ci/fixtures/python-unittest.xml +++ b/ci/fixtures/python-unittest.xml @@ -1,45 +1,23 @@ - - - - - - + + - - - - - + - - - - Some output - - - + - + - - - - - + - - - - \ No newline at end of file From 814de9f343597e0aca30c2c3aff3aa931552b821 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:27:23 +0200 Subject: [PATCH 07/10] better script --- .github/workflows/test.yaml | 2 +- check-junit-upload.js | 14 ++++++++------ ci/fixtures/{ => subfolder}/python-unittest.xml | 0 3 files changed, 9 insertions(+), 7 deletions(-) rename ci/fixtures/{ => subfolder}/python-unittest.xml (100%) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 4a64c23..7bf2c86 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,7 +9,7 @@ on: - cron: '0 0 * * *' # Runs at midnight UTC every day jobs: - test-glob-pattern: + test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 diff --git a/check-junit-upload.js b/check-junit-upload.js index d8802a0..5f6526e 100644 --- a/check-junit-upload.js +++ b/check-junit-upload.js @@ -5,6 +5,8 @@ const { client, v2 } = require("@datadog/datadog-api-client") const configuration = client.createConfiguration(); const apiInstance = new v2.CIVisibilityTestsApi(configuration); +const EXPECTED_NUM_TESTS = 32 + const params = { filterQuery: `@test.service:${process.env.DD_SERVICE} @git.commit.sha:${process.env.GITHUB_SHA}`, filterFrom: new Date(new Date().getTime() + -300 * 1000), // Last 5 minutes @@ -31,13 +33,13 @@ function waitFor (waitSeconds) { return new Promise(resolve => setTimeout(() => resolve(), waitSeconds * 1000)) } -async function checkJunitUpload (testLevel) { +async function checkJunitUpload () { let numAttempts = 0 let isSuccess = false let data = [] while (numAttempts++ < MAX_NUM_ATTEMPTS && !isSuccess) { - data = await getTestData(`test_level:${testLevel} ${process.env.EXTRA_TAGS}`) - if (data.length > 0) { + data = await getTestData(`test_level:test ${process.env.EXTRA_TAGS}`) + if (data.length === EXPECTED_NUM_TESTS) { isSuccess = true } else { const isLastAttempt = numAttempts === MAX_NUM_ATTEMPTS @@ -48,12 +50,12 @@ async function checkJunitUpload (testLevel) { } } if (isSuccess) { - console.log(`✅ Successful check: the API returned ${data.length} ${testLevel}s.`) + console.log(`✅ Successful check: the API returned ${data.length} tests.`) process.exit(0) } else { - console.log(`❌ Failed check: the API did not return any ${testLevel}s for the given filter.`) + console.log(`❌ Failed check: the API returned ${data.length} tests but ${EXPECTED_NUM_TESTS} were expected.`) process.exit(1) } } -checkJunitUpload("test") \ No newline at end of file +checkJunitUpload() \ No newline at end of file diff --git a/ci/fixtures/python-unittest.xml b/ci/fixtures/subfolder/python-unittest.xml similarity index 100% rename from ci/fixtures/python-unittest.xml rename to ci/fixtures/subfolder/python-unittest.xml From 7bb35e2859848d7209ec975224a6be425ee9058c Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:34:33 +0200 Subject: [PATCH 08/10] better --- .github/workflows/test.yaml | 4 ++++ README.md | 2 +- check-junit-upload.js | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7bf2c86..f85c9c1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -10,6 +10,9 @@ on: jobs: test: + strategy: + matrix: + version: [14, 16, 18, 20] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -22,6 +25,7 @@ jobs: service: junit-upload-github-action-tests env: ci tags: "foo:bar,alpha:bravo" + node-version: ${{ matrix.version}} - name: Check that test data can be queried run: | npm install @datadog/datadog-api-client diff --git a/README.md b/README.md index 8a1efaf..0f3d3d9 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ The action has the following options: | `datadog-site` | The Datadog site to upload the files to. | True | `datadoghq.com` | | `files` | Path to file or folder containing XML files to upload | True | `.` | | `concurrency` | Controls the maximum number of concurrent file uploads | True | `20` | -| `node-version` | The node version to use to install the datadog-ci. It must be `>=10.24.1` | True | `20` | +| `node-version` | The node version to use to install the datadog-ci. It must be `>=14` | True | `20` | | `tags` | Optional extra tags to add to the tests | False | | | `env` | Optional environment to add to the tests | False | | | `logs` | When set to "true" enables forwarding content from the XML reports as Logs. The content inside ``, ``, and `` is collected as logs. Logs from elements inside a `` are automatically connected to the test. | False | | diff --git a/check-junit-upload.js b/check-junit-upload.js index 5f6526e..7dd1855 100644 --- a/check-junit-upload.js +++ b/check-junit-upload.js @@ -11,7 +11,7 @@ const params = { filterQuery: `@test.service:${process.env.DD_SERVICE} @git.commit.sha:${process.env.GITHUB_SHA}`, filterFrom: new Date(new Date().getTime() + -300 * 1000), // Last 5 minutes filterTo: new Date(), - pageLimit: 5, + pageLimit: 50, }; const CHECK_INTERVAL_SECONDS = 10 From b41300f4215853e5cffd7395c47c954491e565e2 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:38:50 +0200 Subject: [PATCH 09/10] depend on node version --- .github/workflows/test.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index f85c9c1..24b6072 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -24,14 +24,14 @@ jobs: files: '**/fixtures/**' service: junit-upload-github-action-tests env: ci - tags: "foo:bar,alpha:bravo" + tags: "foo:bar,alpha:bravo,test.node.version:${{ matrix.version}}" node-version: ${{ matrix.version}} - name: Check that test data can be queried run: | npm install @datadog/datadog-api-client node ./check-junit-upload.js env: - EXTRA_TAGS: "@foo:bar @alpha:bravo" + EXTRA_TAGS: "@foo:bar @alpha:bravo @test.node.version:${{ matrix.version}}" DD_API_KEY: ${{ secrets.DD_API_KEY_CI_VISIBILITY }} DD_APP_KEY: ${{ secrets.DD_APP_KEY_CI_VISIBILITY }} DD_SERVICE: junit-upload-github-action-tests \ No newline at end of file From 8cc74cabfaef0cfed61bbb3f5591d354d9f1cd86 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Tue, 28 May 2024 12:42:05 +0200 Subject: [PATCH 10/10] remove unnecessary changes --- ci/fixtures/subfolder/python-unittest.xml | 26 ++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/ci/fixtures/subfolder/python-unittest.xml b/ci/fixtures/subfolder/python-unittest.xml index 3e87958..5d03d19 100644 --- a/ci/fixtures/subfolder/python-unittest.xml +++ b/ci/fixtures/subfolder/python-unittest.xml @@ -1,12 +1,25 @@ - - + + + + + + + + + + + + Some output + + + @@ -15,9 +28,16 @@ self.assertEqual(True, False) AssertionError: True != False ]]> + + + + + + + + - \ No newline at end of file