From 52fa27d07a5df9e82992cc0846b314775d1e148f Mon Sep 17 00:00:00 2001 From: ZehuaCao <47251317+Romanticoseu@users.noreply.github.com> Date: Fri, 4 Nov 2022 23:42:28 +0800 Subject: [PATCH] Add proxy support (#250) --- README.md | 8 +++++ action.yml | 2 ++ index.test.js | 10 +++++- settings.js | 31 +++++++++++++++++ settings.test.js | 77 ++++++++++++++++++++++++++++++++++++++++++ templates/proxy.xml | 8 +++++ templates/settings.xml | 1 + 7 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 templates/proxy.xml diff --git a/README.md b/README.md index a371b8e4..787a73e8 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,14 @@ steps: mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]' ``` +## ```settings.xml``` with proxies section +```yml +step: +- uses: s4u/maven-settings-action@v2.7.0 + with: + proxies: '[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol", "host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]' +``` + ## ```settings.xml``` with properties ```yml steps: diff --git a/action.yml b/action.yml index 4e01883a..ece66437 100644 --- a/action.yml +++ b/action.yml @@ -15,6 +15,8 @@ inputs: mirrors: description: 'mirrors definition in json array, eg: [{"id": "id", "name": "name", "mirrorOf": "mirrorOf", "url": "url"}]' required: false + proxies: + description: 'proxies definition in json array, eg: [{"id": "id", "active": "active", "protocol": "protocol", "host": "hostl", "port": "port", "nonProxyHosts", "nonProxyHosts"}]' properties: description: 'json array with properties, eg [{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]' required: false diff --git a/index.test.js b/index.test.js index 3f93fe3b..afeb9e41 100644 --- a/index.test.js +++ b/index.test.js @@ -68,6 +68,7 @@ test('run with all feature', () => { process.env['INPUT_GITHUBSERVER'] = true; process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]'; + process.env['INPUT_PROXIES'] = '[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol", "host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]'; process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]' process.env['INPUT_APACHESNAPSHOTS'] = true; @@ -75,7 +76,6 @@ test('run with all feature', () => { process.env['INPUT_ORACLEREPO'] = true; cp.spawnSync('node', [ `${indexPath}` ], { env: process.env, stdio: 'inherit' }); - const settingsStatus = fs.lstatSync(settingsPath); expect(settingsStatus.isFile()).toBeTruthy(); expect(settingsStatus.size).toBeGreaterThan(0); @@ -242,5 +242,13 @@ test('run with all feature', () => { mirror Off * mirror url + + proxyId + isActive + proxyProtocol + proxyHost + proxyPort + nonProxyHost + `); }) diff --git a/settings.js b/settings.js index 18ebfdf0..0940e474 100644 --- a/settings.js +++ b/settings.js @@ -152,6 +152,35 @@ function fillMirrors(template) { JSON.parse(mirrors).forEach((mirror) => fillMirror(template, mirror.id, mirror.name, mirror.mirrorOf, mirror.url)); } +function fillProxies(template) { + + const proxies = core.getInput('proxies'); + + if (!proxies) { + return; + } + + JSON.parse(proxies).forEach((proxy) => fillProxy(template, proxy.id, proxy.active, proxy.protocol, proxy.host, proxy.port, proxy.nonProxyHosts)); +} + +function fillProxy(template, id, active, protocol, host, port, nonProxyHosts) { + if(!id || !active || !protocol || !host || !port || !nonProxyHosts) { + core.setFailed('proxies must contain id, active, protocol, host, port and nonProxyHosts'); + return; + } + + const proxyXml = getTemplate('proxy.xml'); + proxyXml.getElementsByTagName("id")[0].textContent = id; + proxyXml.getElementsByTagName("active")[0].textContent = active; + proxyXml.getElementsByTagName("protocol")[0].textContent = protocol; + proxyXml.getElementsByTagName("host")[0].textContent = host; + proxyXml.getElementsByTagName("port")[0].textContent = port; + proxyXml.getElementsByTagName("nonProxyHosts")[0].textContent = nonProxyHosts; + + const proxiesXml = template.getElementsByTagName('proxies')[0]; + proxiesXml.appendChild(proxyXml); +} + function isInputTrue(inputName) { const val = core.getInput(inputName); return val && val.toLocaleLowerCase() == 'true'; @@ -230,6 +259,7 @@ function generate() { } const settingsXml = getTemplate('settings.xml'); + fillProxies(settingsXml); fillMirrors(settingsXml); fillServers(settingsXml, 'servers'); fillServers(settingsXml, 'oracleServers'); @@ -263,6 +293,7 @@ module.exports = { writeSettings, fillMirrors, fillServers, + fillProxies, fillServerForGithub, fillProperties, addApacheSnapshots, diff --git a/settings.test.js b/settings.test.js index 226b9ec7..cdcd432f 100644 --- a/settings.test.js +++ b/settings.test.js @@ -436,6 +436,83 @@ test('fillMirrors incorrect fields', () => { ); }); +test("fillProxies do noting if no params", () => { + const xml = stringAsXml(""); + + settings.fillProxies(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(""); + expect(consoleOutput).toEqual([]); +}) + +test("fillProxies one proxy", () => { + const xml = stringAsXml(""); + + process.env['INPUT_PROXIES'] = `[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol", + "host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]`; + + settings.fillProxies(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(` + proxyId + isActive + proxyProtocol + proxyHost + proxyPort + nonProxyHost +`) + +}) + +test("fillProxies two proxies", () => { + const xml = stringAsXml(""); + + process.env['INPUT_PROXIES'] = `[{"id": "proxyId1", "active": "isActive1", "protocol": "proxyProtocol1", + "host": "proxyHost1", "port": "proxyPort1", "nonProxyHosts": "nonProxyHost1"}, + {"id": "proxyId2", "active": "isActive2", "protocol": "proxyProtocol2", + "host": "proxyHost2", "port": "proxyPort2", "nonProxyHosts": "nonProxyHost2"}]`; + + settings.fillProxies(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(` + proxyId1 + isActive1 + proxyProtocol1 + proxyHost1 + proxyPort1 + nonProxyHost1 + + proxyId2 + isActive2 + proxyProtocol2 + proxyHost2 + proxyPort2 + nonProxyHost2 +`) + +}) + +test('fillProxies incorrect fields', () => { + + const xml = stringAsXml(""); + + process.env['INPUT_PROXIES'] = '[{"idx": "id1"}]'; + + settings.fillProxies(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe(''); + expect(consoleOutput).toEqual( + expect.arrayContaining([ + expect.stringMatching(/::error::proxies must contain id, active, protocol, host, port and nonProxyHosts/) + ]) + ); +}) + test('addApacheSnapshots', () => { process.env['INPUT_APACHESNAPSHOTS'] = "true"; diff --git a/templates/proxy.xml b/templates/proxy.xml new file mode 100644 index 00000000..b87a311e --- /dev/null +++ b/templates/proxy.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/templates/settings.xml b/templates/settings.xml index f977e9b3..715d5880 100644 --- a/templates/settings.xml +++ b/templates/settings.xml @@ -3,4 +3,5 @@ +