diff --git a/src/ci_providers/index.ts b/src/ci_providers/index.ts index 5055427db..a757a896c 100644 --- a/src/ci_providers/index.ts +++ b/src/ci_providers/index.ts @@ -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' @@ -23,6 +24,7 @@ const providerList: IProvider[] = [ providerAppveyorci, providerAzurepipelines, providerBitbucket, + providerBitrise, providerBuildkite, providerCircleci, providerCirrus, diff --git a/src/ci_providers/provider_bitrise.ts b/src/ci_providers/provider_bitrise.ts new file mode 100644 index 000000000..bdcccb370 --- /dev/null +++ b/src/ci_providers/provider_bitrise.ts @@ -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', + ] +} diff --git a/test/providers/provider_bitrise.test.ts b/test/providers/provider_bitrise.test.ts new file mode 100644 index 000000000..397640c7b --- /dev/null +++ b/test/providers/provider_bitrise.test.ts @@ -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) + }) +})