From c36b1066686960251656760039d15604ab67eca8 Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Thu, 15 Oct 2020 23:44:42 +0200 Subject: [PATCH] Oracle Maven repository fix: #62 --- .gitattributes | 3 + .github/workflows/test.yml | 2 + README.md | 16 +++ action.yml | 9 +- index.test.js | 126 ++++++++++++++++- package-lock.json | 7 +- package.json | 5 +- settings.js | 111 +++++++-------- settings.test.js | 237 ++++++++++++++++++++++++-------- templates/mirrors.xml | 7 + templates/oracle-repo.xml | 31 +++++ templates/oracleServers.xml | 23 ++++ templates/properties.xml | 8 ++ templates/servers.xml | 6 + templates/settings.xml | 48 +------ templates/sonatype-snapshot.xml | 31 +++++ 16 files changed, 488 insertions(+), 182 deletions(-) create mode 100644 .gitattributes create mode 100644 templates/mirrors.xml create mode 100644 templates/oracle-repo.xml create mode 100644 templates/oracleServers.xml create mode 100644 templates/properties.xml create mode 100644 templates/servers.xml create mode 100644 templates/sonatype-snapshot.xml diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..e1caa914 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +*.js eol=lf +*.yml eol=lf +*.xml eol=lf diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index daa4c056..4d3b945f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -37,6 +37,8 @@ jobs: properties: '[{"prop1": "value1"}, {"prop2": "value2"}]' mirrors: '[{"id": "mirrorId", "name": "mirrorName", "mirrorOf": "mirrorOf", "url": "mirrorUrl"}]' sonatypeSnapshots: true + oracleServers: '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]' + oracleRepo: true - run: cat ~/.m2/settings.xml shell: bash diff --git a/README.md b/README.md index 2e734c92..8fe309f3 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,22 @@ 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) +```yml +steps: +- uses: s4u/maven-settings-action@v2.1.1 + with: + 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) +```yml +steps: +- uses: s4u/maven-settings-action@v2.1.1 + with: + oracleRepo: true +``` + # License The scripts and documentation in this project are released under the [MIT License](LICENSE) diff --git a/action.yml b/action.yml index aac3959c..e0903d0c 100644 --- a/action.yml +++ b/action.yml @@ -27,8 +27,13 @@ inputs: description: 'add to settings.xml servers server-id: github; username=$GITHUB_ACTOR and password=$GITHUB_TOKEN' default: "true" required: false - - + oracleServers: + description: 'servers definition in joson array, eg: [{"id": "serverId", "username": "username", "password": "password"}] for Oracle repository' + required: false + oracleRepo: + description: 'add Oracle Maven Repository' + default: "false" + required: false runs: using: 'node12' diff --git a/index.test.js b/index.test.js index 353cdf18..370cf92d 100644 --- a/index.test.js +++ b/index.test.js @@ -63,18 +63,132 @@ afterAll(() => { test('run with all feature', () => { - process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]'; + process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "sUsername", "password": "sPassword"}]'; + process.env['INPUT_ORACLESERVERS'] = '[{"id": "oServerId", "username": "oUsername", "password": "oPassword"}]'; + process.env['INPUT_GITHUBSERVER'] = true; + + process.env['INPUT_MIRRORS'] = '[{"id": "mirrorId", "name": "mirror Name", "mirrorOf": "mirror Off *", "url": "mirror url"}]'; process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]' - process.env['INPUT_SONATYPESNAPSHOT'] = true; - cp.execSync(`node ${indexPath}`, { env: process.env }).toString(); + process.env['INPUT_SONATYPESNAPSHOTS'] = true; + process.env['INPUT_ORACLEREPO'] = true; + + cp.execSync(`node ${indexPath}`, { env: process.env, stdio: 'inherit' }); const settingsStatus = fs.lstatSync(settingsPath); expect(settingsStatus.isFile()).toBeTruthy(); expect(settingsStatus.size).toBeGreaterThan(0); const settingsBody = fs.readFileSync(settingsPath).toString(); - expect(settingsBody).toMatch(''); - expect(settingsBody).toMatch('serverIdusernamepassword'); - expect(settingsBody).toMatch('prop1'); + expect(settingsBody).toBe(` + false + + + _properties_ + + true + + value1value2 + + + _sonatype-snapshots_ + + true + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + _maven.oracle.com_ + + true + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + + + + + serverId + sUsername + sPassword + + + oServerId + oUsername + oPassword + + + ANY + ANY + OAM 11g + + + + + + http.protocol.allow-circular-redirects + %b,true + + + + + + + + github + \${env.GITHUB_ACTOR} + \${env.GITHUB_TOKEN} + + + + mirrorId + mirror Name + mirror Off * + mirror url + +`); }) diff --git a/package-lock.json b/package-lock.json index 445b063c..aab0bb53 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "maven-settings-action", - "version": "1.0.0", + "version": "2.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -6341,11 +6341,6 @@ "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.3.0.tgz", "integrity": "sha512-z9s6k3wxE+aZHgXYxSTpGDo7BYOUfJsIRyoZiX6HTjwpwfS2wpQBQKa2fD+ShLyPkqDYo5ud7KitmLZ2Cd6r0g==" }, - "xpath": { - "version": "0.0.30", - "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.30.tgz", - "integrity": "sha512-R/uBDaAWrWdBjkn/lVd1E9CPty6/Els0Y14u21mdrnSfEswZaxTx4VQK4v3y9f0yYD5KcXZ53djRNcEi2RV6Wg==" - }, "y18n": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/y18n/-/y18n-4.0.0.tgz", diff --git a/package.json b/package.json index 923506ee..77b80292 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "maven-settings-action", - "version": "1.0.0", + "version": "2.2.0", "description": "Prepare maven settings", "main": "index.js", "scripts": { @@ -25,8 +25,7 @@ "homepage": "https://github.com/s4u/maven-settings-action#readme", "dependencies": { "@actions/core": "^1.2.6", - "xmldom": "^0.3.0", - "xpath": "0.0.30" + "xmldom": "^0.3.0" }, "devDependencies": { "eslint": "^7.11.0", diff --git a/settings.js b/settings.js index 26ef882c..71f88687 100644 --- a/settings.js +++ b/settings.js @@ -28,15 +28,13 @@ const path = require('path'); const fs = require('fs'); const DOMParser = require('xmldom').DOMParser; const XMLSerializer = require('xmldom').XMLSerializer; -const xpath = require('xpath'); - function getSettingsPath() { return path.join(os.homedir(), '.m2', 'settings.xml'); } -function getSettingsTemplate() { - const templatePath = path.join(__dirname, 'templates', 'settings.xml'); +function getTemplate(templateName) { + const templatePath = path.join(__dirname, 'templates', templateName); const templateStr = fs.readFileSync(templatePath).toString(); return new DOMParser().parseFromString(templateStr, 'text/xml'); } @@ -52,65 +50,45 @@ function writeSettings(settingsPath, templateXml) { fs.writeFileSync(settingsPath, settingStr); } -function createElementWithText(template, tag, text) { - const tagXml = template.createElement(tag); - tagXml.textContent = text; - return tagXml; -} - -function fillServer(template, id, username, password) { - - const serverXml = template.createElement('server'); +function fillServer(templateXml, templateName, id, username, password) { if (!id || !username || !password) { - core.setFailed('servers must contain id, username and password'); + core.setFailed(templateName + ' must contain id, username and password'); return; } - const idXml = createElementWithText(template, 'id', id); - serverXml.appendChild(idXml); - - const usernameXml = createElementWithText(template, 'username', username); - serverXml.appendChild(usernameXml); - - const passwordXml = createElementWithText(template, 'password', password); - serverXml.appendChild(passwordXml); + const serverXml = getTemplate(templateName + '.xml') + serverXml.getElementsByTagName('id')[0].textContent = id; + serverXml.getElementsByTagName('username')[0].textContent = username; + serverXml.getElementsByTagName('password')[0].textContent = password; - const serversXml = template.getElementsByTagName('servers')[0]; + const serversXml = templateXml.getElementsByTagName('servers')[0]; serversXml.appendChild(serverXml); } -function fillServers(template) { +function fillServers(template, templateName) { - const servers = core.getInput('servers'); + const servers = core.getInput(templateName); if (!servers) { return; } - JSON.parse(servers).forEach((server) => fillServer(template, server.id, server.username, server.password)); + JSON.parse(servers).forEach((server) => fillServer(template, templateName, 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 mirrorXml = getTemplate('mirrors.xml'); + mirrorXml.getElementsByTagName('id')[0].textContent = id; + mirrorXml.getElementsByTagName('name')[0].textContent = name; + mirrorXml.getElementsByTagName('mirrorOf')[0].textContent = mirrorOf; + mirrorXml.getElementsByTagName('url')[0].textContent = url; const mirrorsXml = template.getElementsByTagName('mirrors')[0]; mirrorsXml.appendChild(mirrorXml); @@ -138,42 +116,48 @@ function fillServerForGithub(templateXml) { return; } - fillServer(templateXml, 'github', '${env.GITHUB_ACTOR}', '${env.GITHUB_TOKEN}'); -} - -function activateProfile(template, profileId) { - const activeByDefault = xpath - .select(`/settings/profiles/profile[id[contains(text(),"${profileId}")]]/activation/activeByDefault`, template); - - if (activeByDefault) { - activeByDefault[0].textContent = 'true'; - } + fillServer(templateXml, 'servers', 'github', '${env.GITHUB_ACTOR}', '${env.GITHUB_TOKEN}'); } function fillProperties(template) { const properties = core.getInput('properties'); + if (!properties) { return; } - activateProfile(template, '_properties_') - const propertiesXml = xpath - .select(`/settings/profiles/profile[id[contains(text(),"_properties_")]]/properties`, template)[0]; + const propertiesProfileXml = getTemplate('properties.xml'); + const propertiesXml = propertiesProfileXml.getElementsByTagName('properties')[0]; JSON.parse(properties).forEach((property) => { - for (const key in property) { const keyXml = template.createElement(key); keyXml.textContent = property[key]; propertiesXml.appendChild(keyXml); } }); + + const profilesXml = template.getElementsByTagName('profiles')[0]; + profilesXml.appendChild(propertiesProfileXml); + +} + +function addProfile(template, profileName) { + const sonatypeXml = getTemplate(profileName); + const profilesXml = template.getElementsByTagName('profiles')[0]; + profilesXml.appendChild(sonatypeXml); } function addSonatypeSnapshots(template) { if (isInputTrue('sonatypeSnapshots')) { - activateProfile(template, '_sonatype-snapshots_') + addProfile(template, 'sonatype-snapshot.xml') + } +} + +function addOracleRepo(template) { + if (isInputTrue('oracleRepo')) { + addProfile(template, 'oracle-repo.xml') } } @@ -192,13 +176,15 @@ function generate() { } } - const templateXml = getSettingsTemplate(); - fillMirrors(templateXml); - fillServers(templateXml); - fillServerForGithub(templateXml); - fillProperties(templateXml); - addSonatypeSnapshots(templateXml); - writeSettings(settingsPath, templateXml); + const settingsXml = getTemplate('settings.xml'); + fillMirrors(settingsXml); + fillServers(settingsXml, 'servers'); + fillServers(settingsXml, 'oracleServers'); + fillServerForGithub(settingsXml); + fillProperties(settingsXml); + addSonatypeSnapshots(settingsXml); + addOracleRepo(settingsXml); + writeSettings(settingsPath, settingsXml); core.saveState('maven-settings', 'ok'); } @@ -219,13 +205,14 @@ function cleanup() { } module.exports = { - getSettingsTemplate, + getTemplate, writeSettings, fillMirrors, fillServers, fillServerForGithub, fillProperties, addSonatypeSnapshots, + addOracleRepo, generate, cleanup } diff --git a/settings.test.js b/settings.test.js index 968dc10f..1c62b026 100644 --- a/settings.test.js +++ b/settings.test.js @@ -99,7 +99,7 @@ afterEach(() => { }); test('template should be read', () => { - const template = settings.getSettingsTemplate(); + const template = settings.getTemplate('settings.xml'); expect(template).toBeDefined(); }); @@ -121,7 +121,7 @@ test('fillServers do nothing if no params', () => { const xml = new DOMParser().parseFromString(""); - settings.fillServers(xml); + settings.fillServers(xml, 'servers'); const xmlStr = new XMLSerializer().serializeToString(xml); @@ -134,13 +134,17 @@ test('fillServers one server', () => { process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]'; - settings.fillServers(xml); + settings.fillServers(xml, 'servers'); const xmlStr = new XMLSerializer().serializeToString(xml); - expect(xmlStr).toBe("" + - "id1username1password1" + - ""); + expect(xmlStr).toBe(` + + id1 + username1 + password1 +`); + }); test('fillServers two servers', () => { @@ -150,23 +154,30 @@ test('fillServers two servers', () => { process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\ {"id": "id2", "username": "username2", "password":"password2"}]'; - settings.fillServers(xml); + settings.fillServers(xml, 'servers'); const xmlStr = new XMLSerializer().serializeToString(xml); - expect(xmlStr).toBe("" + - "id1username1password1" + - "id2username2password2" + - ""); + expect(xmlStr).toBe(` + + id1 + username1 + password1 + + + id2 + username2 + password2 +`); }); -test('fillServers incorrect fields', () => { +test('fill servers incorrect fields', () => { const xml = new DOMParser().parseFromString(""); process.env['INPUT_SERVERS'] = '[{"idx": "id1"}]'; - settings.fillServers(xml); + settings.fillServers(xml, 'servers'); const xmlStr = new XMLSerializer().serializeToString(xml); @@ -178,6 +189,41 @@ test('fillServers incorrect fields', () => { ); }); +test('fill oracleServers', () => { + + const xml = new DOMParser().parseFromString(""); + + process.env['INPUT_ORACLESERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]'; + + settings.fillServers(xml, 'oracleServers'); + + const xmlStr = new XMLSerializer().serializeToString(xml); + + expect(xmlStr).toBe(` + + id1 + username1 + password1 + + + ANY + ANY + OAM 11g + + + + + + http.protocol.allow-circular-redirects + %b,true + + + + + +`); +}); + test('fillServers github', () => { const xml = new DOMParser().parseFromString(""); @@ -188,7 +234,12 @@ test('fillServers github', () => { const xmlStr = new XMLSerializer().serializeToString(xml); - expect(xmlStr).toBe('github${env.GITHUB_ACTOR}${env.GITHUB_TOKEN}'); + expect(xmlStr).toBe(` + + github + \${env.GITHUB_ACTOR} + \${env.GITHUB_TOKEN} +`); expect(consoleOutput).toEqual([]); }); @@ -201,6 +252,7 @@ test('fillMirrors do nothing if no params', () => { const xmlStr = new XMLSerializer().serializeToString(xml); expect(xmlStr).toBe(""); + expect(consoleOutput).toEqual([]); }); test('fillMirrors one mirror', () => { @@ -213,9 +265,14 @@ test('fillMirrors one mirror', () => { const xmlStr = new XMLSerializer().serializeToString(xml); - expect(xmlStr).toBe("" + - "id1namemirrorOfurl" + - ""); + expect(xmlStr).toBe(` + + id1 + name + mirrorOf + url +`); + expect(consoleOutput).toEqual([]); }); test('fillMirrors two mirrors', () => { @@ -228,9 +285,20 @@ test('fillMirrors two mirrors', () => { const xmlStr = new XMLSerializer().serializeToString(xml); - expect(xmlStr).toBe("" + - "id1name1mirrorOf1url1id2name2mirrorOf2url2" + - ""); + expect(xmlStr).toBe(` + + id1 + name1 + mirrorOf1 + url1 + + + id2 + name2 + mirrorOf2 + url2 +`); + expect(consoleOutput).toEqual([]); }); test('fillMirrors incorrect fields', () => { @@ -251,56 +319,107 @@ test('fillMirrors incorrect fields', () => { ); }); -test('addSonatypeSnapshots activate', () => { +test('addSonatypeSnapshots', () => { process.env['INPUT_SONATYPESNAPSHOTS'] = "true"; - settings.addSonatypeSnapshots(xmlTestProfile); + const xml = new DOMParser().parseFromString(''); - const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile); - expect(xmlStr).toBe(` - - - _properties_ - - false - - - - - _sonatype-snapshots_ - - true - - - - `); + settings.addSonatypeSnapshots(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(` + + _sonatype-snapshots_ + + true + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + +`); +}); + +test('addOracleRepo', () => { + + process.env['INPUT_ORACLEREPO'] = "true"; + + const xml = new DOMParser().parseFromString(''); + + settings.addOracleRepo(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(` + + _maven.oracle.com_ + + true + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + +`); }); test('fillProperties', () => { process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'; - settings.fillProperties(xmlTestProfile); + const xml = new DOMParser().parseFromString(''); - const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile); - expect(xmlStr).toBe(` - - - _properties_ - - true - - propertyValue1propertyValue2 - - - _sonatype-snapshots_ - - false - - - - `); + settings.fillProperties(xml); + + const xmlStr = new XMLSerializer().serializeToString(xml); + expect(xmlStr).toBe(` + + _properties_ + + true + + propertyValue1propertyValue2 +`); }) test('fillProperties do nothing if no params', () => { diff --git a/templates/mirrors.xml b/templates/mirrors.xml new file mode 100644 index 00000000..a5acf138 --- /dev/null +++ b/templates/mirrors.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/templates/oracle-repo.xml b/templates/oracle-repo.xml new file mode 100644 index 00000000..551d70cf --- /dev/null +++ b/templates/oracle-repo.xml @@ -0,0 +1,31 @@ + + + _maven.oracle.com_ + + true + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + + + + maven.oracle.com + https://maven.oracle.com + + true + + + false + + + + diff --git a/templates/oracleServers.xml b/templates/oracleServers.xml new file mode 100644 index 00000000..62fdcf05 --- /dev/null +++ b/templates/oracleServers.xml @@ -0,0 +1,23 @@ + + + + + + + + ANY + ANY + OAM 11g + + + + + + http.protocol.allow-circular-redirects + %b,true + + + + + + diff --git a/templates/properties.xml b/templates/properties.xml new file mode 100644 index 00000000..3e4c1bd4 --- /dev/null +++ b/templates/properties.xml @@ -0,0 +1,8 @@ + + + _properties_ + + true + + + diff --git a/templates/servers.xml b/templates/servers.xml new file mode 100644 index 00000000..97de7be4 --- /dev/null +++ b/templates/servers.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/templates/settings.xml b/templates/settings.xml index fe88380a..f977e9b3 100644 --- a/templates/settings.xml +++ b/templates/settings.xml @@ -1,46 +1,6 @@ - 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 + + + diff --git a/templates/sonatype-snapshot.xml b/templates/sonatype-snapshot.xml new file mode 100644 index 00000000..285f73c6 --- /dev/null +++ b/templates/sonatype-snapshot.xml @@ -0,0 +1,31 @@ + + + _sonatype-snapshots_ + + true + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + sonatype-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + +