Skip to content

Commit 1dd04cb

Browse files
author
Søren Boisen
committed
Allow array of servers to be put in settings.xml
1 parent 8bb50d9 commit 1dd04cb

8 files changed

Lines changed: 66 additions & 46 deletions

File tree

__tests__/auth.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ describe('auth tests', () => {
3838
process.env[`INPUT_SETTINGS-PATH`] = altHome;
3939
await io.rmRF(altHome); // ensure it doesn't already exist
4040

41-
await auth.configAuthentication(id, username, password);
41+
await auth.configAuthentication([{id, username, password}]);
4242

4343
expect(fs.existsSync(m2Dir)).toBe(false);
4444
expect(fs.existsSync(settingsFile)).toBe(false);
4545

4646
expect(fs.existsSync(altHome)).toBe(true);
4747
expect(fs.existsSync(altSettingsFile)).toBe(true);
4848
expect(fs.readFileSync(altSettingsFile, 'utf-8')).toEqual(
49-
auth.generate(id, username, password)
49+
auth.generate([{id, username, password}])
5050
);
5151

5252
delete process.env[`INPUT_SETTINGS-PATH`];
@@ -58,12 +58,12 @@ describe('auth tests', () => {
5858
const username = 'UNAME';
5959
const password = 'TOKEN';
6060

61-
await auth.configAuthentication(id, username, password);
61+
await auth.configAuthentication([{id, username, password}]);
6262

6363
expect(fs.existsSync(m2Dir)).toBe(true);
6464
expect(fs.existsSync(settingsFile)).toBe(true);
6565
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
66-
auth.generate(id, username, password)
66+
auth.generate([{id, username, password}])
6767
);
6868
}, 100000);
6969

@@ -73,12 +73,12 @@ describe('auth tests', () => {
7373
const password = 'TOKEN';
7474
const gpgPassphrase = 'GPG';
7575

76-
await auth.configAuthentication(id, username, password, gpgPassphrase);
76+
await auth.configAuthentication([{id, username, password}], gpgPassphrase);
7777

7878
expect(fs.existsSync(m2Dir)).toBe(true);
7979
expect(fs.existsSync(settingsFile)).toBe(true);
8080
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
81-
auth.generate(id, username, password, gpgPassphrase)
81+
auth.generate([{id, username, password}], gpgPassphrase)
8282
);
8383
}, 100000);
8484

@@ -92,12 +92,12 @@ describe('auth tests', () => {
9292
expect(fs.existsSync(m2Dir)).toBe(true);
9393
expect(fs.existsSync(settingsFile)).toBe(true);
9494

95-
await auth.configAuthentication(id, username, password);
95+
await auth.configAuthentication([{id, username, password}]);
9696

9797
expect(fs.existsSync(m2Dir)).toBe(true);
9898
expect(fs.existsSync(settingsFile)).toBe(true);
9999
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
100-
auth.generate(id, username, password)
100+
auth.generate([{id, username, password}])
101101
);
102102
}, 100000);
103103

