Skip to content

Commit 52fa27d

Browse files
authored
Add proxy support (#250)
1 parent 4a3e3d7 commit 52fa27d

File tree

7 files changed

+136
-1
lines changed

7 files changed

+136
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ steps:
101101
mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]'
102102
```
103103

104+
## ```settings.xml``` with proxies section
105+
```yml
106+
step:
107+
- uses: s4u/[email protected]
108+
with:
109+
proxies: '[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol", "host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]'
110+
```
111+
104112
## ```settings.xml``` with properties
105113
```yml
106114
steps:

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ inputs:
1515
mirrors:
1616
description: 'mirrors definition in json array, eg: [{"id": "id", "name": "name", "mirrorOf": "mirrorOf", "url": "url"}]'
1717
required: false
18+
proxies:
19+
description: 'proxies definition in json array, eg: [{"id": "id", "active": "active", "protocol": "protocol", "host": "hostl", "port": "port", "nonProxyHosts", "nonProxyHosts"}]'
1820
properties:
1921
description: 'json array with properties, eg [{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
2022
required: false

index.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ test('run with all feature', () => {
6868
process.env['INPUT_GITHUBSERVER'] = true;
6969

7070
process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]';
71+
process.env['INPUT_PROXIES'] = '[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol", "host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]';
7172
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'
7273

7374
process.env['INPUT_APACHESNAPSHOTS'] = true;
7475
process.env['INPUT_SONATYPESNAPSHOTS'] = true;
7576
process.env['INPUT_ORACLEREPO'] = true;
7677

7778
cp.spawnSync('node', [ `${indexPath}` ], { env: process.env, stdio: 'inherit' });
78-
7979
const settingsStatus = fs.lstatSync(settingsPath);
8080
expect(settingsStatus.isFile()).toBeTruthy();
8181
expect(settingsStatus.size).toBeGreaterThan(0);
@@ -242,5 +242,13 @@ test('run with all feature', () => {
242242
<mirrorOf>mirror Off *</mirrorOf>
243243
<url>mirror url</url>
244244
</mirror></mirrors>
245+
<proxies><proxy>
246+
<id>proxyId</id>
247+
<active>isActive</active>
248+
<protocol>proxyProtocol</protocol>
249+
<host>proxyHost</host>
250+
<port>proxyPort</port>
251+
<nonProxyHosts>nonProxyHost</nonProxyHosts>
252+
</proxy></proxies>
245253
</settings>`);
246254
})

settings.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,35 @@ function fillMirrors(template) {
152152
JSON.parse(mirrors).forEach((mirror) => fillMirror(template, mirror.id, mirror.name, mirror.mirrorOf, mirror.url));
153153
}
154154

155+
function fillProxies(template) {
156+
157+
const proxies = core.getInput('proxies');
158+
159+
if (!proxies) {
160+
return;
161+
}
162+
163+
JSON.parse(proxies).forEach((proxy) => fillProxy(template, proxy.id, proxy.active, proxy.protocol, proxy.host, proxy.port, proxy.nonProxyHosts));
164+
}
165+
166+
function fillProxy(template, id, active, protocol, host, port, nonProxyHosts) {
167+
if(!id || !active || !protocol || !host || !port || !nonProxyHosts) {
168+
core.setFailed('proxies must contain id, active, protocol, host, port and nonProxyHosts');
169+
return;
170+
}
171+
172+
const proxyXml = getTemplate('proxy.xml');
173+
proxyXml.getElementsByTagName("id")[0].textContent = id;
174+
proxyXml.getElementsByTagName("active")[0].textContent = active;
175+
proxyXml.getElementsByTagName("protocol")[0].textContent = protocol;
176+
proxyXml.getElementsByTagName("host")[0].textContent = host;
177+
proxyXml.getElementsByTagName("port")[0].textContent = port;
178+
proxyXml.getElementsByTagName("nonProxyHosts")[0].textContent = nonProxyHosts;
179+
180+
const proxiesXml = template.getElementsByTagName('proxies')[0];
181+
proxiesXml.appendChild(proxyXml);
182+
}
183+
155184
function isInputTrue(inputName) {
156185
const val = core.getInput(inputName);
157186
return val && val.toLocaleLowerCase() == 'true';
@@ -230,6 +259,7 @@ function generate() {
230259
}
231260

232261
const settingsXml = getTemplate('settings.xml');
262+
fillProxies(settingsXml);
233263
fillMirrors(settingsXml);
234264
fillServers(settingsXml, 'servers');
235265
fillServers(settingsXml, 'oracleServers');
@@ -263,6 +293,7 @@ module.exports = {
263293
writeSettings,
264294
fillMirrors,
265295
fillServers,
296+
fillProxies,
266297
fillServerForGithub,
267298
fillProperties,
268299
addApacheSnapshots,

settings.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,83 @@ test('fillMirrors incorrect fields', () => {
436436
);
437437
});
438438

439+
test("fillProxies do noting if no params", () => {
440+
const xml = stringAsXml("<proxies/>");
441+
442+
settings.fillProxies(xml);
443+
444+
const xmlStr = new XMLSerializer().serializeToString(xml);
445+
expect(xmlStr).toBe("<proxies/>");
446+
expect(consoleOutput).toEqual([]);
447+
})
448+
449+
test("fillProxies one proxy", () => {
450+
const xml = stringAsXml("<proxies/>");
451+
452+
process.env['INPUT_PROXIES'] = `[{"id": "proxyId", "active": "isActive", "protocol": "proxyProtocol",
453+
"host": "proxyHost", "port": "proxyPort", "nonProxyHosts": "nonProxyHost"}]`;
454+
455+
settings.fillProxies(xml);
456+
457+
const xmlStr = new XMLSerializer().serializeToString(xml);
458+
expect(xmlStr).toBe(`<proxies><proxy>
459+
<id>proxyId</id>
460+
<active>isActive</active>
461+
<protocol>proxyProtocol</protocol>
462+
<host>proxyHost</host>
463+
<port>proxyPort</port>
464+
<nonProxyHosts>nonProxyHost</nonProxyHosts>
465+
</proxy></proxies>`)
466+
467+
})
468+
469+
test("fillProxies two proxies", () => {
470+
const xml = stringAsXml("<proxies/>");
471+
472+
process.env['INPUT_PROXIES'] = `[{"id": "proxyId1", "active": "isActive1", "protocol": "proxyProtocol1",
473+
"host": "proxyHost1", "port": "proxyPort1", "nonProxyHosts": "nonProxyHost1"},
474+
{"id": "proxyId2", "active": "isActive2", "protocol": "proxyProtocol2",
475+
"host": "proxyHost2", "port": "proxyPort2", "nonProxyHosts": "nonProxyHost2"}]`;
476+
477+
settings.fillProxies(xml);
478+
479+
const xmlStr = new XMLSerializer().serializeToString(xml);
480+
expect(xmlStr).toBe(`<proxies><proxy>
481+
<id>proxyId1</id>
482+
<active>isActive1</active>
483+
<protocol>proxyProtocol1</protocol>
484+
<host>proxyHost1</host>
485+
<port>proxyPort1</port>
486+
<nonProxyHosts>nonProxyHost1</nonProxyHosts>
487+
</proxy><proxy>
488+
<id>proxyId2</id>
489+
<active>isActive2</active>
490+
<protocol>proxyProtocol2</protocol>
491+
<host>proxyHost2</host>
492+
<port>proxyPort2</port>
493+
<nonProxyHosts>nonProxyHost2</nonProxyHosts>
494+
</proxy></proxies>`)
495+
496+
})
497+
498+
test('fillProxies incorrect fields', () => {
499+
500+
const xml = stringAsXml("<proxies/>");
501+
502+
process.env['INPUT_PROXIES'] = '[{"idx": "id1"}]';
503+
504+
settings.fillProxies(xml);
505+
506+
const xmlStr = new XMLSerializer().serializeToString(xml);
507+
508+
expect(xmlStr).toBe('<proxies/>');
509+
expect(consoleOutput).toEqual(
510+
expect.arrayContaining([
511+
expect.stringMatching(/::error::proxies must contain id, active, protocol, host, port and nonProxyHosts/)
512+
])
513+
);
514+
})
515+
439516
test('addApacheSnapshots', () => {
440517

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

templates/proxy.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<proxy>
2+
<id />
3+
<active />
4+
<protocol />
5+
<host />
6+
<port />
7+
<nonProxyHosts />
8+
</proxy>

templates/settings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
<profiles/>
44
<servers />
55
<mirrors />
6+
<proxies />
67
</settings>

0 commit comments

Comments
 (0)