-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #670 from codecov/feature/bitrise-provider
feat: add bitrise provider
- Loading branch information
Showing
3 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |