create github server in settings

This commit is contained in:
Slawomir Jaranowski
2020-02-22 13:44:35 +01:00
parent c17275b0ee
commit df80e35e53
3 changed files with 109 additions and 16 deletions

View File

@ -20,6 +20,11 @@ inputs:
description: 'override existing settings.xml file' description: 'override existing settings.xml file'
default: "true" default: "true"
required: false required: false
githubServer:
description: 'add to settings.xml servers server-id: github; username=$GITHUB_ACTOR and password=$GITHUB_TOKEN'
default: "true"
required: false
runs: runs:

View File

@ -24,6 +24,7 @@ THE SOFTWARE.
const core = require('@actions/core'); const core = require('@actions/core');
const os = require('os'); const os = require('os');
const process = require('process');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const DOMParser = require('xmldom').DOMParser; const DOMParser = require('xmldom').DOMParser;
@ -52,6 +53,34 @@ function writeSettings(settingsPath, templateXml) {
fs.writeFileSync(settingsPath, settingStr); fs.writeFileSync(settingsPath, settingStr);
} }
function createElementWithText(template, tag, text) {
const tagXml = template.createElement(tag);
tagXml.textContent = text;
return tagXml;
}
function fillServer(template, id, username, password) {
const serverXml = template.createElement('server');
if (!id || !username || !password) {
core.setFailed('servers must contain id, username and password');
return;
}
const idXml = createElementWithText(template, 'id', id);
serverXml.appendChild(idXml);
const usernameXml = createElementWithText(template, 'username', username);
serverXml.appendChild(usernameXml);
const passwordXml = createElementWithText(template, 'password', password);
serverXml.appendChild(passwordXml);
const serversXml = template.getElementsByTagName('servers')[0];
serversXml.appendChild(serverXml);
}
function fillServers(template) { function fillServers(template) {
const servers = core.getInput('servers'); const servers = core.getInput('servers');
@ -60,19 +89,26 @@ function fillServers(template) {
return; return;
} }
const serversXml = template.getElementsByTagName('servers')[0]; JSON.parse(servers).forEach((server) => fillServer(template, server.id, server.username, server.password));
}
JSON.parse(servers).forEach((server) => { function isInputTrue(inputName) {
const val = core.getInput(inputName);
return val && val.toLocaleLowerCase() == 'true';
}
const serverXml = template.createElement('server'); function fillServerForGithub(templateXml) {
serversXml.appendChild(serverXml);
for (const key in server) { if (!isInputTrue('githubServer')) {
const keyXml = template.createElement(key); return;
keyXml.textContent = server[key]; }
serverXml.appendChild(keyXml);
} if (!process.env['GITHUB_ACTOR'] || !process.env['GITHUB_TOKEN'] ) {
}); core.warning('GITHUB_ACTOR or GITHUB_TOKEN environment variable are not set, github server will not be added');
return;
}
fillServer(templateXml, 'github', process.env['GITHUB_ACTOR'], process.env['GITHUB_TOKEN']);
} }
function activateProfile(template, profileId) { function activateProfile(template, profileId) {
@ -106,8 +142,7 @@ function fillProperties(template) {
} }
function addSonatypeSnapshots(template) { function addSonatypeSnapshots(template) {
const val = core.getInput('sonatypeSnapshots'); if (isInputTrue('sonatypeSnapshots')) {
if (val && val.toLocaleLowerCase() == 'true') {
activateProfile(template, '_sonatype-snapshots_') activateProfile(template, '_sonatype-snapshots_')
} }
} }
@ -119,8 +154,7 @@ function generate() {
core.info('Prepare maven setings: ' + settingsPath); core.info('Prepare maven setings: ' + settingsPath);
if (fs.existsSync(settingsPath)) { if (fs.existsSync(settingsPath)) {
const val = core.getInput("override"); if (isInputTrue('override')) {
if (val && val.toLocaleLowerCase() == 'true') {
core.info('maven settings.xml already exists - override'); core.info('maven settings.xml already exists - override');
} else { } else {
core.warning('maven settings.xml already exists - skip'); core.warning('maven settings.xml already exists - skip');
@ -130,6 +164,7 @@ function generate() {
const templateXml = getSettingsTemplate(); const templateXml = getSettingsTemplate();
fillServers(templateXml); fillServers(templateXml);
fillServerForGithub(templateXml);
fillProperties(templateXml); fillProperties(templateXml);
addSonatypeSnapshots(templateXml); addSonatypeSnapshots(templateXml);
writeSettings(settingsPath, templateXml); writeSettings(settingsPath, templateXml);
@ -156,6 +191,7 @@ module.exports = {
getSettingsTemplate, getSettingsTemplate,
writeSettings, writeSettings,
fillServers, fillServers,
fillServerForGithub,
fillProperties, fillProperties,
addSonatypeSnapshots, addSonatypeSnapshots,
generate, generate,

View File

@ -87,7 +87,7 @@ afterAll(() => {
afterEach(() => { afterEach(() => {
for (key in process.env) { for (key in process.env) {
if (key.match(/^INPUT_/)) { if (key.match(/^INPUT_/) || key.match(/^GITHUB_/)) {
delete process.env[key]; delete process.env[key];
} }
} }
@ -162,6 +162,58 @@ test('fillServers two servers', () => {
"</servers>"); "</servers>");
}); });
test('fillServers incorrect fields', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"idx": "id1"}]';
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe('<servers/>');
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/::error::servers must contain id, username and password/)
])
);
});
test('fillServers github', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_GITHUBSERVER'] = 'true';
process.env['GITHUB_ACTOR'] = 'githubActor';
process.env['GITHUB_TOKEN'] = 'githubToken';
settings.fillServerForGithub(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe('<servers><server><id>github</id><username>githubActor</username><password>githubToken</password></server></servers>');
expect(consoleOutput).toEqual([]);
});
test('fillServers github - no GITHUB_* env', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_GITHUBSERVER'] = 'true';
settings.fillServerForGithub(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe('<servers/>');
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/::warning::GITHUB_ACTOR or GITHUB_TOKEN environment variable are not set, github server will not be added/)
])
);
});
test('addSonatypeSnapshots activate', () => { test('addSonatypeSnapshots activate', () => {
process.env['INPUT_SONATYPESNAPSHOTS'] = "true"; process.env['INPUT_SONATYPESNAPSHOTS'] = "true";
@ -279,7 +331,7 @@ test('cleanup - ok', () => {
test('genereate', () => { test('genereate', () => {
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]'; process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]';
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]' process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'
process.env['INPUT_SONATYPESNAPSHOT'] = true; process.env['INPUT_SONATYPESNAPSHOT'] = true;