From 1b34539e65d1db6389715369b354ea124699f95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Steinbr=C3=BCck?= Date: Sun, 5 Apr 2020 16:26:31 +0200 Subject: [PATCH] feat: add mirror support --- action.yml | 3 ++ settings.js | 38 ++++++++++++++++++ settings.test.js | 61 +++++++++++++++++++++++++++- templates/settings.xml | 90 +++++++++++++++++++++--------------------- 4 files changed, 144 insertions(+), 48 deletions(-) diff --git a/action.yml b/action.yml index 62556e63..051aadc6 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,9 @@ inputs: servers: description: 'servers definition in joson array, eg: [{"id": "serverId", "username": "username", "password": "password"}]' required: false + mirrors: + description: 'mirrors definition in json array, eg: [{"id": "id", "name": "name", "mirrorOf": "mirrorOf", "url: "url"}]' + required: false properties: description: 'json array with properties, eg [{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]' required: false diff --git a/settings.js b/settings.js index f6d9e0a8..26ef882c 100644 --- a/settings.js +++ b/settings.js @@ -91,6 +91,42 @@ function fillServers(template) { JSON.parse(servers).forEach((server) => fillServer(template, server.id, server.username, server.password)); } +function fillMirror(template, id, name, mirrorOf, url) { + + const mirrorXml = template.createElement('mirror'); + + if (!id || !name || !mirrorOf || !url) { + core.setFailed('mirrors must contain id, name, mirrorOf and url'); + return; + } + + const idXml = createElementWithText(template, 'id', id); + mirrorXml.appendChild(idXml); + + const nameXml = createElementWithText(template, 'name', name); + mirrorXml.appendChild(nameXml); + + const mirrorOfXml = createElementWithText(template, 'mirrorOf', mirrorOf); + mirrorXml.appendChild(mirrorOfXml); + + const urlXml = createElementWithText(template, 'url', url); + mirrorXml.appendChild(urlXml); + + const mirrorsXml = template.getElementsByTagName('mirrors')[0]; + mirrorsXml.appendChild(mirrorXml); +} + +function fillMirrors(template) { + + const mirrors = core.getInput('mirrors'); + + if (!mirrors) { + return; + } + + JSON.parse(mirrors).forEach((mirror) => fillMirror(template, mirror.id, mirror.name, mirror.mirrorOf, mirror.url)); +} + function isInputTrue(inputName) { const val = core.getInput(inputName); return val && val.toLocaleLowerCase() == 'true'; @@ -157,6 +193,7 @@ function generate() { } const templateXml = getSettingsTemplate(); + fillMirrors(templateXml); fillServers(templateXml); fillServerForGithub(templateXml); fillProperties(templateXml); @@ -184,6 +221,7 @@ function cleanup() { module.exports = { getSettingsTemplate, writeSettings, + fillMirrors, fillServers, fillServerForGithub, fillProperties, diff --git a/settings.test.js b/settings.test.js index d9eb8c53..968dc10f 100644 --- a/settings.test.js +++ b/settings.test.js @@ -99,7 +99,6 @@ afterEach(() => { }); test('template should be read', () => { - const template = settings.getSettingsTemplate(); expect(template).toBeDefined(); @@ -133,7 +132,6 @@ test('fillServers one server', () => { const xml = new DOMParser().parseFromString(""); - process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]'; settings.fillServers(xml); @@ -194,6 +192,65 @@ test('fillServers github', () => { expect(consoleOutput).toEqual([]); }); +test('fillMirrors do nothing if no params', () => { + + const xml = new DOMParser().parseFromString(""); + + settings.fillMirrors(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe(""); +}); + +test('fillMirrors one mirror', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name", "mirrorOf":"mirrorOf", "url":"url"}]'; + + settings.fillMirrors(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe("" + + "id1namemirrorOfurl" + + ""); +}); + +test('fillMirrors two mirrors', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name1", "mirrorOf":"mirrorOf1", "url":"url1"},{"id": "id2", "name": "name2", "mirrorOf":"mirrorOf2", "url":"url2"}]'; + + settings.fillMirrors(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe("" + + "id1name1mirrorOf1url1id2name2mirrorOf2url2" + + ""); +}); + +test('fillMirrors incorrect fields', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_MIRRORS'] = '[{"idx": "id1"}]'; + + settings.fillMirrors(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe(''); + expect(consoleOutput).toEqual( + expect.arrayContaining([ + expect.stringMatching(/::error::mirrors must contain id, name, mirrorOf and url/) + ]) + ); +}); + test('addSonatypeSnapshots activate', () => { process.env['INPUT_SONATYPESNAPSHOTS'] = "true"; diff --git a/templates/settings.xml b/templates/settings.xml index 0ac93007..fe88380a 100644 --- a/templates/settings.xml +++ b/templates/settings.xml @@ -1,48 +1,46 @@ - - false - - - - _properties_ - - false - - - - - - _sonatype-snapshots_ - - false - - - - sonatype-snapshots - https://oss.sonatype.org/content/repositories/snapshots - - false - - - true - - - - - - sonatype-snapshots - https://oss.sonatype.org/content/repositories/snapshots - - false - - - true - - - - - - - - + false + + + + _properties_ + + false + + + + + + _sonatype-snapshots_ + + false + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + +