Skip to content
This repository has been archived by the owner on Dec 6, 2022. It is now read-only.

Commit

Permalink
Add overwrite-settings to disable overwriting settings.xml (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
joschi authored Jul 20, 2020
1 parent 971327c commit 5f767b2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 13 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ The two `settings.xml` files created from the above example look like the follow
</servers>
```

***NOTE: The `settings.xml` file is created in the Actions $HOME directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.***
***NOTE:*** The `settings.xml` file is created in the Actions $HOME directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.

If you don't want to overwrite the `settings.xml` file, you can set `overwrite-settings: false`.

See the help docs on [Publishing a Package](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-apache-maven-for-use-with-github-packages#publishing-a-package) for more information on the `pom.xml` file.

Expand Down
20 changes: 20 additions & 0 deletions __tests__/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ describe('auth tests', () => {
);
}, 100000);

it('does not overwrite existing settings.xml files', async () => {
const id = 'packages';
const username = 'USERNAME';
const password = 'PASSWORD';

fs.mkdirSync(m2Dir, {recursive: true});
fs.writeFileSync(settingsFile, 'FAKE FILE');
expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);

await auth.configAuthentication(id, username, password, false);

expect(fs.existsSync(m2Dir)).toBe(true);
expect(fs.existsSync(settingsFile)).toBe(true);
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual('FAKE FILE');
}, 100000);

it('does not create settings.xml without required parameters', async () => {
await auth.configAuthentication('FOO');

Expand All @@ -94,6 +111,7 @@ describe('auth tests', () => {
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD)
);
fs.unlinkSync(settingsFile);

await auth.configAuthentication(undefined, 'BAR', undefined);

Expand All @@ -102,6 +120,7 @@ describe('auth tests', () => {
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD)
);
fs.unlinkSync(settingsFile);

await auth.configAuthentication(undefined, undefined, 'BAZ');

Expand All @@ -110,6 +129,7 @@ describe('auth tests', () => {
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ')
);
fs.unlinkSync(settingsFile);

await auth.configAuthentication();

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ inputs:
settings-path:
description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
required: false
overwrite-settings:
description: 'Overwrite the settings.xml file if it exists. Default is "true".'
required: false
outputs:
path:
description: 'Path to where the java environment has been installed (same as $JAVA_HOME)'
Expand Down
18 changes: 12 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const DEFAULT_PASSWORD = 'GITHUB_TOKEN';
export async function configAuthentication(
id = DEFAULT_ID,
username = DEFAULT_USERNAME,
password = DEFAULT_PASSWORD
password = DEFAULT_PASSWORD,
overwriteSettings = true
) {
console.log(
`creating ${SETTINGS_FILE} with server-id: ${id};`,
Expand All @@ -28,7 +29,7 @@ export async function configAuthentication(
);
await io.mkdirP(directory);
core.debug(`created directory ${directory}`);
await write(directory, generate(id, username, password));
await write(directory, generate(id, username, password), overwriteSettings);
}

function escapeXML(value: string) {
Expand Down Expand Up @@ -59,12 +60,20 @@ export function generate(
`;
}

async function write(directory: string, settings: string) {
async function write(
directory: string,
settings: string,
overwriteSettings: boolean
) {
const location = path.join(directory, SETTINGS_FILE);
if (fs.existsSync(location)) {
const exists = fs.existsSync(location);
if (exists && overwriteSettings) {
console.warn(`overwriting existing file ${location}`);
} else {
} else if (!exists) {
console.log(`writing ${location}`);
} else {
console.log(`not overwriting existing file ${location}`);
return;
}

return fs.writeFileSync(location, settings, {
Expand Down
9 changes: 8 additions & 1 deletion src/setup-jdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ async function run() {
core.getInput('server-username', {required: false}) || undefined;
const password =
core.getInput('server-password', {required: false}) || undefined;
const overwriteSettings =
core.getInput('overwrite-settings', {required: false}) || 'true';

await auth.configAuthentication(id, username, password);
await auth.configAuthentication(
id,
username,
password,
overwriteSettings === 'true'
);
} catch (error) {
core.setFailed(error.message);
}
Expand Down

0 comments on commit 5f767b2

Please sign in to comment.