Skip to content

Commit

Permalink
Merge pull request #670 from codecov/feature/bitrise-provider
Browse files Browse the repository at this point in the history
feat: add bitrise provider
  • Loading branch information
mitchell-codecov authored Mar 21, 2022
2 parents 6c2c486 + f02e1cf commit a9d7e5a
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ci_providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IProvider } from '../types'
import * as providerAppveyorci from './provider_appveyorci'
import * as providerAzurepipelines from './provider_azurepipelines'
import * as providerBitbucket from './provider_bitbucket'
import * as providerBitrise from './provider_bitrise'
import * as providerBuildkite from './provider_buildkite'
import * as providerCircleci from './provider_circleci'
import * as providerCirrus from './provider_cirrus'
Expand All @@ -23,6 +24,7 @@ const providerList: IProvider[] = [
providerAppveyorci,
providerAzurepipelines,
providerBitbucket,
providerBitrise,
providerBuildkite,
providerCircleci,
providerCirrus,
Expand Down
74 changes: 74 additions & 0 deletions src/ci_providers/provider_bitrise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { IServiceParams, UploaderEnvs, UploaderInputs } from '../types'

import { parseSlugFromRemoteAddr } from '../helpers/git'

export function detect(envs: UploaderEnvs): boolean {
return Boolean(envs.CI) && Boolean(envs.BITRISE_IO)
}

function _getBuild(inputs: UploaderInputs): string {
const { args, environment: envs } = inputs
return args.build || envs.BITRISE_BUILD_NUMBER || ''
}

function _getBuildURL(inputs: UploaderInputs): string {
const { environment: envs } = inputs
return envs.BITRISE_BUILD_URL || ''
}

function _getBranch(inputs: UploaderInputs): string {
const { args, environment: envs } = inputs
return args.branch || envs.BITRISE_GIT_BRANCH || ''
}

function _getJob() {
return ''
}

function _getPR(inputs: UploaderInputs): string {
const { args, environment: envs } = inputs
return args.pr || envs.BITRISE_PULL_REQUEST || ''
}

function _getService(): string {
return 'bitrise'
}

export function getServiceName(): string {
return 'Bitrise CI'
}

function _getSHA(inputs: UploaderInputs): string {
const { args, environment: envs } = inputs
return args.sha || envs.GIT_CLONE_COMMIT_HASH || ''
}

function _getSlug(inputs: UploaderInputs): string {
const { args } = inputs
return args.slug || parseSlugFromRemoteAddr('') || ''
}

export function getServiceParams(inputs: UploaderInputs): IServiceParams {
return {
branch: _getBranch(inputs),
build: _getBuild(inputs),
buildURL: _getBuildURL(inputs),
commit: _getSHA(inputs),
job: _getJob(),
pr: _getPR(inputs),
service: _getService(),
slug: _getSlug(inputs),
}
}

export function getEnvVarNames(): string[] {
return [
'BITRISE_BUILD_NUMBER',
'BITRISE_BUILD_URL',
'BITRISE_GIT_BRANCH',
'BITRISE_IO',
'BITRISE_PULL_REQUEST',
'CI',
'GIT_CLONE_COMMIT_HASH',
]
}
177 changes: 177 additions & 0 deletions test/providers/provider_bitrise.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import td from 'testdouble'
import childProcess from 'child_process'
import { IServiceParams, UploaderInputs } from '../../src/types'
import { createEmptyArgs } from '../test_helpers'

import * as providerBitrise from '../../src/ci_providers//provider_bitrise'

describe('Bitrise Params', () => {
afterEach(() => {
td.reset()
})

describe('detect()', () => {
it('does not run without Bitrise env variable', () => {
const inputs: UploaderInputs = {
args: { ...createEmptyArgs() },
environment: {},
}
const detected = providerBitrise.detect(inputs.environment)
expect(detected).toBeFalsy()
})

it('does not run with only CI env variable', () => {
const inputs: UploaderInputs= {
args: { ...createEmptyArgs() },
environment: {
CI: 'true',
},
}
const detected = providerBitrise.detect(inputs.environment)
expect(detected).toBeFalsy()
})

it('runs with Bitrise env variables', () => {
const inputs: UploaderInputs= {
args: { ...createEmptyArgs() },
environment: {
BITRISE_IO: 'true',
CI: 'true',
},
}
const detected = providerBitrise.detect(inputs.environment)
expect(detected).toBeTruthy()
})
})

// This should test that the provider outputs proper default values
it('gets the correct params on no env variables', () => {
const inputs: UploaderInputs = {
args: { ...createEmptyArgs() },
environment: {
BITRISE_IO: 'true',
CI: 'true',
},
}
const expected: IServiceParams = {
branch: '',
build: '',
buildURL: '',
commit: '',
job: '',
pr: '',
service: 'bitrise',
slug: '',
}
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['config', '--get', 'remote.origin.url']),
).thenReturn({ stdout: '' })

const params = providerBitrise.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})

// This should test that the provider outputs proper parameters when a push event is created
it('gets the correct params on push', () => {
const inputs: UploaderInputs = {
args: { ...createEmptyArgs() },
environment: {
BITRISE_BUILD_NUMBER: '2',
BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2',
BITRISE_GIT_BRANCH: 'main',
BITRISE_IO: 'true',
CI: 'true',
GIT_CLONE_COMMIT_HASH: 'testingSha',
},
}
const expected: IServiceParams = {
branch: 'main',
build: '2',
buildURL: 'https://bitrise.com/testOrg/testRepo/2',
commit: 'testingSha',
job: '',
pr: '',
service: 'bitrise',
slug: 'testOrg/testRepo',
}
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['config', '--get', 'remote.origin.url']),
).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' })
const params = providerBitrise.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})

