Skip to content

Commit 7a112dc

Browse files
refactor: make actions/core mockable
1 parent a4663db commit 7a112dc

File tree

2 files changed

+66
-35
lines changed

2 files changed

+66
-35
lines changed

dist/main/index.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28008,19 +28008,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
2800828008
return (mod && mod.__esModule) ? mod : { "default": mod };
2800928009
};
2801028010
Object.defineProperty(exports, "__esModule", ({ value: true }));
28011+
exports.runAuth = runAuth;
2801128012
const core = __importStar(__nccwpck_require__(2186));
2801228013
const axios_1 = __importDefault(__nccwpck_require__(8757));
2801328014
const fs = __importStar(__nccwpck_require__(7147));
2801428015
const path = __importStar(__nccwpck_require__(1017));
2801528016
const os = __importStar(__nccwpck_require__(2037));
28016-
async function run() {
28017+
async function runAuth(coreAdapter = core) {
2801728018
try {
28018-
const clientId = core.getInput('client_id');
28019-
const keySecret = core.getInput('key_secret');
28020-
const baseUrl = core.getInput('base_url');
28021-
core.debug(`Client ID: ${clientId}`);
28022-
core.debug(`Key Secret: ${keySecret}`);
28023-
core.debug(`Base URL: ${baseUrl}`);
28019+
const clientId = coreAdapter.getInput('client_id');
28020+
const keySecret = coreAdapter.getInput('key_secret');
28021+
const baseUrl = coreAdapter.getInput('base_url');
28022+
coreAdapter.debug(`Client ID: ${clientId}`);
28023+
coreAdapter.debug(`Key Secret: ${keySecret}`);
28024+
coreAdapter.debug(`Base URL: ${baseUrl}`);
2802428025
// Authenticate and get the token
2802528026
try {
2802628027
const authResponse = await axios_1.default.post(`${baseUrl}/api/login`, `grant_type=client_credentials&client_id=${clientId}&client_secret=${keySecret}`, {
@@ -28030,7 +28031,7 @@ async function run() {
2803028031
maxRedirects: 5 // Follow redirects
2803128032
});
2803228033
const token = authResponse.data.access_token;
28033-
core.debug(`Token: ${token}`);
28034+
coreAdapter.debug(`Token: ${token}`);
2803428035
// Write token and other variables to a temporary file
2803528036
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
2803628037
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
@@ -28039,36 +28040,43 @@ async function run() {
2803928040
baseUrl,
2804028041
};
2804128042
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
28042-
core.debug(`Token file path: ${tokenFilePath}`);
28043+
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
2804328044
// Indicate successful login
28044-
core.info('Login was successful.');
28045+
coreAdapter.info('Login was successful.');
2804528046
// Output the token file path
28046-
core.setOutput('token_file', tokenFilePath);
28047+
coreAdapter.setOutput('token_file', tokenFilePath);
2804728048
// Read token from the file
2804828049
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
2804928050
const fileToken = fileTokenData.token;
2805028051
}
2805128052
catch (authError) {
2805228053
if (axios_1.default.isAxiosError(authError)) {
2805328054
if (authError.response) {
28054-
core.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
28055-
core.error(`Status code: ${authError.response.status}`);
28055+
coreAdapter.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
28056+
coreAdapter.error(`Status code: ${authError.response.status}`);
2805628057
}
2805728058
else {
28058-
core.error(`Authentication error message: ${authError.message}`);
28059+
coreAdapter.error(`Authentication error message: ${authError.message}`);
2805928060
}
2806028061
}
2806128062
else {
28062-
core.error(`Unexpected error: ${authError}`);
28063+
coreAdapter.error(`Unexpected error: ${authError}`);
2806328064
}
2806428065
throw authError;
2806528066
}
2806628067
}
2806728068
catch (error) {
28068-
core.setFailed(`Action failed with error: ${error}`);
28069+
coreAdapter.setFailed(`Action failed with error: ${error}`);
28070+
throw error;
2806928071
}
2807028072
}
28071-
run();
28073+
async function run() {
28074+
await runAuth(core);
28075+
}
28076+
// Only run if this file is executed directly (not imported)
28077+
if (require.main === require.cache[eval('__filename')]) {
28078+
run();
28079+
}
2807228080

2807328081

2807428082
/***/ }),

