From a3e6ce2153fa695e13226f8273de18260700ff4e Mon Sep 17 00:00:00 2001 From: Bryan Clark Date: Fri, 15 Nov 2019 16:01:13 -0800 Subject: [PATCH] Adding maven auth support --- __tests__/auth.test.ts | 36 ++++++++++++++++++++++++++++++++ lib/auth.js | 47 ++++++++++++++++++++++++++++++++++++++++++ lib/setup-java.js | 45 ++++++++++++++++++++++++++++++++++++++++ src/auth.ts | 31 ++++++++++++++++++++++++++++ src/setup-java.ts | 8 +++++++ 5 files changed, 167 insertions(+) create mode 100644 __tests__/auth.test.ts create mode 100644 lib/auth.js create mode 100644 lib/setup-java.js create mode 100644 src/auth.ts diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts new file mode 100644 index 0000000..937d119 --- /dev/null +++ b/__tests__/auth.test.ts @@ -0,0 +1,36 @@ +import io = require('@actions/io'); +import fs = require('fs'); +import path = require('path'); +import child_process = require('child_process'); + +const m2Dir = path.join(__dirname, '.m2'); +const settingsFile = path.join(m2Dir, 'settings.xml'); + +import * as auth from '../src/auth'; + +describe('auth tests', () => { + beforeAll(async () => { + await io.rmRF(m2Dir); + }, 300000); + + afterAll(async () => { + try { + await io.rmRF(m2Dir); + } catch { + console.log('Failed to remove test directories'); + } + }, 100000); + + it('Creates settings.xml file with username and password', async () => { + const username = 'bluebottle'; + const password = 'SingleOrigin'; + + await auth.configAuthentication(username, password); + + expect(fs.existsSync(m2Dir)).toBe(true); + expect(fs.existsSync(settingsFile)).toBe(true); + expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual( + auth.generate(username, password) + ); + }, 100000); +}); diff --git a/lib/auth.js b/lib/auth.js new file mode 100644 index 0000000..33e643b --- /dev/null +++ b/lib/auth.js @@ -0,0 +1,47 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const fs = __importStar(require("fs")); +const os = __importStar(require("os")); +const path = __importStar(require("path")); +const io = __importStar(require("@actions/io")); +function configAuthentication(username, password) { + return __awaiter(this, void 0, void 0, function* () { + const directory = path.join(os.homedir(), '.m2'); + yield io.mkdirP(directory); + yield write(directory, generate(username, password)); + }); +} +exports.configAuthentication = configAuthentication; +// only exported for testing purposes +function generate(username = '${actions.username}', password = '${actions.password}') { + return ` + + + ${username} + ${password} + + + + `; +} +exports.generate = generate; +function write(directory, settings) { + return __awaiter(this, void 0, void 0, function* () { + return fs.writeFileSync(path.join(directory, 'settings.xml'), settings); + }); +} diff --git a/lib/setup-java.js b/lib/setup-java.js new file mode 100644 index 0000000..1028cf8 --- /dev/null +++ b/lib/setup-java.js @@ -0,0 +1,45 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; + result["default"] = mod; + return result; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const installer = __importStar(require("./installer")); +const auth = __importStar(require("./auth")); +const path = __importStar(require("path")); +function run() { + return __awaiter(this, void 0, void 0, function* () { + try { + let version = core.getInput('version'); + if (!version) { + version = core.getInput('java-version', { required: true }); + } + const arch = core.getInput('architecture', { required: true }); + const jdkFile = core.getInput('jdkFile', { required: false }) || ''; + yield installer.getJava(version, arch, jdkFile); + const username = core.getInput('username', { required: false }); + const password = core.getInput('password', { required: false }); + if (username && password) { + yield auth.configAuthentication(username, password); + } + const matchersPath = path.join(__dirname, '..', '.github'); + console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); + } + catch (error) { + core.setFailed(error.message); + } + }); +} +run(); diff --git a/src/auth.ts b/src/auth.ts new file mode 100644 index 0000000..328c54f --- /dev/null +++ b/src/auth.ts @@ -0,0 +1,31 @@ +import * as fs from 'fs'; +import * as os from 'os'; +import * as path from 'path'; +import * as core from '@actions/core'; +import * as io from '@actions/io'; + +export async function configAuthentication(username: string, password: string) { + const directory: string = path.join(os.homedir(), '.m2'); + await io.mkdirP(directory); + await write(directory, generate(username, password)); +} + +// only exported for testing purposes +export function generate( + username = '${actions.username}', + password = '${actions.password}' +) { + return ` + + + ${username} + ${password} + + + + `; +} + +async function write(directory: string, settings: string) { + return fs.writeFileSync(path.join(directory, 'settings.xml'), settings); +} diff --git a/src/setup-java.ts b/src/setup-java.ts index 1d26bff..e6b179e 100644 --- a/src/setup-java.ts +++ b/src/setup-java.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core'; import * as installer from './installer'; +import * as auth from './auth'; import * as path from 'path'; async function run() { @@ -14,6 +15,13 @@ async function run() { await installer.getJava(version, arch, jdkFile, javaPackage); + const username = core.getInput('username', {required: false}); + const password = core.getInput('password', {required: false}); + + if (username && password) { + await auth.configAuthentication(username, password); + } + const matchersPath = path.join(__dirname, '..', '.github'); console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); } catch (error) {