Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed the attribute support for servers. #95

Merged
merged 5 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ afterAll(() => {

test('run with all feature', () => {

process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]';
process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]';
process.env['INPUT_GITHUBSERVER'] = true;
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]';
awhitford marked this conversation as resolved.
Show resolved Hide resolved
process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]';
process.env['INPUT_GITHUBSERVER'] = true;

process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]';
process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]';
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'

process.env['INPUT_APACHESNAPSHOTS'] = true;
Expand All @@ -80,7 +80,7 @@ test('run with all feature', () => {
expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0);

const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, '');
const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^\s*$(?:\r\n?|\n)/gm, '');
expect(settingsBody).toBe(`<settings>
<interactiveMode>false</interactiveMode>
<profiles>
Expand Down Expand Up @@ -214,7 +214,6 @@ test('run with all feature', () => {
<id>github</id>
<username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password>

</server></servers>
<mirrors>
<mirror>
Expand Down
41 changes: 24 additions & 17 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function jsonToXml(templateXml, xmlTag, json) {
for (const key in json) {
const keyXml = templateXml.createElement(key);
const value = json[key];
if ( value instanceof Object) {
if (value instanceof Object) {
jsonToXml(templateXml, keyXml, value);
} else {
keyXml.textContent = value;
Expand All @@ -63,28 +63,32 @@ function jsonToXml(templateXml, xmlTag, json) {
}
}

function fillServer(templateXml, templateName, id, username, password, configurations) {
function fillServer(templateXml, templateName, id, username, password, privateKey, passphrase, filePermissions, directoryPermissions, configurations) {

if (!id || ((!username || !password) && !configurations) ) {
core.setFailed(templateName + ' must contain id, (username and password) or configuration');
if (!id || (!username && !configurations)) {
core.setFailed(templateName + ' must contain id, and username or configuration');
return;
}

const serverXml = getTemplate(templateName + '.xml')
serverXml.getElementsByTagName('id')[0].textContent = id;

const usernameTag = serverXml.getElementsByTagName('username')[0];
if (username) {
usernameTag.textContent = username;
} else {
serverXml.documentElement.removeChild(usernameTag);
}

const passwordTag = serverXml.getElementsByTagName('password')[0];
if (password) {
passwordTag.textContent = password;
} else {
serverXml.documentElement.removeChild(passwordTag);
const serverTags = {
'username': username,
'password': password,
'privateKey': privateKey,
'passphrase': passphrase,
'filePermissions': filePermissions,
'directoryPermissions': directoryPermissions
};
for (const tag in serverTags) {
const serverTag = serverXml.getElementsByTagName(tag)[0];
const tagValue = serverTags[tag];
if (tagValue) {
serverTag.textContent = tagValue;
} else {
serverXml.documentElement.removeChild(serverTag);
}
}

const configurationTag = serverXml.getElementsByTagName('configuration')[0];
Expand All @@ -109,7 +113,10 @@ function fillServers(template, templateName) {
}

JSON.parse(servers).forEach((server) =>
fillServer(template, templateName, server.id, server.username, server.password, server.configuration));
fillServer(template, templateName, server.id, server.username,
server.password, server.privateKey, server.passphrase,
server.filePermissions, server.directoryPermissions,
server.configuration));
}

function fillMirror(template, id, name, mirrorOf, url) {
Expand Down
89 changes: 36 additions & 53 deletions settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');

var consoleOutput = [];

function stringAsXml(str) {
return new DOMParser().parseFromString(str, 'text/xml');
}

function xmlAsString(xml) {
return new XMLSerializer().serializeToString(xml).replace(/^\s*$(?:\r\n?|\n)/gm, '');
}

beforeAll(() => {
if (!fs.existsSync(testHomePath)) {
fs.mkdirSync(testHomePath);
Expand All @@ -51,7 +59,7 @@ beforeAll(() => {
});

beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`<settings>
xmlTestProfile = stringAsXml(`<settings>
<profiles>
<profile>
<id>_properties_</id>
Expand Down Expand Up @@ -119,7 +127,7 @@ test('xml should be write', () => {

test('fillServers do nothing if no params', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

settings.fillServers(xml, 'servers');

Expand All @@ -130,54 +138,46 @@ test('fillServers do nothing if no params', () => {

test('fillServers one server', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
<password>password1</password>

</server></servers>`);

});

test('fillServers with username and configuration', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>

<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});

test('fillServers with username, password and configuration', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
Expand All @@ -188,71 +188,59 @@ test('fillServers with username, password and configuration', () => {

test('fillServers with configuration', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>


<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});

test('fillServers with configuration subLevel', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>


<configuration><prop1><prop11>value11</prop11><prop12>value12</prop12></prop1><prop2>value2</prop2></configuration>
</server></servers>`);
});

test('fillServers two servers', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\
{"id": "id2", "username": "username2", "password":"password2"}]';

settings.fillServers(xml, 'servers');

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
<password>password1</password>

</server>
<server>
<id>id2</id>
<username>username2</username>
<password>password2</password>

</server></servers>`);
});

test('fill servers incorrect fields', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_SERVERS'] = '[{"idx": "id1"}]';

Expand All @@ -263,22 +251,20 @@ test('fill servers incorrect fields', () => {
expect(xmlStr).toBe('<servers/>');
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/)
expect.stringMatching(/::error::servers must contain id, and username or configuration/)
])
);
});

test('fill oracleServers', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_ORACLESERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';

settings.fillServers(xml, 'oracleServers');

const xmlStr = new XMLSerializer().serializeToString(xml);

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
Expand All @@ -305,27 +291,24 @@ test('fill oracleServers', () => {

test('fillServers github', () => {

const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");

process.env['INPUT_GITHUBSERVER'] = 'true';

settings.fillServerForGithub(xml);

const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');

expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>github</id>
<username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password>

</server></servers>`);
expect(consoleOutput).toEqual([]);
});

test('fillMirrors do nothing if no params', () => {

const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");

settings.fillMirrors(xml);

Expand All @@ -337,7 +320,7 @@ test('fillMirrors do nothing if no params', () => {

test('fillMirrors one mirror', () => {

const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");

process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name", "mirrorOf":"mirrorOf", "url":"url"}]';

Expand All @@ -357,7 +340,7 @@ test('fillMirrors one mirror', () => {

test('fillMirrors two mirrors', () => {

const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");

process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name1", "mirrorOf":"mirrorOf1", "url":"url1"},{"id": "id2", "name": "name2", "mirrorOf":"mirrorOf2", "url":"url2"}]';

Expand All @@ -383,7 +366,7 @@ test('fillMirrors two mirrors', () => {

test('fillMirrors incorrect fields', () => {

const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");

process.env['INPUT_MIRRORS'] = '[{"idx": "id1"}]';

Expand All @@ -403,7 +386,7 @@ test('addApacheSnapshots', () => {

process.env['INPUT_APACHESNAPSHOTS'] = "true";

const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');

settings.addApacheSnapshots(xml);

Expand Down Expand Up @@ -445,7 +428,7 @@ test('addSonatypeSnapshots', () => {

process.env['INPUT_SONATYPESNAPSHOTS'] = "true";

const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');

settings.addSonatypeSnapshots(xml);

Expand Down Expand Up @@ -487,7 +470,7 @@ test('addOracleRepo', () => {

process.env['INPUT_ORACLEREPO'] = "true";

const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');

settings.addOracleRepo(xml);

Expand Down Expand Up @@ -529,7 +512,7 @@ test('fillProperties', () => {

process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]';

const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');

settings.fillProperties(xml);

Expand Down
Loading