Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make buildNumber optional #51

Merged
merged 2 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- [JavaScript] make `buildNumber` optional ([#51](https://github.com/cucumber/ci-environment/pull/51))

## [8.1.0] - 2022-01-02
### Added
- [JavaScript] - package as hybrid esm/commonjs module ([#47](https://github.com/cucumber/ci-environment/pull/47))
Expand Down
18 changes: 6 additions & 12 deletions CiEnvironments.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"git": {
"remote": "${bamboo_planRepository_repositoryUrl}",
"revision": "${bamboo_planRepository_revision}",
"branch": "${bamboo_planRepository_branch}",
"tag": null
"branch": "${bamboo_planRepository_branch}"
}
},
{
Expand Down Expand Up @@ -61,8 +60,7 @@
"git": {
"remote": "${CF_COMMIT_URL/(.*)\\/commit.+$/\\1}.git",
"revision": "${CF_REVISION}",
"branch": "${CF_BRANCH}",
"tag": null
"branch": "${CF_BRANCH}"
}
},
{
Expand All @@ -72,8 +70,7 @@
"git": {
"remote": "${CI_PULL_REQUEST/(.*)\\/pull\\/\\d+/\\1.git}",
"revision": "${CI_COMMIT_ID}",
"branch": "${CI_BRANCH}",
"tag": null
"branch": "${CI_BRANCH}"
}
},
{
Expand Down Expand Up @@ -105,8 +102,7 @@
"git": {
"remote": "${GO_SCM_*_PR_URL/(.*)\\/pull\\/\\d+/\\1.git}",
"revision": "${GO_REVISION}",
"branch": "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}",
"tag": null
"branch": "${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}"
}
},
{
Expand All @@ -116,8 +112,7 @@
"git": {
"remote": "${GIT_URL}",
"revision": "${GIT_COMMIT}",
"branch": "${GIT_LOCAL_BRANCH}",
"tag": null
"branch": "${GIT_LOCAL_BRANCH}"
}
},
{
Expand Down Expand Up @@ -149,8 +144,7 @@
"git": {
"remote": "https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}.git",
"revision": "${WERCKER_GIT_COMMIT}",
"branch": "${WERCKER_GIT_BRANCH}",
"tag": null
"branch": "${WERCKER_GIT_BRANCH}"
}
}
]
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ by CI servers.

If a CI server is detected, a `CiEnvironment` struct is returned:

```json
```javascript
{
"name": "...",
"url": "...",
"buildNumber": "...",
"git": {
"remote": "...",
"buildNumber": "...", // optional
"git": { // optional
"remote": "...",
"revision": "...",
"branch": "...",
"tag": "..."
"branch": "...", // optional
"tag": "..." // optional
}
}
```

Note that some fields are optional (they may not be set if the corresponding environment
variables are not defined).

Some CI servers expose usernames and passwords in the environment variable
that is used to detect `git.remote`. For security reasons, this library removes
the username and password from the `git.remote` field in the `CiEnvironment` struct.
Expand Down
6 changes: 0 additions & 6 deletions javascript/src/CiEnvironments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: '${bamboo_planRepository_repositoryUrl}',
revision: '${bamboo_planRepository_revision}',
branch: '${bamboo_planRepository_branch}',
tag: null,
},
},
{
Expand Down Expand Up @@ -66,7 +65,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: '${CF_COMMIT_URL/(.*)\\/commit.+$/\\1}.git',
revision: '${CF_REVISION}',
branch: '${CF_BRANCH}',
tag: null,
},
},
{
Expand All @@ -77,7 +75,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: '${CI_PULL_REQUEST/(.*)\\/pull\\/\\d+/\\1.git}',
revision: '${CI_COMMIT_ID}',
branch: '${CI_BRANCH}',
tag: null,
},
},
{
Expand Down Expand Up @@ -110,7 +107,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: '${GO_SCM_*_PR_URL/(.*)\\/pull\\/\\d+/\\1.git}',
revision: '${GO_REVISION}',
branch: '${GO_SCM_*_PR_BRANCH/.*:(.*)/\\1}',
tag: null,
},
},
{
Expand All @@ -121,7 +117,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: '${GIT_URL}',
revision: '${GIT_COMMIT}',
branch: '${GIT_LOCAL_BRANCH}',
tag: null,
},
},
{
Expand Down Expand Up @@ -154,7 +149,6 @@ export const CiEnvironments: readonly CiEnvironment[] = [
remote: 'https://${WERCKER_GIT_DOMAIN}/${WERCKER_GIT_OWNER}/${WERCKER_GIT_REPOSITORY}.git',
revision: '${WERCKER_GIT_COMMIT}',
branch: '${WERCKER_GIT_BRANCH}',
tag: null,
},
},
]
10 changes: 5 additions & 5 deletions javascript/src/detectCiEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ export function removeUserInfoFromUrl(value: string): string {
}

function detectGit(ciEnvironment: CiEnvironment, env: Env): Git | undefined {
const revision = evaluateVariableExpression(ciEnvironment.git.revision, env)
const revision = evaluateVariableExpression(ciEnvironment.git?.revision, env)
if (!revision) {
return undefined
}

const remote = evaluateVariableExpression(ciEnvironment.git.remote, env)
const remote = evaluateVariableExpression(ciEnvironment.git?.remote, env)
if (!remote) {
return undefined
}

const tag = evaluateVariableExpression(ciEnvironment.git.tag, env)
const branch = evaluateVariableExpression(ciEnvironment.git.branch, env)
const tag = evaluateVariableExpression(ciEnvironment.git?.tag, env)
const branch = evaluateVariableExpression(ciEnvironment.git?.branch, env)

return {
revision,
Expand All @@ -47,12 +47,12 @@ function detectGit(ciEnvironment: CiEnvironment, env: Env): Git | undefined {

function detect(ciEnvironment: CiEnvironment, env: Env): CiEnvironment | undefined {
const url = evaluateVariableExpression(ciEnvironment.url, env)
const buildNumber = evaluateVariableExpression(ciEnvironment.buildNumber, env)
if (url === undefined) {
// The url is what consumers will use as the primary key for a build
// If this cannot be determined, we return nothing.
return undefined
}
const buildNumber = evaluateVariableExpression(ciEnvironment.buildNumber, env)
const git = detectGit(ciEnvironment, env)

return {
Expand Down
2 changes: 1 addition & 1 deletion javascript/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type CiEnvironment = {
name: string
url: string
buildNumber: string
buildNumber?: string
git?: Git
}

Expand Down
10 changes: 1 addition & 9 deletions javascript/test/removeUserInfoFromUrlTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ import assert from 'assert'
import { removeUserInfoFromUrl } from '../src/detectCiEnvironment.js'

describe('removeUserInfoFromUrl', () => {
it('returns undefined for undefined', () => {
assert.strictEqual(removeUserInfoFromUrl(undefined), undefined)
})

it('returns null for null', () => {
assert.strictEqual(removeUserInfoFromUrl(null), null)
})

it('returns empty string for empty string', () => {
assert.strictEqual(removeUserInfoFromUrl(null), null)
assert.strictEqual(removeUserInfoFromUrl(''), '')
})

it('leaves the data intact when no sensitive information is detected', () => {
Expand Down
2 changes: 1 addition & 1 deletion javascript/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"outDir": "dist",
"downlevelIteration": true,
"skipLibCheck": true,
"strictNullChecks": false,
"strictNullChecks": true,
"experimentalDecorators": true,
"noEmit": true
}
Expand Down
1 change: 1 addition & 0 deletions ruby/lib/cucumber/ci_environment/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CiEnvironments.json
156 changes: 0 additions & 156 deletions ruby/lib/cucumber/ci_environment/CiEnvironments.json

This file was deleted.