From cb094434d00868f2c6d91051023088e8af67d3b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Tue, 4 Jan 2022 21:45:34 +0000 Subject: [PATCH 1/2] Make buildNumber optional --- CiEnvironments.json | 18 +- README.md | 15 +- javascript/src/CiEnvironments.ts | 6 - javascript/src/detectCiEnvironment.ts | 10 +- javascript/src/types.ts | 2 +- javascript/test/removeUserInfoFromUrlTest.ts | 10 +- javascript/tsconfig.json | 2 +- ruby/lib/cucumber/ci_environment/.gitignore | 1 + .../ci_environment/CiEnvironments.json | 156 ------------------ 9 files changed, 24 insertions(+), 196 deletions(-) create mode 100644 ruby/lib/cucumber/ci_environment/.gitignore delete mode 100644 ruby/lib/cucumber/ci_environment/CiEnvironments.json diff --git a/CiEnvironments.json b/CiEnvironments.json index f9c7072e..302ae214 100644 --- a/CiEnvironments.json +++ b/CiEnvironments.json @@ -17,8 +17,7 @@ "git": { "remote": "${bamboo_planRepository_repositoryUrl}", "revision": "${bamboo_planRepository_revision}", - "branch": "${bamboo_planRepository_branch}", - "tag": null + "branch": "${bamboo_planRepository_branch}" } }, { @@ -61,8 +60,7 @@ "git": { "remote": "${CF_COMMIT_URL/(.*)\\/commit.+$/\\1}.git", "revision": "${CF_REVISION}", - "branch": "${CF_BRANCH}", - "tag": null + "branch": "${CF_BRANCH}" } }, { @@ -72,8 +70,7 @@ "git": { "remote": "${CI_PULL_REQUEST/(.*)\\/pull\\/\\d+/\\1.git}", "revision": "${CI_COMMIT_ID}", - "branch": "${CI_BRANCH}", - "tag": null + "branch": "${CI_BRANCH}" } }, { @@ -105,8 +102,7 @@ "git": { "remote": "${GO_SCM_*_PR_URL/(.*)\\/pull\\/\\d+/\\1.git}", "revision": "${GO_REVISION}", - "branch": "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}", - "tag": null + "branch": "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}" } }, { @@ -116,8 +112,7 @@ "git": { "remote": "${GIT_URL}", "revision": "${GIT_COMMIT}", - "branch": "${GIT_LOCAL_BRANCH}", - "tag": null + "branch": "${GIT_LOCAL_BRANCH}" } }, { @@ -149,8 +144,7 @@ "git": { "remote": "https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}.git", "revision": "${WERCKER_GIT_COMMIT}", - "branch": "${WERCKER_GIT_BRANCH}", - "tag": null + "branch": "${WERCKER_GIT_BRANCH}" } } ] diff --git a/README.md b/README.md index c4ed6570..2ff8c09c 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,23 @@ by CI servers. If a CI server is detected, a `CiEnvironment` struct is returned: -```json +```javascript { "name": "...", "url": "...", - "buildNumber": "...", - "git": { - "remote": "...", + "buildNumber": "...", // optional + "git": { // optional + "remote": "...", "revision": "...", - "branch": "...", - "tag": "..." + "branch": "...", // optional + "tag": "..." // optional } } ``` +Note that some fields are optional (they may not be set if the corresponding environment +variables are not defined). + Some CI servers expose usernames and passwords in the environment variable that is used to detect `git.remote`. For security reasons, this library removes the username and password from the `git.remote` field in the `CiEnvironment` struct. diff --git a/javascript/src/CiEnvironments.ts b/javascript/src/CiEnvironments.ts index 18249748..fb5960fc 100644 --- a/javascript/src/CiEnvironments.ts +++ b/javascript/src/CiEnvironments.ts @@ -22,7 +22,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: '${bamboo_planRepository_repositoryUrl}', revision: '${bamboo_planRepository_revision}', branch: '${bamboo_planRepository_branch}', - tag: null, }, }, { @@ -66,7 +65,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: '${CF_COMMIT_URL/(.*)\\/commit.+$/\\1}.git', revision: '${CF_REVISION}', branch: '${CF_BRANCH}', - tag: null, }, }, { @@ -77,7 +75,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: '${CI_PULL_REQUEST/(.*)\\/pull\\/\\d+/\\1.git}', revision: '${CI_COMMIT_ID}', branch: '${CI_BRANCH}', - tag: null, }, }, { @@ -110,7 +107,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: '${GO_SCM_*_PR_URL/(.*)\\/pull\\/\\d+/\\1.git}', revision: '${GO_REVISION}', branch: '${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}', - tag: null, }, }, { @@ -121,7 +117,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: '${GIT_URL}', revision: '${GIT_COMMIT}', branch: '${GIT_LOCAL_BRANCH}', - tag: null, }, }, { @@ -154,7 +149,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [ remote: 'https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}.git', revision: '${WERCKER_GIT_COMMIT}', branch: '${WERCKER_GIT_BRANCH}', - tag: null, }, }, ] diff --git a/javascript/src/detectCiEnvironment.ts b/javascript/src/detectCiEnvironment.ts index 53bd56d0..19c9ddcf 100644 --- a/javascript/src/detectCiEnvironment.ts +++ b/javascript/src/detectCiEnvironment.ts @@ -24,18 +24,18 @@ export function removeUserInfoFromUrl(value: string): string { } function detectGit(ciEnvironment: CiEnvironment, env: Env): Git | undefined { - const revision = evaluateVariableExpression(ciEnvironment.git.revision, env) + const revision = evaluateVariableExpression(ciEnvironment.git?.revision, env) if (!revision) { return undefined } - const remote = evaluateVariableExpression(ciEnvironment.git.remote, env) + const remote = evaluateVariableExpression(ciEnvironment.git?.remote, env) if (!remote) { return undefined } - const tag = evaluateVariableExpression(ciEnvironment.git.tag, env) - const branch = evaluateVariableExpression(ciEnvironment.git.branch, env) + const tag = evaluateVariableExpression(ciEnvironment.git?.tag, env) + const branch = evaluateVariableExpression(ciEnvironment.git?.branch, env) return { revision, @@ -47,12 +47,12 @@ function detectGit(ciEnvironment: CiEnvironment, env: Env): Git | undefined { function detect(ciEnvironment: CiEnvironment, env: Env): CiEnvironment | undefined { const url = evaluateVariableExpression(ciEnvironment.url, env) - const buildNumber = evaluateVariableExpression(ciEnvironment.buildNumber, env) if (url === undefined) { // The url is what consumers will use as the primary key for a build // If this cannot be determined, we return nothing. return undefined } + const buildNumber = evaluateVariableExpression(ciEnvironment.buildNumber, env) const git = detectGit(ciEnvironment, env) return { diff --git a/javascript/src/types.ts b/javascript/src/types.ts index 14f347f9..c2315d3f 100644 --- a/javascript/src/types.ts +++ b/javascript/src/types.ts @@ -1,7 +1,7 @@ export type CiEnvironment = { name: string url: string - buildNumber: string + buildNumber?: string git?: Git } diff --git a/javascript/test/removeUserInfoFromUrlTest.ts b/javascript/test/removeUserInfoFromUrlTest.ts index 5257935a..a1cdfcb1 100644 --- a/javascript/test/removeUserInfoFromUrlTest.ts +++ b/javascript/test/removeUserInfoFromUrlTest.ts @@ -3,16 +3,8 @@ import assert from 'assert' import { removeUserInfoFromUrl } from '../src/detectCiEnvironment.js' describe('removeUserInfoFromUrl', () => { - it('returns undefined for undefined', () => { - assert.strictEqual(removeUserInfoFromUrl(undefined), undefined) - }) - - it('returns null for null', () => { - assert.strictEqual(removeUserInfoFromUrl(null), null) - }) - it('returns empty string for empty string', () => { - assert.strictEqual(removeUserInfoFromUrl(null), null) + assert.strictEqual(removeUserInfoFromUrl(''), '') }) it('leaves the data intact when no sensitive information is detected', () => { diff --git a/javascript/tsconfig.json b/javascript/tsconfig.json index 16532e4f..36d16c69 100644 --- a/javascript/tsconfig.json +++ b/javascript/tsconfig.json @@ -17,7 +17,7 @@ "outDir": "dist", "downlevelIteration": true, "skipLibCheck": true, - "strictNullChecks": false, + "strictNullChecks": true, "experimentalDecorators": true, "noEmit": true } diff --git a/ruby/lib/cucumber/ci_environment/.gitignore b/ruby/lib/cucumber/ci_environment/.gitignore new file mode 100644 index 00000000..ab44ddb1 --- /dev/null +++ b/ruby/lib/cucumber/ci_environment/.gitignore @@ -0,0 +1 @@ +CiEnvironments.json diff --git a/ruby/lib/cucumber/ci_environment/CiEnvironments.json b/ruby/lib/cucumber/ci_environment/CiEnvironments.json deleted file mode 100644 index f9c7072e..00000000 --- a/ruby/lib/cucumber/ci_environment/CiEnvironments.json +++ /dev/null @@ -1,156 +0,0 @@ -[ - { - "name": "Azure Pipelines", - "url": "${BUILD_BUILDURI}", - "buildNumber": "${BUILD_BUILDNUMBER}", - "git": { - "remote": "${BUILD_REPOSITORY_URI}", - "revision": "${BUILD_SOURCEVERSION}", - "branch": "${BUILD_SOURCEBRANCH/refs\/heads\/(.*)/\\1}", - "tag": "${BUILD_SOURCEBRANCH/refs\/tags\/(.*)/\\1}" - } - }, - { - "name": "Bamboo", - "url": "${bamboo_buildResultsUrl}", - "buildNumber": "${bamboo_buildNumber}", - "git": { - "remote": "${bamboo_planRepository_repositoryUrl}", - "revision": "${bamboo_planRepository_revision}", - "branch": "${bamboo_planRepository_branch}", - "tag": null - } - }, - { - "name": "Buddy", - "url": "${BUDDY_EXECUTION_URL}", - "buildNumber": "${BUDDY_EXECUTION_ID}", - "git": { - "remote": "${BUDDY_SCM_URL}", - "revision": "${BUDDY_EXECUTION_REVISION}", - "branch": "${BUDDY_EXECUTION_BRANCH}", - "tag": "${BUDDY_EXECUTION_TAG}" - } - }, - { - "name": "Bitrise", - "url": "${BITRISE_BUILD_URL}", - "buildNumber": "${BITRISE_BUILD_NUMBER}", - "git": { - "remote": "${GIT_REPOSITORY_URL}", - "revision": "${BITRISE_GIT_COMMIT}", - "branch": "${BITRISE_GIT_BRANCH}", - "tag": "${BITRISE_GIT_TAG}" - } - }, - { - "name": "CircleCI", - "url": "${CIRCLE_BUILD_URL}", - "buildNumber": "${CIRCLE_BUILD_NUM}", - "git": { - "remote": "${CIRCLE_REPOSITORY_URL}", - "revision": "${CIRCLE_SHA1}", - "branch": "${CIRCLE_BRANCH}", - "tag": "${CIRCLE_TAG}" - } - }, - { - "name": "CodeFresh", - "url": "${CF_BUILD_URL}", - "buildNumber": "${CF_BUILD_ID}", - "git": { - "remote": "${CF_COMMIT_URL/(.*)\\/commit.+$/\\1}.git", - "revision": "${CF_REVISION}", - "branch": "${CF_BRANCH}", - "tag": null - } - }, - { - "name": "CodeShip", - "url": "${CI_BUILD_URL}", - "buildNumber": "${CI_BUILD_NUMBER}", - "git": { - "remote": "${CI_PULL_REQUEST/(.*)\\/pull\\/\\d+/\\1.git}", - "revision": "${CI_COMMIT_ID}", - "branch": "${CI_BRANCH}", - "tag": null - } - }, - { - "name": "GitHub Actions", - "url": "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}", - "buildNumber": "${GITHUB_RUN_ID}", - "git": { - "remote": "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git", - "revision": "${GITHUB_SHA}", - "branch": "${GITHUB_REF/refs\/heads\/(.*)/\\1}", - "tag": "${GITHUB_REF/refs\/tags\/(.*)/\\1}" - } - }, - { - "name": "GitLab", - "url": "${CI_JOB_URL}", - "buildNumber": "${CI_JOB_ID}", - "git": { - "remote": "${CI_REPOSITORY_URL}", - "revision": "${CI_COMMIT_SHA}", - "branch": "${CI_COMMIT_BRANCH}", - "tag": "${CI_COMMIT_TAG}" - } - }, - { - "name": "GoCD", - "url": "${GO_SERVER_URL}/pipelines/${GO_PIPELINE_NAME}/${GO_PIPELINE_COUNTER}/${GO_STAGE_NAME}/${GO_STAGE_COUNTER}", - "buildNumber": "${GO_PIPELINE_NAME}/${GO_PIPELINE_COUNTER}/${GO_STAGE_NAME}/${GO_STAGE_COUNTER}", - "git": { - "remote": "${GO_SCM_*_PR_URL/(.*)\\/pull\\/\\d+/\\1.git}", - "revision": "${GO_REVISION}", - "branch": "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}", - "tag": null - } - }, - { - "name": "Jenkins", - "url": "${BUILD_URL}", - "buildNumber": "${BUILD_NUMBER}", - "git": { - "remote": "${GIT_URL}", - "revision": "${GIT_COMMIT}", - "branch": "${GIT_LOCAL_BRANCH}", - "tag": null - } - }, - { - "name": "Semaphore", - "url": "${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}", - "buildNumber": "${SEMAPHORE_JOB_ID}", - "git": { - "remote": "${SEMAPHORE_GIT_URL}", - "revision": "${SEMAPHORE_GIT_SHA}", - "branch": "${SEMAPHORE_GIT_BRANCH}", - "tag": "${SEMAPHORE_GIT_TAG_NAME}" - } - }, - { - "name": "Travis CI", - "url": "${TRAVIS_BUILD_WEB_URL}", - "buildNumber": "${TRAVIS_JOB_NUMBER}", - "git": { - "remote": "https://github.com/${TRAVIS_REPO_SLUG}.git", - "revision": "${TRAVIS_COMMIT}", - "branch": "${TRAVIS_BRANCH}", - "tag": "${TRAVIS_TAG}" - } - }, - { - "name": "Wercker", - "url": "${WERCKER_RUN_URL}", - "buildNumber": "${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}", - "git": { - "remote": "https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}.git", - "revision": "${WERCKER_GIT_COMMIT}", - "branch": "${WERCKER_GIT_BRANCH}", - "tag": null - } - } -] From af8e8478dc69daa63f9fc4f3604bd99ec46166b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Tue, 4 Jan 2022 21:49:10 +0000 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0221b89..11dbde4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed +- [JavaScript] make `buildNumber` optional ([#51](https://github.com/cucumber/ci-environment/pull/51)) + ## [8.1.0] - 2022-01-02 ### Added - [JavaScript] - package as hybrid esm/commonjs module ([#47](https://github.com/cucumber/ci-environment/pull/47))