mirror of
https://github.com/s4u/maven-settings-action.git
synced 2026-02-12 00:05:49 +08:00
Add support for custom repositories
This commit is contained in:
committed by
Slawomir Jaranowski
parent
25432ff633
commit
879f94d6bf
@ -168,6 +168,15 @@ steps:
|
||||
oracleRepo: true
|
||||
```
|
||||
|
||||
## ```settings.xml``` with custom repositories
|
||||
```yml
|
||||
steps:
|
||||
- uses: s4u/maven-settings-action@v2.8.0
|
||||
with:
|
||||
repositories: '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
|
||||
```
|
||||
|
||||
|
||||
## GitHub actions secrets
|
||||
|
||||
It is also possible pass in Github Secrets e.g.
|
||||
|
||||
@ -43,6 +43,9 @@ inputs:
|
||||
description: 'add Oracle Maven Repository'
|
||||
default: "false"
|
||||
required: false
|
||||
repositories:
|
||||
description: 'list of custom repositories as json array, e.g: [{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
|
||||
required: false
|
||||
|
||||
runs:
|
||||
using: 'node16'
|
||||
|
||||
@ -74,6 +74,7 @@ test('run with all feature', () => {
|
||||
process.env['INPUT_APACHESNAPSHOTS'] = true;
|
||||
process.env['INPUT_SONATYPESNAPSHOTS'] = true;
|
||||
process.env['INPUT_ORACLEREPO'] = true;
|
||||
process.env['INPUT_REPOSITORIES'] = '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
|
||||
|
||||
cp.spawnSync('node', [ `${indexPath}` ], { env: process.env, stdio: 'inherit' });
|
||||
const settingsStatus = fs.lstatSync(settingsPath);
|
||||
@ -200,6 +201,19 @@ test('run with all feature', () => {
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>_custom_repositories_</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<repositories> <repository>
|
||||
<id>repoId</id>
|
||||
<name>repoName</name>
|
||||
<url>url</url>
|
||||
<snapshots><enabled>true</enabled></snapshots>
|
||||
</repository></repositories>
|
||||
<pluginRepositories/>
|
||||
</profile></profiles>
|
||||
<servers>
|
||||
<server>
|
||||
@ -251,4 +265,4 @@ test('run with all feature', () => {
|
||||
<nonProxyHosts>nonProxyHost</nonProxyHosts>
|
||||
</proxy></proxies>
|
||||
</settings>`);
|
||||
})
|
||||
})
|
||||
63
settings.js
63
settings.js
@ -124,6 +124,59 @@ function fillServers(template, templateName) {
|
||||
server.configuration));
|
||||
}
|
||||
|
||||
function fillRepository(templateXml, templateName, id, name, url, snapshots) {
|
||||
|
||||
if (!id || !url) {
|
||||
core.setFailed(templateName + ' must contain id and url');
|
||||
return;
|
||||
}
|
||||
|
||||
const repositoryXml = getTemplate(templateName + '.xml')
|
||||
repositoryXml.getElementsByTagName('id')[0].textContent = id;
|
||||
|
||||
const repositoryTags = {
|
||||
'name': name,
|
||||
'url': url
|
||||
};
|
||||
for (const tag in repositoryTags) {
|
||||
const repositoryTag = repositoryXml.getElementsByTagName(tag)[0];
|
||||
const tagValue = repositoryTags[tag];
|
||||
if (tagValue) {
|
||||
repositoryTag.textContent = tagValue;
|
||||
} else {
|
||||
repositoryXml.documentElement.removeChild(repositoryTag);
|
||||
}
|
||||
}
|
||||
|
||||
const snapshotsTag = repositoryXml.getElementsByTagName('snapshots')[0];
|
||||
if (snapshots) {
|
||||
jsonToXml(templateXml, snapshotsTag, snapshots);
|
||||
} else {
|
||||
repositoryXml.documentElement.removeChild(snapshotsTag);
|
||||
}
|
||||
|
||||
const repositoriesXml = templateXml.getElementsByTagName('repositories')[0];
|
||||
repositoriesXml.appendChild(repositoryXml);
|
||||
}
|
||||
|
||||
function fillRepositories(template, templateName) {
|
||||
|
||||
const repositories = core.getInput(templateName);
|
||||
|
||||
if (!repositories) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
const customRepositoriesTemplate = getTemplate('custom-repositories.xml');
|
||||
const profilesXml = template.getElementsByTagName('profiles')[0];
|
||||
profilesXml.appendChild(customRepositoriesTemplate);
|
||||
|
||||
JSON.parse(repositories).forEach((repository) =>
|
||||
fillRepository(customRepositoriesTemplate, templateName, repository.id, repository.name, repository.url,
|
||||
repository.snapshots));
|
||||
}
|
||||
|
||||
function fillMirror(template, id, name, mirrorOf, url) {
|
||||
|
||||
if (!id || !name || !mirrorOf || !url) {
|
||||
@ -247,7 +300,7 @@ function generate() {
|
||||
|
||||
const settingsPath = getSettingsPath();
|
||||
|
||||
core.info('Prepare maven setings: ' + settingsPath);
|
||||
core.info('Prepare maven settings: ' + settingsPath);
|
||||
|
||||
if (fs.existsSync(settingsPath)) {
|
||||
if (isInputTrue('override')) {
|
||||
@ -268,6 +321,7 @@ function generate() {
|
||||
addApacheSnapshots(settingsXml);
|
||||
addSonatypeSnapshots(settingsXml);
|
||||
addOracleRepo(settingsXml);
|
||||
fillRepositories(settingsXml,'repositories')
|
||||
writeSettings(settingsPath, settingsXml);
|
||||
core.saveState('maven-settings', 'ok');
|
||||
}
|
||||
@ -279,12 +333,12 @@ function cleanup() {
|
||||
if (mavenSettingsState == 'ok') {
|
||||
if (fs.existsSync(settingsPath)) {
|
||||
fs.unlinkSync(settingsPath);
|
||||
core.info('Cleanup maven setings: ' + settingsPath + ' - file was removed');
|
||||
core.info('Cleanup maven settings: ' + settingsPath + ' - file was removed');
|
||||
} else {
|
||||
core.warning('Cleanup maven setings: ' + settingsPath + ' - file not exist');
|
||||
core.warning('Cleanup maven settings: ' + settingsPath + ' - file not exist');
|
||||
}
|
||||
} else {
|
||||
core.info('Cleanup maven setings: ' + settingsPath + ' - file wasn\'t generated by action');
|
||||
core.info('Cleanup maven settings: ' + settingsPath + ' - file wasn\'t generated by action');
|
||||
}
|
||||
}
|
||||
|
||||
@ -296,6 +350,7 @@ module.exports = {
|
||||
fillProxies,
|
||||
fillServerForGithub,
|
||||
fillProperties,
|
||||
fillRepositories,
|
||||
addApacheSnapshots,
|
||||
addSonatypeSnapshots,
|
||||
addOracleRepo,
|
||||
|
||||
@ -708,7 +708,7 @@ test('cleanup - not generated', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/Cleanup maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file wasn\'t generated by action/)
|
||||
expect.stringMatching(/Cleanup maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file wasn\'t generated by action/)
|
||||
])
|
||||
);
|
||||
})
|
||||
@ -721,7 +721,7 @@ test('cleanup - not exist', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/::warning::Cleanup maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file not exist/)
|
||||
expect.stringMatching(/::warning::Cleanup maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file not exist/)
|
||||
])
|
||||
);
|
||||
})
|
||||
@ -735,13 +735,13 @@ test('cleanup - ok', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/Cleanup maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file was removed/)
|
||||
expect.stringMatching(/Cleanup maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml - file was removed/)
|
||||
])
|
||||
);
|
||||
expect(fs.existsSync(settingsPath)).not.toBeTruthy();
|
||||
})
|
||||
|
||||
test('genereate', () => {
|
||||
test('generate', () => {
|
||||
|
||||
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]';
|
||||
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'
|
||||
@ -751,13 +751,13 @@ test('genereate', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/Prepare maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/Prepare maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/:save-state name=maven-settings::ok/)
|
||||
])
|
||||
);
|
||||
})
|
||||
|
||||
test('genereate - skip', () => {
|
||||
test('generate - skip', () => {
|
||||
|
||||
fs.closeSync(fs.openSync(settingsPath, 'w'));
|
||||
|
||||
@ -765,13 +765,13 @@ test('genereate - skip', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/Prepare maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/Prepare maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/::warning::maven settings.xml already exists - skip/)
|
||||
])
|
||||
);
|
||||
})
|
||||
|
||||
test('genereate - override', () => {
|
||||
test('generate - override', () => {
|
||||
|
||||
fs.closeSync(fs.openSync(settingsPath, 'w'));
|
||||
process.env['INPUT_OVERRIDE'] = 'true';
|
||||
@ -780,7 +780,7 @@ test('genereate - override', () => {
|
||||
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/Prepare maven setings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/Prepare maven settings: \..+[\/\\]{1,2}\.m2[\/\\]{1,2}settings.xml/),
|
||||
expect.stringMatching(/maven settings.xml already exists - override/),
|
||||
expect.stringMatching(/:save-state name=maven-settings::ok/)
|
||||
])
|
||||
@ -801,3 +801,59 @@ test('generate - custom path', () => {
|
||||
expect(settingsStatus.isFile()).toBeTruthy();
|
||||
expect(settingsStatus.size).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
test('addCustomRepositories - one with snapshots one without', () => {
|
||||
|
||||
process.env['INPUT_REPOSITORIES'] = '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}},{"id":"repoId2","url":"url2"}]'
|
||||
|
||||
const xml = stringAsXml('<profiles/>');
|
||||
|
||||
settings.fillRepositories(xml,'repositories');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml);
|
||||
expect(xmlStr).toBe(`<profiles>
|
||||
<profile>
|
||||
<id>_custom_repositories_</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<repositories> <repository>
|
||||
<id>repoId</id>
|
||||
<name>repoName</name>
|
||||
<url>url</url>
|
||||
<snapshots><enabled>true</enabled></snapshots>
|
||||
</repository> <repository>
|
||||
<id>repoId2</id>
|
||||
|
||||
<url>url2</url>
|
||||
|
||||
</repository></repositories>
|
||||
<pluginRepositories/>
|
||||
</profile></profiles>`);
|
||||
});
|
||||
|
||||
test('addCustomRepositories - fail if url is missing', () => {
|
||||
|
||||
process.env['INPUT_REPOSITORIES'] = '[{"id":"repoId","name":"repoName"}]'
|
||||
|
||||
const xml = stringAsXml('<profiles/>');
|
||||
|
||||
settings.fillRepositories(xml,'repositories');
|
||||
|
||||
const xmlStr = new XMLSerializer().serializeToString(xml);
|
||||
|
||||
expect(xmlStr).toBe(`<profiles>
|
||||
<profile>
|
||||
<id>_custom_repositories_</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<repositories/>
|
||||
<pluginRepositories/>
|
||||
</profile></profiles>`);
|
||||
expect(consoleOutput).toEqual(
|
||||
expect.arrayContaining([
|
||||
expect.stringMatching(/::error::repositories must contain id and url/)
|
||||
])
|
||||
);
|
||||
});
|
||||
9
templates/custom-repositories.xml
Normal file
9
templates/custom-repositories.xml
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
<profile>
|
||||
<id>_custom_repositories_</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<repositories/>
|
||||
<pluginRepositories/>
|
||||
</profile>
|
||||
6
templates/repositories.xml
Normal file
6
templates/repositories.xml
Normal file
@ -0,0 +1,6 @@
|
||||
<repository>
|
||||
<id/>
|
||||
<name/>
|
||||
<url/>
|
||||
<snapshots/>
|
||||
</repository>
|
||||
Reference in New Issue
Block a user