it('gets the correct params on pr', () => {
const inputs: UploaderInputs = {
args: { ...createEmptyArgs() },
environment: {
BITRISE_BUILD_NUMBER: '2',
BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2',
BITRISE_GIT_BRANCH: 'main',
BITRISE_IO: 'true',
BITRISE_PULL_REQUEST: '3',
CI: 'true',
GIT_CLONE_COMMIT_HASH: 'testingSha',
},
}
const expected: IServiceParams = {
branch: 'main',
build: '2',
buildURL: 'https://bitrise.com/testOrg/testRepo/2',
commit: 'testingSha',
job: '',
pr: '3',
service: 'bitrise',
slug: 'testOrg/testRepo',
}
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['config', '--get', 'remote.origin.url']),
).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' })
const params = providerBitrise.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})

// This should test that the provider outputs proper parameters when given overrides
it('gets the correct params on overrides', () => {
const inputs: UploaderInputs = {
args: {
...createEmptyArgs(),
...{
branch: 'test',
build: '10',
pr: '11',
sha: 'otherTestingSha',
slug: 'neworg/newRepo',
},
},
environment: {
BITRISE_BUILD_NUMBER: '2',
BITRISE_BUILD_URL: 'https://bitrise.com/testOrg/testRepo/2',
BITRISE_GIT_BRANCH: 'main',
BITRISE_IO: 'true',
BITRISE_PULL_REQUEST: '3',
CI: 'true',
GIT_CLONE_COMMIT_HASH: 'testingSha',
},
}
const expected: IServiceParams = {
branch: 'test',
build: '10',
buildURL: 'https://bitrise.com/testOrg/testRepo/2',
commit: 'otherTestingSha',
job: '',
pr: '11',
service: 'bitrise',
slug: 'neworg/newRepo',
}
const spawnSync = td.replace(childProcess, 'spawnSync')
td.when(
spawnSync('git', ['config', '--get', 'remote.origin.url']),
).thenReturn({ stdout: 'https://github.com/testOrg/testRepo.git' })
const params = providerBitrise.getServiceParams(inputs)
expect(params).toMatchObject(expected)
})
})

0 comments on commit a9d7e5a

Please sign in to comment.