diff --git a/__tests__/auth.test.ts b/__tests__/auth.test.ts new file mode 100644 index 00000000..937d119d --- /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 00000000..33e643be --- /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 00000000..1028cf84 --- /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 00000000..328c54f7 --- /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 1d26bffe..e6b179ed 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) {