From 5f767b2758c4e417be60b5d9c3dee67005b490d2 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Mon, 20 Jul 2020 13:19:37 +0200 Subject: [PATCH] Add `overwrite-settings` to disable overwriting settings.xml (#11) Closes #10 Refs actions/setup-java#79 --- README.md | 4 +++- __tests__/auth.test.ts | 20 ++++++++++++++++++++ action.yml | 3 +++ dist/index.js | 18 ++++++++++++------ src/auth.ts | 19 ++++++++++++++----- src/setup-jdk.ts | 9 ++++++++- 6 files changed, 60 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 2ab38edc..1435eb74 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,9 @@ The two `settings.xml` files created from the above example look like the follow ``` -***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. diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts index 13509682..3a02e054 100644 --- a/__tests__/auth.test.ts +++ b/__tests__/auth.test.ts @@ -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'); @@ -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); @@ -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'); @@ -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(); diff --git a/action.yml b/action.yml index c9a2c98c..f4aacc4f 100644 --- a/action.yml +++ b/action.yml @@ -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)' diff --git a/dist/index.js b/dist/index.js index 5449fb12..2bed2a67 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2884,7 +2884,7 @@ exports.SETTINGS_FILE = 'settings.xml'; exports.DEFAULT_ID = 'github'; exports.DEFAULT_USERNAME = 'GITHUB_ACTOR'; exports.DEFAULT_PASSWORD = 'GITHUB_TOKEN'; -function configAuthentication(id = exports.DEFAULT_ID, username = exports.DEFAULT_USERNAME, password = exports.DEFAULT_PASSWORD) { +function configAuthentication(id = exports.DEFAULT_ID, username = exports.DEFAULT_USERNAME, password = exports.DEFAULT_PASSWORD, overwriteSettings = true) { return __awaiter(this, void 0, void 0, function* () { console.log(`creating ${exports.SETTINGS_FILE} with server-id: ${id};`, `environment variables: username=\$${username} and password=\$${password}`); // when an alternate m2 location is specified use only that location (no .m2 directory) @@ -2892,7 +2892,7 @@ function configAuthentication(id = exports.DEFAULT_ID, username = exports.DEFAUL const directory = path.join(core.getInput('settings-path') || os.homedir(), core.getInput('settings-path') ? '' : exports.M2_DIR); yield io.mkdirP(directory); core.debug(`created directory ${directory}`); - yield write(directory, generate(id, username, password)); + yield write(directory, generate(id, username, password), overwriteSettings); }); } exports.configAuthentication = configAuthentication; @@ -2919,15 +2919,20 @@ function generate(id = exports.DEFAULT_ID, username = exports.DEFAULT_USERNAME, `; } exports.generate = generate; -function write(directory, settings) { +function write(directory, settings, overwriteSettings) { return __awaiter(this, void 0, void 0, function* () { const location = path.join(directory, exports.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, { encoding: 'utf-8', flag: 'w' @@ -4325,7 +4330,8 @@ function run() { const id = core.getInput('server-id', { required: false }) || undefined; const username = core.getInput('server-username', { required: false }) || undefined; const password = core.getInput('server-password', { required: false }) || undefined; - yield auth.configAuthentication(id, username, password); + const overwriteSettings = core.getInput('overwrite-settings', { required: false }) || 'true'; + yield auth.configAuthentication(id, username, password, overwriteSettings === 'true'); } catch (error) { core.setFailed(error.message); diff --git a/src/auth.ts b/src/auth.ts index 2e7c6e8e..affa25e8 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -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};`, @@ -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) { @@ -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, { diff --git a/src/setup-jdk.ts b/src/setup-jdk.ts index 56c5343d..5d57870c 100644 --- a/src/setup-jdk.ts +++ b/src/setup-jdk.ts @@ -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); }