@@ -118,7 +118,7 @@ describe('auth tests', () => {
118118
</servers>
119119
</settings>`;
120120

121-
expect(auth.generate(id, username, password)).toEqual(expectedSettings);
121+
expect(auth.generate([{id, username, password}])).toEqual(expectedSettings);
122122
});
123123

124124
it('generates valid settings.xml with additional configuration', () => {
@@ -143,7 +143,7 @@ describe('auth tests', () => {
143143
</servers>
144144
</settings>`;
145145

146-
expect(auth.generate(id, username, password, gpgPassphrase)).toEqual(
146+
expect(auth.generate([{id, username, password}], gpgPassphrase)).toEqual(
147147
expectedSettings
148148
);
149149
});

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ inputs:
3636
authentication to the Apache Maven repository. Default is $GITHUB_TOKEN'
3737
required: false
3838
default: 'GITHUB_TOKEN'
39+
servers:
40+
description: 'IDs and environment variable names for authentication to the
41+
Apache Maven repositories.'
42+
required: false
3943
settings-path:
4044
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
4145
required: false

dist/cleanup/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ exports.INPUT_JDK_FILE = 'jdkFile';
962962
exports.INPUT_SERVER_ID = 'server-id';
963963
exports.INPUT_SERVER_USERNAME = 'server-username';
964964
exports.INPUT_SERVER_PASSWORD = 'server-password';
965+
exports.INPUT_SERVERS = 'servers';
965966
exports.INPUT_SETTINGS_PATH = 'settings-path';
966967
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
967968
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';

dist/setup/index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13207,33 +13207,32 @@ const xmlbuilder2_1 = __webpack_require__(255);
1320713207
const constants = __importStar(__webpack_require__(694));
1320813208
exports.M2_DIR = '.m2';
1320913209
exports.SETTINGS_FILE = 'settings.xml';
13210-
function configAuthentication(id, username, password, gpgPassphrase = undefined) {
13210+
function configAuthentication(servers, gpgPassphrase = undefined) {
1321113211
return __awaiter(this, void 0, void 0, function* () {
13212-
console.log(`creating ${exports.SETTINGS_FILE} with server-id: ${id};`, 'environment variables:', `username=\$${username},`, `password=\$${password},`, `and gpg-passphrase=${gpgPassphrase ? '$' + gpgPassphrase : null}`);
13212+
console.log(`creating ${exports.SETTINGS_FILE} with gpg-passphrase=${gpgPassphrase ? '$' + gpgPassphrase : null} and servers:`);
13213+
servers.forEach(s => console.log(`server with id: ${s.id}; environment variables: username=\$${s.username}, password=\$${s.password}`));
1321313214
// when an alternate m2 location is specified use only that location (no .m2 directory)
1321413215
// otherwise use the home/.m2/ path
1321513216
const settingsDirectory = path.join(core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(), core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : exports.M2_DIR);
1321613217
yield io.mkdirP(settingsDirectory);
1321713218
core.debug(`created directory ${settingsDirectory}`);
13218-
yield write(settingsDirectory, generate(id, username, password, gpgPassphrase));
13219+
yield write(settingsDirectory, generate(servers, gpgPassphrase));
1321913220
});
1322013221
}
1322113222
exports.configAuthentication = configAuthentication;
1322213223
// only exported for testing purposes
13223-
function generate(id, username, password, gpgPassphrase = undefined) {
13224+
function generate(servers, gpgPassphrase = undefined) {
1322413225
const xmlObj = {
1322513226
settings: {
1322613227
'@xmlns': 'http://maven.apache.org/SETTINGS/1.0.0',
1322713228
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
1322813229
'@xsi:schemaLocation': 'http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd',
1322913230
servers: {
13230-
server: [
13231-
{
13232-
id: id,
13233-
username: `\${env.${username}}`,
13234-
password: `\${env.${password}}`
13235-
}
13236-
]
13231+
server: servers.map(server => ({
13232+
id: server.id,
13233+
username: `\${env.${server.username}}`,
13234+
password: `\${env.${server.password}}`
13235+
}))
1323713236
}
1323813237
}
1323913238
};
@@ -25662,6 +25661,7 @@ exports.INPUT_JDK_FILE = 'jdkFile';
2566225661
exports.INPUT_SERVER_ID = 'server-id';
2566325662
exports.INPUT_SERVER_USERNAME = 'server-username';
2566425663
exports.INPUT_SERVER_PASSWORD = 'server-password';
25664+
exports.INPUT_SERVERS = 'servers';
2566525665
exports.INPUT_SETTINGS_PATH = 'settings-path';
2566625666
exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
2566725667
exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase';
@@ -28702,14 +28702,20 @@ function run() {
2870228702
const password = core.getInput(constants.INPUT_SERVER_PASSWORD, {
2870328703
required: false
2870428704
});
28705+
const serversJson = core.getInput(constants.INPUT_SERVERS, {
28706+
required: false
28707+
});
2870528708
const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY, { required: false }) ||
2870628709
constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
2870728710
const gpgPassphrase = core.getInput(constants.INPUT_GPG_PASSPHRASE, { required: false }) ||
2870828711
(gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined);
2870928712
if (gpgPrivateKey) {
2871028713
core.setSecret(gpgPrivateKey);
2871128714
}
28712-
yield auth.configAuthentication(id, username, password, gpgPassphrase);
28715+
const servers = serversJson
28716+
? JSON.parse(serversJson)
28717+
: [{ id, username, password }];
28718+
yield auth.configAuthentication(servers, gpgPassphrase);
2871328719
if (gpgPrivateKey) {
2871428720
core.info('importing private key');
2871528721
const keyFingerprint = (yield gpg.importKey(gpgPrivateKey)) || '';

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setup-java",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"private": true,
55
"description": "setup java action",
66
"main": "dist/setup/index.js",

src/auth.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ import * as constants from './constants';
99
export const M2_DIR = '.m2';
1010
export const SETTINGS_FILE = 'settings.xml';
1111

12+
export interface Server {
13+
readonly id: string;
14+
readonly username: string;
15+
readonly password: string;
16+
}
17+
1218
export async function configAuthentication(
13-
id: string,
14-
username: string,
15-
password: string,
19+
servers: Server[],
1620
gpgPassphrase: string | undefined = undefined
1721
) {
1822
console.log(
19-
`creating ${SETTINGS_FILE} with server-id: ${id};`,
20-
'environment variables:',
21-
`username=\$${username},`,
22-
`password=\$${password},`,
23-
`and gpg-passphrase=${gpgPassphrase ? '$' + gpgPassphrase : null}`
23+
`creating ${SETTINGS_FILE} with gpg-passphrase=${
24+
gpgPassphrase ? '$' + gpgPassphrase : null
25+
} and servers:`
26+
);
27+
servers.forEach(s =>
28+
console.log(
29+
`server with id: ${s.id}; environment variables: username=\$${s.username}, password=\$${s.password}`
30+
)
2431
);
2532
// when an alternate m2 location is specified use only that location (no .m2 directory)
2633
// otherwise use the home/.m2/ path
@@ -30,17 +37,12 @@ export async function configAuthentication(
3037
);
3138
await io.mkdirP(settingsDirectory);
3239
core.debug(`created directory ${settingsDirectory}`);
33-
await write(
34-
settingsDirectory,
35-
generate(id, username, password, gpgPassphrase)
36-
);
40+
await write(settingsDirectory, generate(servers, gpgPassphrase));
3741
}
3842

3943
// only exported for testing purposes
4044
export function generate(
41-
id: string,
42-
username: string,
43-
password: string,
45+
servers: Server[],
4446
gpgPassphrase: string | undefined = undefined
4547
) {
4648
const xmlObj: {[key: string]: any} = {
@@ -50,13 +52,11 @@ export function generate(
5052
'@xsi:schemaLocation':
5153
'http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd',
5254
servers: {
53-
server: [
54-
{
55-
id: id,
56-
username: `\${env.${username}}`,
57-
password: `\${env.${password}}`
58-
}
59-
]
55+
server: servers.map(server => ({
56+
id: server.id,
57+
username: `\${env.${server.username}}`,
58+
password: `\${env.${server.password}}`
59+
}))
6060
}
6161
}
6262
};

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const INPUT_JDK_FILE = 'jdkFile';
66
export const INPUT_SERVER_ID = 'server-id';
77
export const INPUT_SERVER_USERNAME = 'server-username';
88
export const INPUT_SERVER_PASSWORD = 'server-password';
9+
export const INPUT_SERVERS = 'servers';
910
export const INPUT_SETTINGS_PATH = 'settings-path';
1011
export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key';
1112
export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase';

src/setup-java.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ async function run() {
3434
const password = core.getInput(constants.INPUT_SERVER_PASSWORD, {
3535
required: false
3636
});
37+
const serversJson = core.getInput(constants.INPUT_SERVERS, {
38+
required: false
39+
});
40+
3741
const gpgPrivateKey =
3842
core.getInput(constants.INPUT_GPG_PRIVATE_KEY, {required: false}) ||
3943
constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;
@@ -45,7 +49,11 @@ async function run() {
4549
core.setSecret(gpgPrivateKey);
4650
}
4751

48-
await auth.configAuthentication(id, username, password, gpgPassphrase);
52+
const servers = serversJson
53+
? JSON.parse(serversJson)
54+
: [{id, username, password}];
55+
56+
await auth.configAuthentication(servers, gpgPassphrase);
4957

5058
if (gpgPrivateKey) {
5159
core.info('importing private key');

0 commit comments

Comments
 (0)