Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom repositories #319

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
16 changes: 15 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -251,4 +265,4 @@ test('run with all feature', () => {
<nonProxyHosts>nonProxyHost</nonProxyHosts>
</proxy></proxies>
</settings>`);
})
})
63 changes: 59 additions & 4 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')) {
Expand All @@ -268,6 +321,7 @@ function generate() {
addApacheSnapshots(settingsXml);
addSonatypeSnapshots(settingsXml);
addOracleRepo(settingsXml);
fillRepositories(settingsXml,'repositories')
writeSettings(settingsPath, settingsXml);
core.saveState('maven-settings', 'ok');
}
Expand All @@ -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');
}
}

Expand All @@ -296,6 +350,7 @@ module.exports = {
fillProxies,
fillServerForGithub,
fillProperties,
fillRepositories,
addApacheSnapshots,
addSonatypeSnapshots,
addOracleRepo,
Expand Down
74 changes: 65 additions & 9 deletions settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
])
);
})
Expand All @@ -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/)
])
);
})
Expand All @@ -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"}]'
Expand All @@ -751,27 +751,27 @@ 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'));

settings.generate();

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';
Expand All @@ -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/)
])
Expand All @@ -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 changes: 9 additions & 0 deletions templates/custom-repositories.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

<profile>
<id>_custom_repositories_</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories/>
<pluginRepositories/>
</profile>
6 changes: 6 additions & 0 deletions templates/repositories.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<repository>
<id/>
<name/>
<url/>
<snapshots/>
</repository>