Skip to content

Commit

Permalink
fix(teamcity): fallback for missing/erroneous properties file
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Dec 17, 2018
1 parent 00d5fb1 commit 345476f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
22 changes: 16 additions & 6 deletions services/teamcity.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
// https://confluence.jetbrains.com/display/TCD10/Predefined+Build+Parameters

const javaProperties = require('java-properties');
const {branch} = require('../lib/git');

const PROPERTIES_MAPPING = {root: 'teamcity.build.workingDir', branch: 'teamcity.build.branch'};

const getProperties = env => {
const safeReadProperties = filePath => {
try {
return javaProperties.of(filePath);
} catch (error) {
return undefined;
}
};

const getProperties = ({env, cwd}) => {
const buildProperties = env.TEAMCITY_BUILD_PROPERTIES_FILE
? javaProperties.of(env.TEAMCITY_BUILD_PROPERTIES_FILE)
? safeReadProperties(env.TEAMCITY_BUILD_PROPERTIES_FILE)
: undefined;
const configFile = buildProperties ? buildProperties.get('teamcity.configuration.properties.file') : undefined;
const configProperties = configFile ? javaProperties.of(configFile) : configFile;
const configProperties = configFile ? safeReadProperties(configFile) : configFile;

return Object.keys(PROPERTIES_MAPPING).reduce(
(result, key) =>
Object.assign(result, {
[key]:
(buildProperties ? buildProperties.get(PROPERTIES_MAPPING[key]) : undefined) ||
(configProperties ? configProperties.get(PROPERTIES_MAPPING[key]) : undefined),
(configProperties ? configProperties.get(PROPERTIES_MAPPING[key]) : undefined) ||
(key === 'branch' ? branch({env, cwd}) : undefined),
}),
{}
);
Expand All @@ -26,7 +36,7 @@ module.exports = {
detect({env}) {
return Boolean(env.TEAMCITY_VERSION);
},
configuration({env}) {
configuration({env, cwd}) {
return Object.assign(
{
name: 'TeamCity',
Expand All @@ -35,7 +45,7 @@ module.exports = {
build: env.BUILD_NUMBER,
slug: env.TEAMCITY_BUILDCONF_NAME,
},
getProperties(env)
getProperties({env, cwd})
);
},
};
62 changes: 50 additions & 12 deletions test/services/teamcity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import test from 'ava';
import tempy from 'tempy';
import teamcity from '../../services/teamcity';
import {gitRepo} from '../helpers/git-utils';

const env = {
TEAMCITY_VERSION: '2017.1.2 (build 46812)',
Expand All @@ -26,18 +27,6 @@ test('Push - with build properties file', t => {
});
});

test('Push - without build properties file', t => {
t.deepEqual(teamcity.configuration({env}), {
name: 'TeamCity',
service: 'teamcity',
commit: '5678',
build: '91011',
branch: undefined,
root: undefined,
slug: 'owner/repo',
});
});

test('Push - with build and config properties files', t => {
const buildFile = tempy.file({extension: 'properties'});
const configFile = tempy.file({extension: 'properties'});
Expand Down Expand Up @@ -79,3 +68,52 @@ test('Push - prioritize build properties file values', t => {
slug: 'owner/repo',
});
});

test('Push - without build properties file', async t => {
const {cwd} = await gitRepo(true);

t.deepEqual(teamcity.configuration({env, cwd}), {
name: 'TeamCity',
service: 'teamcity',
commit: '5678',
build: '91011',
branch: 'master',
root: undefined,
slug: 'owner/repo',
});
});

test('Push - with build and missing config properties files', async t => {
const {cwd} = await gitRepo(true);

const buildFile = tempy.file({extension: 'properties'});
const buildProperties = ['teamcity.build.branch=master', 'teamcity.configuration.properties.file=/tmp/null'];
fs.writeFileSync(buildFile, buildProperties.join('\n') + '\n');

t.deepEqual(teamcity.configuration({env: Object.assign({}, env, {TEAMCITY_BUILD_PROPERTIES_FILE: buildFile}), cwd}), {
name: 'TeamCity',
service: 'teamcity',
commit: '5678',
build: '91011',
branch: 'master',
root: undefined,
slug: 'owner/repo',
});
});

test('Push - with missing build properties files', async t => {
const {cwd} = await gitRepo(true);

t.deepEqual(
teamcity.configuration({env: Object.assign({}, env, {TEAMCITY_BUILD_PROPERTIES_FILE: '/tmp/null'}), cwd}),
{
name: 'TeamCity',
service: 'teamcity',
commit: '5678',
build: '91011',
branch: 'master',
root: undefined,
slug: 'owner/repo',
}
);
});

0 comments on commit 345476f

Please sign in to comment.