From 1e2aa8ba5c9ae2d20ab1a1e11b67ec7120503408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 09:47:07 +0000 Subject: [PATCH 01/14] NPM: Remove depencency on messages --- javascript/package-lock.json | 68 ------------------- javascript/package.json | 4 -- javascript/src/ciDict.ts | 14 ++++ .../{createMeta.ts => detectCiEnvironment.ts} | 58 ++++------------ javascript/src/index.ts | 4 +- javascript/src/printMeta.ts | 14 ---- javascript/src/types.ts | 19 +++--- javascript/test/acceptance/createMetaTest.ts | 12 ++-- javascript/test/createMetaTest.ts | 57 +++++++--------- javascript/test/removeUserInfoFromUrlTest.ts | 2 +- 10 files changed, 72 insertions(+), 180 deletions(-) rename javascript/src/{createMeta.ts => detectCiEnvironment.ts} (59%) delete mode 100644 javascript/src/printMeta.ts diff --git a/javascript/package-lock.json b/javascript/package-lock.json index b27de8e3..0b4bcbe6 100644 --- a/javascript/package-lock.json +++ b/javascript/package-lock.json @@ -8,9 +8,6 @@ "name": "@cucumber/create-meta", "version": "6.0.4", "license": "MIT", - "dependencies": { - "@cucumber/messages": "^17.1.1" - }, "devDependencies": { "@types/glob": "7.2.0", "@types/mocha": "9.0.0", @@ -52,17 +49,6 @@ "node": ">=12" } }, - "node_modules/@cucumber/messages": { - "version": "17.1.1", - "resolved": "https://registry.npmjs.org/@cucumber/messages/-/messages-17.1.1.tgz", - "integrity": "sha512-KQMn2Ag+1g1CXp/zKQ7LLqmuHjuQwuXw0N2u5SrDk8r72zPt36SxmDSJK7w6HiFTI+3p5ZuzwLi4S5jop3Tx4g==", - "dependencies": { - "@types/uuid": "8.3.1", - "class-transformer": "0.4.0", - "reflect-metadata": "0.1.13", - "uuid": "8.3.2" - } - }, "node_modules/@eslint/eslintrc": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.0.5.tgz", @@ -211,11 +197,6 @@ "integrity": "sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw==", "dev": true }, - "node_modules/@types/uuid": { - "version": "8.3.1", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.1.tgz", - "integrity": "sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==" - }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.6.0.tgz", @@ -687,11 +668,6 @@ "fsevents": "~2.3.2" } }, - "node_modules/class-transformer": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.4.0.tgz", - "integrity": "sha512-ETWD/H2TbWbKEi7m9N4Km5+cw1hNcqJSxlSYhsLsNjQzWWiZIYA1zafxpK9PwVfaZ6AqR5rrjPVUBGESm5tQUA==" - }, "node_modules/cliui": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", @@ -2611,11 +2587,6 @@ "node": ">=8.10.0" } }, - "node_modules/reflect-metadata": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", - "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" - }, "node_modules/regexpp": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.2.0.tgz", @@ -3064,14 +3035,6 @@ "punycode": "^2.1.0" } }, - "node_modules/uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==", - "bin": { - "uuid": "dist/bin/uuid" - } - }, "node_modules/v8-compile-cache": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz", @@ -3242,17 +3205,6 @@ "@cspotcode/source-map-consumer": "0.8.0" } }, - "@cucumber/messages": { - "version": "17.1.1", - "resolved": "https://registry.npmjs.org/@cucumber/messages/-/messages-17.1.1.tgz", - "integrity": "sha512-KQMn2Ag+1g1CXp/zKQ7LLqmuHjuQwuXw0N2u5SrDk8r72zPt36SxmDSJK7w6HiFTI+3p5ZuzwLi4S5jop3Tx4g==", - "requires": { - "@types/uuid": "8.3.1", - "class-transformer": "0.4.0", - "reflect-metadata": "0.1.13", - "uuid": "8.3.2" - } - }, "@eslint/eslintrc": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.0.5.tgz", @@ -3385,11 +3337,6 @@ "integrity": "sha512-KB0sixD67CeecHC33MYn+eYARkqTheIRNuu97y2XMjR7Wu3XibO1vaY6VBV6O/a89SPI81cEUIYT87UqUWlZNw==", "dev": true }, - "@types/uuid": { - "version": "8.3.1", - "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-8.3.1.tgz", - "integrity": "sha512-Y2mHTRAbqfFkpjldbkHGY8JIzRN6XqYRliG8/24FcHm2D2PwW24fl5xMRTVGdrb7iMrwCaIEbLWerGIkXuFWVg==" - }, "@typescript-eslint/eslint-plugin": { "version": "5.6.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.6.0.tgz", @@ -3702,11 +3649,6 @@ "readdirp": "~3.6.0" } }, - "class-transformer": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.4.0.tgz", - "integrity": "sha512-ETWD/H2TbWbKEi7m9N4Km5+cw1hNcqJSxlSYhsLsNjQzWWiZIYA1zafxpK9PwVfaZ6AqR5rrjPVUBGESm5tQUA==" - }, "cliui": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/cliui/-/cliui-7.0.4.tgz", @@ -5109,11 +5051,6 @@ "picomatch": "^2.2.1" } }, - "reflect-metadata": { - "version": "0.1.13", - "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", - "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" - }, "regexpp": { "version": "3.2.0", "resolved": "https://registry.npmjs.org/regexpp/-/regexpp-3.2.0.tgz", @@ -5411,11 +5348,6 @@ "punycode": "^2.1.0" } }, - "uuid": { - "version": "8.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", - "integrity": "sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==" - }, "v8-compile-cache": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz", diff --git a/javascript/package.json b/javascript/package.json index b31c32ad..930d8db4 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -8,7 +8,6 @@ "pretest": "npm run build-ci-dict", "test": "mocha", "prepublishOnly": "tsc --build tsconfig.build.json", - "print-meta": "ts-node --require source-map-support/register --require tsconfig-paths/register ./src/printMeta.ts", "eslint-fix": "eslint --ext ts,tsx --max-warnings 0 --fix src test", "eslint": "eslint --ext ts,tsx --max-warnings 0 src test", "build-ci-dict": "cat ciDict.ts.header > src/ciDict.ts && cat ../ciDict.json >> src/ciDict.ts && eslint --fix src/ciDict.ts" @@ -45,9 +44,6 @@ "tsconfig-paths": "3.12.0", "typescript": "4.5.2" }, - "dependencies": { - "@cucumber/messages": "^17.1.1" - }, "directories": { "test": "test" } diff --git a/javascript/src/ciDict.ts b/javascript/src/ciDict.ts index b7e92327..02c4d8eb 100644 --- a/javascript/src/ciDict.ts +++ b/javascript/src/ciDict.ts @@ -4,6 +4,7 @@ import { CiDict } from './types' export const ciDict: CiDict = { 'Azure Pipelines': { + name: 'Azure Pipelines', url: '${BUILD_BUILDURI}', buildNumber: '${BUILD_BUILDNUMBER}', git: { @@ -14,6 +15,7 @@ export const ciDict: CiDict = { }, }, Bamboo: { + name: 'Bamboo', url: '${bamboo_buildResultsUrl}', buildNumber: '${bamboo_buildNumber}', git: { @@ -24,6 +26,7 @@ export const ciDict: CiDict = { }, }, Buddy: { + name: 'Buddy', url: '${BUDDY_EXECUTION_URL}', buildNumber: '${BUDDY_EXECUTION_ID}', git: { @@ -34,6 +37,7 @@ export const ciDict: CiDict = { }, }, Bitrise: { + name: 'Bitrise', url: '${BITRISE_BUILD_URL}', buildNumber: '${BITRISE_BUILD_NUMBER}', git: { @@ -44,6 +48,7 @@ export const ciDict: CiDict = { }, }, CircleCI: { + name: 'CircleCI', url: '${CIRCLE_BUILD_URL}', buildNumber: '${CIRCLE_BUILD_NUM}', git: { @@ -54,6 +59,7 @@ export const ciDict: CiDict = { }, }, CodeFresh: { + name: 'CodeFresh', url: '${CF_BUILD_URL}', buildNumber: '${CF_BUILD_ID}', git: { @@ -64,6 +70,7 @@ export const ciDict: CiDict = { }, }, CodeShip: { + name: 'CodeShip', url: '${CI_BUILD_URL}', buildNumber: '${CI_BUILD_NUMBER}', git: { @@ -74,6 +81,7 @@ export const ciDict: CiDict = { }, }, 'GitHub Actions': { + name: 'GitHub Actions', url: '${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}', buildNumber: '${GITHUB_RUN_ID}', git: { @@ -84,6 +92,7 @@ export const ciDict: CiDict = { }, }, GitLab: { + name: 'GitLab', url: '${CI_JOB_URL}', buildNumber: '${CI_JOB_ID}', git: { @@ -94,6 +103,7 @@ export const ciDict: CiDict = { }, }, GoCD: { + 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: { @@ -104,6 +114,7 @@ export const ciDict: CiDict = { }, }, Jenkins: { + name: 'Jenkins', url: '${BUILD_URL}', buildNumber: '${BUILD_NUMBER}', git: { @@ -114,6 +125,7 @@ export const ciDict: CiDict = { }, }, Semaphore: { + name: 'Semaphore', url: '${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}', buildNumber: '${SEMAPHORE_JOB_ID}', git: { @@ -124,6 +136,7 @@ export const ciDict: CiDict = { }, }, 'Travis CI': { + name: 'Travis CI', url: '${TRAVIS_BUILD_WEB_URL}', buildNumber: '${TRAVIS_JOB_NUMBER}', git: { @@ -134,6 +147,7 @@ export const ciDict: CiDict = { }, }, Wercker: { + name: 'Wercker', url: '${WERCKER_RUN_URL}', buildNumber: '${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}', git: { diff --git a/javascript/src/createMeta.ts b/javascript/src/detectCiEnvironment.ts similarity index 59% rename from javascript/src/createMeta.ts rename to javascript/src/detectCiEnvironment.ts index 5b5fbff3..596e13b5 100644 --- a/javascript/src/createMeta.ts +++ b/javascript/src/detectCiEnvironment.ts @@ -1,50 +1,18 @@ -import * as messages from '@cucumber/messages' -import os from 'os' import { format as formatUrl, parse as parseUrl } from 'url' import { ciDict as defaultCiDict } from './ciDict' import evaluateVariableExpression from './evaluateVariableExpression' -import { CiDict, CiSystem, Env } from './types' +import { CiDict, CiEnvironment, Env, Git } from './types' -type GitInfo = { - remote: string - revision: string - branch: string - tag?: string -} - -export default function createMeta( - toolName: string, - toolVersion: string, - envDict: Env, - ciDict?: CiDict -): messages.Meta { +export default function detectCiEnvironment(env: Env, ciDict?: CiDict): CiEnvironment { if (ciDict === undefined) { ciDict = defaultCiDict } - return { - protocolVersion: messages.version, - implementation: { - name: toolName, - version: toolVersion, - }, - cpu: { - name: os.arch(), - }, - os: { - name: os.platform(), - version: os.release(), - }, - runtime: { - name: 'node.js', - version: process.versions.node, - }, - ci: detectCI(ciDict, envDict), - } + return detectCI(ciDict, env) } -export function detectCI(ciDict: CiDict, envDict: Env): messages.Ci | undefined { - const detected: messages.Ci[] = [] +export function detectCI(ciDict: CiDict, envDict: Env): CiEnvironment { + const detected: CiEnvironment[] = [] for (const [ciName, ciSystem] of Object.entries(ciDict)) { const ci = createCi(ciName, ciSystem, envDict) if (ci) { @@ -75,7 +43,11 @@ export function removeUserInfoFromUrl(value: string): string { return formatUrl(url) } -function createCi(ciName: string, ciSystem: CiSystem, envDict: Env): messages.Ci | undefined { +function createCi( + ciName: string, + ciSystem: CiEnvironment, + envDict: Env +): CiEnvironment | undefined { const url = evaluateVariableExpression(ciSystem.url, envDict) const buildNumber = evaluateVariableExpression(ciSystem.buildNumber, envDict) if (url === undefined) { @@ -84,16 +56,12 @@ function createCi(ciName: string, ciSystem: CiSystem, envDict: Env): messages.Ci return undefined } - const branch = evaluateVariableExpression(ciSystem.git.branch, envDict) const tag = evaluateVariableExpression(ciSystem.git.tag, envDict) - const git: GitInfo = { + const git: Git = { remote: removeUserInfoFromUrl(evaluateVariableExpression(ciSystem.git.remote, envDict)), revision: evaluateVariableExpression(ciSystem.git.revision, envDict), - branch: branch, - } - - if (tag) { - git['tag'] = tag + branch: evaluateVariableExpression(ciSystem.git.branch, envDict), + ...(tag ? { tag } : {}), } return { diff --git a/javascript/src/index.ts b/javascript/src/index.ts index af1626ef..828c56a8 100644 --- a/javascript/src/index.ts +++ b/javascript/src/index.ts @@ -1,3 +1,3 @@ -import createMeta from './createMeta' +import detectCiEnvironment from './detectCiEnvironment' -export default createMeta +export default detectCiEnvironment diff --git a/javascript/src/printMeta.ts b/javascript/src/printMeta.ts deleted file mode 100644 index 6232b759..00000000 --- a/javascript/src/printMeta.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { promises as fs } from 'fs' -import { Writable } from 'stream' - -import { ciDict } from './ciDict' -import { detectCI } from './createMeta' - -export async function main(envPath: string, stdout: Writable) { - const envData = await fs.readFile(envPath, 'utf-8') - const entries = envData.split('\n').map((line) => line.split('=')) - const env = Object.fromEntries(entries) - const ci = detectCI(ciDict, env) - stdout.write(JSON.stringify(ci, null, 2) + '\n') -} -main(process.argv[2], process.stdout).catch((err) => console.error(err.backtrace)) diff --git a/javascript/src/types.ts b/javascript/src/types.ts index 0cfd52c9..d656e03f 100644 --- a/javascript/src/types.ts +++ b/javascript/src/types.ts @@ -1,13 +1,16 @@ -export interface CiSystem { +export type CiEnvironment = { + name: string url: string buildNumber: string - git: { - remote: string | undefined - branch: string | undefined - revision: string | undefined - tag: string | undefined - } + git: Git } -export type CiDict = Record +export type Git = { + remote?: string + branch?: string + revision?: string + tag?: string +} + +export type CiDict = Record export type Env = Record diff --git a/javascript/test/acceptance/createMetaTest.ts b/javascript/test/acceptance/createMetaTest.ts index 4c1b34aa..e7b122e0 100644 --- a/javascript/test/acceptance/createMetaTest.ts +++ b/javascript/test/acceptance/createMetaTest.ts @@ -2,7 +2,7 @@ import assert from 'assert' import fs from 'fs' import path from 'path' -import CreateMeta from '../../src/createMeta' +import detectCiEnvironment from '../../src/detectCiEnvironment' import { Env } from '../../src/types' const TEST_DATA_DIR = '../testdata' @@ -15,17 +15,17 @@ describe('CreateMeta', () => { return } - const env_data = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}`, { encoding: 'utf8' }) - const entries = env_data.split(/\n/).map((line) => line.split(/=/)) + const envData = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}`, { encoding: 'utf8' }) + const entries = envData.split(/\n/).map((line) => line.split(/=/)) const env: Env = Object.fromEntries(entries) - const meta = CreateMeta('cucumber-something', '1.2.3', env) + const ciEnvironment = detectCiEnvironment(env) - const expected_json = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}.json`, { + const expectedJson = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}.json`, { encoding: 'utf8', }) it(`with ${path.basename(test_data_file, '.txt')}`, () => { - assert.deepStrictEqual(meta.ci, JSON.parse(expected_json)) + assert.deepStrictEqual(ciEnvironment, JSON.parse(expectedJson)) }) }) }) diff --git a/javascript/test/createMetaTest.ts b/javascript/test/createMetaTest.ts index f3e58d39..3a24b38d 100644 --- a/javascript/test/createMetaTest.ts +++ b/javascript/test/createMetaTest.ts @@ -1,16 +1,9 @@ -import * as messages from '@cucumber/messages' import assert from 'assert' -import createMeta from '../src/createMeta' +import detectCiEnvironment from '../src/detectCiEnvironment' +import { CiEnvironment } from '../src/types' describe('createMeta', () => { - it('defines the implementation product', () => { - const meta = createMeta('someTool', '1.2.3', {}, {}) - - assert.strictEqual(meta.implementation.name, 'someTool') - assert.strictEqual(meta.implementation.version, '1.2.3') - }) - it('detects CircleCI', () => { const envDict = { CIRCLE_BUILD_URL: 'the-url', @@ -21,8 +14,8 @@ describe('createMeta', () => { CIRCLE_BUILD_NUM: '234', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(envDict) + const expected: CiEnvironment = { name: 'CircleCI', url: 'the-url', buildNumber: '234', @@ -33,11 +26,11 @@ describe('createMeta', () => { tag: 'the-tag', }, } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) it('detects GitHub Actions', () => { - const envDict = { + const env = { GITHUB_SERVER_URL: 'https://github.com', GITHUB_REPOSITORY: 'cucumber/cucumber-ruby', GITHUB_RUN_ID: '140170388', @@ -45,8 +38,8 @@ describe('createMeta', () => { GITHUB_REF: 'refs/tags/the-tag', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(env) + const expected: CiEnvironment = { name: 'GitHub Actions', url: 'https://github.com/cucumber/cucumber-ruby/actions/runs/140170388', buildNumber: '140170388', @@ -57,11 +50,11 @@ describe('createMeta', () => { tag: 'the-tag', }, } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) it('detects GitHub Actions with custom base url', () => { - const envDict = { + const env = { GITHUB_SERVER_URL: 'https://github.company.com', GITHUB_REPOSITORY: 'cucumber/cucumber-ruby', GITHUB_RUN_ID: '140170388', @@ -69,8 +62,8 @@ describe('createMeta', () => { GITHUB_REF: 'refs/heads/the-branch', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(env) + const expected: CiEnvironment = { name: 'GitHub Actions', url: 'https://github.company.com/cucumber/cucumber-ruby/actions/runs/140170388', buildNumber: '140170388', @@ -80,11 +73,11 @@ describe('createMeta', () => { revision: 'the-revision', }, } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) it('post-processes git refs to branch', () => { - const envDict = { + const env = { BUILD_BUILDURI: 'the-url', BUILD_REPOSITORY_URI: 'the-remote', BUILD_SOURCEBRANCH: 'refs/heads/main', @@ -92,8 +85,8 @@ describe('createMeta', () => { BUILD_BUILDNUMBER: '456', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(env) + const expected: CiEnvironment = { name: 'Azure Pipelines', url: 'the-url', buildNumber: '456', @@ -103,11 +96,11 @@ describe('createMeta', () => { revision: 'the-revision', }, } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) it('post-processes git refs to tag', () => { - const envDict = { + const env = { BUILD_BUILDURI: 'the-url', BUILD_REPOSITORY_URI: 'the-remote', BUILD_SOURCEBRANCH: 'refs/tags/v1.2.3', @@ -115,8 +108,8 @@ describe('createMeta', () => { BUILD_BUILDNUMBER: '456', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(env) + const expected: CiEnvironment = { name: 'Azure Pipelines', url: 'the-url', buildNumber: '456', @@ -127,11 +120,11 @@ describe('createMeta', () => { tag: 'v1.2.3', }, } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) it('extracts build number from url', () => { - const envDict = { + const env = { WERCKER_GIT_BRANCH: 'main', WERCKER_GIT_COMMIT: '057f8fe233b17629af084064c2a7b8d1dbb795ad', WERCKER_GIT_DOMAIN: 'github.com', @@ -140,8 +133,8 @@ describe('createMeta', () => { WERCKER_RUN_URL: 'https://cihost.com/path/to/build/629af084064c2', } - const meta = createMeta('someTool', '1.2.3', envDict) - const ci: messages.Ci = { + const ciEnvironment = detectCiEnvironment(env) + const expected: CiEnvironment = { git: { branch: 'main', remote: 'https://github.com/cucumber-ltd/shouty.rb.git', @@ -151,6 +144,6 @@ describe('createMeta', () => { url: 'https://cihost.com/path/to/build/629af084064c2', buildNumber: '629af084064c2', } - assert.deepStrictEqual(meta.ci, ci) + assert.deepStrictEqual(ciEnvironment, expected) }) }) diff --git a/javascript/test/removeUserInfoFromUrlTest.ts b/javascript/test/removeUserInfoFromUrlTest.ts index b06a16f1..03a51442 100644 --- a/javascript/test/removeUserInfoFromUrlTest.ts +++ b/javascript/test/removeUserInfoFromUrlTest.ts @@ -1,6 +1,6 @@ import assert from 'assert' -import { removeUserInfoFromUrl } from '../src/createMeta' +import { removeUserInfoFromUrl } from '../src/detectCiEnvironment' describe('removeUserInfoFromUrl', () => { it('returns undefined for undefined', () => { From 3119a9e92136bfddf94cf6595e908694f78beebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 09:57:54 +0000 Subject: [PATCH 02/14] Simplify tests --- ciDict.json | 14 ++ javascript/bin/print-meta | 3 - javascript/package-lock.json | 98 ++++++------ javascript/package.json | 3 +- javascript/test/acceptance/createMetaTest.ts | 31 ---- javascript/test/acceptanceTest.ts | 23 +++ javascript/test/createMetaTest.ts | 149 ------------------- 7 files changed, 81 insertions(+), 240 deletions(-) delete mode 100755 javascript/bin/print-meta delete mode 100644 javascript/test/acceptance/createMetaTest.ts create mode 100644 javascript/test/acceptanceTest.ts delete mode 100644 javascript/test/createMetaTest.ts diff --git a/ciDict.json b/ciDict.json index 4cf94ba4..9d7d27c2 100644 --- a/ciDict.json +++ b/ciDict.json @@ -1,5 +1,6 @@ { "Azure Pipelines": { + "name": "Azure Pipelines", "url": "${BUILD_BUILDURI}", "buildNumber": "${BUILD_BUILDNUMBER}", "git": { @@ -10,6 +11,7 @@ } }, "Bamboo": { + "name": "Bamboo", "url": "${bamboo_buildResultsUrl}", "buildNumber": "${bamboo_buildNumber}", "git": { @@ -20,6 +22,7 @@ } }, "Buddy": { + "name": "Buddy", "url": "${BUDDY_EXECUTION_URL}", "buildNumber": "${BUDDY_EXECUTION_ID}", "git": { @@ -30,6 +33,7 @@ } }, "Bitrise": { + "name": "Bitrise", "url": "${BITRISE_BUILD_URL}", "buildNumber": "${BITRISE_BUILD_NUMBER}", "git": { @@ -40,6 +44,7 @@ } }, "CircleCI": { + "name": "CircleCI", "url": "${CIRCLE_BUILD_URL}", "buildNumber": "${CIRCLE_BUILD_NUM}", "git": { @@ -50,6 +55,7 @@ } }, "CodeFresh": { + "name": "CodeFresh", "url": "${CF_BUILD_URL}", "buildNumber": "${CF_BUILD_ID}", "git": { @@ -60,6 +66,7 @@ } }, "CodeShip": { + "name": "CodeShip", "url": "${CI_BUILD_URL}", "buildNumber": "${CI_BUILD_NUMBER}", "git": { @@ -70,6 +77,7 @@ } }, "GitHub Actions": { + "name": "GitHub Actions", "url": "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}", "buildNumber": "${GITHUB_RUN_ID}", "git": { @@ -80,6 +88,7 @@ } }, "GitLab": { + "name": "GitLab", "url": "${CI_JOB_URL}", "buildNumber": "${CI_JOB_ID}", "git": { @@ -90,6 +99,7 @@ } }, "GoCD": { + "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": { @@ -100,6 +110,7 @@ } }, "Jenkins": { + "name": "Jenkins", "url": "${BUILD_URL}", "buildNumber": "${BUILD_NUMBER}", "git": { @@ -110,6 +121,7 @@ } }, "Semaphore": { + "name": "Semaphore", "url": "${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}", "buildNumber": "${SEMAPHORE_JOB_ID}", "git": { @@ -120,6 +132,7 @@ } }, "Travis CI": { + "name": "Travis CI", "url": "${TRAVIS_BUILD_WEB_URL}", "buildNumber": "${TRAVIS_JOB_NUMBER}", "git": { @@ -130,6 +143,7 @@ } }, "Wercker": { + "name": "Wercker", "url": "${WERCKER_RUN_URL}", "buildNumber": "${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}", "git": { diff --git a/javascript/bin/print-meta b/javascript/bin/print-meta deleted file mode 100755 index 0057d24a..00000000 --- a/javascript/bin/print-meta +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node -require('source-map-support').install() -require('../dist/src/printMeta') diff --git a/javascript/package-lock.json b/javascript/package-lock.json index 0b4bcbe6..276a33ce 100644 --- a/javascript/package-lock.json +++ b/javascript/package-lock.json @@ -20,11 +20,10 @@ "eslint-plugin-node": "11.1.0", "eslint-plugin-prettier": "4.0.0", "eslint-plugin-simple-import-sort": "7.0.0", + "glob": "7.2.0", "mocha": "9.1.3", "prettier": "2.5.1", - "source-map-support": "0.5.21", "ts-node": "10.4.0", - "tsconfig-paths": "3.12.0", "typescript": "4.5.2" } }, @@ -579,12 +578,6 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, - "node_modules/buffer-from": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true - }, "node_modules/call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", @@ -1577,9 +1570,9 @@ } }, "node_modules/glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", "dev": true, "dependencies": { "fs.realpath": "^1.0.0", @@ -2208,6 +2201,26 @@ "url": "https://opencollective.com/mochajs" } }, + "node_modules/mocha/node_modules/glob": { + "version": "7.1.7", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", + "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", + "dev": true, + "dependencies": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + }, + "engines": { + "node": "*" + }, + "funding": { + "url": "https://github.com/sponsors/isaacs" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -2766,25 +2779,6 @@ "node": ">=8" } }, - "node_modules/source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true, - "engines": { - "node": ">=0.10.0" - } - }, - "node_modules/source-map-support": { - "version": "0.5.21", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", - "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", - "dev": true, - "dependencies": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } - }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -3584,12 +3578,6 @@ "integrity": "sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw==", "dev": true }, - "buffer-from": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.2.tgz", - "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", - "dev": true - }, "call-bind": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz", @@ -4349,9 +4337,9 @@ } }, "glob": { - "version": "7.1.7", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", - "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.0.tgz", + "integrity": "sha512-lmLf6gtyrPq8tTjSmrO94wBeQbFR3HbLHbuyD69wuyQkImp2hWqMGB47OX65FBkPffO641IP9jWa1z4ivqG26Q==", "dev": true, "requires": { "fs.realpath": "^1.0.0", @@ -4787,6 +4775,22 @@ "yargs": "16.2.0", "yargs-parser": "20.2.4", "yargs-unparser": "2.0.0" + }, + "dependencies": { + "glob": { + "version": "7.1.7", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.7.tgz", + "integrity": "sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + } } }, "ms": { @@ -5159,22 +5163,6 @@ "integrity": "sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==", "dev": true }, - "source-map": { - "version": "0.6.1", - "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", - "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==", - "dev": true - }, - "source-map-support": { - "version": "0.5.21", - "resolved": "https://registry.npmjs.org/source-map-support/-/source-map-support-0.5.21.tgz", - "integrity": "sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==", - "dev": true, - "requires": { - "buffer-from": "^1.0.0", - "source-map": "^0.6.0" - } - }, "string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", diff --git a/javascript/package.json b/javascript/package.json index 930d8db4..afe9f368 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -37,11 +37,10 @@ "eslint-plugin-node": "11.1.0", "eslint-plugin-prettier": "4.0.0", "eslint-plugin-simple-import-sort": "7.0.0", + "glob": "7.2.0", "mocha": "9.1.3", "prettier": "2.5.1", - "source-map-support": "0.5.21", "ts-node": "10.4.0", - "tsconfig-paths": "3.12.0", "typescript": "4.5.2" }, "directories": { diff --git a/javascript/test/acceptance/createMetaTest.ts b/javascript/test/acceptance/createMetaTest.ts deleted file mode 100644 index e7b122e0..00000000 --- a/javascript/test/acceptance/createMetaTest.ts +++ /dev/null @@ -1,31 +0,0 @@ -import assert from 'assert' -import fs from 'fs' -import path from 'path' - -import detectCiEnvironment from '../../src/detectCiEnvironment' -import { Env } from '../../src/types' - -const TEST_DATA_DIR = '../testdata' - -describe('CreateMeta', () => { - const test_data_files = fs.readdirSync(TEST_DATA_DIR) - - test_data_files.forEach((test_data_file) => { - if (path.extname(test_data_file) !== '.txt') { - return - } - - const envData = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}`, { encoding: 'utf8' }) - const entries = envData.split(/\n/).map((line) => line.split(/=/)) - const env: Env = Object.fromEntries(entries) - const ciEnvironment = detectCiEnvironment(env) - - const expectedJson = fs.readFileSync(`${TEST_DATA_DIR}/${test_data_file}.json`, { - encoding: 'utf8', - }) - - it(`with ${path.basename(test_data_file, '.txt')}`, () => { - assert.deepStrictEqual(ciEnvironment, JSON.parse(expectedJson)) - }) - }) -}) diff --git a/javascript/test/acceptanceTest.ts b/javascript/test/acceptanceTest.ts new file mode 100644 index 00000000..a82d079b --- /dev/null +++ b/javascript/test/acceptanceTest.ts @@ -0,0 +1,23 @@ +import assert from 'assert' +import fs from 'fs' +import glob from 'glob' +import path from 'path' + +import detectCiEnvironment from '../src/detectCiEnvironment' +import { Env } from '../src/types' + +describe('detectCiEnvironment', () => { + for (const txt of glob.sync(`${__dirname}/../../testdata/*.txt`)) { + it(`detects ${path.basename(txt, '.txt')}`, () => { + const envData = fs.readFileSync(txt, { encoding: 'utf8' }) + const entries = envData.split(/\n/).map((line) => line.split(/=/)) + const env: Env = Object.fromEntries(entries) + const ciEnvironment = detectCiEnvironment(env) + + const expectedJson = fs.readFileSync(`${txt}.json`, { + encoding: 'utf8', + }) + assert.deepStrictEqual(ciEnvironment, JSON.parse(expectedJson)) + }) + } +}) diff --git a/javascript/test/createMetaTest.ts b/javascript/test/createMetaTest.ts deleted file mode 100644 index 3a24b38d..00000000 --- a/javascript/test/createMetaTest.ts +++ /dev/null @@ -1,149 +0,0 @@ -import assert from 'assert' - -import detectCiEnvironment from '../src/detectCiEnvironment' -import { CiEnvironment } from '../src/types' - -describe('createMeta', () => { - it('detects CircleCI', () => { - const envDict = { - CIRCLE_BUILD_URL: 'the-url', - CIRCLE_REPOSITORY_URL: 'the-remote', - CIRCLE_BRANCH: 'the-branch', - CIRCLE_SHA1: 'the-revision', - CIRCLE_TAG: 'the-tag', - CIRCLE_BUILD_NUM: '234', - } - - const ciEnvironment = detectCiEnvironment(envDict) - const expected: CiEnvironment = { - name: 'CircleCI', - url: 'the-url', - buildNumber: '234', - git: { - remote: 'the-remote', - branch: 'the-branch', - revision: 'the-revision', - tag: 'the-tag', - }, - } - assert.deepStrictEqual(ciEnvironment, expected) - }) - - it('detects GitHub Actions', () => { - const env = { - GITHUB_SERVER_URL: 'https://github.com', - GITHUB_REPOSITORY: 'cucumber/cucumber-ruby', - GITHUB_RUN_ID: '140170388', - GITHUB_SHA: 'the-revision', - GITHUB_REF: 'refs/tags/the-tag', - } - - const ciEnvironment = detectCiEnvironment(env) - const expected: CiEnvironment = { - name: 'GitHub Actions', - url: 'https://github.com/cucumber/cucumber-ruby/actions/runs/140170388', - buildNumber: '140170388', - git: { - remote: 'https://github.com/cucumber/cucumber-ruby.git', - branch: undefined, - revision: 'the-revision', - tag: 'the-tag', - }, - } - assert.deepStrictEqual(ciEnvironment, expected) - }) - - it('detects GitHub Actions with custom base url', () => { - const env = { - GITHUB_SERVER_URL: 'https://github.company.com', - GITHUB_REPOSITORY: 'cucumber/cucumber-ruby', - GITHUB_RUN_ID: '140170388', - GITHUB_SHA: 'the-revision', - GITHUB_REF: 'refs/heads/the-branch', - } - - const ciEnvironment = detectCiEnvironment(env) - const expected: CiEnvironment = { - name: 'GitHub Actions', - url: 'https://github.company.com/cucumber/cucumber-ruby/actions/runs/140170388', - buildNumber: '140170388', - git: { - remote: 'https://github.company.com/cucumber/cucumber-ruby.git', - branch: 'the-branch', - revision: 'the-revision', - }, - } - assert.deepStrictEqual(ciEnvironment, expected) - }) - - it('post-processes git refs to branch', () => { - const env = { - BUILD_BUILDURI: 'the-url', - BUILD_REPOSITORY_URI: 'the-remote', - BUILD_SOURCEBRANCH: 'refs/heads/main', - BUILD_SOURCEVERSION: 'the-revision', - BUILD_BUILDNUMBER: '456', - } - - const ciEnvironment = detectCiEnvironment(env) - const expected: CiEnvironment = { - name: 'Azure Pipelines', - url: 'the-url', - buildNumber: '456', - git: { - remote: 'the-remote', - branch: 'main', - revision: 'the-revision', - }, - } - assert.deepStrictEqual(ciEnvironment, expected) - }) - - it('post-processes git refs to tag', () => { - const env = { - BUILD_BUILDURI: 'the-url', - BUILD_REPOSITORY_URI: 'the-remote', - BUILD_SOURCEBRANCH: 'refs/tags/v1.2.3', - BUILD_SOURCEVERSION: 'the-revision', - BUILD_BUILDNUMBER: '456', - } - - const ciEnvironment = detectCiEnvironment(env) - const expected: CiEnvironment = { - name: 'Azure Pipelines', - url: 'the-url', - buildNumber: '456', - git: { - remote: 'the-remote', - branch: undefined, - revision: 'the-revision', - tag: 'v1.2.3', - }, - } - assert.deepStrictEqual(ciEnvironment, expected) - }) - - it('extracts build number from url', () => { - const env = { - WERCKER_GIT_BRANCH: 'main', - WERCKER_GIT_COMMIT: '057f8fe233b17629af084064c2a7b8d1dbb795ad', - WERCKER_GIT_DOMAIN: 'github.com', - WERCKER_GIT_OWNER: 'cucumber-ltd', - WERCKER_GIT_REPOSITORY: 'shouty.rb', - WERCKER_RUN_URL: 'https://cihost.com/path/to/build/629af084064c2', - } - - const ciEnvironment = detectCiEnvironment(env) - const expected: CiEnvironment = { - git: { - branch: 'main', - remote: 'https://github.com/cucumber-ltd/shouty.rb.git', - revision: '057f8fe233b17629af084064c2a7b8d1dbb795ad', - }, - name: 'Wercker', - url: 'https://cihost.com/path/to/build/629af084064c2', - buildNumber: '629af084064c2', - } - assert.deepStrictEqual(ciEnvironment, expected) - }) -}) From 0b9b9ffa15dc3f1df866b11cd95a84dac4094e5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 10:07:26 +0000 Subject: [PATCH 03/14] Replace deprecated url api with w3c one --- javascript/src/detectCiEnvironment.ts | 14 ++++++++------ javascript/test/removeUserInfoFromUrlTest.ts | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/javascript/src/detectCiEnvironment.ts b/javascript/src/detectCiEnvironment.ts index 596e13b5..7fd27677 100644 --- a/javascript/src/detectCiEnvironment.ts +++ b/javascript/src/detectCiEnvironment.ts @@ -1,5 +1,3 @@ -import { format as formatUrl, parse as parseUrl } from 'url' - import { ciDict as defaultCiDict } from './ciDict' import evaluateVariableExpression from './evaluateVariableExpression' import { CiDict, CiEnvironment, Env, Git } from './types' @@ -37,10 +35,14 @@ export function detectCI(ciDict: CiDict, envDict: Env): CiEnvironment { export function removeUserInfoFromUrl(value: string): string { if (!value) return value - const url = parseUrl(value) - if (url.auth === null) return value - url.auth = null - return formatUrl(url) + try { + const url = new URL(value) + url.password = '' + url.username = '' + return url.toString() + } catch (ignore) { + return value + } } function createCi( diff --git a/javascript/test/removeUserInfoFromUrlTest.ts b/javascript/test/removeUserInfoFromUrlTest.ts index 03a51442..a4e9714e 100644 --- a/javascript/test/removeUserInfoFromUrlTest.ts +++ b/javascript/test/removeUserInfoFromUrlTest.ts @@ -29,7 +29,7 @@ describe('removeUserInfoFromUrl', () => { it('removes credentials when found', () => { assert.strictEqual( - removeUserInfoFromUrl('http://login@example.com/git/repo.git'), + removeUserInfoFromUrl('http://aslak@example.com/git/repo.git'), 'http://example.com/git/repo.git' ) }) From 0ae3503bb3e81c0003587878f05b0cdf2f7fa5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 10:22:23 +0000 Subject: [PATCH 04/14] Use an array to define ci environments --- ciDict.json => CiEnvironments.json | 32 +++++----- javascript/CiEnvironments.ts.header | 5 ++ javascript/ciDict.ts.header | 5 -- javascript/package.json | 4 +- .../src/{ciDict.ts => CiEnvironments.ts} | 36 +++++------ javascript/src/detectCiEnvironment.ts | 59 ++++++------------- javascript/src/types.ts | 4 +- 7 files changed, 62 insertions(+), 83 deletions(-) rename ciDict.json => CiEnvironments.json (94%) create mode 100644 javascript/CiEnvironments.ts.header delete mode 100644 javascript/ciDict.ts.header rename javascript/src/{ciDict.ts => CiEnvironments.ts} (92%) diff --git a/ciDict.json b/CiEnvironments.json similarity index 94% rename from ciDict.json rename to CiEnvironments.json index 9d7d27c2..f9c7072e 100644 --- a/ciDict.json +++ b/CiEnvironments.json @@ -1,5 +1,5 @@ -{ - "Azure Pipelines": { +[ + { "name": "Azure Pipelines", "url": "${BUILD_BUILDURI}", "buildNumber": "${BUILD_BUILDNUMBER}", @@ -10,7 +10,7 @@ "tag": "${BUILD_SOURCEBRANCH/refs\/tags\/(.*)/\\1}" } }, - "Bamboo": { + { "name": "Bamboo", "url": "${bamboo_buildResultsUrl}", "buildNumber": "${bamboo_buildNumber}", @@ -21,7 +21,7 @@ "tag": null } }, - "Buddy": { + { "name": "Buddy", "url": "${BUDDY_EXECUTION_URL}", "buildNumber": "${BUDDY_EXECUTION_ID}", @@ -32,7 +32,7 @@ "tag": "${BUDDY_EXECUTION_TAG}" } }, - "Bitrise": { + { "name": "Bitrise", "url": "${BITRISE_BUILD_URL}", "buildNumber": "${BITRISE_BUILD_NUMBER}", @@ -43,7 +43,7 @@ "tag": "${BITRISE_GIT_TAG}" } }, - "CircleCI": { + { "name": "CircleCI", "url": "${CIRCLE_BUILD_URL}", "buildNumber": "${CIRCLE_BUILD_NUM}", @@ -54,7 +54,7 @@ "tag": "${CIRCLE_TAG}" } }, - "CodeFresh": { + { "name": "CodeFresh", "url": "${CF_BUILD_URL}", "buildNumber": "${CF_BUILD_ID}", @@ -65,7 +65,7 @@ "tag": null } }, - "CodeShip": { + { "name": "CodeShip", "url": "${CI_BUILD_URL}", "buildNumber": "${CI_BUILD_NUMBER}", @@ -76,7 +76,7 @@ "tag": null } }, - "GitHub Actions": { + { "name": "GitHub Actions", "url": "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}", "buildNumber": "${GITHUB_RUN_ID}", @@ -87,7 +87,7 @@ "tag": "${GITHUB_REF/refs\/tags\/(.*)/\\1}" } }, - "GitLab": { + { "name": "GitLab", "url": "${CI_JOB_URL}", "buildNumber": "${CI_JOB_ID}", @@ -98,7 +98,7 @@ "tag": "${CI_COMMIT_TAG}" } }, - "GoCD": { + { "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}", @@ -109,7 +109,7 @@ "tag": null } }, - "Jenkins": { + { "name": "Jenkins", "url": "${BUILD_URL}", "buildNumber": "${BUILD_NUMBER}", @@ -120,7 +120,7 @@ "tag": null } }, - "Semaphore": { + { "name": "Semaphore", "url": "${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}", "buildNumber": "${SEMAPHORE_JOB_ID}", @@ -131,7 +131,7 @@ "tag": "${SEMAPHORE_GIT_TAG_NAME}" } }, - "Travis CI": { + { "name": "Travis CI", "url": "${TRAVIS_BUILD_WEB_URL}", "buildNumber": "${TRAVIS_JOB_NUMBER}", @@ -142,7 +142,7 @@ "tag": "${TRAVIS_TAG}" } }, - "Wercker": { + { "name": "Wercker", "url": "${WERCKER_RUN_URL}", "buildNumber": "${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}", @@ -153,4 +153,4 @@ "tag": null } } -} +] diff --git a/javascript/CiEnvironments.ts.header b/javascript/CiEnvironments.ts.header new file mode 100644 index 00000000..c8922d2d --- /dev/null +++ b/javascript/CiEnvironments.ts.header @@ -0,0 +1,5 @@ +// This file is auto-generated using npm run build-ci-environments + +import { CiEnvironment } from './types' + +export const CiEnvironments: readonly CiEnvironment[] = diff --git a/javascript/ciDict.ts.header b/javascript/ciDict.ts.header deleted file mode 100644 index 8737db84..00000000 --- a/javascript/ciDict.ts.header +++ /dev/null @@ -1,5 +0,0 @@ -// This file is auto-generated using npm run build-ci-dict - -import { CiDict } from './types' - -export const ciDict: CiDict = \ No newline at end of file diff --git a/javascript/package.json b/javascript/package.json index afe9f368..f004fa32 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -5,12 +5,12 @@ "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "scripts": { - "pretest": "npm run build-ci-dict", + "pretest": "npm run build-ci-environments", "test": "mocha", "prepublishOnly": "tsc --build tsconfig.build.json", "eslint-fix": "eslint --ext ts,tsx --max-warnings 0 --fix src test", "eslint": "eslint --ext ts,tsx --max-warnings 0 src test", - "build-ci-dict": "cat ciDict.ts.header > src/ciDict.ts && cat ../ciDict.json >> src/ciDict.ts && eslint --fix src/ciDict.ts" + "build-ci-environments": "cat CiEnvironments.ts.header > src/CiEnvironments.ts && cat ../CiEnvironments.json >> src/CiEnvironments.ts && eslint --fix src/CiEnvironments.ts" }, "repository": { "type": "git", diff --git a/javascript/src/ciDict.ts b/javascript/src/CiEnvironments.ts similarity index 92% rename from javascript/src/ciDict.ts rename to javascript/src/CiEnvironments.ts index 02c4d8eb..8224da61 100644 --- a/javascript/src/ciDict.ts +++ b/javascript/src/CiEnvironments.ts @@ -1,9 +1,9 @@ -// This file is auto-generated using npm run build-ci-dict +// This file is auto-generated using npm run build-ci-environments -import { CiDict } from './types' +import { CiEnvironment } from './types' -export const ciDict: CiDict = { - 'Azure Pipelines': { +export const CiEnvironments: readonly CiEnvironment[] = [ + { name: 'Azure Pipelines', url: '${BUILD_BUILDURI}', buildNumber: '${BUILD_BUILDNUMBER}', @@ -14,7 +14,7 @@ export const ciDict: CiDict = { tag: '${BUILD_SOURCEBRANCH/refs/tags/(.*)/\\1}', }, }, - Bamboo: { + { name: 'Bamboo', url: '${bamboo_buildResultsUrl}', buildNumber: '${bamboo_buildNumber}', @@ -25,7 +25,7 @@ export const ciDict: CiDict = { tag: null, }, }, - Buddy: { + { name: 'Buddy', url: '${BUDDY_EXECUTION_URL}', buildNumber: '${BUDDY_EXECUTION_ID}', @@ -36,7 +36,7 @@ export const ciDict: CiDict = { tag: '${BUDDY_EXECUTION_TAG}', }, }, - Bitrise: { + { name: 'Bitrise', url: '${BITRISE_BUILD_URL}', buildNumber: '${BITRISE_BUILD_NUMBER}', @@ -47,7 +47,7 @@ export const ciDict: CiDict = { tag: '${BITRISE_GIT_TAG}', }, }, - CircleCI: { + { name: 'CircleCI', url: '${CIRCLE_BUILD_URL}', buildNumber: '${CIRCLE_BUILD_NUM}', @@ -58,7 +58,7 @@ export const ciDict: CiDict = { tag: '${CIRCLE_TAG}', }, }, - CodeFresh: { + { name: 'CodeFresh', url: '${CF_BUILD_URL}', buildNumber: '${CF_BUILD_ID}', @@ -69,7 +69,7 @@ export const ciDict: CiDict = { tag: null, }, }, - CodeShip: { + { name: 'CodeShip', url: '${CI_BUILD_URL}', buildNumber: '${CI_BUILD_NUMBER}', @@ -80,7 +80,7 @@ export const ciDict: CiDict = { tag: null, }, }, - 'GitHub Actions': { + { name: 'GitHub Actions', url: '${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}', buildNumber: '${GITHUB_RUN_ID}', @@ -91,7 +91,7 @@ export const ciDict: CiDict = { tag: '${GITHUB_REF/refs/tags/(.*)/\\1}', }, }, - GitLab: { + { name: 'GitLab', url: '${CI_JOB_URL}', buildNumber: '${CI_JOB_ID}', @@ -102,7 +102,7 @@ export const ciDict: CiDict = { tag: '${CI_COMMIT_TAG}', }, }, - GoCD: { + { 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}', @@ -113,7 +113,7 @@ export const ciDict: CiDict = { tag: null, }, }, - Jenkins: { + { name: 'Jenkins', url: '${BUILD_URL}', buildNumber: '${BUILD_NUMBER}', @@ -124,7 +124,7 @@ export const ciDict: CiDict = { tag: null, }, }, - Semaphore: { + { name: 'Semaphore', url: '${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}', buildNumber: '${SEMAPHORE_JOB_ID}', @@ -135,7 +135,7 @@ export const ciDict: CiDict = { tag: '${SEMAPHORE_GIT_TAG_NAME}', }, }, - 'Travis CI': { + { name: 'Travis CI', url: '${TRAVIS_BUILD_WEB_URL}', buildNumber: '${TRAVIS_JOB_NUMBER}', @@ -146,7 +146,7 @@ export const ciDict: CiDict = { tag: '${TRAVIS_TAG}', }, }, - Wercker: { + { name: 'Wercker', url: '${WERCKER_RUN_URL}', buildNumber: '${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}', @@ -157,4 +157,4 @@ export const ciDict: CiDict = { tag: null, }, }, -} +] diff --git a/javascript/src/detectCiEnvironment.ts b/javascript/src/detectCiEnvironment.ts index 7fd27677..e8103ee2 100644 --- a/javascript/src/detectCiEnvironment.ts +++ b/javascript/src/detectCiEnvironment.ts @@ -1,36 +1,17 @@ -import { ciDict as defaultCiDict } from './ciDict' +import { CiEnvironments } from './CiEnvironments' import evaluateVariableExpression from './evaluateVariableExpression' -import { CiDict, CiEnvironment, Env, Git } from './types' +import { CiEnvironment, Env, Git } from './types' -export default function detectCiEnvironment(env: Env, ciDict?: CiDict): CiEnvironment { - if (ciDict === undefined) { - ciDict = defaultCiDict - } - return detectCI(ciDict, env) -} - -export function detectCI(ciDict: CiDict, envDict: Env): CiEnvironment { - const detected: CiEnvironment[] = [] - for (const [ciName, ciSystem] of Object.entries(ciDict)) { - const ci = createCi(ciName, ciSystem, envDict) - if (ci) { - detected.push(ci) +export default function detectCiEnvironment( + env: Env, + ciEnvironments = CiEnvironments +): CiEnvironment | undefined { + for (const ciEnvironment of ciEnvironments) { + const detected = detect(ciEnvironment, env) + if (detected) { + return detected } } - if (detected.length !== 1) { - return undefined - } - if (detected.length > 1) { - console.error( - `@cucumber/create-meta WARNING: Detected more than one CI: ${JSON.stringify( - detected, - null, - 2 - )}` - ) - console.error('Using the first one.') - } - return detected[0] } export function removeUserInfoFromUrl(value: string): string { @@ -45,29 +26,25 @@ export function removeUserInfoFromUrl(value: string): string { } } -function createCi( - ciName: string, - ciSystem: CiEnvironment, - envDict: Env -): CiEnvironment | undefined { - const url = evaluateVariableExpression(ciSystem.url, envDict) - const buildNumber = evaluateVariableExpression(ciSystem.buildNumber, envDict) +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 tag = evaluateVariableExpression(ciSystem.git.tag, envDict) + const tag = evaluateVariableExpression(ciEnvironment.git.tag, env) const git: Git = { - remote: removeUserInfoFromUrl(evaluateVariableExpression(ciSystem.git.remote, envDict)), - revision: evaluateVariableExpression(ciSystem.git.revision, envDict), - branch: evaluateVariableExpression(ciSystem.git.branch, envDict), + remote: removeUserInfoFromUrl(evaluateVariableExpression(ciEnvironment.git.remote, env)), + revision: evaluateVariableExpression(ciEnvironment.git.revision, env), + branch: evaluateVariableExpression(ciEnvironment.git.branch, env), ...(tag ? { tag } : {}), } return { - name: ciName, + name: ciEnvironment.name, url, buildNumber, git: git, diff --git a/javascript/src/types.ts b/javascript/src/types.ts index d656e03f..8ec63e9a 100644 --- a/javascript/src/types.ts +++ b/javascript/src/types.ts @@ -12,5 +12,7 @@ export type Git = { tag?: string } -export type CiDict = Record +export type CiEnvironments = { + ciEnvironments: readonly CiEnvironment[] +} export type Env = Record From 1d19ca0938e7a2f1e5213ccd37042678ca176581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 11:18:27 +0000 Subject: [PATCH 05/14] Refactor Java implementation --- java/pom.xml | 10 +- ...CiServers.groovy => CiEnvironments.groovy} | 8 +- .../io/cucumber/createmeta/CiEnvironments.gsp | 11 ++ .../io/cucumber/createmeta/CiServers.gsp | 11 -- .../io/cucumber/createmeta/CiEnvironment.java | 15 +++ .../createmeta/CiEnvironmentImpl.java | 124 ++++++++++++++++++ .../io/cucumber/createmeta/CiTemplate.java | 29 ---- .../io/cucumber/createmeta/CreateMeta.java | 74 ----------- .../createmeta/DetectCiEnvironment.java | 31 +++++ .../createmeta/VariableExpression.java | 10 +- .../cucumber/createmeta/CreateMetaTest.java | 97 -------------- ...Test.java => DetectCiEnvironmentTest.java} | 24 ++-- .../createmeta/RemoveUserInfoFromUrlTest.java | 58 ++++---- 13 files changed, 232 insertions(+), 270 deletions(-) rename java/src/main/groovy/io/cucumber/createmeta/{CiServers.groovy => CiEnvironments.groovy} (65%) create mode 100644 java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp delete mode 100644 java/src/main/groovy/io/cucumber/createmeta/CiServers.gsp create mode 100644 java/src/main/java/io/cucumber/createmeta/CiEnvironment.java create mode 100644 java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java delete mode 100644 java/src/main/java/io/cucumber/createmeta/CiTemplate.java delete mode 100644 java/src/main/java/io/cucumber/createmeta/CreateMeta.java create mode 100644 java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java delete mode 100644 java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java rename java/src/test/java/io/cucumber/createmeta/{acceptance/CreateMetaTest.java => DetectCiEnvironmentTest.java} (78%) diff --git a/java/pom.xml b/java/pom.xml index e0097587..023493a7 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -39,12 +39,6 @@ - - io.cucumber - messages - [17.1.1,18.0.0) - - org.hamcrest hamcrest @@ -85,7 +79,7 @@ execute - ${project.basedir}/src/main/groovy/io/cucumber/createmeta/CiServers.groovy + ${project.basedir}/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy @@ -102,7 +96,7 @@ - ${basedir}/target/generated-sources/ci-servers/java + ${basedir}/target/generated-sources/ci-environments/java diff --git a/java/src/main/groovy/io/cucumber/createmeta/CiServers.groovy b/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy similarity index 65% rename from java/src/main/groovy/io/cucumber/createmeta/CiServers.groovy rename to java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy index ed77d44f..5d84b8ab 100644 --- a/java/src/main/groovy/io/cucumber/createmeta/CiServers.groovy +++ b/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy @@ -3,16 +3,16 @@ import groovy.json.JsonSlurper import java.nio.file.Files SimpleTemplateEngine engine = new SimpleTemplateEngine() -def templateSource = new File(project.basedir, "src/main/groovy/io/cucumber/createmeta/CiServers.gsp").getText() +def templateSource = new File(project.basedir, "src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp").getText() def jsonSlurper = new JsonSlurper() -def ciServers = jsonSlurper.parseText(new File(project.basedir, "../ciDict.json").getText()) +def ciEnvironments = jsonSlurper.parseText(new File(project.basedir, "../CiEnvironments.json").getText()) def toJava(s) { return s == null ? 'null' : "\"${s.replace("\\", "\\\\")}\"" } -def binding = ["ciServers": ciServers, "toJava": this.&toJava] +def binding = ["ciEnvironments": ciEnvironments, "toJava": this.&toJava] def template = engine.createTemplate(templateSource).make(binding) -def file = new File(project.basedir, "target/generated-sources/ci-servers/java/io/cucumber/createmeta/CiServers.java") +def file = new File(project.basedir, "target/generated-sources/ci-environments/java/io/cucumber/createmeta/CiEnvironments.java") Files.createDirectories(file.parentFile.toPath()) file.write(template.toString(), "UTF-8") diff --git a/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp b/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp new file mode 100644 index 00000000..e45cc276 --- /dev/null +++ b/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp @@ -0,0 +1,11 @@ +package io.cucumber.createmeta; + +import static java.util.Arrays.asList; + +class CiEnvironments { + public static final Iterable TEMPLATES = asList( +<% ciEnvironments.eachWithIndex {ciEnvironment, index-> %> + new CiEnvironmentImpl(<%= toJava(ciEnvironment.name) %>, <%= toJava(ciEnvironment.url) %>, <%= toJava(ciEnvironment.buildNumber) %>, new CiEnvironmentImpl.Git(<%= toJava(ciEnvironment.git.remote) %>, <%= toJava(ciEnvironment.git.revision) %>, <%= toJava(ciEnvironment.git.branch) %>, <%= toJava(ciEnvironment.git.tag) %>)) <%= index < ciEnvironments.size() -1 ? "," : "" %> +<% } %> + ); +} diff --git a/java/src/main/groovy/io/cucumber/createmeta/CiServers.gsp b/java/src/main/groovy/io/cucumber/createmeta/CiServers.gsp deleted file mode 100644 index 7ebe2c37..00000000 --- a/java/src/main/groovy/io/cucumber/createmeta/CiServers.gsp +++ /dev/null @@ -1,11 +0,0 @@ -package io.cucumber.createmeta; - -import java.util.Arrays; - -class CiServers { - public static final Iterable TEMPLATES = Arrays.asList( -<% ciServers.eachWithIndex {name, ci, index-> %> - new CiTemplate(<%= toJava(name) %>, <%= toJava(ci.url) %>, <%= toJava(ci.buildNumber) %>, new CiTemplate.GitTemplate(<%= toJava(ci.git.remote) %>, <%= toJava(ci.git.revision) %>, <%= toJava(ci.git.branch) %>, <%= toJava(ci.git.tag) %>)) <%= index < ciServers.size() -1 ? "," : "" %> -<% } %> - ); -} diff --git a/java/src/main/java/io/cucumber/createmeta/CiEnvironment.java b/java/src/main/java/io/cucumber/createmeta/CiEnvironment.java new file mode 100644 index 00000000..85962d55 --- /dev/null +++ b/java/src/main/java/io/cucumber/createmeta/CiEnvironment.java @@ -0,0 +1,15 @@ +package io.cucumber.createmeta; + +public interface CiEnvironment { + String getName(); + String getUrl(); + String getBuildNumber(); + Git getGit(); + + interface Git { + String getRemote(); + String getRevision(); + String getBranch(); + String getTag(); + } +} diff --git a/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java b/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java new file mode 100644 index 00000000..fe3c77c5 --- /dev/null +++ b/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java @@ -0,0 +1,124 @@ +package io.cucumber.createmeta; + +import java.util.Map; +import java.util.Objects; + +import static io.cucumber.createmeta.DetectCiEnvironment.removeUserInfoFromUrl; +import static io.cucumber.createmeta.VariableExpression.evaluate; + +final class CiEnvironmentImpl implements CiEnvironment { + public String name; + public String url; + public String buildNumber; + public Git git; + + CiEnvironmentImpl() { + } + + CiEnvironmentImpl(String name, String url, String buildNumber, Git git) { + this.name = name; + this.url = url; + this.buildNumber = buildNumber; + this.git = git; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getUrl() { + return url; + } + + @Override + public String getBuildNumber() { + return buildNumber; + } + + @Override + public CiEnvironment.Git getGit() { + return git; + } + + CiEnvironment detect(Map env) { + String url = evaluate(getUrl(), env); + if (url == null) return null; + + return new CiEnvironmentImpl( + name, + url, + evaluate(getBuildNumber(), env), + new CiEnvironmentImpl.Git( + removeUserInfoFromUrl(evaluate(git.remote, env)), + evaluate(git.revision, env), + evaluate(git.branch, env), + evaluate(git.tag, env) + ) + ); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + CiEnvironmentImpl that = (CiEnvironmentImpl) o; + return Objects.equals(name, that.name) && Objects.equals(url, that.url) && Objects.equals(buildNumber, that.buildNumber) && Objects.equals(git, that.git); + } + + @Override + public int hashCode() { + return Objects.hash(name, url, buildNumber, git); + } + + final static class Git implements CiEnvironment.Git { + public String remote; + public String revision; + public String branch; + public String tag; + + Git() { + } + + Git(String remote, String revision, String branch, String tag) { + this.remote = remote; + this.revision = revision; + this.branch = branch; + this.tag = tag; + } + + @Override + public String getRemote() { + return remote; + } + + @Override + public String getRevision() { + return revision; + } + + @Override + public String getBranch() { + return branch; + } + + @Override + public String getTag() { + return tag; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Git git = (Git) o; + return Objects.equals(remote, git.remote) && Objects.equals(revision, git.revision) && Objects.equals(branch, git.branch) && Objects.equals(tag, git.tag); + } + + @Override + public int hashCode() { + return Objects.hash(remote, revision, branch, tag); + } + } +} diff --git a/java/src/main/java/io/cucumber/createmeta/CiTemplate.java b/java/src/main/java/io/cucumber/createmeta/CiTemplate.java deleted file mode 100644 index 5954f32a..00000000 --- a/java/src/main/java/io/cucumber/createmeta/CiTemplate.java +++ /dev/null @@ -1,29 +0,0 @@ -package io.cucumber.createmeta; - -final class CiTemplate { - public final String name; - public final String url; - public final String buildNumber; - public final GitTemplate git; - - CiTemplate(String name, String url, String buildNumber, GitTemplate git) { - this.name = name; - this.url = url; - this.buildNumber = buildNumber; - this.git = git; - } - - final static class GitTemplate { - public final String remote; - public final String revision; - public final String branch; - public final String tag; - - public GitTemplate(String remote, String revision, String branch, String tag) { - this.remote = remote; - this.revision = revision; - this.branch = branch; - this.tag = tag; - } - } -} diff --git a/java/src/main/java/io/cucumber/createmeta/CreateMeta.java b/java/src/main/java/io/cucumber/createmeta/CreateMeta.java deleted file mode 100644 index ff5ae070..00000000 --- a/java/src/main/java/io/cucumber/createmeta/CreateMeta.java +++ /dev/null @@ -1,74 +0,0 @@ -package io.cucumber.createmeta; - -import io.cucumber.messages.ProtocolVersion; -import io.cucumber.messages.types.Ci; -import io.cucumber.messages.types.Git; -import io.cucumber.messages.types.Meta; -import io.cucumber.messages.types.Product; - -import java.net.URI; -import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static io.cucumber.createmeta.VariableExpression.evaluate; - -public final class CreateMeta { - private CreateMeta(){ - - } - - public static Meta createMeta( - String implementationName, - String implementationVersion, - Map env - ) { - return new Meta( - ProtocolVersion.getVersion(), - new Product(implementationName, implementationVersion), - new Product(System.getProperty("java.vm.name"), System.getProperty("java.vm.version")), - new Product(System.getProperty("os.name"), null), - new Product(System.getProperty("os.arch"), null), - detectCI(env) - ); - } - - public static String removeUserInfoFromUrl(String value) { - if (value == null) return null; - try { - URI uri = URI.create(value); - return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()).toASCIIString(); - } catch (URISyntaxException | IllegalArgumentException e) { - return value; - } - } - - static Ci detectCI(Map env) { - List detected = new ArrayList<>(); - for (CiTemplate ciTemplate : CiServers.TEMPLATES) { - Ci ci = createCi(ciTemplate, env); - if (ci != null) { - detected.add(ci); - } - } - return detected.size() == 1 ? detected.get(0) : null; - } - - private static Ci createCi(CiTemplate ciTemplate, Map env) { - String url = evaluate(ciTemplate.url, env); - if (url == null) return null; - String buildNumber = evaluate(ciTemplate.buildNumber, env); - String remote = removeUserInfoFromUrl(evaluate(ciTemplate.git.remote, env)); - String revision = evaluate(ciTemplate.git.revision, env); - String branch = evaluate(ciTemplate.git.branch, env); - String tag = evaluate(ciTemplate.git.tag, env); - - return new Ci( - ciTemplate.name, - url, - buildNumber, - new Git(remote, revision, branch, tag) - ); - } -} diff --git a/java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java b/java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java new file mode 100644 index 00000000..56a58be8 --- /dev/null +++ b/java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java @@ -0,0 +1,31 @@ +package io.cucumber.createmeta; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + +public final class DetectCiEnvironment { + private DetectCiEnvironment() { + + } + + public static CiEnvironment detectCiEnvironment(Map env) { + for (CiEnvironmentImpl ciTemplate : CiEnvironments.TEMPLATES) { + CiEnvironment detected = ciTemplate.detect(env); + if (detected != null) { + return detected; + } + } + return null; + } + + static String removeUserInfoFromUrl(String value) { + if (value == null) return null; + try { + URI uri = URI.create(value); + return new URI(uri.getScheme(), null, uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()).toASCIIString(); + } catch (URISyntaxException | IllegalArgumentException e) { + return value; + } + } +} diff --git a/java/src/main/java/io/cucumber/createmeta/VariableExpression.java b/java/src/main/java/io/cucumber/createmeta/VariableExpression.java index ddf37724..0b79f111 100644 --- a/java/src/main/java/io/cucumber/createmeta/VariableExpression.java +++ b/java/src/main/java/io/cucumber/createmeta/VariableExpression.java @@ -4,21 +4,21 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -public final class VariableExpression { +final class VariableExpression { private static final Pattern variablePattern = Pattern.compile("\\$\\{(.*?)(?:(? env) { - if(expression == null) return null; + static String evaluate(String expression, Map env) { + if (expression == null) return null; Matcher variableMatcher = variablePattern.matcher(expression); StringBuffer sb = new StringBuffer(); while (variableMatcher.find()) { String variable = variableMatcher.group(1); String value = getValue(env, variable); - if(value == null) { + if (value == null) { return null; } String pattern = variableMatcher.group(2); diff --git a/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java b/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java deleted file mode 100644 index 57eba4a5..00000000 --- a/java/src/test/java/io/cucumber/createmeta/CreateMetaTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package io.cucumber.createmeta; - -import io.cucumber.messages.types.Ci; -import io.cucumber.messages.types.Git; -import io.cucumber.messages.types.Meta; -import io.cucumber.messages.JSON; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.util.HashMap; - -import static io.cucumber.createmeta.CreateMeta.createMeta; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.matchesPattern; -import static org.junit.jupiter.api.Assertions.assertEquals; - -class CreateMetaTest { - @Test - void it_provides_the_correct_tool_name() { - Meta meta = createMeta("cucumber-jvm", "3.2.1", new HashMap<>()); - assertEquals("cucumber-jvm", meta.getImplementation().getName()); - } - - @Test - void it_provides_the_correct_tool_version() { - Meta meta = createMeta("cucumber-jvm", "3.2.1", new HashMap<>()); - assertEquals("3.2.1", meta.getImplementation().getVersion()); - } - - @Test - void it_provides_the_correct_protocol_version() { - Meta meta = createMeta("cucumber-jvm", "3.2.1", new HashMap<>()); - assertThat(meta.getProtocolVersion(), matchesPattern("\\d+\\.\\d+\\.\\d+(-SNAPSHOT)?")); - } - - @Test - void it_provides_the_correct_jvm_version_and_name() { - Meta meta = createMeta("cucumber-jvm", "3.2.1", new HashMap<>()); - assertThat(meta.getRuntime().getName(), matchesPattern("(OpenJDK).*")); - } - - @Test - void it_detects_github_actions() throws IOException { - HashMap env = new HashMap() {{ - put("GITHUB_SERVER_URL", "https://github.company.com"); - put("GITHUB_REPOSITORY", "cucumber/cucumber-ruby"); - put("GITHUB_RUN_ID", "140170388"); - put("GITHUB_SHA", "the-revision"); - put("GITHUB_REF", "refs/tags/the-tag"); - }}; - Meta meta = createMeta("cucumber-jvm", "3.2.1", env); - - assertEquals(new Ci( - "GitHub Actions", - "https://github.company.com/cucumber/cucumber-ruby/actions/runs/140170388", - "140170388", - new Git( - "https://github.company.com/cucumber/cucumber-ruby.git", - "the-revision", - null, - "the-tag" - ) - ), - meta.getCi()); - } - - @Test - void it_detects_go_cd() throws IOException { - // https://github.com/gocd/gocd/blob/3e948218bfb163c5c3d2bf5140cb4a12f110769e/server/src/main/webapp/WEB-INF/rails/config/routes.rb#L55 - HashMap env = new HashMap() {{ - put("GO_SERVER_URL", "https://mygocd.com"); - put("GO_PIPELINE_NAME", "my-pipeline"); - put("GO_PIPELINE_COUNTER", "1234"); - put("GO_STAGE_NAME", "my-stage"); - put("GO_STAGE_COUNTER", "456"); - put("GO_REVISION", "the-revision"); - // https://github.com/ashwanthkumar/gocd-build-github-pull-requests - put("GO_SCM_MY_MATERIAL_PR_BRANCH", "ashwankthkumar:feature-1"); - put("GO_SCM_MY_MATERIAL_PR_URL", "https://github.com/owner/repo/pull/1669"); - }}; - Meta meta = createMeta("cucumber-jvm", "3.2.1", env); - - assertEquals(new Ci( - "GoCD", - "https://mygocd.com/pipelines/my-pipeline/1234/my-stage/456", - "my-pipeline/1234/my-stage/456", - new Git( - "https://github.com/owner/repo.git", - "the-revision", - "feature-1", - null - ) - ), - meta.getCi(), - "The Git JSON was:\n" + JSON.writeValueAsString(meta.getCi())); - } -} diff --git a/java/src/test/java/io/cucumber/createmeta/acceptance/CreateMetaTest.java b/java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java similarity index 78% rename from java/src/test/java/io/cucumber/createmeta/acceptance/CreateMetaTest.java rename to java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java index b647baa7..105d19c7 100644 --- a/java/src/test/java/io/cucumber/createmeta/acceptance/CreateMetaTest.java +++ b/java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java @@ -1,9 +1,7 @@ -package io.cucumber.createmeta.acceptance; +package io.cucumber.createmeta; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; -import io.cucumber.messages.types.Ci; -import io.cucumber.messages.types.Meta; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.converter.ArgumentConversionException; @@ -22,12 +20,12 @@ import java.util.List; import java.util.Map; -import static io.cucumber.createmeta.CreateMeta.createMeta; +import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; import static java.nio.file.Files.newBufferedReader; import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertEquals; -class CreateMetaTest { +class DetectCiEnvironmentTest { private static final ObjectMapper mapper = new ObjectMapper() .enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); @@ -40,18 +38,18 @@ private static List acceptance_tests_pass() throws IOException { @ParameterizedTest @MethodSource - void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) throws IOException { - Meta meta = createMeta("cucumber-jvm", "1.2.3", expectation.env); - assertEquals(expectation.expectedCi, meta.getCi()); + void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) { + CiEnvironment ciEnvironment = detectCiEnvironment(expectation.env); + assertEquals(expectation.expected, ciEnvironment); } static class Expectation { public final Map env; - public final Ci expectedCi; + public final CiEnvironment expected; - Expectation(Map env, Ci expectedCi) { + Expectation(Map env, CiEnvironment expected) { this.env = env; - this.expectedCi = expectedCi; + this.expected = expected; } } @@ -72,8 +70,8 @@ public Expectation convert(Object source, ParameterContext context) throws Argum env.put(parts[0], parts[1]); } } - Ci expectedCi = mapper.readValue(new File(path.toString() + ".json"), Ci.class); - return new Expectation(env, expectedCi); + CiEnvironment expected = mapper.readValue(new File(path + ".json"), CiEnvironmentImpl.class); + return new Expectation(env, expected); } catch (IOException e) { throw new ArgumentConversionException("Could not load " + source, e); } diff --git a/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java b/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java index 34dbaf3e..48ecd8f8 100644 --- a/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java +++ b/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java @@ -6,33 +6,33 @@ import static org.junit.jupiter.api.Assertions.assertNull; class RemoveUserInfoFromUrlTest { - @Test - void returns_null_for_null() { - assertNull(CreateMeta.removeUserInfoFromUrl(null)); - } - - @Test - void returns_empty_string_for_empty_string() { - assertEquals("", CreateMeta.removeUserInfoFromUrl("")); - } - - @Test - void leaves_the_data_intact_when_no_sensitive_information_is_detected() { - assertEquals("pretty safe", CreateMeta.removeUserInfoFromUrl("pretty safe")); - } - - @Test - void with_URLS_leaves_intact_when_no_password_is_found() { - assertEquals("https://example.com/git/repo.git", CreateMeta.removeUserInfoFromUrl("https://example.com/git/repo.git")); - } - - @Test - void with_URLS_removes_credentials_when_found() { - assertEquals("http://example.com/git/repo.git", CreateMeta.removeUserInfoFromUrl("http://login@example.com/git/repo.git")); - } - - @Test - void with_URLS_removes_credentials_and_passwords_when_found() { - assertEquals("ssh://example.com/git/repo.git", CreateMeta.removeUserInfoFromUrl("ssh://login:password@example.com/git/repo.git")); - } + @Test + void returns_null_for_null() { + assertNull(DetectCiEnvironment.removeUserInfoFromUrl(null)); + } + + @Test + void returns_empty_string_for_empty_string() { + assertEquals("", DetectCiEnvironment.removeUserInfoFromUrl("")); + } + + @Test + void leaves_the_data_intact_when_no_sensitive_information_is_detected() { + assertEquals("pretty safe", DetectCiEnvironment.removeUserInfoFromUrl("pretty safe")); + } + + @Test + void with_URLS_leaves_intact_when_no_password_is_found() { + assertEquals("https://example.com/git/repo.git", DetectCiEnvironment.removeUserInfoFromUrl("https://example.com/git/repo.git")); + } + + @Test + void with_URLS_removes_credentials_when_found() { + assertEquals("http://example.com/git/repo.git", DetectCiEnvironment.removeUserInfoFromUrl("http://login@example.com/git/repo.git")); + } + + @Test + void with_URLS_removes_credentials_and_passwords_when_found() { + assertEquals("ssh://example.com/git/repo.git", DetectCiEnvironment.removeUserInfoFromUrl("ssh://login:password@example.com/git/repo.git")); + } } From d71bc8fc124cc7efeb6fe81b48bfff0d3113ecca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 11:51:22 +0000 Subject: [PATCH 06/14] Refactor ruby --- .github/workflows/release-ruby.yml | 5 -- .github/workflows/test-ruby.yml | 4 +- ruby/.rspec | 1 - ruby/Rakefile | 6 ++ ruby/cucumber-create-meta.gemspec | 3 - ruby/lib/cucumber/ci_environment.rb | 53 +++++++++++++ .../CiEnvironments.json} | 46 ++++++++---- .../variable_expression.rb | 2 +- ruby/lib/cucumber/create_meta.rb | 74 ------------------- ruby/rake/copy_ci_dict.rb | 7 -- ruby/spec/acceptance/create_meta_spec.rb | 25 ------- .../ci_environment/ci_environment_spec.rb | 21 ++++++ .../evaluate_variable_expression_spec.rb | 24 +++--- .../remove_userinfo_from_url_spec.rb | 29 ++++++++ ruby/spec/cucumber/create_meta_spec.rb | 36 --------- .../cucumber/remove_userinfo_from_url_spec.rb | 29 -------- ruby/spec/spec_helper.rb | 10 --- 17 files changed, 154 insertions(+), 221 deletions(-) create mode 100644 ruby/lib/cucumber/ci_environment.rb rename ruby/lib/cucumber/{ciDict.json => ci_environment/CiEnvironments.json} (90%) rename ruby/lib/cucumber/{create_meta => ci_environment}/variable_expression.rb (98%) delete mode 100644 ruby/lib/cucumber/create_meta.rb delete mode 100644 ruby/rake/copy_ci_dict.rb delete mode 100644 ruby/spec/acceptance/create_meta_spec.rb create mode 100644 ruby/spec/cucumber/ci_environment/ci_environment_spec.rb rename ruby/spec/cucumber/{ => ci_environment}/evaluate_variable_expression_spec.rb (50%) create mode 100644 ruby/spec/cucumber/ci_environment/remove_userinfo_from_url_spec.rb delete mode 100644 ruby/spec/cucumber/create_meta_spec.rb delete mode 100644 ruby/spec/cucumber/remove_userinfo_from_url_spec.rb delete mode 100644 ruby/spec/spec_helper.rb diff --git a/.github/workflows/release-ruby.yml b/.github/workflows/release-ruby.yml index 6e1ff300..54f903cf 100644 --- a/.github/workflows/release-ruby.yml +++ b/.github/workflows/release-ruby.yml @@ -21,11 +21,6 @@ jobs: steps: - uses: actions/checkout@v2 - - - name: Synchronize CI Dict - working-directory: ruby - run: bundle exec rake copy_ci_dict - - name: Publish ruby gem uses: cucumber/action-publish-rubygem@v1.0.0 with: diff --git a/.github/workflows/test-ruby.yml b/.github/workflows/test-ruby.yml index 1b22f66f..ac43e024 100644 --- a/.github/workflows/test-ruby.yml +++ b/.github/workflows/test-ruby.yml @@ -32,6 +32,6 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true working-directory: ruby - - name: bundle exec rspec + - name: bundle exec rake working-directory: ruby - run: bundle exec rspec \ No newline at end of file + run: bundle exec rake diff --git a/ruby/.rspec b/ruby/.rspec index e77f4dc9..4e1e0d2f 100644 --- a/ruby/.rspec +++ b/ruby/.rspec @@ -1,2 +1 @@ --color ---require 'spec_helper' \ No newline at end of file diff --git a/ruby/Rakefile b/ruby/Rakefile index bae6c771..a6035e58 100644 --- a/ruby/Rakefile +++ b/ruby/Rakefile @@ -11,4 +11,10 @@ Dir['./rake/*.rb'].each do |f| require f end +file 'lib/cucumber/ci_environment/CiEnvironments.json' => '../CiEnvironments.json' do |t| + FileUtils.cp t.prerequisites[0], t.name +end + +task 'spec:warnings' => 'lib/cucumber/ci_environment/CiEnvironments.json' + task default: ['spec:warnings'] diff --git a/ruby/cucumber-create-meta.gemspec b/ruby/cucumber-create-meta.gemspec index c5ce6f31..9a8e5b0a 100644 --- a/ruby/cucumber-create-meta.gemspec +++ b/ruby/cucumber-create-meta.gemspec @@ -20,9 +20,6 @@ Gem::Specification.new do |s| 'source_code_uri' => 'https://github.com/cucumber/create-meta/tree/main/ruby' } - s.add_dependency 'cucumber-messages', '~> 17.1', '>= 17.1.1' - s.add_dependency 'sys-uname', '~> 1.2', '>= 1.2.2' - s.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' s.add_development_dependency 'rspec', '~> 3.10', '>= 3.10.0' diff --git a/ruby/lib/cucumber/ci_environment.rb b/ruby/lib/cucumber/ci_environment.rb new file mode 100644 index 00000000..2d0785a2 --- /dev/null +++ b/ruby/lib/cucumber/ci_environment.rb @@ -0,0 +1,53 @@ +require 'uri' +require 'json' +require 'cucumber/ci_environment/variable_expression' + +module Cucumber + module CiEnvironment + extend VariableExpression + CI_ENVIRONMENTS_PATH = File.join(File.dirname(__FILE__), 'ci_environment/CiEnvironments.json') + + def detect_ci_environment(env = ENV) + ci_environments = JSON.parse(IO.read(CI_ENVIRONMENTS_PATH)) + ci_environments.each do |ci_environment| + detected = detect(ci_environment, env) + return detected unless detected.nil? + end + end + + def detect(ci_environment, env) + url = evaluate(ci_environment['url'], env) + return nil if url.nil? + + result = { + name: ci_environment['name'], + url: url, + buildNumber: evaluate(ci_environment['buildNumber'], env), + git: { + remote: remove_userinfo_from_url(evaluate(ci_environment['git']['remote'], env)), + revision: evaluate(ci_environment['git']['revision'], env), + branch: evaluate(ci_environment['git']['branch'], env), + } + } + tag = evaluate(ci_environment['git']['tag'], env) + if tag + result[:git][:tag] = tag + end + result + end + + def remove_userinfo_from_url(value) + return nil if value.nil? + + begin + uri = URI(value) + uri.userinfo = '' + uri.to_s + rescue StandardError + value + end + end + + module_function :detect_ci_environment, :detect, :remove_userinfo_from_url + end +end diff --git a/ruby/lib/cucumber/ciDict.json b/ruby/lib/cucumber/ci_environment/CiEnvironments.json similarity index 90% rename from ruby/lib/cucumber/ciDict.json rename to ruby/lib/cucumber/ci_environment/CiEnvironments.json index 4cf94ba4..f9c7072e 100644 --- a/ruby/lib/cucumber/ciDict.json +++ b/ruby/lib/cucumber/ci_environment/CiEnvironments.json @@ -1,5 +1,6 @@ -{ - "Azure Pipelines": { +[ + { + "name": "Azure Pipelines", "url": "${BUILD_BUILDURI}", "buildNumber": "${BUILD_BUILDNUMBER}", "git": { @@ -9,7 +10,8 @@ "tag": "${BUILD_SOURCEBRANCH/refs\/tags\/(.*)/\\1}" } }, - "Bamboo": { + { + "name": "Bamboo", "url": "${bamboo_buildResultsUrl}", "buildNumber": "${bamboo_buildNumber}", "git": { @@ -19,7 +21,8 @@ "tag": null } }, - "Buddy": { + { + "name": "Buddy", "url": "${BUDDY_EXECUTION_URL}", "buildNumber": "${BUDDY_EXECUTION_ID}", "git": { @@ -29,7 +32,8 @@ "tag": "${BUDDY_EXECUTION_TAG}" } }, - "Bitrise": { + { + "name": "Bitrise", "url": "${BITRISE_BUILD_URL}", "buildNumber": "${BITRISE_BUILD_NUMBER}", "git": { @@ -39,7 +43,8 @@ "tag": "${BITRISE_GIT_TAG}" } }, - "CircleCI": { + { + "name": "CircleCI", "url": "${CIRCLE_BUILD_URL}", "buildNumber": "${CIRCLE_BUILD_NUM}", "git": { @@ -49,7 +54,8 @@ "tag": "${CIRCLE_TAG}" } }, - "CodeFresh": { + { + "name": "CodeFresh", "url": "${CF_BUILD_URL}", "buildNumber": "${CF_BUILD_ID}", "git": { @@ -59,7 +65,8 @@ "tag": null } }, - "CodeShip": { + { + "name": "CodeShip", "url": "${CI_BUILD_URL}", "buildNumber": "${CI_BUILD_NUMBER}", "git": { @@ -69,7 +76,8 @@ "tag": null } }, - "GitHub Actions": { + { + "name": "GitHub Actions", "url": "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}", "buildNumber": "${GITHUB_RUN_ID}", "git": { @@ -79,7 +87,8 @@ "tag": "${GITHUB_REF/refs\/tags\/(.*)/\\1}" } }, - "GitLab": { + { + "name": "GitLab", "url": "${CI_JOB_URL}", "buildNumber": "${CI_JOB_ID}", "git": { @@ -89,7 +98,8 @@ "tag": "${CI_COMMIT_TAG}" } }, - "GoCD": { + { + "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": { @@ -99,7 +109,8 @@ "tag": null } }, - "Jenkins": { + { + "name": "Jenkins", "url": "${BUILD_URL}", "buildNumber": "${BUILD_NUMBER}", "git": { @@ -109,7 +120,8 @@ "tag": null } }, - "Semaphore": { + { + "name": "Semaphore", "url": "${SEMAPHORE_ORGANIZATION_URL}/jobs/${SEMAPHORE_JOB_ID}", "buildNumber": "${SEMAPHORE_JOB_ID}", "git": { @@ -119,7 +131,8 @@ "tag": "${SEMAPHORE_GIT_TAG_NAME}" } }, - "Travis CI": { + { + "name": "Travis CI", "url": "${TRAVIS_BUILD_WEB_URL}", "buildNumber": "${TRAVIS_JOB_NUMBER}", "git": { @@ -129,7 +142,8 @@ "tag": "${TRAVIS_TAG}" } }, - "Wercker": { + { + "name": "Wercker", "url": "${WERCKER_RUN_URL}", "buildNumber": "${WERCKER_RUN_URL/.*\\/([^\\/]+)$/\\1}", "git": { @@ -139,4 +153,4 @@ "tag": null } } -} +] diff --git a/ruby/lib/cucumber/create_meta/variable_expression.rb b/ruby/lib/cucumber/ci_environment/variable_expression.rb similarity index 98% rename from ruby/lib/cucumber/create_meta/variable_expression.rb rename to ruby/lib/cucumber/ci_environment/variable_expression.rb index 0164d7a3..42c59d62 100644 --- a/ruby/lib/cucumber/create_meta/variable_expression.rb +++ b/ruby/lib/cucumber/ci_environment/variable_expression.rb @@ -1,5 +1,5 @@ module Cucumber - module CreateMeta + module CiEnvironment module VariableExpression def evaluate(expression, env) return nil if expression.nil? diff --git a/ruby/lib/cucumber/create_meta.rb b/ruby/lib/cucumber/create_meta.rb deleted file mode 100644 index 455a1508..00000000 --- a/ruby/lib/cucumber/create_meta.rb +++ /dev/null @@ -1,74 +0,0 @@ -require 'uri' -require 'sys/uname' -require 'json' -require 'cucumber/messages' -require 'cucumber/create_meta/variable_expression' - -module Cucumber - module CreateMeta - extend Cucumber::CreateMeta::VariableExpression - CI_DICT_FILE_PATH = File.join(File.dirname(__FILE__), 'ciDict.json') - - def create_meta(tool_name, tool_version, env = ENV) - Cucumber::Messages::Meta.new( - protocol_version: Cucumber::Messages::VERSION, - implementation: Cucumber::Messages::Product.new( - name: tool_name, - version: tool_version - ), - runtime: Cucumber::Messages::Product.new( - name: RUBY_ENGINE, - version: RUBY_VERSION - ), - os: Cucumber::Messages::Product.new( - name: RbConfig::CONFIG['target_os'], - version: Sys::Uname.uname.version - ), - cpu: Cucumber::Messages::Product.new( - name: RbConfig::CONFIG['target_cpu'] - ), - ci: detect_ci(env) - ) - end - - def detect_ci(env) - ci_dict = JSON.parse(IO.read(CI_DICT_FILE_PATH)) - detected = ci_dict.map do |ci_name, ci_system| - create_ci(ci_name, ci_system, env) - end.compact - - detected.length == 1 ? detected[0] : nil - end - - def create_ci(ci_name, ci_system, env) - url = evaluate(ci_system['url'], env) - return nil if url.nil? - - Cucumber::Messages::Ci.new( - url: url, - name: ci_name, - build_number: evaluate(ci_system['buildNumber'], env), - git: Cucumber::Messages::Git.new( - remote: remove_userinfo_from_url(evaluate(ci_system['git']['remote'], env)), - revision: evaluate(ci_system['git']['revision'], env), - branch: evaluate(ci_system['git']['branch'], env), - tag: evaluate(ci_system['git']['tag'], env) - ) - ) - end - - def remove_userinfo_from_url(value) - return nil if value.nil? - - begin - uri = URI(value) - uri.userinfo = '' - uri.to_s - rescue StandardError - value - end - end - - module_function :create_meta, :detect_ci, :create_ci, :remove_userinfo_from_url - end -end diff --git a/ruby/rake/copy_ci_dict.rb b/ruby/rake/copy_ci_dict.rb deleted file mode 100644 index 48cb11f2..00000000 --- a/ruby/rake/copy_ci_dict.rb +++ /dev/null @@ -1,7 +0,0 @@ -# frozen_string_literal: true - -require 'rake' - -task :copy_ci_dict do - FileUtils.cp '../ciDict.json', 'lib/cucumber/ciDict.json' -end diff --git a/ruby/spec/acceptance/create_meta_spec.rb b/ruby/spec/acceptance/create_meta_spec.rb deleted file mode 100644 index 7202dfab..00000000 --- a/ruby/spec/acceptance/create_meta_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require 'cucumber/create_meta' -require 'json' - -TEST_DATA_DIR = '../testdata' - -describe 'CreateMeta' do - Dir.each_child(TEST_DATA_DIR) do |test_data_file| - next unless File.extname(test_data_file) == '.txt' - - context "with #{File.basename(test_data_file, '.*')}" do - subject { JSON.parse(meta.ci.to_json) } - - let(:meta) { Cucumber::CreateMeta.create_meta('cucumber-something', '1.2.3', env) } - let(:env) { Hash[entries] } - let(:entries) { env_data.split(/\n/).map { |line| line.split(/=/) } } - let(:env_data) { IO.read("#{TEST_DATA_DIR}/#{test_data_file}") } - - let(:expected_json) { File.read("#{TEST_DATA_DIR}/#{test_data_file}.json") } - - it { is_expected.to eq JSON.parse(expected_json) } - end - end -end diff --git a/ruby/spec/cucumber/ci_environment/ci_environment_spec.rb b/ruby/spec/cucumber/ci_environment/ci_environment_spec.rb new file mode 100644 index 00000000..92c4a5fa --- /dev/null +++ b/ruby/spec/cucumber/ci_environment/ci_environment_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'cucumber/ci_environment' +require 'json' + +describe 'detect_ci_environment' do + Dir.glob("../testdata/*.txt") do |test_data_file| + context "with #{File.basename(test_data_file, '.txt')}" do + subject { JSON.parse(ci_environment.to_json) } + + let(:ci_environment) { Cucumber::CiEnvironment.detect_ci_environment(env) } + let(:env) { Hash[entries] } + let(:entries) { env_data.split(/\n/).map { |line| line.split(/=/) } } + let(:env_data) { IO.read(test_data_file) } + + let(:expected_json) { File.read("#{test_data_file}.json") } + + it { is_expected.to eq JSON.parse(expected_json) } + end + end +end diff --git a/ruby/spec/cucumber/evaluate_variable_expression_spec.rb b/ruby/spec/cucumber/ci_environment/evaluate_variable_expression_spec.rb similarity index 50% rename from ruby/spec/cucumber/evaluate_variable_expression_spec.rb rename to ruby/spec/cucumber/ci_environment/evaluate_variable_expression_spec.rb index 83b68ae1..634fc98d 100644 --- a/ruby/spec/cucumber/evaluate_variable_expression_spec.rb +++ b/ruby/spec/cucumber/ci_environment/evaluate_variable_expression_spec.rb @@ -1,34 +1,34 @@ -require 'cucumber/create_meta' +require 'cucumber/ci_environment' -describe 'Cucumber::CreateMeta::VariableExpression.evaluate' do - include Cucumber::CreateMeta::VariableExpression +describe 'Cucumber::CiEnvironment::VariableExpression.evaluate' do + include Cucumber::CiEnvironment::VariableExpression it 'returns nil when a variable is undefined' do - expression = "hello-${SOME_VAR}"; - result = evaluate(expression, {}); + expression = "hello-${SOME_VAR}" + result = evaluate(expression, {}) expect(result).to eq(nil) end it 'gets a value without replacement' do - expression = "${SOME_VAR}"; - result = evaluate(expression, {'SOME_VAR' => 'some_value'}); + expression = "${SOME_VAR}" + result = evaluate(expression, { 'SOME_VAR' => 'some_value' }) expect(result).to eq('some_value') end it 'captures a group' do - expression = "${SOME_REF/refs\/heads\/(.*)/\\1}"; - result = evaluate(expression, {'SOME_REF' => 'refs/heads/main'}); + expression = "${SOME_REF/refs\/heads\/(.*)/\\1}" + result = evaluate(expression, { 'SOME_REF' => 'refs/heads/main' }) expect(result).to eq('main') end it 'works with star wildcard in var' do - expression = "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}"; - result = evaluate(expression, {'GO_SCM_MY_MATERIAL_PR_BRANCH' => 'ashwankthkumar:feature-1'}); + expression = "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}" + result = evaluate(expression, { 'GO_SCM_MY_MATERIAL_PR_BRANCH' => 'ashwankthkumar:feature-1' }) expect(result).to eq('feature-1') end it 'evaluates a complex expression' do - expression = "hello-${VAR1}-${VAR2/(.*) (.*)/\\2-\\1}-world"; + expression = "hello-${VAR1}-${VAR2/(.*) (.*)/\\2-\\1}-world" result = evaluate(expression, { 'VAR1' => 'amazing', 'VAR2' => 'gorgeous beautiful' diff --git a/ruby/spec/cucumber/ci_environment/remove_userinfo_from_url_spec.rb b/ruby/spec/cucumber/ci_environment/remove_userinfo_from_url_spec.rb new file mode 100644 index 00000000..20bd1e25 --- /dev/null +++ b/ruby/spec/cucumber/ci_environment/remove_userinfo_from_url_spec.rb @@ -0,0 +1,29 @@ +require 'cucumber/ci_environment' + +describe 'remove_user_info_from_url' do + it 'returns nil for nil' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url(nil)).to be_nil + end + + it 'returns empty string for empty string' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url('')).to eq('') + end + + it 'leaves the data intact when no sensitive information is detected' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url('pretty safe')).to eq('pretty safe') + end + + context 'with URLs' do + it 'leaves intact when no password is found' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url('https://example.com/git/repo.git')).to eq('https://example.com/git/repo.git') + end + + it 'removes credentials when found' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url('http://login@example.com/git/repo.git')).to eq('http://example.com/git/repo.git') + end + + it 'removes credentials and passwords when found' do + expect(Cucumber::CiEnvironment.remove_userinfo_from_url('ssh://login:password@example.com/git/repo.git')).to eq('ssh://example.com/git/repo.git') + end + end +end diff --git a/ruby/spec/cucumber/create_meta_spec.rb b/ruby/spec/cucumber/create_meta_spec.rb deleted file mode 100644 index 552bc794..00000000 --- a/ruby/spec/cucumber/create_meta_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -require 'cucumber/create_meta' - -describe 'create_meta' do - it 'generates a Meta message with platform information' do - meta = Cucumber::CreateMeta.create_meta('cucumba-ruby', 'X.Y.Z', []) - - expect(meta.protocol_version).to match(/\d+\.\d+\.\d+/) - expect(meta.implementation.name).to eq('cucumba-ruby') - expect(meta.implementation.version).to eq('X.Y.Z') - expect(meta.runtime.name).to match(/(jruby|ruby)/) - expect(meta.runtime.version).to eq(RUBY_VERSION) - expect(meta.os.name).to match(/.+/) - expect(meta.os.version).to match(/.+/) - expect(meta.cpu.name).to match(/.+/) - expect(meta.ci).to be_nil - end - - it 'generates a Meta message with CI information' do - env = { - 'BUILD_URL' => 'url of the build', - 'BUILD_NUMBER' => '42', - 'GIT_URL' => 'url of git', - 'GIT_COMMIT' => 'git_sha1', - 'GIT_LOCAL_BRANCH' => 'main' - } - - meta = Cucumber::CreateMeta.create_meta('cucumba-ruby', 'X.Y.Z', env) - - expect(meta.ci).not_to be_nil - expect(meta.ci.url).to eq env['BUILD_URL'] - expect(meta.ci.build_number).to eq env['BUILD_NUMBER'] - expect(meta.ci.git.remote).to eq env['GIT_URL'] - expect(meta.ci.git.revision).to eq env['GIT_COMMIT'] - expect(meta.ci.git.branch).to eq env['GIT_LOCAL_BRANCH'] - end -end diff --git a/ruby/spec/cucumber/remove_userinfo_from_url_spec.rb b/ruby/spec/cucumber/remove_userinfo_from_url_spec.rb deleted file mode 100644 index 63cd2bac..00000000 --- a/ruby/spec/cucumber/remove_userinfo_from_url_spec.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'cucumber/create_meta' - -describe 'remove_user_info_from_url' do - it 'returns nil for nil' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url(nil)).to be_nil - end - - it 'returns empty string for empty string' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url('')).to eq('') - end - - it 'leaves the data intact when no sensitive information is detected' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url('pretty safe')).to eq('pretty safe') - end - - context 'with URLs' do - it 'leaves intact when no password is found' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url('https://example.com/git/repo.git')).to eq('https://example.com/git/repo.git') - end - - it 'removes credentials when found' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url('http://login@example.com/git/repo.git')).to eq('http://example.com/git/repo.git') - end - - it 'removes credentials and passwords when found' do - expect(Cucumber::CreateMeta.remove_userinfo_from_url('ssh://login:password@example.com/git/repo.git')).to eq('ssh://example.com/git/repo.git') - end - end -end \ No newline at end of file diff --git a/ruby/spec/spec_helper.rb b/ruby/spec/spec_helper.rb deleted file mode 100644 index 9e65f2a5..00000000 --- a/ruby/spec/spec_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -require 'rake' - -RSpec.configure do |config| - config.before(:suite) do - load 'Rakefile' - Rake::Task[:copy_ci_dict].invoke - end -end From dbd4176047a689e172fc2098b51df446b8c5d301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 12:11:56 +0000 Subject: [PATCH 07/14] Update documentation --- ARCHITECTURE.md | 5 +- CONTRIBUTING.md | 12 +-- Makefile | 2 - README.md | 59 ++++++++++-- default.mk | 89 ------------------- .../createmeta/CiEnvironmentImpl.java | 20 +++++ .../createmeta/CiEnvironmentExample.java | 10 +++ javascript/src/detectCiEnvironment.ts | 7 +- javascript/test/acceptanceTest.ts | 2 +- ruby/lib/cucumber/ci_environment.rb | 2 +- update-testdata.rb | 4 +- 11 files changed, 98 insertions(+), 114 deletions(-) delete mode 100644 Makefile delete mode 100644 default.mk create mode 100644 java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 2dd5b80b..4c4b0447 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -2,8 +2,9 @@ ## CI definitions -The `ciDict.json` file contains definitions of various CI servers. Each property of a CI -server definition is an expression that evaluates a value from one or more environment variables. +The `CiEnvironment.json` file contains an array of `CiEnvironment` structs for +all the supported CI servers. Each property of a `CiEnvironment` struct +is an expression that evaluates a value from one or more environment variables. The expression syntax for environment variables can use the form `${variable/pattern/replacement}`, similar to [bash parameter substitution](https://tldp.org/LDP/abs/html/parameter-substitution.html), diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0a816807..708f22ae 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -13,18 +13,18 @@ If you want to see support for a new CI server, please submit a pull request. Here are the steps: -* Modify the file `ciDict.json`, using environment variables to extract information. +* Add a new entry to `CiEnvironments.json`. See [ARCHITECTURE.md](./ARCHITECTURE.md#ci-definitions) for more information on - `ciDict.json`. + `CiEnvironments.json`. * Add an approval test in `testdata/YourCi.txt` and `testdata/YourCi.txt.json`. Then build and run the tests for all implementations: - cd java && mvn test - cd ../javascript && npm test - cd ../ruby && bundle exec rspec + cd java && mvn clean install + cd ../javascript && npm install && npm test + cd ../ruby && bundle && bundle exec rake -If all tests pass, commit your code and send us a pull request. Bonus points if you +If all tests pass, commit your code and submit a pull request. Bonus points if you also update `CHANGELOG.md` and `README.md`. You might want to look at the source code for [danger](https://github.com/danger/danger/tree/master/lib/danger/ci_source) diff --git a/Makefile b/Makefile deleted file mode 100644 index 167c008d..00000000 --- a/Makefile +++ /dev/null @@ -1,2 +0,0 @@ -LANGUAGES ?= javascript ruby java -include default.mk diff --git a/README.md b/README.md index b4bc92e5..e2a6b3bf 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,57 @@ -# createMeta +# CiEnvironment -Utility function for creating system-specific `Meta` messages. -## Supported CI systems +This library detects the CI environment based on environment variables defined +by CI servers. -The `ci` field of the `Meta` message contains values from environment variables -defined by the following supported CI and build servers: +If a CI server is detected, a struct of the following structure is returned: + +```json +{ + "name": "...", + "url": "...", + "buildNumber": "...", + "git": { + "remote": "...", + "revision": "...", + "branch": "..." + } +} +``` + +## Usage + +### TypeScript + +```typescript +import detectCiEnvironment from '@cucumber/ci-environment' + +const ciEnvironment = detectCiEnvironment(process.env) +console.log(JSON.stringify(ciEnvironment, null, 2)) +``` + +### Java + +```java +import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; + +public class CiEnvironmentExample { + public static void main(String[] args) { + CiEnvironment ciEnvironment = detectCiEnvironment(System.getenv()); + System.out.println("ciEnvironment = " + ciEnvironment); + } +} +``` + +### Ruby + +```ruby +require 'cucumber/ci_environment' + +ci_environment = Cucumber::CiEnvironment.detect_ci_environment(ENV) +p ci_environment +``` + +## Supported CI servers * [Azure Pipelines](https://docs.microsoft.com/en-us/azure/devops/pipelines/build/variables?tabs=yaml&view=azure-devops#build-variables) * [Bamboo](https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html) @@ -21,6 +68,6 @@ defined by the following supported CI and build servers: * [Travis CI](https://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables) * [Wercker](https://devcenter.wercker.com/administration/environment-variables/available-env-vars/) -## Adding new CI system / contributing +## Contributing Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for more information. diff --git a/default.mk b/default.mk deleted file mode 100644 index a0d8dd28..00000000 --- a/default.mk +++ /dev/null @@ -1,89 +0,0 @@ -# Please update /.templates/default.mk and sync: -# -# source scripts/functions.sh && rsync_files -# -SHELL := /usr/bin/env bash -ALPINE = $(shell which apk 2> /dev/null) -LIBNAME = $(shell basename $$(pwd)) -LANGUAGES ?= $(wildcard */) - -# https://stackoverflow.com/questions/2483182/recursive-wildcards-in-gnu-make -rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d)) - -default: $(patsubst %,default-%,$(LANGUAGES)) -.PHONY: default - -default-%: % - if [[ -d $< ]]; then cd $< && make default; fi -.PHONY: default-% - -# Need to declare these phonies to avoid errors for packages without a particular language -.PHONY: c dotnet go java javascript objective-c perl python ruby - -update-dependencies: $(patsubst %,update-dependencies-%,$(LANGUAGES)) -.PHONY: update-dependencies - -update-dependencies-%: % - if [[ -d $< ]]; then cd $< && make update-dependencies; fi -.PHONY: update-dependencies-% - -update-changelog: -ifdef NEW_VERSION - cat CHANGELOG.md | ../scripts/update_changelog.sh $(NEW_VERSION) > CHANGELOG.md.tmp - mv CHANGELOG.md.tmp CHANGELOG.md -else - @echo -e "\033[0;31mNEW_VERSION is not defined. Can't update version :-(\033[0m" - exit 1 -endif -.PHONY: update-changelog - -pre-release: update-changelog $(patsubst %,pre-release-%,$(LANGUAGES)) -.PHONY: pre-release - -pre-release-%: % - if [[ -d $< ]]; then cd $< && make pre-release; fi -.PHONY: pre-release-% - -release: publish -.PHONY: release - -publish: create-and-push-release-tag $(patsubst %,publish-%,$(LANGUAGES)) -.PHONY: publish - -publish-%: % - if [[ -d $< ]]; then cd $< && make publish; fi -.PHONY: publish-% - -create-and-push-release-tag: - [ -f '/home/cukebot/configure' ] && /home/cukebot/configure - git commit --gpg-sign --all --message "Release $(LIBNAME) v$(NEW_VERSION)" - git tag --sign "$(LIBNAME)/v$(NEW_VERSION)" -m "Release $(LIBNAME) v$(NEW_VERSION)" - git push --tags -.PHONY: create-and-push-release-tag - -post-release: $(patsubst %,post-release-%,$(LANGUAGES)) -.PHONY: post-release - -post-release: commit-and-push-post-release - -post-release-%: % - if [[ -d $< ]]; then cd $< && make post-release; fi -.PHONY: post-release-% - -commit-and-push-post-release: -ifdef NEW_VERSION - git push --tags - git commit --gpg-sign --all --message "Post release $(LIBNAME) v$(NEW_VERSION)" 2> /dev/null || true - git push -else - @echo -e "\033[0;31mNEW_VERSION is not defined.\033[0m" - exit 1 -endif -.PHONY: commit-and-push-post-release - -clean: $(patsubst %,clean-%,$(LANGUAGES)) -.PHONY: clean - -clean-%: % - if [[ -d $< ]]; then cd $< && make clean; fi -.PHONY: clean-% diff --git a/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java b/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java index fe3c77c5..dd98a0d1 100644 --- a/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java +++ b/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java @@ -72,6 +72,16 @@ public int hashCode() { return Objects.hash(name, url, buildNumber, git); } + @Override + public String toString() { + return "CiEnvironmentImpl{" + + "name='" + name + '\'' + + ", url='" + url + '\'' + + ", buildNumber='" + buildNumber + '\'' + + ", git=" + git + + '}'; + } + final static class Git implements CiEnvironment.Git { public String remote; public String revision; @@ -120,5 +130,15 @@ public boolean equals(Object o) { public int hashCode() { return Objects.hash(remote, revision, branch, tag); } + + @Override + public String toString() { + return "Git{" + + "remote='" + remote + '\'' + + ", revision='" + revision + '\'' + + ", branch='" + branch + '\'' + + ", tag='" + tag + '\'' + + '}'; + } } } diff --git a/java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java b/java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java new file mode 100644 index 00000000..4c125663 --- /dev/null +++ b/java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java @@ -0,0 +1,10 @@ +package io.cucumber.createmeta; + +import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; + +public class CiEnvironmentExample { + public static void main(String[] args) { + CiEnvironment ciEnvironment = detectCiEnvironment(System.getenv()); + System.out.println("ciEnvironment = " + ciEnvironment); + } +} diff --git a/javascript/src/detectCiEnvironment.ts b/javascript/src/detectCiEnvironment.ts index e8103ee2..bbb60249 100644 --- a/javascript/src/detectCiEnvironment.ts +++ b/javascript/src/detectCiEnvironment.ts @@ -2,11 +2,8 @@ import { CiEnvironments } from './CiEnvironments' import evaluateVariableExpression from './evaluateVariableExpression' import { CiEnvironment, Env, Git } from './types' -export default function detectCiEnvironment( - env: Env, - ciEnvironments = CiEnvironments -): CiEnvironment | undefined { - for (const ciEnvironment of ciEnvironments) { +export default function detectCiEnvironment(env: Env): CiEnvironment | undefined { + for (const ciEnvironment of CiEnvironments) { const detected = detect(ciEnvironment, env) if (detected) { return detected diff --git a/javascript/test/acceptanceTest.ts b/javascript/test/acceptanceTest.ts index a82d079b..9dabed3e 100644 --- a/javascript/test/acceptanceTest.ts +++ b/javascript/test/acceptanceTest.ts @@ -3,7 +3,7 @@ import fs from 'fs' import glob from 'glob' import path from 'path' -import detectCiEnvironment from '../src/detectCiEnvironment' +import detectCiEnvironment from '../src/index' import { Env } from '../src/types' describe('detectCiEnvironment', () => { diff --git a/ruby/lib/cucumber/ci_environment.rb b/ruby/lib/cucumber/ci_environment.rb index 2d0785a2..9f0ea1cf 100644 --- a/ruby/lib/cucumber/ci_environment.rb +++ b/ruby/lib/cucumber/ci_environment.rb @@ -7,7 +7,7 @@ module CiEnvironment extend VariableExpression CI_ENVIRONMENTS_PATH = File.join(File.dirname(__FILE__), 'ci_environment/CiEnvironments.json') - def detect_ci_environment(env = ENV) + def detect_ci_environment(env) ci_environments = JSON.parse(IO.read(CI_ENVIRONMENTS_PATH)) ci_environments.each do |ci_environment| detected = detect(ci_environment, env) diff --git a/update-testdata.rb b/update-testdata.rb index b63b4165..c5ed3c02 100644 --- a/update-testdata.rb +++ b/update-testdata.rb @@ -1,10 +1,10 @@ require 'json' require 'set' -# This script can be used to update the test data when the ciDict.json file is modified. +# This script can be used to update the test data when the CiEnvironments.json file is modified. # After running this script, the testdata should be inspected and updated manually -ciDict = JSON.parse(File.read(File.dirname(__FILE__) + '/ciDict.json')) +ciDict = JSON.parse(File.read(File.dirname(__FILE__) + '/CiEnvironments.json')) def collect_vars(ob, env_vars) ob.each do |key, val| From 3f6c87eebfc4eb8f449dd5b535bc4be33f779192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 12:21:30 +0000 Subject: [PATCH 08/14] Renames --- .github/workflows/release-github.yml | 6 ++-- .github/workflows/release-java.yml | 6 ++-- .github/workflows/release-javascript.yml | 6 ++-- .github/workflows/release-ruby.yml | 6 ++-- CHANGELOG.md | 36 +++++++++---------- CONTRIBUTING.md | 2 +- README.md | 2 +- java/pom.xml | 18 +++++----- .../CiEnvironments.groovy | 4 +-- .../CiEnvironments.gsp | 2 +- .../CiEnvironment.java | 2 +- .../CiEnvironmentImpl.java | 6 ++-- .../DetectCiEnvironment.java | 2 +- .../VariableExpression.java | 2 +- .../CiEnvironmentExample.java | 4 +-- .../DetectCiEnvironmentTest.java | 4 +-- .../RemoveUserInfoFromUrlTest.java | 2 +- .../VariableExpressionTest.java | 4 +-- javascript/package-lock.json | 4 +-- javascript/package.json | 14 ++++---- javascript/tsconfig.json | 2 +- ...emspec => cucumber-ci-environment.gemspec} | 12 +++---- 22 files changed, 73 insertions(+), 73 deletions(-) rename java/src/main/groovy/io/cucumber/{createmeta => cienvironment}/CiEnvironments.groovy (85%) rename java/src/main/groovy/io/cucumber/{createmeta => cienvironment}/CiEnvironments.gsp (94%) rename java/src/main/java/io/cucumber/{createmeta => cienvironment}/CiEnvironment.java (88%) rename java/src/main/java/io/cucumber/{createmeta => cienvironment}/CiEnvironmentImpl.java (95%) rename java/src/main/java/io/cucumber/{createmeta => cienvironment}/DetectCiEnvironment.java (96%) rename java/src/main/java/io/cucumber/{createmeta => cienvironment}/VariableExpression.java (98%) rename java/src/test/java/io/cucumber/{createmeta => cienvironment}/CiEnvironmentExample.java (66%) rename java/src/test/java/io/cucumber/{createmeta => cienvironment}/DetectCiEnvironmentTest.java (96%) rename java/src/test/java/io/cucumber/{createmeta => cienvironment}/RemoveUserInfoFromUrlTest.java (97%) rename java/src/test/java/io/cucumber/{createmeta => cienvironment}/VariableExpressionTest.java (94%) rename ruby/{cucumber-create-meta.gemspec => cucumber-ci-environment.gemspec} (67%) diff --git a/.github/workflows/release-github.yml b/.github/workflows/release-github.yml index e5e17b36..13676579 100644 --- a/.github/workflows/release-github.yml +++ b/.github/workflows/release-github.yml @@ -1,14 +1,14 @@ -name: Release create-meta on github +name: Release on github on: push: branches: - release/* - + jobs: pre-release-check: - uses: cucumber/create-meta/.github/workflows/prerelease-checks.yml@main + uses: cucumber/ci-environment/.github/workflows/prerelease-checks.yml@main create-github-release: name: Create GitHub Release and Git tag diff --git a/.github/workflows/release-java.yml b/.github/workflows/release-java.yml index 45ae0b23..22d3749c 100644 --- a/.github/workflows/release-java.yml +++ b/.github/workflows/release-java.yml @@ -1,4 +1,4 @@ -name: Release create-meta maven package +name: Release maven package on: push: @@ -8,10 +8,10 @@ on: jobs: pre-release-check: - uses: cucumber/create-meta/.github/workflows/prerelease-checks.yml@main + uses: cucumber/ci-environment/.github/workflows/prerelease-checks.yml@main test-java: - uses: cucumber/create-meta/.github/workflows/test-java.yml@main + uses: cucumber/ci-environment/.github/workflows/test-java.yml@main publish-mvn: name: Publish Maven Package diff --git a/.github/workflows/release-javascript.yml b/.github/workflows/release-javascript.yml index e6a30796..91c84f1c 100644 --- a/.github/workflows/release-javascript.yml +++ b/.github/workflows/release-javascript.yml @@ -1,4 +1,4 @@ -name: Release create-meta npm module +name: Release ci-environment npm module on: push: @@ -8,10 +8,10 @@ on: jobs: pre-release-check: - uses: cucumber/create-meta/.github/workflows/prerelease-checks.yml@main + uses: cucumber/ci-environment/.github/workflows/prerelease-checks.yml@main test-javascript: - uses: cucumber/create-meta/.github/workflows/test-javascript.yml@main + uses: cucumber/ci-environment/.github/workflows/test-javascript.yml@main publish-npm: name: Publish NPM module diff --git a/.github/workflows/release-ruby.yml b/.github/workflows/release-ruby.yml index 54f903cf..b3c04e35 100644 --- a/.github/workflows/release-ruby.yml +++ b/.github/workflows/release-ruby.yml @@ -1,4 +1,4 @@ -name: Release create-meta ruby gem +name: Release ci-environment ruby gem on: push: @@ -8,10 +8,10 @@ on: jobs: pre-release-check: - uses: cucumber/create-meta/.github/workflows/prerelease-checks.yml@main + uses: cucumber/ci-environment/.github/workflows/prerelease-checks.yml@main test-ruby: - uses: cucumber/create-meta/.github/workflows/test-ruby.yml@main + uses: cucumber/ci-environment/.github/workflows/test-ruby.yml@main publish-rubygem: name: Publish Ruby Gem diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d09db2a..831ba054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,14 +10,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [6.0.4] - 2021-11-16 ### Fixed -- Update links in descriptor files to point to the new repository at https://github.com/cucumber/create-meta +- Update links in descriptor files to point to the new repository at https://github.com/cucumber/ci-environment - [Ruby][Java] Fix ciDict.json was missing from the Ruby Gem and the Java Artifact - ([PR#13](https://github.com/cucumber/create-meta/pull/13)) + ([PR#13](https://github.com/cucumber/ci-environment/pull/13)) ## [6.0.3] - 2021-11-15 ### Fixed - [JavaScript] Removed tag property from git object if it is undefined -([PR#4](https://github.com/cucumber/create-meta/pull/4)) +([PR#4](https://github.com/cucumber/ci-environment/pull/4)) ## [6.0.2] - 2021-10-18 ### Fixed @@ -112,21 +112,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - First release -[Unreleased]: https://github.com/cucumber/create-meta/compare/v6.0.4...main -[6.0.4]: https://github.com/cucumber/create-meta/compare/v6.0.3...v6.0.4 -[6.0.3]: https://github.com/cucumber/create-meta/compare/v6.0.2...v6.0.3 -[6.0.2]: https://github.com/cucumber/create-meta/compare/v6.0.1...v6.0.2 -[6.0.1]: https://github.com/cucumber/create-meta/compare/v6.0.0...v6.0.1 -[6.0.0]: https://github.com/cucumber/create-meta/compare/v5.0.0...v6.0.0 -[5.0.0]: https://github.com/cucumber/create-meta/compare/v4.0.0...v5.0.0 -[4.0.0]: https://github.com/cucumber/create-meta/compare/v3.0.0...v4.0.0 -[3.0.0]: https://github.com/cucumber/create-meta/compare/v2.0.4...v3.0.0 -[2.0.4]: https://github.com/cucumber/create-meta/compare/v2.0.2...v2.0.4 -[2.0.2]: https://github.com/cucumber/create-meta/compare/v2.0.1...v2.0.2 -[2.0.1]: https://github.com/cucumber/create-meta/compare/v2.0.0...v2.0.1 -[2.0.0]: https://github.com/cucumber/create-meta/compare/v1.2.0...v2.0.0 -[1.2.0]: https://github.com/cucumber/create-meta/compare/v1.1.0...v1.2.0 -[1.1.0]: https://github.com/cucumber/create-meta/compare/v1.0.0...v1.1.0 +[Unreleased]: https://github.com/cucumber/ci-environment/compare/v6.0.4...main +[6.0.4]: https://github.com/cucumber/ci-environment/compare/v6.0.3...v6.0.4 +[6.0.3]: https://github.com/cucumber/ci-environment/compare/v6.0.2...v6.0.3 +[6.0.2]: https://github.com/cucumber/ci-environment/compare/v6.0.1...v6.0.2 +[6.0.1]: https://github.com/cucumber/ci-environment/compare/v6.0.0...v6.0.1 +[6.0.0]: https://github.com/cucumber/ci-environment/compare/v5.0.0...v6.0.0 +[5.0.0]: https://github.com/cucumber/ci-environment/compare/v4.0.0...v5.0.0 +[4.0.0]: https://github.com/cucumber/ci-environment/compare/v3.0.0...v4.0.0 +[3.0.0]: https://github.com/cucumber/ci-environment/compare/v2.0.4...v3.0.0 +[2.0.4]: https://github.com/cucumber/ci-environment/compare/v2.0.2...v2.0.4 +[2.0.2]: https://github.com/cucumber/ci-environment/compare/v2.0.1...v2.0.2 +[2.0.1]: https://github.com/cucumber/ci-environment/compare/v2.0.0...v2.0.1 +[2.0.0]: https://github.com/cucumber/ci-environment/compare/v1.2.0...v2.0.0 +[1.2.0]: https://github.com/cucumber/ci-environment/compare/v1.1.0...v1.2.0 +[1.1.0]: https://github.com/cucumber/ci-environment/compare/v1.0.0...v1.1.0 [1.0.0]: https://github.com/cucumber/cucumber/releases/tag/v1.0.0 in alphabetical order --> diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 708f22ae..76e58b0b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # Contributing -Thank you for considering contributing to Cucumber create-meta! +Thank you for considering contributing to Cucumber ci-environment! ## Code of Conduct diff --git a/README.md b/README.md index e2a6b3bf..28577387 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ console.log(JSON.stringify(ciEnvironment, null, 2)) ### Java ```java -import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; +import static io.cucumber.cienvironment.DetectCiEnvironment.detectCiEnvironment; public class CiEnvironmentExample { public static void main(String[] args) { diff --git a/java/pom.xml b/java/pom.xml index 023493a7..eb3c16b1 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -9,21 +9,21 @@ 2.1.2 - create-meta + ci-environment 6.0.5-SNAPSHOT jar - Cucumber create meta - Produce the meta message for Cucumber-JVM - https://github.com/cucumber/create-meta + Cucumber CiEnvironment + Detect CI Environment from environment variables + https://github.com/cucumber/ci-environment - io.cucumber.createmeta + io.cucumber.cienvironment - scm:git:git://github.com/cucumber/create-meta.git - scm:git:git@github.com:cucumber/create-meta.git - git://github.com/cucumber/create-meta.git + scm:git:git://github.com/cucumber/ci-environment.git + scm:git:git@github.com:cucumber/ci-environment.git + git://github.com/cucumber/ci-environment.git @@ -79,7 +79,7 @@ execute - ${project.basedir}/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy + ${project.basedir}/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.groovy diff --git a/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy b/java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.groovy similarity index 85% rename from java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy rename to java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.groovy index 5d84b8ab..45f0dd45 100644 --- a/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.groovy +++ b/java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.groovy @@ -3,7 +3,7 @@ import groovy.json.JsonSlurper import java.nio.file.Files SimpleTemplateEngine engine = new SimpleTemplateEngine() -def templateSource = new File(project.basedir, "src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp").getText() +def templateSource = new File(project.basedir, "src/main/groovy/io/cucumber/cienvironment/CiEnvironments.gsp").getText() def jsonSlurper = new JsonSlurper() def ciEnvironments = jsonSlurper.parseText(new File(project.basedir, "../CiEnvironments.json").getText()) @@ -13,6 +13,6 @@ def toJava(s) { def binding = ["ciEnvironments": ciEnvironments, "toJava": this.&toJava] def template = engine.createTemplate(templateSource).make(binding) -def file = new File(project.basedir, "target/generated-sources/ci-environments/java/io/cucumber/createmeta/CiEnvironments.java") +def file = new File(project.basedir, "target/generated-sources/ci-environments/java/io/cucumber/cienvironment/CiEnvironments.java") Files.createDirectories(file.parentFile.toPath()) file.write(template.toString(), "UTF-8") diff --git a/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp b/java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.gsp similarity index 94% rename from java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp rename to java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.gsp index e45cc276..539d51dd 100644 --- a/java/src/main/groovy/io/cucumber/createmeta/CiEnvironments.gsp +++ b/java/src/main/groovy/io/cucumber/cienvironment/CiEnvironments.gsp @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import static java.util.Arrays.asList; diff --git a/java/src/main/java/io/cucumber/createmeta/CiEnvironment.java b/java/src/main/java/io/cucumber/cienvironment/CiEnvironment.java similarity index 88% rename from java/src/main/java/io/cucumber/createmeta/CiEnvironment.java rename to java/src/main/java/io/cucumber/cienvironment/CiEnvironment.java index 85962d55..5f53af0c 100644 --- a/java/src/main/java/io/cucumber/createmeta/CiEnvironment.java +++ b/java/src/main/java/io/cucumber/cienvironment/CiEnvironment.java @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; public interface CiEnvironment { String getName(); diff --git a/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java b/java/src/main/java/io/cucumber/cienvironment/CiEnvironmentImpl.java similarity index 95% rename from java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java rename to java/src/main/java/io/cucumber/cienvironment/CiEnvironmentImpl.java index dd98a0d1..38d98448 100644 --- a/java/src/main/java/io/cucumber/createmeta/CiEnvironmentImpl.java +++ b/java/src/main/java/io/cucumber/cienvironment/CiEnvironmentImpl.java @@ -1,10 +1,10 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import java.util.Map; import java.util.Objects; -import static io.cucumber.createmeta.DetectCiEnvironment.removeUserInfoFromUrl; -import static io.cucumber.createmeta.VariableExpression.evaluate; +import static io.cucumber.cienvironment.DetectCiEnvironment.removeUserInfoFromUrl; +import static io.cucumber.cienvironment.VariableExpression.evaluate; final class CiEnvironmentImpl implements CiEnvironment { public String name; diff --git a/java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java b/java/src/main/java/io/cucumber/cienvironment/DetectCiEnvironment.java similarity index 96% rename from java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java rename to java/src/main/java/io/cucumber/cienvironment/DetectCiEnvironment.java index 56a58be8..87fdbd1c 100644 --- a/java/src/main/java/io/cucumber/createmeta/DetectCiEnvironment.java +++ b/java/src/main/java/io/cucumber/cienvironment/DetectCiEnvironment.java @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import java.net.URI; import java.net.URISyntaxException; diff --git a/java/src/main/java/io/cucumber/createmeta/VariableExpression.java b/java/src/main/java/io/cucumber/cienvironment/VariableExpression.java similarity index 98% rename from java/src/main/java/io/cucumber/createmeta/VariableExpression.java rename to java/src/main/java/io/cucumber/cienvironment/VariableExpression.java index 0b79f111..804fa547 100644 --- a/java/src/main/java/io/cucumber/createmeta/VariableExpression.java +++ b/java/src/main/java/io/cucumber/cienvironment/VariableExpression.java @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import java.util.Map; import java.util.regex.Matcher; diff --git a/java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java b/java/src/test/java/io/cucumber/cienvironment/CiEnvironmentExample.java similarity index 66% rename from java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java rename to java/src/test/java/io/cucumber/cienvironment/CiEnvironmentExample.java index 4c125663..95ddf5ae 100644 --- a/java/src/test/java/io/cucumber/createmeta/CiEnvironmentExample.java +++ b/java/src/test/java/io/cucumber/cienvironment/CiEnvironmentExample.java @@ -1,6 +1,6 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; -import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; +import static io.cucumber.cienvironment.DetectCiEnvironment.detectCiEnvironment; public class CiEnvironmentExample { public static void main(String[] args) { diff --git a/java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java b/java/src/test/java/io/cucumber/cienvironment/DetectCiEnvironmentTest.java similarity index 96% rename from java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java rename to java/src/test/java/io/cucumber/cienvironment/DetectCiEnvironmentTest.java index 105d19c7..cf07e8dd 100644 --- a/java/src/test/java/io/cucumber/createmeta/DetectCiEnvironmentTest.java +++ b/java/src/test/java/io/cucumber/cienvironment/DetectCiEnvironmentTest.java @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; @@ -20,7 +20,7 @@ import java.util.List; import java.util.Map; -import static io.cucumber.createmeta.DetectCiEnvironment.detectCiEnvironment; +import static io.cucumber.cienvironment.DetectCiEnvironment.detectCiEnvironment; import static java.nio.file.Files.newBufferedReader; import static java.nio.file.Files.newDirectoryStream; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java b/java/src/test/java/io/cucumber/cienvironment/RemoveUserInfoFromUrlTest.java similarity index 97% rename from java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java rename to java/src/test/java/io/cucumber/cienvironment/RemoveUserInfoFromUrlTest.java index 48ecd8f8..5e5cae51 100644 --- a/java/src/test/java/io/cucumber/createmeta/RemoveUserInfoFromUrlTest.java +++ b/java/src/test/java/io/cucumber/cienvironment/RemoveUserInfoFromUrlTest.java @@ -1,4 +1,4 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import org.junit.jupiter.api.Test; diff --git a/java/src/test/java/io/cucumber/createmeta/VariableExpressionTest.java b/java/src/test/java/io/cucumber/cienvironment/VariableExpressionTest.java similarity index 94% rename from java/src/test/java/io/cucumber/createmeta/VariableExpressionTest.java rename to java/src/test/java/io/cucumber/cienvironment/VariableExpressionTest.java index 1ecb2650..5fe646b1 100644 --- a/java/src/test/java/io/cucumber/createmeta/VariableExpressionTest.java +++ b/java/src/test/java/io/cucumber/cienvironment/VariableExpressionTest.java @@ -1,10 +1,10 @@ -package io.cucumber.createmeta; +package io.cucumber.cienvironment; import org.junit.jupiter.api.Test; import java.util.HashMap; -import static io.cucumber.createmeta.VariableExpression.evaluate; +import static io.cucumber.cienvironment.VariableExpression.evaluate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; diff --git a/javascript/package-lock.json b/javascript/package-lock.json index 276a33ce..6909318d 100644 --- a/javascript/package-lock.json +++ b/javascript/package-lock.json @@ -1,11 +1,11 @@ { - "name": "@cucumber/create-meta", + "name": "@cucumber/ci-environment", "version": "6.0.4", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "@cucumber/create-meta", + "name": "@cucumber/ci-environment", "version": "6.0.4", "license": "MIT", "devDependencies": { diff --git a/javascript/package.json b/javascript/package.json index f004fa32..1e5b82fa 100644 --- a/javascript/package.json +++ b/javascript/package.json @@ -1,20 +1,20 @@ { - "name": "@cucumber/create-meta", + "name": "@cucumber/ci-environment", "version": "6.0.4", - "description": "Produce the meta message for Cucumber JavaScript", + "description": "Detect CI Environment from environment variables", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "scripts": { - "pretest": "npm run build-ci-environments", + "pretest": "npm run generate-ci-environments-ts", "test": "mocha", "prepublishOnly": "tsc --build tsconfig.build.json", "eslint-fix": "eslint --ext ts,tsx --max-warnings 0 --fix src test", "eslint": "eslint --ext ts,tsx --max-warnings 0 src test", - "build-ci-environments": "cat CiEnvironments.ts.header > src/CiEnvironments.ts && cat ../CiEnvironments.json >> src/CiEnvironments.ts && eslint --fix src/CiEnvironments.ts" + "generate-ci-environments-ts": "cat CiEnvironments.ts.header > src/CiEnvironments.ts && cat ../CiEnvironments.json >> src/CiEnvironments.ts && eslint --fix src/CiEnvironments.ts" }, "repository": { "type": "git", - "url": "git://github.com/cucumber/create-meta.git" + "url": "git://github.com/cucumber/ci-environment.git" }, "keywords": [ "cucumber" @@ -22,9 +22,9 @@ "author": "Cucumber Limited ", "license": "MIT", "bugs": { - "url": "https://github.com/cucumber/create-meta/issues" + "url": "https://github.com/cucumber/ci-environment/issues" }, - "homepage": "https://github.com/cucumber/create-meta", + "homepage": "https://github.com/cucumber/ci-environment", "devDependencies": { "@types/glob": "7.2.0", "@types/mocha": "9.0.0", diff --git a/javascript/tsconfig.json b/javascript/tsconfig.json index d67f53f4..9e8b28bc 100644 --- a/javascript/tsconfig.json +++ b/javascript/tsconfig.json @@ -8,7 +8,7 @@ "@cucumber/gherkin": ["gherkin/javascript/src/index.ts"], "@cucumber/gherkin-streams": ["gherkin-streams/javascript/src/index.ts"], "@cucumber/gherkin-utils": ["gherkin-utils/javascript/src/index.ts"], - "@cucumber/create-meta": ["create-meta/javascript/src/index.ts"], + "@cucumber/ci-environment": ["ci-environment/javascript/src/index.ts"], "@cucumber/fake-cucumber": ["fake-cucumber/javascript/src/index.ts"], "@cucumber/query": ["query/javascript/src/index.ts"], "@cucumber/compatibility-kit": ["compatibility-kit/javascript/src/index.ts"], diff --git a/ruby/cucumber-create-meta.gemspec b/ruby/cucumber-ci-environment.gemspec similarity index 67% rename from ruby/cucumber-create-meta.gemspec rename to ruby/cucumber-ci-environment.gemspec index 9a8e5b0a..aff0c344 100644 --- a/ruby/cucumber-create-meta.gemspec +++ b/ruby/cucumber-ci-environment.gemspec @@ -1,23 +1,23 @@ version = File.read(File.expand_path('VERSION', __dir__)).strip Gem::Specification.new do |s| - s.name = 'cucumber-create-meta' + s.name = 'cucumber-ci-environment' s.version = version s.authors = ['Vincent PrĂȘtre'] - s.description = 'Produce the meta message for Cucumber Ruby' + s.description = 'Detect CI Environment from environment variables' s.summary = "#{s.name}-#{s.version}" s.email = 'cukes@googlegroups.com' - s.homepage = 'https://github.com/cucumber/create-meta' + s.homepage = 'https://github.com/cucumber/ci-environment' s.platform = Gem::Platform::RUBY s.license = 'MIT' s.required_ruby_version = '>= 2.3' s.metadata = { - 'bug_tracker_uri' => 'https://github.com/cucumber/create-meta/issues', - 'changelog_uri' => 'https://github.com/cucumber/create-meta/blob/main/CHANGELOG.md', + 'bug_tracker_uri' => 'https://github.com/cucumber/ci-environment/issues', + 'changelog_uri' => 'https://github.com/cucumber/ci-environment/blob/main/CHANGELOG.md', 'documentation_uri' => 'https://cucumber.io/docs/gherkin/', 'mailing_list_uri' => 'https://groups.google.com/forum/#!forum/cukes', - 'source_code_uri' => 'https://github.com/cucumber/create-meta/tree/main/ruby' + 'source_code_uri' => 'https://github.com/cucumber/ci-environment/tree/main/ruby' } s.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' From 67ab4db955c44af7921d5ea3275de14d1c5337b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 12:35:39 +0000 Subject: [PATCH 09/14] More docs --- README.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 28577387..f24d423e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This library detects the CI environment based on environment variables defined by CI servers. -If a CI server is detected, a struct of the following structure is returned: +If a CI server is detected, a `CiEnvironment` struct is returned: ```json { @@ -18,10 +18,16 @@ If a CI server is detected, a struct of the following structure is returned: } ``` -## Usage +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. ### TypeScript +```shell +npm install @cucumber/ci-environment +``` + ```typescript import detectCiEnvironment from '@cucumber/ci-environment' @@ -31,6 +37,13 @@ console.log(JSON.stringify(ciEnvironment, null, 2)) ### Java +```xml + + io.cucumber + ci-environment + +``` + ```java import static io.cucumber.cienvironment.DetectCiEnvironment.detectCiEnvironment; @@ -44,6 +57,10 @@ public class CiEnvironmentExample { ### Ruby +```Gemfile +gem 'cucumber-ci-environment' +``` + ```ruby require 'cucumber/ci_environment' From 8627ff50f872297f2f121089befe51f6ad36e20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 12:37:49 +0000 Subject: [PATCH 10/14] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 831ba054..1aecb4f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed +- This library has been renamed to `ci-environment` and no longer depends on Cucumber `messages`. + ([PR#34](https://github.com/cucumber/ci-environment/pull/34)) + ## [6.0.4] - 2021-11-16 ### Fixed From b7b1a4e6c96a72bedf3852ae31e25874721dd140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 13:29:39 +0000 Subject: [PATCH 11/14] Copy CiEnvironments.json in CI, fail if dirty --- .github/workflows/release-ruby.yml | 4 ++++ .github/workflows/test-java.yml | 1 + .github/workflows/test-javascript.yml | 5 +++-- .github/workflows/test-ruby.yml | 1 + scripts/fail-if-dirty | 11 +++++++++++ 5 files changed, 20 insertions(+), 2 deletions(-) create mode 100755 scripts/fail-if-dirty diff --git a/.github/workflows/release-ruby.yml b/.github/workflows/release-ruby.yml index b3c04e35..44caee89 100644 --- a/.github/workflows/release-ruby.yml +++ b/.github/workflows/release-ruby.yml @@ -21,6 +21,10 @@ jobs: steps: - uses: actions/checkout@v2 + - name: Synchronize CiEnvironments.json + working-directory: ruby + run: bundle exec rake lib/cucumber/ci_environment/CiEnvironments.json + - run: ./scripts/fail-if-dirty.sh - name: Publish ruby gem uses: cucumber/action-publish-rubygem@v1.0.0 with: diff --git a/.github/workflows/test-java.yml b/.github/workflows/test-java.yml index 24f9aef2..45bb2015 100644 --- a/.github/workflows/test-java.yml +++ b/.github/workflows/test-java.yml @@ -37,3 +37,4 @@ jobs: working-directory: java - run: ./scripts/check-jar.sh working-directory: java + - run: ./scripts/fail-if-dirty.sh diff --git a/.github/workflows/test-javascript.yml b/.github/workflows/test-javascript.yml index ce0a1e7c..21fbb818 100644 --- a/.github/workflows/test-javascript.yml +++ b/.github/workflows/test-javascript.yml @@ -36,5 +36,6 @@ jobs: cache-dependency-path: javascript/package-lock.json - run: npm install-test working-directory: javascript - - run: npm run eslint - working-directory: javascript \ No newline at end of file + - run: npm run eslint-fix + working-directory: javascript + - run: ./scripts/fail-if-dirty.sh diff --git a/.github/workflows/test-ruby.yml b/.github/workflows/test-ruby.yml index ac43e024..1da17ea6 100644 --- a/.github/workflows/test-ruby.yml +++ b/.github/workflows/test-ruby.yml @@ -35,3 +35,4 @@ jobs: - name: bundle exec rake working-directory: ruby run: bundle exec rake + - run: ./scripts/fail-if-dirty.sh diff --git a/scripts/fail-if-dirty b/scripts/fail-if-dirty new file mode 100755 index 00000000..cad7407e --- /dev/null +++ b/scripts/fail-if-dirty @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# This script fails if the git working copy is dirty, and prints the diff +set -e + +diff=$(git diff) +if [[ ! -z "${diff}" ]]; then + >&2 echo "The working copy is dirty:" + >&2 echo "${diff}" + exit 1 +fi + From c1f0003fb692c5a2840acb67a16e9b3d89a008cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 13:31:42 +0000 Subject: [PATCH 12/14] Fix file path --- .github/workflows/release-ruby.yml | 2 +- .github/workflows/test-java.yml | 2 +- .github/workflows/test-javascript.yml | 2 +- .github/workflows/test-ruby.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release-ruby.yml b/.github/workflows/release-ruby.yml index 44caee89..8f170c6e 100644 --- a/.github/workflows/release-ruby.yml +++ b/.github/workflows/release-ruby.yml @@ -24,7 +24,7 @@ jobs: - name: Synchronize CiEnvironments.json working-directory: ruby run: bundle exec rake lib/cucumber/ci_environment/CiEnvironments.json - - run: ./scripts/fail-if-dirty.sh + - run: ./scripts/fail-if-dirty - name: Publish ruby gem uses: cucumber/action-publish-rubygem@v1.0.0 with: diff --git a/.github/workflows/test-java.yml b/.github/workflows/test-java.yml index 45bb2015..9d8afd66 100644 --- a/.github/workflows/test-java.yml +++ b/.github/workflows/test-java.yml @@ -37,4 +37,4 @@ jobs: working-directory: java - run: ./scripts/check-jar.sh working-directory: java - - run: ./scripts/fail-if-dirty.sh + - run: ./scripts/fail-if-dirty diff --git a/.github/workflows/test-javascript.yml b/.github/workflows/test-javascript.yml index 21fbb818..344c1649 100644 --- a/.github/workflows/test-javascript.yml +++ b/.github/workflows/test-javascript.yml @@ -38,4 +38,4 @@ jobs: working-directory: javascript - run: npm run eslint-fix working-directory: javascript - - run: ./scripts/fail-if-dirty.sh + - run: ./scripts/fail-if-dirty diff --git a/.github/workflows/test-ruby.yml b/.github/workflows/test-ruby.yml index 1da17ea6..b425808a 100644 --- a/.github/workflows/test-ruby.yml +++ b/.github/workflows/test-ruby.yml @@ -35,4 +35,4 @@ jobs: - name: bundle exec rake working-directory: ruby run: bundle exec rake - - run: ./scripts/fail-if-dirty.sh + - run: ./scripts/fail-if-dirty From 916815d914f0b24c13b9ac424a741b6a423a3ab5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 13:34:18 +0000 Subject: [PATCH 13/14] Replace install-test with ci test --- .github/workflows/test-javascript.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-javascript.yml b/.github/workflows/test-javascript.yml index 344c1649..698b315e 100644 --- a/.github/workflows/test-javascript.yml +++ b/.github/workflows/test-javascript.yml @@ -34,7 +34,9 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' cache-dependency-path: javascript/package-lock.json - - run: npm install-test + - run: npm ci + working-directory: javascript + - run: npm test working-directory: javascript - run: npm run eslint-fix working-directory: javascript From 5f5e7a8322f1ab8765c35d149a12f2c9a82bce7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aslak=20Helles=C3=B8y?= Date: Wed, 8 Dec 2021 13:36:31 +0000 Subject: [PATCH 14/14] USe the shorter install-ci-test --- .github/workflows/test-javascript.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/test-javascript.yml b/.github/workflows/test-javascript.yml index 698b315e..c1670953 100644 --- a/.github/workflows/test-javascript.yml +++ b/.github/workflows/test-javascript.yml @@ -34,9 +34,7 @@ jobs: node-version: ${{ matrix.node-version }} cache: 'npm' cache-dependency-path: javascript/package-lock.json - - run: npm ci - working-directory: javascript - - run: npm test + - run: npm install-ci-test working-directory: javascript - run: npm run eslint-fix working-directory: javascript