diff --git a/.github/workflows/capture.yml b/.github/workflows/capture.yml deleted file mode 100644 index 7e3613c..0000000 --- a/.github/workflows/capture.yml +++ /dev/null @@ -1,9 +0,0 @@ -name: Capture -on: - workflow_dispatch: -jobs: - publish: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - run: printenv 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", 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 new file mode 100644 index 0000000..9320da0 --- /dev/null +++ b/src/helpers/ci.js @@ -0,0 +1,62 @@ +const ENV = process.env; + +function getCIInformation() { + if (ENV.GITHUB_ACTIONS) { + return getGitHubActionsInformation(); + } + if (ENV.JENKINS_URL) { + return getJenkinsInformation(); + } + if (ENV.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI) { + return getAzureDevOpsInformation(); + } +} + +function getGitHubActionsInformation() { + return { + ci: 'GITHUB_ACTIONS', + 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 + } +} + +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 +} \ No newline at end of file