init commit

This commit is contained in:
Slawomir Jaranowski
2019-10-01 22:14:15 +02:00
commit 67c2286c8f
13 changed files with 5926 additions and 0 deletions

17
.eslintrc.json Normal file
View File

@ -0,0 +1,17 @@
{
"env": {
"commonjs": true,
"es6": true,
"node": true
},
"extends": "eslint:recommended",
"globals": {
"Atomics": "readonly",
"SharedArrayBuffer": "readonly"
},
"parserOptions": {
"ecmaVersion": 2018
},
"rules": {
}
}

19
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,19 @@
name: "Test Action"
on:
pull_request:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- run: npm ci
- run: npm test
- uses: ./
with:
servers: '[{"id": "serverId", "username": "username", "password": "password"}]'
properties: '[{"prop1": "value1"}, {"prop2": "value2"}]'
sonatype-snapshots: true
- run: cat ~/.m2/settings.xml

69
.gitignore vendored Normal file
View File

@ -0,0 +1,69 @@
# comment this out distribution branches
node_modules/
# Editors
.vscode
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Other Dependency directories
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
# Mac OS specific
.DS_Store

22
LICENSE Executable file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2019 Slawomir Jaranowski and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

46
README.md Normal file
View File

@ -0,0 +1,46 @@
# maven-settings-action
This action setup maven environment for use in action by:
- create maven setings.xml
- set ```interactiveMode``` to false - useful in CI system
# Usage
See [action.yml](action.yml)
Crate default ```settings.xml```:
```yml
steps:
- uses: s4u/maven-settings-action@v1
```
Create ```settings.xml``` with server section:
```yml
steps:
- uses: s4u/maven-settings-action@v1
with:
servers: '[{"id": "serverId", "username": "username", "password": "password"}]'
```
Create ```settings.xml``` with maven properties:
```yml
steps:
- uses: s4u/maven-settings-action@v1
with:
properties: '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
```
Create ```settings.xml``` with https://oss.sonatype.org/content/repositories/snapshots in repository list
```yml
steps:
- uses: s4u/maven-settings-action@v1
with:
sonatype-snapshots: true
```
# License
The scripts and documentation in this project are released under the [MIT License](LICENSE)
# Contributions
Contributions are welcome!

19
action.yml Normal file
View File

@ -0,0 +1,19 @@
name: 'maven-setings-action'
description: 'Prepare maven settings.xml'
inputs:
servers:
description: 'servers definition in joson array, eg: [{"id": "serverId", "username": "username", "password": "password"}]'
required: false
properties:
description: 'json array with properties, eg [{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]'
required: false
sonatype-snapshots:
description: 'add https://oss.sonatype.org/content/repositories/snapshots to repository list - true or false'
default: "false"
required: false
runs:
using: 'node12'
main: 'index.js'

32
index.js Normal file
View File

@ -0,0 +1,32 @@
const core = require('@actions/core');
const path = require('path');
const fs = require('fs');
const settings = require('./settings');
// most @actions toolkit packages have async methods
async function run() {
try {
const settingsPath = path.join(process.env.HOME, '.m2', 'settings.xml');
core.info('Prepare maven setings: ' + settingsPath);
if ( fs.existsSync(settingsPath) ) {
core.warning('maven settings.xml already exists - skip');
return;
}
const templateXml = settings.getSettingsTemplate();
settings.fillServers(templateXml);
settings.fillProperties(templateXml);
settings.addSonatypeSnapshots(templateXml);
settings.writeSettings(settingsPath, templateXml);
} catch (error) {
core.setFailed(error.message);
}
}
run();
module.exports = { run };

42
index.test.js Normal file
View File

@ -0,0 +1,42 @@
const process = require('process');
const cp = require('child_process');
const path = require('path');
const fs = require('fs');
const testHomePath = path.join(__dirname, 'temp');
const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
const indexPath = path.join(__dirname, 'index.js');
beforeAll(() => {
if (!fs.existsSync(testHomePath)) {
fs.mkdirSync(testHomePath);
}
process.env['HOME'] = testHomePath;
});
afterEach(() => {
try {
fs.unlinkSync(settingsPath);
} catch (error) {
console.error(error.message);
}
});
afterAll(() => {
try {
fs.rmdirSync(path.join(testHomePath, ".m2"));
fs.rmdirSync(testHomePath);
} catch (error) {
console.error(error.message);
}
});
test('run with default values', () => {
console.log(cp.execSync(`node ${indexPath}`, { env: process.env }).toString());
const settingsStatus = fs.lstatSync(settingsPath);
expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0);
})

5354
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

35
package.json Normal file
View File

