Skip to content

Commit a3e6ce2

Browse files
author
Bryan Clark
committed
Adding maven auth support
1 parent 081536e commit a3e6ce2

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

__tests__/auth.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import io = require('@actions/io');
2+
import fs = require('fs');
3+
import path = require('path');
4+
import child_process = require('child_process');
5+
6+
const m2Dir = path.join(__dirname, '.m2');
7+
const settingsFile = path.join(m2Dir, 'settings.xml');
8+
9+
import * as auth from '../src/auth';
10+
11+
describe('auth tests', () => {
12+
beforeAll(async () => {
13+
await io.rmRF(m2Dir);
14+
}, 300000);
15+
16+
afterAll(async () => {
17+
try {
18+
await io.rmRF(m2Dir);
19+
} catch {
20+
console.log('Failed to remove test directories');
21+
}
22+
}, 100000);
23+
24+
it('Creates settings.xml file with username and password', async () => {
25+
const username = 'bluebottle';
26+
const password = 'SingleOrigin';
27+
28+
await auth.configAuthentication(username, password);
29+
30+
expect(fs.existsSync(m2Dir)).toBe(true);
31+
expect(fs.existsSync(settingsFile)).toBe(true);
32+
expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
33+
auth.generate(username, password)
34+
);
35+
}, 100000);
36+
});

lib/auth.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importStar = (this && this.__importStar) || function (mod) {
11+
if (mod && mod.__esModule) return mod;
12+
var result = {};
13+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
14+
result["default"] = mod;
15+
return result;
16+
};
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
const fs = __importStar(require("fs"));
19+
const os = __importStar(require("os"));
20+
const path = __importStar(require("path"));
21+
const io = __importStar(require("@actions/io"));
22+
function configAuthentication(username, password) {
23+
return __awaiter(this, void 0, void 0, function* () {
24+
const directory = path.join(os.homedir(), '.m2');
25+
yield io.mkdirP(directory);
26+
yield write(directory, generate(username, password));
27+
});
28+
}
29+
exports.configAuthentication = configAuthentication;
30+
// only exported for testing purposes
31+
function generate(username = '${actions.username}', password = '${actions.password}') {
32+
return `<settings>
33+
<servers>
34+
<server>
35+
<username>${username}</username>
36+
<password>${password}</password>
37+
</server>
38+
</servers>
39+
</settings>
40+
`;
41+
}
42+
exports.generate = generate;
43+
function write(directory, settings) {
44+
return __awaiter(this, void 0, void 0, function* () {
45+
return fs.writeFileSync(path.join(directory, 'settings.xml'), settings);
46+
});
47+
}

lib/setup-java.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
return new (P || (P = Promise))(function (resolve, reject) {
4+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6+
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8+
});
9+
};
10+
var __importStar = (this && this.__importStar) || function (mod) {
11+
if (mod && mod.__esModule) return mod;
12+
var result = {};
13+
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
14+
result["default"] = mod;
15+
return result;
16+
};
17+
Object.defineProperty(exports, "__esModule", { value: true });
18+
const core = __importStar(require("@actions/core"));
19+
const installer = __importStar(require("./installer"));
20+
const auth = __importStar(require("./auth"));
21+
const path = __importStar(require("path"));
22+
function run() {
23+
return __awaiter(this, void 0, void 0, function* () {
24+
try {
25+
let version = core.getInput('version');
26+
if (!version) {
27+
version = core.getInput('java-version', { required: true });
28+
}
29+
const arch = core.getInput('architecture', { required: true });
30+
const jdkFile = core.getInput('jdkFile', { required: false }) || '';
31+
yield installer.getJava(version, arch, jdkFile);
32+
const username = core.getInput('username', { required: false });
33+
const password = core.getInput('password', { required: false });
34+
if (username && password) {
35+
yield auth.configAuthentication(username, password);
36+
}
37+
const matchersPath = path.join(__dirname, '..', '.github');
38+
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
39+
}
40+
catch (error) {
41+
core.setFailed(error.message);
42+
}
43+
});
44+
}
45+
run();

src/auth.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as fs from 'fs';
2+
import * as os from 'os';
3+
import * as path from 'path';
4+
import * as core from '@actions/core';
5+
import * as io from '@actions/io';
6+
7+
export async function configAuthentication(username: string, password: string) {
8+
const directory: string = path.join(os.homedir(), '.m2');
9+
await io.mkdirP(directory);
10+
await write(directory, generate(username, password));
11+
}
12+
13+
// only exported for testing purposes
14+
export function generate(
15+
username = '${actions.username}',
16+
password = '${actions.password}'
17+
) {
18+
return `<settings>
19+
<servers>
20+
<server>
21+
<username>${username}</username>
22+
<password>${password}</password>
23+
</server>
24+
</servers>
25+
</settings>
26+
`;
27+
}
28+
29+
async function write(directory: string, settings: string) {
30+
return fs.writeFileSync(path.join(directory, 'settings.xml'), settings);
31+
}

src/setup-java.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from '@actions/core';
22
import * as installer from './installer';
3+
import * as auth from './auth';
34
import * as path from 'path';
45

56
async function run() {
@@ -14,6 +15,13 @@ async function run() {
1415

1516
await installer.getJava(version, arch, jdkFile, javaPackage);
1617

18+
const username = core.getInput('username', {required: false});
19+
const password = core.getInput('password', {required: false});
20+
21+
if (username && password) {
22+
await auth.configAuthentication(username, password);
23+
}
24+
1725
const matchersPath = path.join(__dirname, '..', '.github');
1826
console.log(`##[add-matcher]${path.join(matchersPath, 'java.json')}`);
1927
} catch (error) {

0 commit comments

Comments
 (0)