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

Resolves s4u/maven-settings-action#348, plugin repositories support #349

Merged
merged 1 commit into from
Nov 3, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ steps:
result will be:

```xml
<server>
<servers><server>
<id>serverId</id>
<configuration>
<item1>value1</item1>
Expand Down Expand Up @@ -176,6 +176,14 @@ steps:
repositories: '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
```

## ```settings.xml``` with custom plugin repositories
```yml
steps:
- uses: s4u/maven-settings-action@v3.0.0
with:
pluginRepositories: '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
```


## GitHub actions secrets

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ inputs:
repositories:
description: 'list of custom repositories as json array, e.g: [{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
required: false
pluginRepositories:
description: 'list of custom plugin repositories as json array, e.g: [{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
required: false

runs:
using: 'node20'
Expand Down
2 changes: 1 addition & 1 deletion cleanup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ afterAll(() => {
}

try {
fs.rmdirSync(testHomePath);
fs.rmSync(testHomePath, { recursive: true });
} catch (error) {
}
});
Expand Down
12 changes: 9 additions & 3 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ afterAll(() => {
}

try {
fs.rmdirSync(testHomePath);
fs.rmSync(testHomePath, { recursive: true });
} catch (error) {
}
});
Expand All @@ -75,6 +75,7 @@ test('run with all feature', () => {
process.env['INPUT_SONATYPESNAPSHOTS'] = true;
process.env['INPUT_ORACLEREPO'] = true;
process.env['INPUT_REPOSITORIES'] = '[{"id":"repoId","name":"repoName","url":"url","snapshots":{"enabled":true}}]'
process.env['INPUT_PLUGINREPOSITORIES'] = '[{"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 @@ -213,7 +214,12 @@ test('run with all feature', () => {
<url>url</url>
<snapshots><enabled>true</enabled></snapshots>
</repository></repositories>
<pluginRepositories/>
<pluginRepositories> <pluginRepository>
<id>repoId</id>
<name>repoName</name>
<url>url</url>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository></pluginRepositories>
</profile></profiles>
<servers>
<server>
Expand Down Expand Up @@ -265,4 +271,4 @@ test('run with all feature', () => {
<nonProxyHosts>nonProxyHost</nonProxyHosts>
</proxy></proxies>
</settings>`);
})
})
43 changes: 29 additions & 14 deletions settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function fillServers(template, templateName) {
server.configuration));
}

function fillRepository(templateXml, templateName, id, name, url, snapshots) {
function fillRepository(templateXml, templateName, id, name, url, releases, snapshots) {

if (!id || !url) {
core.setFailed(templateName + ' must contain id and url');
Expand All @@ -148,22 +148,30 @@ function fillRepository(templateXml, templateName, id, name, url, snapshots) {
}
}

const snapshotsTag = repositoryXml.getElementsByTagName('snapshots')[0];
if (snapshots) {
jsonToXml(templateXml, snapshotsTag, snapshots);
} else {
repositoryXml.documentElement.removeChild(snapshotsTag);
const additionalTags = {
'releases': releases,
'snapshots': snapshots
};
for (const tag in additionalTags) {
const repositoryTag = repositoryXml.getElementsByTagName(tag)[0];
const tagValue = additionalTags[tag];
if (tagValue) {
jsonToXml(templateXml, repositoryTag, tagValue);
} else {
repositoryXml.documentElement.removeChild(repositoryTag);
}
}

const repositoriesXml = templateXml.getElementsByTagName('repositories')[0];
const repositoriesXml = templateXml.getElementsByTagName(templateName)[0];
repositoriesXml.appendChild(repositoryXml);
}

function fillRepositories(template, templateName) {
function fillRepositories(template) {

const repositories = core.getInput(templateName);
const repositories = core.getInput('repositories');
const pluginRepositories = core.getInput('pluginRepositories');

if (!repositories) {
if (!repositories && !pluginRepositories) {
return;
}

Expand All @@ -172,9 +180,16 @@ function fillRepositories(template, templateName) {
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));
if (repositories) {
JSON.parse(repositories).forEach((repository) =>
fillRepository(customRepositoriesTemplate, 'repositories',
repository.id, repository.name, repository.url, repository.releases, repository.snapshots));
}
if (pluginRepositories) {
JSON.parse(pluginRepositories).forEach((repository) =>
fillRepository(customRepositoriesTemplate, 'pluginRepositories',
repository.id, repository.name, repository.url, repository.releases, repository.snapshots));
}
}

function fillMirror(template, id, name, mirrorOf, url) {
Expand Down Expand Up @@ -321,7 +336,7 @@ function generate() {
addApacheSnapshots(settingsXml);
addSonatypeSnapshots(settingsXml);
addOracleRepo(settingsXml);
fillRepositories(settingsXml,'repositories')
fillRepositories(settingsXml)
writeSettings(settingsPath, settingsXml);
core.saveState('maven-settings', 'ok');
}
Expand Down
47 changes: 45 additions & 2 deletions settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ afterAll(() => {
}

try {
fs.rmdirSync(testHomePath);
fs.rmSync(testHomePath, { recursive: true });
} catch (error) {
}
});
Expand Down Expand Up @@ -821,17 +821,60 @@ test('addCustomRepositories - one with snapshots one without', () => {
<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('addCustomPluginRepositories - one with releases and snapshots one without', () => {

process.env['INPUT_PLUGINREPOSITORIES'] = `
[{"id":"repoId",
"name":"repoName",
"url":"url",
"releases":{"enabled":false},
"snapshots":{"enabled":true}
},{
"id":"repoId2",
"url":"url2"
}]`

const xml = stringAsXml('<profiles/>');

settings.fillRepositories(xml,'pluginRepositories');

const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe(`<profiles>
<profile>
<id>_custom_repositories_</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories/>
<pluginRepositories> <pluginRepository>
<id>repoId</id>
<name>repoName</name>
<url>url</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository> <pluginRepository>
<id>repoId2</id>

<url>url2</url>


</pluginRepository></pluginRepositories>
</profile></profiles>`);
});

test('addCustomRepositories - fail if url is missing', () => {

process.env['INPUT_REPOSITORIES'] = '[{"id":"repoId","name":"repoName"}]'
Expand All @@ -856,4 +899,4 @@ test('addCustomRepositories - fail if url is missing', () => {
expect.stringMatching(/::error::repositories must contain id and url/)
])
);
});
});
7 changes: 7 additions & 0 deletions templates/pluginRepositories.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<pluginRepository>
<id/>
<name/>
<url/>
<releases/>
<snapshots/>
</pluginRepository>
3 changes: 2 additions & 1 deletion templates/repositories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
<id/>
<name/>
<url/>
<releases/>
<snapshots/>
</repository>
</repository>