From bc7c9eb290cbf3cb1b83e1c897113792fe5240c8 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sun, 3 Mar 2024 12:46:53 +0530 Subject: [PATCH 1/3] temp: print ci info --- .github/workflows/capture.yml | 5 ++++- src/helpers/ci.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/helpers/ci.js diff --git a/.github/workflows/capture.yml b/.github/workflows/capture.yml index 7e3613c..8d4be04 100644 --- a/.github/workflows/capture.yml +++ b/.github/workflows/capture.yml @@ -6,4 +6,7 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - - run: printenv + - uses: actions/setup-node@v1 + with: + node-version: '20.x' + - run: node src/helpers/ci.js diff --git a/src/helpers/ci.js b/src/helpers/ci.js new file mode 100644 index 0000000..0b585b5 --- /dev/null +++ b/src/helpers/ci.js @@ -0,0 +1,23 @@ +function getCIInformation() { + if (process.env.GITHUB_ACTIONS) { + return getGitHubActionsInformation(); + } +} + +function getGitHubActionsInformation() { + return { + ci: 'GITHUB_ACTIONS', + repository_url: process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY, + repository_name: process.env.GITHUB_REPOSITORY, + repository_ref: process.env.GITHUB_REF, + build_url: process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY + '/commit/' + process.env.GITHUB_SHA + '/checks/' + process.env.GITHUB_RUN_ID, + build_number: process.env.GITHUB_RUN_NUMBER, + build_name: process.env.GITHUB_WORKFLOW, + } +} + +console.log(getCIInformation()) + +module.exports = { + getCIInformation +} \ No newline at end of file From 2687fff25c27096131a6588eb6d6e94b2d8c7d33 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sun, 3 Mar 2024 14:42:13 +0530 Subject: [PATCH 2/3] feat: add ci information --- .github/workflows/capture.yml | 12 -------- src/beats/index.js | 17 +++++++---- src/helpers/ci.js | 55 ++++++++++++++++++++++++++++++----- 3 files changed, 59 insertions(+), 25 deletions(-) delete mode 100644 .github/workflows/capture.yml diff --git a/.github/workflows/capture.yml b/.github/workflows/capture.yml deleted file mode 100644 index 8d4be04..0000000 --- a/.github/workflows/capture.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Capture -on: - workflow_dispatch: -jobs: - publish: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: '20.x' - - run: node src/helpers/ci.js diff --git a/src/beats/index.js b/src/beats/index.js index ca8cc43..5a5f99b 100644 --- a/src/beats/index.js +++ b/src/beats/index.js @@ -1,5 +1,6 @@ const request = require('phin-retry'); const TestResult = require('test-results-parser/src/models/TestResult'); +const { getCIInformation } = require('../helpers/ci'); const BASE_URL = process.env.TEST_BEATS_URL || "http://localhost:9393"; @@ -22,16 +23,22 @@ async function run(config, result) { */ async function publishTestResults(config, result) { try { + const payload = { + project: config.project, + build: config.build, + ...result + } + const ci = getCIInformation(); + if (ci) { + payload.ci_details = [ci]; + } + const response = await request.post({ url: `${BASE_URL}/api/core/v1/test-runs`, headers: { 'x-api-key': config.api_key }, - body: { - project: config.project, - build: config.build, - ...result - } + body: payload }); return response.id; } catch (error) { diff --git a/src/helpers/ci.js b/src/helpers/ci.js index 0b585b5..9320da0 100644 --- a/src/helpers/ci.js +++ b/src/helpers/ci.js @@ -1,22 +1,61 @@ +const ENV = process.env; + function getCIInformation() { - if (process.env.GITHUB_ACTIONS) { + if (ENV.GITHUB_ACTIONS) { return getGitHubActionsInformation(); } + if (ENV.JENKINS_URL) { + return getJenkinsInformation(); + } + if (ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI) { + return getAzureDevOpsInformation(); + } } function getGitHubActionsInformation() { return { ci: 'GITHUB_ACTIONS', - repository_url: process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY, - repository_name: process.env.GITHUB_REPOSITORY, - repository_ref: process.env.GITHUB_REF, - build_url: process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY + '/commit/' + process.env.GITHUB_SHA + '/checks/' + process.env.GITHUB_RUN_ID, - build_number: process.env.GITHUB_RUN_NUMBER, - build_name: process.env.GITHUB_WORKFLOW, + git: 'GITHUB', + repository_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY, + repository_name: ENV.GITHUB_REPOSITORY, + repository_ref: ENV.GITHUB_REF, + repository_commit_sha: ENV.GITHUB_SHA, + build_url: ENV.GITHUB_SERVER_URL + '/' + ENV.GITHUB_REPOSITORY + '/commit/' + ENV.GITHUB_SHA + '/checks/' + ENV.GITHUB_RUN_ID, + build_number: ENV.GITHUB_RUN_NUMBER, + build_name: ENV.GITHUB_WORKFLOW, + user: ENV.GITHUB_ACTOR, + } +} + +function getAzureDevOpsInformation() { + return { + ci: 'AZURE_DEVOPS_PIPELINES', + git: 'AZURE_DEVOPS_REPOS', + repository_url: ENV.BUILD_REPOSITORY_URI, + repository_name: ENV.BUILD_REPOSITORY_NAME, + repository_ref: ENV.BUILD_SOURCEBRANCH, + repository_commit_sha: ENV.BUILD_SOURCEVERSION, + build_url: ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI + ENV.SYSTEM_TEAMPROJECT + '/_build/results?buildId=' + ENV.BUILD_BUILDID, + build_number: ENV.BUILD_BUILDNUMBER, + build_name: ENV.BUILD_DEFINITIONNAME, + user: ENV.BUILD_REQUESTEDFOR } } -console.log(getCIInformation()) +function getJenkinsInformation() { + return { + ci: 'JENKINS', + git: '', + repository_url: ENV.GIT_URL || ENV.GITHUB_URL || ENV.BITBUCKET_URL, + repository_name: ENV.JOB_NAME, + repository_ref: ENV.BRANCH || ENV.BRANCH_NAME, + repository_commit_sha: ENV.GIT_COMMIT || ENV.GIT_COMMIT_SHA || ENV.GITHUB_SHA || ENV.BITBUCKET_COMMIT, + build_url: ENV.BUILD_URL, + build_number: ENV.BUILD_NUMBER, + build_name: ENV.JOB_NAME, + user: ENV.USER || ENV.USERNAME + } +} module.exports = { getCIInformation From c89c1dfef415a4b861a4a39f40682ff3bd3a27c6 Mon Sep 17 00:00:00 2001 From: Anudeep Date: Sun, 3 Mar 2024 14:42:49 +0530 Subject: [PATCH 3/3] bump version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 27c0bab..a6dd326 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "test-results-reporter", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "test-results-reporter", - "version": "1.1.0", + "version": "1.1.1", "license": "ISC", "dependencies": { "async-retry": "^1.3.3", diff --git a/package.json b/package.json index bc52f3d..dc41350 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "test-results-reporter", - "version": "1.1.0", + "version": "1.1.1", "description": "Publish test results to Microsoft Teams, Google Chat, Slack and InfluxDB", "main": "src/index.js", "types": "./src/index.d.ts",