Adding maven auth support

This commit is contained in:
Bryan Clark 2019-11-15 16:01:13 -08:00
parent 081536e071
commit a3e6ce2153
5 changed files with 167 additions and 0 deletions

36
__tests__/auth.test.ts Normal file
View File

@ -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);
});

47
lib/auth.js Normal file
View File

@ -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 `<settings>
<servers>
<server>
<username>${username}</username>
<password>${password}</password>
</server>
</servers>
</settings>
`;
}
exports.generate = generate;
function write(directory, settings) {
return __awaiter(this, void 0, void 0, function* () {
return fs.writeFileSync(path.join(directory, 'settings.xml'), settings);
});
}

45
lib/setup-java.js Normal file
View File

@ -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();

31
src/auth.ts Normal file
View File

@ -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 `<settings>
<servers>
<server>
<username>${username}</username>
<password>${password}</password>
</server>
</servers>
</settings>
`;
}
async function write(directory: string, settings: string) {
return fs.writeFileSync(path.join(directory, 'settings.xml'), settings);
}

View File

@ -1,5 +1,6 @@
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as installer from './installer'; import * as installer from './installer';
import * as auth from './auth';
import * as path from 'path'; import * as path from 'path';
async function run() { async function run() {
@ -14,6 +15,13 @@ async function run() {
await installer.getJava(version, arch, jdkFile, javaPackage); 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'); const matchersPath = path.join(__dirname, '..', '.github');
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`); console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
} catch (error) { } catch (error) {