From 060e4442b57e3493ec256ff15e1aac5578a6fd54 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Mon, 21 Dec 2020 23:28:12 +0100 Subject: [PATCH] Allow defining custom configuration for server fix #84 --- README.md | 114 ++++++++++++++++++++++++++++++++---------- index.test.js | 6 ++- settings.js | 47 ++++++++++++++--- settings.test.js | 88 ++++++++++++++++++++++++++++++-- templates/servers.xml | 1 + 5 files changed, 218 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index effebdb8..04cbf27e 100644 --- a/README.md +++ b/README.md @@ -25,13 +25,14 @@ or use automatic tools like [Dependabot](https://dependabot.com/). # Usage See [action.yml](action.yml) -Create default ```settings.xml```: +## default ```settings.xml``` ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 ``` -Create ```settings.xml``` with servers section: +## ```settings.xml``` with servers section + ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -39,7 +40,42 @@ steps: servers: '[{"id": "serverId", "username": "username", "password": "password"}]' ``` -Create ```settings.xml``` with mirrors section: +## ```settings.xml``` with servers section and additional configuration + +``` yml +steps: +- uses: s4u/maven-settings-action@v2.2.0 + with: + servers: | + [{ + "id": "serverId", + "configuration": { + "item1": "value1", + "item2": { + "item21": "value21", + "item22": "value22" + } + } + }] +``` + +result will be: + +```xml + + serverId + + value1 + + value21 + value22 + + + +``` + + +## ```settings.xml``` with mirrors section ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -47,7 +83,7 @@ steps: mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]' ``` -Create ```settings.xml``` with maven properties: +## ```settings.xml``` with properties ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -55,23 +91,8 @@ steps: properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]' ``` -It is also possible pass in Github Secrets e.g. +## ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list -``` yml - with: - servers: | - [{ - "id": "sonatype-nexus-snapshots", - "username": "${{ secrets.SONATYPE_USERNAME }}", - "password": "${{ secrets.SONATYPE_PASSWORD }}" - }] - -``` - -**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`. - - -Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -79,7 +100,8 @@ steps: sonatypeSnapshots: true ``` -Create ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list +## ```settings.xml``` with https://repository.apache.org/snapshots/ in repository list + ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -87,7 +109,7 @@ steps: apacheSnapshots: true ``` -Do not override existing ```settings.xml```, from version 2.0 file is override by default : +## Do not override existing ```settings.xml```, from version **2.0** file is override by default : ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -95,7 +117,7 @@ steps: override: false ``` -Do not add github to server in ```settings.xml```, by default is added: +## Do not add github to server in ```settings.xml```, by default is added: ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -103,7 +125,8 @@ steps: githubServer: false ``` -Create ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015) +## ```settings.xml``` with special server item configuration for oracle repository [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9015) + ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -111,7 +134,7 @@ steps: oracleServers: '[{"id": "serverId", "username": "username", "password": "password"}]' ``` -Create ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017) +## ```settings.xml``` with [Oracle Maven Repository](https://docs.oracle.com/middleware/1213/core/MAVEN/config_maven_repo.htm#MAVEN9017) ```yml steps: - uses: s4u/maven-settings-action@v2.2.0 @@ -119,7 +142,46 @@ steps: oracleRepo: true ``` -**Note**: When using maven-settings-action in combination with Cache action (actions/cache) it is required to place the cache action **before** maven-settings-action. +## GitHub actions secrets + +It is also possible pass in Github Secrets e.g. + +``` yml +steps: +- uses: s4u/maven-settings-action@v2.2.0 + with: + servers: | + [{ + "id": "sonatype-nexus-snapshots", + "username": "${{ secrets.SONATYPE_USERNAME }}", + "password": "${{ secrets.SONATYPE_PASSWORD }}" + }] +``` + +**Note**: secrets are *not* passed in if the workflow is triggered from a forked repository. See [here](https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets#using-encrypted-secrets-in-a-workflow) for further information. This can be avoided by using `if` triggers on the job e.g. `if: github.event_name == 'push'`. + +# Notes + +**maven-settings-action** should be put at the latest position before maven run in order to avoid override ```setting.xml``` by another action + +```yml + steps: + - uses: actions/checkout@v2 + + - uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: maven-${{ hashFiles('**/pom.xml') }} + restore-keys: maven- + + - uses: actions/setup-java@v1 + with: + java-version: 8 + + - uses: s4u/maven-settings-action@v2.2.0 + + - run: mvn verify +``` # License diff --git a/index.test.js b/index.test.js index 8db92175..26133816 100644 --- a/index.test.js +++ b/index.test.js @@ -63,7 +63,7 @@ afterAll(() => { test('run with all feature', () => { - process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword"}]'; + process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword", "configuration": {"props1": "value1"}}]'; process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]'; process.env['INPUT_GITHUBSERVER'] = true; @@ -80,7 +80,7 @@ test('run with all feature', () => { expect(settingsStatus.isFile()).toBeTruthy(); expect(settingsStatus.size).toBeGreaterThan(0); - const settingsBody = fs.readFileSync(settingsPath).toString(); + const settingsBody = fs.readFileSync(settingsPath).toString().replace(/^ $/mg, ''); expect(settingsBody).toBe(` false @@ -186,6 +186,7 @@ test('run with all feature', () => { serverId sUsername sPassword + value1 oServerId @@ -213,6 +214,7 @@ test('run with all feature', () => { github \${env.GITHUB_ACTOR} \${env.GITHUB_TOKEN} + diff --git a/settings.js b/settings.js index ab731cb7..06e8543e 100644 --- a/settings.js +++ b/settings.js @@ -50,17 +50,51 @@ function writeSettings(settingsPath, templateXml) { fs.writeFileSync(settingsPath, settingStr); } -function fillServer(templateXml, templateName, id, username, password) { +function jsonToXml(templateXml, xmlTag, json) { + for (const key in json) { + const keyXml = templateXml.createElement(key); + const value = json[key]; + if ( value instanceof Object) { + jsonToXml(templateXml, keyXml, value); + } else { + keyXml.textContent = value; + } + xmlTag.appendChild(keyXml); + } +} - if (!id || !username || !password) { - core.setFailed(templateName + ' must contain id, username and password'); +function fillServer(templateXml, templateName, id, username, password, configurations) { + + if (!id || ((!username || !password) && !configurations) ) { + core.setFailed(templateName + ' must contain id, (username and password) or configuration'); return; } const serverXml = getTemplate(templateName + '.xml') serverXml.getElementsByTagName('id')[0].textContent = id; - serverXml.getElementsByTagName('username')[0].textContent = username; - serverXml.getElementsByTagName('password')[0].textContent = password; + + const usernameTag = serverXml.getElementsByTagName('username')[0]; + if (username) { + usernameTag.textContent = username; + } else { + serverXml.documentElement.removeChild(usernameTag); + } + + const passwordTag = serverXml.getElementsByTagName('password')[0]; + if (password) { + passwordTag.textContent = password; + } else { + serverXml.documentElement.removeChild(passwordTag); + } + + const configurationTag = serverXml.getElementsByTagName('configuration')[0]; + if (configurations) { + jsonToXml(templateXml, configurationTag, configurations); + } else { + if (configurationTag.childNodes.length == 0) { + serverXml.documentElement.removeChild(configurationTag); + } + } const serversXml = templateXml.getElementsByTagName('servers')[0]; serversXml.appendChild(serverXml); @@ -74,7 +108,8 @@ function fillServers(template, templateName) { return; } - JSON.parse(servers).forEach((server) => fillServer(template, templateName, server.id, server.username, server.password)); + JSON.parse(servers).forEach((server) => + fillServer(template, templateName, server.id, server.username, server.password, server.configuration)); } function fillMirror(template, id, name, mirrorOf, url) { diff --git a/settings.test.js b/settings.test.js index 0a52af0c..006c8e67 100644 --- a/settings.test.js +++ b/settings.test.js @@ -136,17 +136,94 @@ test('fillServers one server', () => { settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml); + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); expect(xmlStr).toBe(` id1 username1 password1 + `); }); +test('fillServers with username and configuration', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; + + settings.fillServers(xml, 'servers'); + + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') + + expect(xmlStr).toBe(` + + id1 + username + + prop1Valueprop2Value +`); +}); + +test('fillServers with username, password and configuration', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; + + settings.fillServers(xml, 'servers'); + + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') + + expect(xmlStr).toBe(` + + id1 + username + password + prop1Valueprop2Value +`); +}); + +test('fillServers with configuration', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]'; + + settings.fillServers(xml, 'servers'); + + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') + + expect(xmlStr).toBe(` + + id1 + + + prop1Valueprop2Value +`); +}); + +test('fillServers with configuration subLevel', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]'; + + settings.fillServers(xml, 'servers'); + + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '') + + expect(xmlStr).toBe(` + + id1 + + + value11value12value2 +`); +}); + test('fillServers two servers', () => { const xml = new DOMParser().parseFromString(""); @@ -156,18 +233,20 @@ test('fillServers two servers', () => { settings.fillServers(xml, 'servers'); - const xmlStr = new XMLSerializer().serializeToString(xml); + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); expect(xmlStr).toBe(` id1 username1 password1 + id2 username2 password2 + `); }); @@ -184,7 +263,7 @@ test('fill servers incorrect fields', () => { expect(xmlStr).toBe(''); expect(consoleOutput).toEqual( expect.arrayContaining([ - expect.stringMatching(/::error::servers must contain id, username and password/) + expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/) ]) ); }); @@ -232,13 +311,14 @@ test('fillServers github', () => { settings.fillServerForGithub(xml); - const xmlStr = new XMLSerializer().serializeToString(xml); + const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, ''); expect(xmlStr).toBe(` github \${env.GITHUB_ACTOR} \${env.GITHUB_TOKEN} + `); expect(consoleOutput).toEqual([]); }); diff --git a/templates/servers.xml b/templates/servers.xml index 97de7be4..e7f69638 100644 --- a/templates/servers.xml +++ b/templates/servers.xml @@ -3,4 +3,5 @@ +