src/index.ts

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import * as core from '@actions/core';
2-
import * as github from '@actions/github';
32
import axios from 'axios';
43
import * as fs from 'fs';
54
import * as path from 'path';
65
import * as os from 'os';
76

8-
async function run() {
7+
export interface AuthInputs {
8+
client_id: string;
9+
key_secret: string;
10+
base_url: string;
11+
}
12+
13+
// allows stubbing @actions/core in tests
14+
export interface CoreAdapter {
15+
getInput: (name: string) => string;
16+
setOutput: (name: string, value: any) => void;
17+
setFailed: (message: string) => void;
18+
debug: (message: string) => void;
19+
info: (message: string) => void;
20+
error: (message: string) => void;
21+
}
22+
23+
export async function runAuth(coreAdapter: CoreAdapter = core) {
924
try {
10-
const clientId = core.getInput('client_id');
11-
const keySecret = core.getInput('key_secret');
12-
const baseUrl = core.getInput('base_url');
25+
const clientId = coreAdapter.getInput('client_id');
26+
const keySecret = coreAdapter.getInput('key_secret');
27+
const baseUrl = coreAdapter.getInput('base_url');
1328

14-
core.debug(`Client ID: ${clientId}`);
15-
core.debug(`Key Secret: ${keySecret}`);
16-
core.debug(`Base URL: ${baseUrl}`);
29+
coreAdapter.debug(`Client ID: ${clientId}`);
30+
coreAdapter.debug(`Key Secret: ${keySecret}`);
31+
coreAdapter.debug(`Base URL: ${baseUrl}`);
1732

1833
// Authenticate and get the token
1934
try {
@@ -29,7 +44,7 @@ async function run() {
2944
);
3045

3146
const token = authResponse.data.access_token;
32-
core.debug(`Token: ${token}`);
47+
coreAdapter.debug(`Token: ${token}`);
3348

3449
// Write token and other variables to a temporary file
3550
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
@@ -39,13 +54,13 @@ async function run() {
3954
baseUrl,
4055
};
4156
fs.writeFileSync(tokenFilePath, JSON.stringify(tokenData));
42-
core.debug(`Token file path: ${tokenFilePath}`);
57+
coreAdapter.debug(`Token file path: ${tokenFilePath}`);
4358

4459
// Indicate successful login
45-
core.info('Login was successful.');
60+
coreAdapter.info('Login was successful.');
4661

4762
// Output the token file path
48-
core.setOutput('token_file', tokenFilePath);
63+
coreAdapter.setOutput('token_file', tokenFilePath);
4964

5065
// Read token from the file
5166
const fileTokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
@@ -54,20 +69,28 @@ async function run() {
5469
} catch (authError) {
5570
if (axios.isAxiosError(authError)) {
5671
if (authError.response) {
57-
core.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
58-
core.error(`Status code: ${authError.response.status}`);
72+
coreAdapter.error(`Authentication error response: ${JSON.stringify(authError.response.data)}`);
73+
coreAdapter.error(`Status code: ${authError.response.status}`);
5974
} else {
60-
core.error(`Authentication error message: ${authError.message}`);
75+
coreAdapter.error(`Authentication error message: ${authError.message}`);
6176
}
6277
} else {
63-
core.error(`Unexpected error: ${authError}`);
78+
coreAdapter.error(`Unexpected error: ${authError}`);
6479
}
6580
throw authError;
6681
}
6782
} catch (error) {
68-
core.setFailed(`Action failed with error: ${error}`);
83+
coreAdapter.setFailed(`Action failed with error: ${error}`);
84+
throw error;
6985
}
7086
}
7187

72-
run();
88+
async function run() {
89+
await runAuth(core);
90+
}
91+
92+
// Only run if this file is executed directly (not imported)
93+
if (require.main === module) {
94+
run();
95+
}
7396

0 commit comments

Comments
 (0)