From b86b1900447583e4e7c2d54f57bd951c89c0189a Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 12:24:05 +0300 Subject: [PATCH 1/6] add overwrite-settings parameter --- .github/workflows/e2e-publishing.yml | 82 +++++++++++++++++++++++++--- __tests__/auth.test.ts | 27 +++++++-- action.yml | 4 ++ dist/cleanup/index.js | 10 +++- dist/setup/index.js | 31 ++++++++--- docs/advanced-usage.md | 4 +- src/auth.ts | 37 ++++++++++--- src/constants.ts | 1 + src/util.ts | 5 ++ 9 files changed, 165 insertions(+), 36 deletions(-) diff --git a/.github/workflows/e2e-publishing.yml b/.github/workflows/e2e-publishing.yml index 2405e41d5..ecb102368 100644 --- a/.github/workflows/e2e-publishing.yml +++ b/.github/workflows/e2e-publishing.yml @@ -11,6 +11,10 @@ on: paths-ignore: - '**.md' +defaults: + run: + shell: pwsh + jobs: setup-java-publishing: name: Validate settings.xml @@ -34,12 +38,7 @@ jobs: gpg-passphrase: MAVEN_GPG_PASSPHRASE - name: Validate settings.xml run: | - $homePath = $env:USERPROFILE - if (-not $homePath) { - $homePath = $env:HOME - } - $xmlPath = Join-Path $homePath ".m2" "settings.xml" - + $xmlPath = Join-Path $HOME ".m2" "settings.xml" Get-Content $xmlPath | ForEach-Object { Write-Host $_ } [xml]$xml = Get-Content $xmlPath @@ -51,7 +50,73 @@ jobs: if (($servers[1].id -ne 'gpg.passphrase') -or ($servers[1].passphrase -ne '${env.MAVEN_GPG_PASSPHRASE}')) { throw "Generated XML file is incorrect" } - shell: pwsh + + test-publishing-overwrite: + name: Validate settings.xml is not overwritten if flag is false + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Create fake settings.xml + run: | + $xmlPath = Join-Path $HOME ".m2" "settings.xml" + Set-Content -Path $xmlPath -Value "Fake_XML" + - name: setup-java + uses: ./ + id: setup-java + with: + distribution: 'adopt' + java-version: '11' + server-id: maven + server-username: MAVEN_USERNAME + server-password: MAVEN_CENTRAL_TOKEN + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Validate settings.xml is overwritten + run: | + $xmlPath = Join-Path $HOME ".m2" "settings.xml" + Get-Content $xmlPath | ForEach-Object { Write-Host $_ } + + $xml = Get-Content $xmlPath -Raw + if ($raw -notlike 'maven') { + throw "settings.xml file is not overwritten" + } + + test-publishing-skip-overwrite: + name: Validate settings.xml is not overwritten if flag is false + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Create fake settings.xml + run: | + $xmlPath = Join-Path $HOME ".m2" "settings.xml" + Set-Content -Path $xmlPath -Value "Fake_XML" + - name: setup-java + uses: ./ + id: setup-java + with: + distribution: 'adopt' + java-version: '11' + server-id: maven + server-username: MAVEN_USERNAME + server-password: MAVEN_CENTRAL_TOKEN + overwrite-settings: false + gpg-passphrase: MAVEN_GPG_PASSPHRASE + - name: Validate that settings.xml is not overwritten + run: | + $xmlPath = Join-Path $HOME ".m2" "settings.xml" + $content = Get-Content -Path $xmlPath -Raw + if ($content -ne "Fake_XML") { + throw "settings.xml file was overwritten but it should not be" + } test-publishing-custom-location: name: Validate settings.xml in custom location @@ -79,5 +144,4 @@ jobs: $path = Join-Path $env:RUNNER_TEMP "settings.xml" if (-not (Test-Path $path)) { throw "settings.xml file is not found in expected location" - } - shell: pwsh \ No newline at end of file + } \ No newline at end of file diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts index b45b5109e..fccb3fe53 100644 --- a/__tests__/auth.test.ts +++ b/__tests__/auth.test.ts @@ -35,10 +35,9 @@ describe('auth tests', () => { const altHome = path.join(__dirname, 'runner', 'settings'); const altSettingsFile = path.join(altHome, auth.SETTINGS_FILE); - process.env[`INPUT_SETTINGS-PATH`] = altHome; await io.rmRF(altHome); // ensure it doesn't already exist - await auth.createAuthenticationSettings(id, username, password); + await auth.createAuthenticationSettings(id, username, password, altHome, true); expect(fs.existsSync(m2Dir)).toBe(false); expect(fs.existsSync(settingsFile)).toBe(false); @@ -49,7 +48,6 @@ describe('auth tests', () => { auth.generate(id, username, password) ); - delete process.env[`INPUT_SETTINGS-PATH`]; await io.rmRF(altHome); }, 100000); @@ -58,7 +56,7 @@ describe('auth tests', () => { const username = 'UNAME'; const password = 'TOKEN'; - await auth.createAuthenticationSettings(id, username, password); + await auth.createAuthenticationSettings(id, username, password, m2Dir, true); expect(fs.existsSync(m2Dir)).toBe(true); expect(fs.existsSync(settingsFile)).toBe(true); @@ -71,7 +69,7 @@ describe('auth tests', () => { const password = 'TOKEN'; const gpgPassphrase = 'GPG'; - await auth.createAuthenticationSettings(id, username, password, gpgPassphrase); + await auth.createAuthenticationSettings(id, username, password, m2Dir, true, gpgPassphrase); expect(fs.existsSync(m2Dir)).toBe(true); expect(fs.existsSync(settingsFile)).toBe(true); @@ -90,13 +88,30 @@ describe('auth tests', () => { expect(fs.existsSync(m2Dir)).toBe(true); expect(fs.existsSync(settingsFile)).toBe(true); - await auth.createAuthenticationSettings(id, username, password); + await auth.createAuthenticationSettings(id, username, password, m2Dir, true); expect(fs.existsSync(m2Dir)).toBe(true); expect(fs.existsSync(settingsFile)).toBe(true); expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(auth.generate(id, username, password)); }, 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.createAuthenticationSettings(id, username, password, m2Dir, false); + + expect(fs.existsSync(m2Dir)).toBe(true); + expect(fs.existsSync(settingsFile)).toBe(true); + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual('FAKE FILE'); + }, 100000); + it('generates valid settings.xml with minimal configuration', () => { const id = 'packages'; const username = 'USER'; diff --git a/action.yml b/action.yml index df59abdcc..406ad3a3e 100644 --- a/action.yml +++ b/action.yml @@ -38,6 +38,10 @@ 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 + default: true gpg-private-key: description: 'GPG private key to import. Default is empty string.' required: false diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index 6bcd08775..ca595b1bb 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -2685,17 +2685,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0; +exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; const os_1 = __importDefault(__webpack_require__(87)); const path_1 = __importDefault(__webpack_require__(622)); const fs = __importStar(__webpack_require__(747)); const semver = __importStar(__webpack_require__(876)); +const core = __importStar(__webpack_require__(470)); const tc = __importStar(__webpack_require__(533)); function getTempDir() { let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir(); return tempDirectory; } exports.getTempDir = getTempDir; +function getBooleanInput(inputName, defaultValue = false) { + return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE'; +} +exports.getBooleanInput = getBooleanInput; function getVersionFromToolcachePath(toolPath) { if (toolPath) { return path_1.default.basename(path_1.default.dirname(toolPath)); @@ -6824,7 +6829,7 @@ function isUnixExecutable(stats) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; +exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home'; exports.INPUT_JAVA_VERSION = 'java-version'; exports.INPUT_ARCHITECTURE = 'architecture'; @@ -6835,6 +6840,7 @@ exports.INPUT_SERVER_ID = 'server-id'; exports.INPUT_SERVER_USERNAME = 'server-username'; exports.INPUT_SERVER_PASSWORD = 'server-password'; exports.INPUT_SETTINGS_PATH = 'settings-path'; +exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings'; exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key'; exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; diff --git a/dist/setup/index.js b/dist/setup/index.js index bd0168dfc..fa6862961 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -11302,7 +11302,7 @@ exports.HTMLCollectionImpl = HTMLCollectionImpl; "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; +exports.STATE_GPG_PRIVATE_KEY_FINGERPRINT = exports.INPUT_DEFAULT_GPG_PASSPHRASE = exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = exports.INPUT_GPG_PASSPHRASE = exports.INPUT_GPG_PRIVATE_KEY = exports.INPUT_OVERWRITE_SETTINGS = exports.INPUT_SETTINGS_PATH = exports.INPUT_SERVER_PASSWORD = exports.INPUT_SERVER_USERNAME = exports.INPUT_SERVER_ID = exports.INPUT_JDK_FILE = exports.INPUT_DISTRIBUTION = exports.INPUT_JAVA_PACKAGE = exports.INPUT_ARCHITECTURE = exports.INPUT_JAVA_VERSION = exports.MACOS_JAVA_CONTENT_POSTFIX = void 0; exports.MACOS_JAVA_CONTENT_POSTFIX = 'Contents/Home'; exports.INPUT_JAVA_VERSION = 'java-version'; exports.INPUT_ARCHITECTURE = 'architecture'; @@ -11313,6 +11313,7 @@ exports.INPUT_SERVER_ID = 'server-id'; exports.INPUT_SERVER_USERNAME = 'server-username'; exports.INPUT_SERVER_PASSWORD = 'server-password'; exports.INPUT_SETTINGS_PATH = 'settings-path'; +exports.INPUT_OVERWRITE_SETTINGS = 'overwrite-settings'; exports.INPUT_GPG_PRIVATE_KEY = 'gpg-private-key'; exports.INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; exports.INPUT_DEFAULT_GPG_PRIVATE_KEY = undefined; @@ -12935,17 +12936,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getTempDir = void 0; +exports.getToolcachePath = exports.isVersionSatisfies = exports.getDownloadArchiveExtension = exports.extractJdkFile = exports.getVersionFromToolcachePath = exports.getBooleanInput = exports.getTempDir = void 0; const os_1 = __importDefault(__webpack_require__(87)); const path_1 = __importDefault(__webpack_require__(622)); const fs = __importStar(__webpack_require__(747)); const semver = __importStar(__webpack_require__(876)); +const core = __importStar(__webpack_require__(470)); const tc = __importStar(__webpack_require__(139)); function getTempDir() { let tempDirectory = process.env['RUNNER_TEMP'] || os_1.default.tmpdir(); return tempDirectory; } exports.getTempDir = getTempDir; +function getBooleanInput(inputName, defaultValue = false) { + return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE'; +} +exports.getBooleanInput = getBooleanInput; function getVersionFromToolcachePath(toolPath) { if (toolPath) { return path_1.default.basename(path_1.default.dirname(toolPath)); @@ -13257,6 +13263,7 @@ const os = __importStar(__webpack_require__(87)); const xmlbuilder2_1 = __webpack_require__(255); const constants = __importStar(__webpack_require__(211)); const gpg = __importStar(__webpack_require__(884)); +const util_1 = __webpack_require__(322); exports.M2_DIR = '.m2'; exports.SETTINGS_FILE = 'settings.xml'; function configureAuthentication() { @@ -13264,13 +13271,15 @@ function configureAuthentication() { const id = core.getInput(constants.INPUT_SERVER_ID); const username = core.getInput(constants.INPUT_SERVER_USERNAME); const password = core.getInput(constants.INPUT_SERVER_PASSWORD); + const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), exports.M2_DIR); + const overwriteSettings = util_1.getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; const gpgPassphrase = core.getInput(constants.INPUT_GPG_PASSPHRASE) || (gpgPrivateKey ? constants.INPUT_DEFAULT_GPG_PASSPHRASE : undefined); if (gpgPrivateKey) { core.setSecret(gpgPrivateKey); } - yield createAuthenticationSettings(id, username, password, gpgPassphrase); + yield createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase); if (gpgPrivateKey) { core.info('Importing private gpg key'); const keyFingerprint = (yield gpg.importKey(gpgPrivateKey)) || ''; @@ -13279,14 +13288,13 @@ function configureAuthentication() { }); } exports.configureAuthentication = configureAuthentication; -function createAuthenticationSettings(id, username, password, gpgPassphrase = undefined) { +function createAuthenticationSettings(id, username, password, settingsDirectory, overwriteSettings, gpgPassphrase = undefined) { return __awaiter(this, void 0, void 0, function* () { core.info(`Creating ${exports.SETTINGS_FILE} with server-id: ${id}`); // when an alternate m2 location is specified use only that location (no .m2 directory) // otherwise use the home/.m2/ path - const settingsDirectory = path.join(core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(), core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : exports.M2_DIR); yield io.mkdirP(settingsDirectory); - yield write(settingsDirectory, generate(id, username, password, gpgPassphrase)); + yield write(settingsDirectory, generate(id, username, password, gpgPassphrase), overwriteSettings); }); } exports.createAuthenticationSettings = createAuthenticationSettings; @@ -13322,14 +13330,19 @@ function generate(id, username, password, gpgPassphrase) { }); } 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 settingsExists = fs.existsSync(location); + if (settingsExists && overwriteSettings) { core.info(`Overwriting existing file ${location}`); } + else if (!settingsExists) { + core.info(`Writing to ${location}`); + } else { - core.info(`Writing ${location}`); + core.info(`Skipping generation '${location}' - it already exists and overwriting is not required`); + return; } return fs.writeFileSync(location, settings, { encoding: 'utf-8', diff --git a/docs/advanced-usage.md b/docs/advanced-usage.md index 96d31fbf8..c78ca6811 100644 --- a/docs/advanced-usage.md +++ b/docs/advanced-usage.md @@ -206,7 +206,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/.m2 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/.m2 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` ### Extra setup for pom.xml: diff --git a/src/auth.ts b/src/auth.ts index 30810bb8f..184f2f391 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -8,6 +8,7 @@ import * as os from 'os'; import { create as xmlCreate } from 'xmlbuilder2'; import * as constants from './constants'; import * as gpg from './gpg'; +import { getBooleanInput } from './util'; export const M2_DIR = '.m2'; export const SETTINGS_FILE = 'settings.xml'; @@ -16,6 +17,9 @@ export async function configureAuthentication() { const id = core.getInput(constants.INPUT_SERVER_ID); const username = core.getInput(constants.INPUT_SERVER_USERNAME); const password = core.getInput(constants.INPUT_SERVER_PASSWORD); + const settingsDirectory = + core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), M2_DIR); + const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; const gpgPassphrase = @@ -26,7 +30,14 @@ export async function configureAuthentication() { core.setSecret(gpgPrivateKey); } - await createAuthenticationSettings(id, username, password, gpgPassphrase); + await createAuthenticationSettings( + id, + username, + password, + settingsDirectory, + overwriteSettings, + gpgPassphrase + ); if (gpgPrivateKey) { core.info('Importing private gpg key'); @@ -39,17 +50,19 @@ export async function createAuthenticationSettings( id: string, username: string, password: string, + settingsDirectory: string, + overwriteSettings: boolean, gpgPassphrase: string | undefined = undefined ) { core.info(`Creating ${SETTINGS_FILE} with server-id: ${id}`); // when an alternate m2 location is specified use only that location (no .m2 directory) // otherwise use the home/.m2/ path - const settingsDirectory: string = path.join( - core.getInput(constants.INPUT_SETTINGS_PATH) || os.homedir(), - core.getInput(constants.INPUT_SETTINGS_PATH) ? '' : M2_DIR - ); await io.mkdirP(settingsDirectory); - await write(settingsDirectory, generate(id, username, password, gpgPassphrase)); + await write( + settingsDirectory, + generate(id, username, password, gpgPassphrase), + overwriteSettings + ); } // only exported for testing purposes @@ -92,12 +105,18 @@ 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 settingsExists = fs.existsSync(location); + if (settingsExists && overwriteSettings) { core.info(`Overwriting existing file ${location}`); + } else if (!settingsExists) { + core.info(`Writing to ${location}`); } else { - core.info(`Writing ${location}`); + core.info( + `Skipping generation '${location}' - it already exists and overwriting is not required` + ); + return; } return fs.writeFileSync(location, settings, { diff --git a/src/constants.ts b/src/constants.ts index 41fd1d490..99c8351c5 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -8,6 +8,7 @@ export const INPUT_SERVER_ID = 'server-id'; export const INPUT_SERVER_USERNAME = 'server-username'; export const INPUT_SERVER_PASSWORD = 'server-password'; export const INPUT_SETTINGS_PATH = 'settings-path'; +export const INPUT_OVERWRITE_SETTINGS = 'overwrite-settings'; export const INPUT_GPG_PRIVATE_KEY = 'gpg-private-key'; export const INPUT_GPG_PASSPHRASE = 'gpg-passphrase'; diff --git a/src/util.ts b/src/util.ts index 2864faa09..080597a9a 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,6 +2,7 @@ import os from 'os'; import path from 'path'; import * as fs from 'fs'; import * as semver from 'semver'; +import * as core from '@actions/core'; import * as tc from '@actions/tool-cache'; export function getTempDir() { @@ -10,6 +11,10 @@ export function getTempDir() { return tempDirectory; } +export function getBooleanInput(inputName: string, defaultValue: boolean = false) { + return (core.getInput(inputName) || String(defaultValue)).toUpperCase() === 'TRUE'; +} + export function getVersionFromToolcachePath(toolPath: string) { if (toolPath) { return path.basename(path.dirname(toolPath)); From 4d5942db5130a7ca65d76e2ff2149093e368bf56 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 12:32:56 +0300 Subject: [PATCH 2/6] fix e2e tests --- .github/workflows/e2e-publishing.yml | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/e2e-publishing.yml b/.github/workflows/e2e-publishing.yml index ecb102368..9904e7b60 100644 --- a/.github/workflows/e2e-publishing.yml +++ b/.github/workflows/e2e-publishing.yml @@ -52,7 +52,7 @@ jobs: } test-publishing-overwrite: - name: Validate settings.xml is not overwritten if flag is false + name: settings.xml is overwritten if flag is true runs-on: ${{ matrix.os }} strategy: fail-fast: false @@ -63,7 +63,9 @@ jobs: uses: actions/checkout@v2 - name: Create fake settings.xml run: | - $xmlPath = Join-Path $HOME ".m2" "settings.xml" + $xmlDirectory = Join-Path $HOME ".m2" + $xmlPath = Join-Path $xmlDirectory "settings.xml" + New-Item -Path $xmlDirectory -ItemType Directory Set-Content -Path $xmlPath -Value "Fake_XML" - name: setup-java uses: ./ @@ -86,7 +88,7 @@ jobs: } test-publishing-skip-overwrite: - name: Validate settings.xml is not overwritten if flag is false + name: settings.xml is not overwritten if flag is false runs-on: ${{ matrix.os }} strategy: fail-fast: false @@ -97,7 +99,9 @@ jobs: uses: actions/checkout@v2 - name: Create fake settings.xml run: | - $xmlPath = Join-Path $HOME ".m2" "settings.xml" + $xmlDirectory = Join-Path $HOME ".m2" + $xmlPath = Join-Path $xmlDirectory "settings.xml" + New-Item -Path $xmlDirectory -ItemType Directory Set-Content -Path $xmlPath -Value "Fake_XML" - name: setup-java uses: ./ @@ -119,7 +123,7 @@ jobs: } test-publishing-custom-location: - name: Validate settings.xml in custom location + name: settings.xml in custom location runs-on: ${{ matrix.os }} strategy: fail-fast: false From b091e7652f88fde0e3d0d583ff0856031ca72a72 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 12:38:40 +0300 Subject: [PATCH 3/6] print debug --- .github/workflows/e2e-publishing.yml | 4 +++- dist/setup/index.js | 2 +- src/auth.ts | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/e2e-publishing.yml b/.github/workflows/e2e-publishing.yml index 9904e7b60..478e26fc6 100644 --- a/.github/workflows/e2e-publishing.yml +++ b/.github/workflows/e2e-publishing.yml @@ -83,7 +83,7 @@ jobs: Get-Content $xmlPath | ForEach-Object { Write-Host $_ } $xml = Get-Content $xmlPath -Raw - if ($raw -notlike 'maven') { + if ($raw -notlike '*maven*') { throw "settings.xml file is not overwritten" } @@ -118,6 +118,8 @@ jobs: run: | $xmlPath = Join-Path $HOME ".m2" "settings.xml" $content = Get-Content -Path $xmlPath -Raw + Write-Host $content + if ($content -ne "Fake_XML") { throw "settings.xml file was overwritten but it should not be" } diff --git a/dist/setup/index.js b/dist/setup/index.js index fa6862961..f102f9003 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -13341,7 +13341,7 @@ function write(directory, settings, overwriteSettings) { core.info(`Writing to ${location}`); } else { - core.info(`Skipping generation '${location}' - it already exists and overwriting is not required`); + core.info(`Skipping generation ${location} because file already exists and overwriting is not required`); return; } return fs.writeFileSync(location, settings, { diff --git a/src/auth.ts b/src/auth.ts index 184f2f391..a52306b0d 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -114,7 +114,7 @@ async function write(directory: string, settings: string, overwriteSettings: boo core.info(`Writing to ${location}`); } else { core.info( - `Skipping generation '${location}' - it already exists and overwriting is not required` + `Skipping generation ${location} because file already exists and overwriting is not required` ); return; } From 242eb73434aa365db5501d3126bfc569be32ef08 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 12:45:33 +0300 Subject: [PATCH 4/6] fix e2e tests --- .github/workflows/e2e-publishing.yml | 6 +++--- README.md | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/e2e-publishing.yml b/.github/workflows/e2e-publishing.yml index 478e26fc6..90b02b95b 100644 --- a/.github/workflows/e2e-publishing.yml +++ b/.github/workflows/e2e-publishing.yml @@ -82,8 +82,8 @@ jobs: $xmlPath = Join-Path $HOME ".m2" "settings.xml" Get-Content $xmlPath | ForEach-Object { Write-Host $_ } - $xml = Get-Content $xmlPath -Raw - if ($raw -notlike '*maven*') { + $content = Get-Content $xmlPath -Raw + if ($content -notlike '*maven*') { throw "settings.xml file is not overwritten" } @@ -120,7 +120,7 @@ jobs: $content = Get-Content -Path $xmlPath -Raw Write-Host $content - if ($content -ne "Fake_XML") { + if ($content -notlike "*Fake_XML*") { throw "settings.xml file was overwritten but it should not be" } diff --git a/README.md b/README.md index b109577e7..84429bd1a 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ This action provides the following functionality for GitHub Actions runners: Inputs `java-version` and `distribution` are mandatory. See [Supported distributions](../README.md#Supported-distributions) section for a list of available options. ### Basic +**Adopt OpenJDK** ```yaml steps: - uses: actions/checkout@v2 @@ -30,6 +31,17 @@ steps: - run: java -cp java HelloWorldApp ``` +**Zulu OpenJDK** +```yaml +steps: +- uses: actions/checkout@v2 +- uses: actions/setup-java@v2-preview + with: + distribution: 'zulu' # See 'Supported distributions' for available options + java-version: '11' +- run: java -cp java HelloWorldApp +``` + #### Supported version syntax The `java-version` input supports an exact version or a version range using [SemVer](https://semver.org/) notation: - major versions: `8`, `11`, `15` From 514dae8ec507f55791d3d4c603f4b38fb3975547 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 12:53:04 +0300 Subject: [PATCH 5/6] add comment --- dist/setup/index.js | 1 + src/auth.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dist/setup/index.js b/dist/setup/index.js index f102f9003..7055fbc74 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -13272,6 +13272,7 @@ function configureAuthentication() { const username = core.getInput(constants.INPUT_SERVER_USERNAME); const password = core.getInput(constants.INPUT_SERVER_PASSWORD); const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), exports.M2_DIR); + // Consider setting overwriteSettings = false by default in next major release const overwriteSettings = util_1.getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; const gpgPassphrase = core.getInput(constants.INPUT_GPG_PASSPHRASE) || diff --git a/src/auth.ts b/src/auth.ts index a52306b0d..5bc65966b 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -19,6 +19,7 @@ export async function configureAuthentication() { const password = core.getInput(constants.INPUT_SERVER_PASSWORD); const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), M2_DIR); + // Consider setting overwriteSettings = false by default in next major release const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; From 4e1faadfc2c410ffc57cb1d0d9b1600552c0a62c Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Tue, 16 Mar 2021 18:04:19 +0300 Subject: [PATCH 6/6] remove comment --- dist/setup/index.js | 1 - src/auth.ts | 1 - 2 files changed, 2 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index 7055fbc74..f102f9003 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -13272,7 +13272,6 @@ function configureAuthentication() { const username = core.getInput(constants.INPUT_SERVER_USERNAME); const password = core.getInput(constants.INPUT_SERVER_PASSWORD); const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), exports.M2_DIR); - // Consider setting overwriteSettings = false by default in next major release const overwriteSettings = util_1.getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY; const gpgPassphrase = core.getInput(constants.INPUT_GPG_PASSPHRASE) || diff --git a/src/auth.ts b/src/auth.ts index 5bc65966b..a52306b0d 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -19,7 +19,6 @@ export async function configureAuthentication() { const password = core.getInput(constants.INPUT_SERVER_PASSWORD); const settingsDirectory = core.getInput(constants.INPUT_SETTINGS_PATH) || path.join(os.homedir(), M2_DIR); - // Consider setting overwriteSettings = false by default in next major release const overwriteSettings = getBooleanInput(constants.INPUT_OVERWRITE_SETTINGS, true); const gpgPrivateKey = core.getInput(constants.INPUT_GPG_PRIVATE_KEY) || constants.INPUT_DEFAULT_GPG_PRIVATE_KEY;