Skip to content

Commit

Permalink
fix(teamcity): read branch and root from environment variables if…
Browse files Browse the repository at this point in the history
… absent from properties file
  • Loading branch information
pvdlg committed Nov 19, 2018
1 parent 11a0389 commit ff4b400
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
23 changes: 12 additions & 11 deletions services/teamcity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

const javaProperties = require('java-properties');

function getProperties(env) {
const file = env.TEAMCITY_BUILD_PROPERTIES_FILE;
if (!file) {
return {};
}
const properties = javaProperties.of(file);
return {
root: properties.get('teamcity.build.workingDir'),
branch: properties.get('teamcity.build.branch'),
};
}
const PROPERTIES_MAPPING = {root: 'teamcity.build.workingDir', branch: 'teamcity.build.branch'};

const getProperties = env => {
const properties = env.TEAMCITY_BUILD_PROPERTIES_FILE
? javaProperties.of(env.TEAMCITY_BUILD_PROPERTIES_FILE)
: undefined;

return Object.keys(PROPERTIES_MAPPING).reduce((result, key) => {
const property = properties ? properties.get(PROPERTIES_MAPPING[key]) : undefined;
return Object.assign(result, {[key]: typeof property === 'undefined' ? env[PROPERTIES_MAPPING[key]] : property});
}, {});
};

module.exports = {
detect({env}) {
Expand Down
54 changes: 43 additions & 11 deletions test/services/teamcity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,7 @@ const env = {
TEAMCITY_BUILDCONF_NAME: 'owner/repo',
};

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

test('Push', t => {
test('Push - with properties file', t => {
const propertiesFile = tempy.file({extension: 'properties'});
const properties = ['teamcity.build.branch=master', 'teamcity.build.workingDir=/'];
fs.writeFileSync(propertiesFile, properties.join('\n') + '\n');
Expand All @@ -35,3 +25,45 @@ test('Push', t => {
slug: 'owner/repo',
});
});

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

test('Push - prioritize properties file values', t => {
const propertiesFile = tempy.file({extension: 'properties'});
const properties = ['teamcity.build.branch=master'];
fs.writeFileSync(propertiesFile, properties.join('\n') + '\n');

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

0 comments on commit ff4b400

Please sign in to comment.