@ -0,0 +1,35 @@
{
"name": "maven-settings-action",
"version": "1.0.0",
"description": "Prepare maven settings",
"main": "index.js",
"scripts": {
"lint": "eslint index.js settings.js",
"test": "eslint index.js settings.js && jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/s4u/maven-settings-action.git"
},
"keywords": [
"GitHub",
"Actions",
"Setup",
"Maven"
],
"author": "S. Jaranowski",
"license": "MIT",
"bugs": {
"url": "https://github.com/s4u/maven-settings-action/issues"
},
"homepage": "https://github.com/s4u/maven-settings-action#readme",
"dependencies": {
"@actions/core": "^1.1.2",
"xmldom": "^0.1.27",
"xpath": "0.0.27"
},
"devDependencies": {
"eslint": "^6.5.1",
"jest": "^24.9.0"
}
}

90
settings.js Normal file
View File

@ -0,0 +1,90 @@
const core = require('@actions/core');
const path = require('path');
const fs = require('fs');
const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer;
const xpath = require('xpath');
function getSettingsTemplate() {
const templatePath = path.join(__dirname, 'templates', 'settings.xml');
const templateStr = fs.readFileSync(templatePath).toString();
return new DOMParser().parseFromString(templateStr, 'text/xml');
}
function writeSettings(settingsPath, templateXml) {
// create .m2 directory if not exists
if (!fs.existsSync(path.dirname(settingsPath))) {
fs.mkdirSync(path.dirname(settingsPath));
}
const settingStr = new XMLSerializer().serializeToString(templateXml);
fs.writeFileSync(settingsPath, settingStr);
}
function fillServers(template) {
const servers = core.getInput('servers');
if (!servers) {
return;
}
const serversXml = template.getElementsByTagName('servers')[0];
JSON.parse(servers).forEach((server) => {
const serverXml = template.createElement('server');
serversXml.appendChild(serverXml);
for (const key in server) {
const keyXml = template.createElement(key);
keyXml.textContent = server[key];
serverXml.appendChild(keyXml);
}
});
}
function activateProfile(template, profileId) {
const activeByDefault = xpath
.select(`/settings/profiles/profile[id[contains(text(),"${profileId}")]]/activation/activeByDefault`, template);
if (activeByDefault) {
activeByDefault[0].textContent = 'true';
}
}
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];
JSON.parse(properties).forEach((property) => {
for (const key in property) {
const keyXml = template.createElement(key);
keyXml.textContent = property[key];
propertiesXml.appendChild(keyXml);
}
});
}
function addSonatypeSnapshots(template) {
const val = core.getInput('sonatype-snapshots');
if (val && val.toLocaleLowerCase() == 'true') {
activateProfile(template, '_sonatype-snapshots_')
}
}
module.exports = {
getSettingsTemplate,
writeSettings,
fillServers,
fillProperties,
addSonatypeSnapshots
}

133
settings.test.js Normal file
View File

@ -0,0 +1,133 @@
const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer;
const settings = require('./settings');
var xmlTestProfile = undefined;
beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`<settings>
<profiles>
<profile>
<id>_properties_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties/>
</profile>
<profile>
<id>_sonatype-snapshots_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</settings>`);
});
test('template should be read', () => {
const template = settings.getSettingsTemplate();
expect(template).toBeDefined();
});
test('fillServers do nothing if no params', () => {
const xml = new DOMParser().parseFromString("<servers/>");
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("<servers/>");
});
test('fillServers one server', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("<servers>" +
"<server><id>id1</id><username>username1</username><password>password1</password></server>" +
"</servers>");
});
test('fillServers two servers', () => {
const xml = new DOMParser().parseFromString("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\
{"id": "id2", "username": "username2", "password":"password2"}]';
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("<servers>" +
"<server><id>id1</id><username>username1</username><password>password1</password></server>" +
"<server><id>id2</id><username>username2</username><password>password2</password></server>" +
"</servers>");
});
test('addSonatypeSnapshots activate', () => {
process.env['INPUT_SONATYPE-SNAPSHOTS'] = "true";
settings.addSonatypeSnapshots(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`<settings>
<profiles>
<profile>
<id>_properties_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties/>
</profile>
<profile>
<id>_sonatype-snapshots_</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</settings>`);
});
test('fillProperties', () => {
process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]';
settings.fillProperties(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`<settings>
<profiles>
<profile>
<id>_properties_</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties><propertyName1>propertyValue1</propertyName1><propertyName2>propertyValue2</propertyName2></properties>
</profile>
<profile>
<id>_sonatype-snapshots_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</settings>`);
})

48
templates/settings.xml Normal file
View File

@ -0,0 +1,48 @@
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<interactiveMode>false</interactiveMode>
<profiles>
<profile>
<id>_properties_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties />
</profile>
<profile>
<id>_sonatype-snapshots_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>sonatype-snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<servers />
</settings>