Skip to content

Commit 40a48a6

Browse files
refactor: break down run() function into smaller, focused functions
- Extract loadTokenFromFile() to handle token file reading and validation - Extract loadBuildingBlockRunFromBase64() to handle base64 decoding of payload input - Extract loadBuildingBlockRunFromUrl() to handle HTTP GET requests for URL input - Add TypeScript interfaces for TokenData and BuildingBlockRun for better type safety - Simplify run() to orchestrate the helper functions and register the source - Improves code readability and testability by separating concerns
1 parent c21e3e5 commit 40a48a6

File tree

2 files changed

+160
-151
lines changed

2 files changed

+160
-151
lines changed

dist/main/index.js

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -32286,74 +32286,80 @@ const axios_1 = __importDefault(__nccwpck_require__(8757));
3228632286
const fs = __importStar(__nccwpck_require__(7147));
3228732287
const path = __importStar(__nccwpck_require__(1017));
3228832288
const os = __importStar(__nccwpck_require__(2037));
32289+
function loadTokenFromFile() {
32290+
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
32291+
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
32292+
core.debug(`Using token file path: ${tokenFilePath}`);
32293+
if (!fs.existsSync(tokenFilePath)) {
32294+
throw new Error(`Token file does not exist at ${tokenFilePath}`);
32295+
}
32296+
const tokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
32297+
const token = tokenData.token;
32298+
if (!token) {
32299+
throw new Error('Token not found in token file');
32300+
}
32301+
core.debug(`Token: ${token}`);
32302+
return { token, tokenFilePath };
32303+
}
32304+
function loadBuildingBlockRunFromBase64(encodedRun) {
32305+
core.debug('Using buildingBlockRun from GitHub event payload');
32306+
if (!encodedRun) {
32307+
throw new Error('Neither buildingBlockRunUrl input nor buildingBlockRun payload provided');
32308+
}
32309+
const decodedBuildingBlockRun = Buffer.from(encodedRun, 'base64').toString('utf-8');
32310+
const buildingBlockRunJson = JSON.parse(decodedBuildingBlockRun);
32311+
core.debug(`Decoded Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
32312+
return buildingBlockRunJson;
32313+
}
32314+
async function loadBuildingBlockRunFromUrl(url, token) {
32315+
core.debug('Using buildingBlockRunUrl input');
32316+
const headers = {
32317+
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32318+
'Authorization': `Bearer ${token}`
32319+
};
32320+
try {
32321+
const response = await axios_1.default.get(url, { headers });
32322+
const buildingBlockRunJson = response.data;
32323+
core.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
32324+
return buildingBlockRunJson;
32325+
}
32326+
catch (fetchError) {
32327+
if (axios_1.default.isAxiosError(fetchError)) {
32328+
if (fetchError.response) {
32329+
core.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
32330+
core.error(`Status code: ${fetchError.response.status}`);
32331+
}
32332+
else {
32333+
core.error(`Fetch error message: ${fetchError.message}`);
32334+
}
32335+
}
32336+
else {
32337+
core.error(`Unexpected error during fetch: ${fetchError}`);
32338+
}
32339+
throw fetchError;
32340+
}
32341+
}
3228932342
async function run() {
3229032343
try {
3229132344
const stepsInput = core.getInput('steps');
3229232345
const buildingBlockRunUrl = core.getInput('buildingBlockRunUrl');
3229332346
core.debug(`Steps Input: ${stepsInput}`);
3229432347
core.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
32348+
// Load token
32349+
const { token, tokenFilePath } = loadTokenFromFile();
32350+
// Load building block run
3229532351
let buildingBlockRunJson;
32296-
let bbRunUuid;
32297-
let baseUrl;
32298-
let inputs;
32299-
// Determine input source: URL or payload
3230032352
if (buildingBlockRunUrl) {
32301-
// Fetch building block run from URL
32302-
core.debug('Using buildingBlockRunUrl input');
32303-
// Read token from file for authorization
32304-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
32305-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
32306-
if (!fs.existsSync(tokenFilePath)) {
32307-
throw new Error(`Token file does not exist at ${tokenFilePath}`);
32308-
}
32309-
const tokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
32310-
const token = tokenData.token;
32311-
if (!token) {
32312-
throw new Error('Token not found in token file');
32313-
}
32314-
core.debug(`Token: ${token}`);
32315-
// Fetch the building block run from the URL
32316-
const headers = {
32317-
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32318-
'Authorization': `Bearer ${token}`
32319-
};
32320-
try {
32321-
const response = await axios_1.default.get(buildingBlockRunUrl, { headers });
32322-
buildingBlockRunJson = response.data;
32323-
core.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
32324-
}
32325-
catch (fetchError) {
32326-
if (axios_1.default.isAxiosError(fetchError)) {
32327-
if (fetchError.response) {
32328-
core.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
32329-
core.error(`Status code: ${fetchError.response.status}`);
32330-
}
32331-
else {
32332-
core.error(`Fetch error message: ${fetchError.message}`);
32333-
}
32334-
}
32335-
else {
32336-
core.error(`Unexpected error during fetch: ${fetchError}`);
32337-
}
32338-
throw fetchError;
32339-
}
32353+
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token);
3234032354
}
3234132355
else {
32342-
// Use buildingBlockRun from GitHub event payload
32343-
core.debug('Using buildingBlockRun from GitHub event payload');
3234432356
const buildingBlockRun = github.context.payload.inputs.buildingBlockRun;
32345-
core.debug(`Building Block Run: ${buildingBlockRun}`);
32346-
if (!buildingBlockRun) {
32347-
throw new Error('Neither buildingBlockRunUrl input nor buildingBlockRun payload provided');
32348-
}
32349-
// Decode and parse the buildingBlockRun input
32350-
const decodedBuildingBlockRun = Buffer.from(buildingBlockRun, 'base64').toString('utf-8');
32351-
buildingBlockRunJson = JSON.parse(decodedBuildingBlockRun);
32357+
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun);
3235232358
}
3235332359
// Extract common data from buildingBlockRunJson
32354-
bbRunUuid = buildingBlockRunJson.metadata.uuid;
32355-
baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
32356-
inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
32360+
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
32361+
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
32362+
const inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
3235732363
core.debug(`Base URL: ${baseUrl}`);
3235832364
core.debug(`BB Run UUID: ${bbRunUuid}`);
3235932365
// Extract additional inputs
@@ -32372,20 +32378,6 @@ async function run() {
3237232378
// Parse the JSON steps input
3237332379
const steps = JSON.parse(stepsInput);
3237432380
core.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
32375-
// Use the well-known token file location
32376-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
32377-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
32378-
core.debug(`Using token file path: ${tokenFilePath}`);
32379-
// Read token from file
32380-
if (!fs.existsSync(tokenFilePath)) {
32381-
throw new Error(`Token file does not exist at ${tokenFilePath}`);
32382-
}
32383-
const tokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
32384-
const token = tokenData.token;
32385-
if (!token) {
32386-
throw new Error('Token not found in token file');
32387-
}
32388-
core.debug(`Token: ${token}`);
3238932381
// Prepare the request payload and headers
3239032382
const requestPayload = {
3239132383
source: {

src/index.ts

Lines changed: 99 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -5,83 +5,120 @@ import * as fs from 'fs';
55
import * as path from 'path';
66
import * as os from 'os';
77

8-
async function run() {
9-
try {
10-
const stepsInput = core.getInput('steps');
11-
const buildingBlockRunUrl = core.getInput('buildingBlockRunUrl');
8+
interface TokenData {
9+
token: string;
10+
}
1211

13-
core.debug(`Steps Input: ${stepsInput}`);
14-
core.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
12+
interface BuildingBlockRun {
13+
metadata: {
14+
uuid: string;
15+
};
16+
spec: {
17+
buildingBlock: {
18+
spec: {
19+
inputs: any[];
20+
};
21+
};
22+
};
23+
_links: {
24+
meshstackBaseUrl: {
25+
href: string;
26+
};
27+
};
28+
}
1529

16-
let buildingBlockRunJson: any;
17-
let bbRunUuid: string;
18-
let baseUrl: string;
19-
let inputs: any[];
30+
function loadTokenFromFile(): { token: string; tokenFilePath: string } {
31+
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
32+
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
2033

21-
// Determine input source: URL or payload
22-
if (buildingBlockRunUrl) {
23-
// Fetch building block run from URL
24-
core.debug('Using buildingBlockRunUrl input');
25-
26-
// Read token from file for authorization
27-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
28-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
29-
30-
if (!fs.existsSync(tokenFilePath)) {
31-
throw new Error(`Token file does not exist at ${tokenFilePath}`);
32-
}
34+
core.debug(`Using token file path: ${tokenFilePath}`);
35+
36+
if (!fs.existsSync(tokenFilePath)) {
37+
throw new Error(`Token file does not exist at ${tokenFilePath}`);
38+
}
3339

34-
const tokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
35-
const token = tokenData.token;
40+
const tokenData: TokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
41+
const token = tokenData.token;
3642

37-
if (!token) {
38-
throw new Error('Token not found in token file');
39-
}
43+
if (!token) {
44+
throw new Error('Token not found in token file');
45+
}
4046

41-
core.debug(`Token: ${token}`);
47+
core.debug(`Token: ${token}`);
4248

43-
// Fetch the building block run from the URL
44-
const headers = {
45-
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
46-
'Authorization': `Bearer ${token}`
47-
};
49+
return { token, tokenFilePath };
50+
}
4851

49-
try {
50-
const response = await axios.get(buildingBlockRunUrl, { headers });
51-
buildingBlockRunJson = response.data;
52-
core.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
53-
} catch (fetchError) {
54-
if (axios.isAxiosError(fetchError)) {
55-
if (fetchError.response) {
56-
core.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
57-
core.error(`Status code: ${fetchError.response.status}`);
58-
} else {
59-
core.error(`Fetch error message: ${fetchError.message}`);
60-
}
61-
} else {
62-
core.error(`Unexpected error during fetch: ${fetchError}`);
63-
}
64-
throw fetchError;
52+
function loadBuildingBlockRunFromBase64(encodedRun: string): BuildingBlockRun {
53+
core.debug('Using buildingBlockRun from GitHub event payload');
54+
55+
if (!encodedRun) {
56+
throw new Error('Neither buildingBlockRunUrl input nor buildingBlockRun payload provided');
57+
}
58+
59+
const decodedBuildingBlockRun = Buffer.from(encodedRun, 'base64').toString('utf-8');
60+
const buildingBlockRunJson = JSON.parse(decodedBuildingBlockRun);
61+
62+
core.debug(`Decoded Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
63+
64+
return buildingBlockRunJson;
65+
}
66+
67+
async function loadBuildingBlockRunFromUrl(
68+
url: string,
69+
token: string
70+
): Promise<BuildingBlockRun> {
71+
core.debug('Using buildingBlockRunUrl input');
72+
73+
const headers = {
74+
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
75+
'Authorization': `Bearer ${token}`
76+
};
77+
78+
try {
79+
const response = await axios.get(url, { headers });
80+
const buildingBlockRunJson = response.data;
81+
core.debug(`Fetched Building Block Run: ${JSON.stringify(buildingBlockRunJson)}`);
82+
return buildingBlockRunJson;
83+
} catch (fetchError) {
84+
if (axios.isAxiosError(fetchError)) {
85+
if (fetchError.response) {
86+
core.error(`Failed to fetch building block run: ${JSON.stringify(fetchError.response.data)}`);
87+
core.error(`Status code: ${fetchError.response.status}`);
88+
} else {
89+
core.error(`Fetch error message: ${fetchError.message}`);
6590
}
6691
} else {
67-
// Use buildingBlockRun from GitHub event payload
68-
core.debug('Using buildingBlockRun from GitHub event payload');
69-
const buildingBlockRun = github.context.payload.inputs.buildingBlockRun;
70-
core.debug(`Building Block Run: ${buildingBlockRun}`);
92+
core.error(`Unexpected error during fetch: ${fetchError}`);
93+
}
94+
throw fetchError;
95+
}
96+
}
7197

72-
if (!buildingBlockRun) {
73-
throw new Error('Neither buildingBlockRunUrl input nor buildingBlockRun payload provided');
74-
}
98+
async function run() {
99+
try {
100+
const stepsInput = core.getInput('steps');
101+
const buildingBlockRunUrl = core.getInput('buildingBlockRunUrl');
102+
103+
core.debug(`Steps Input: ${stepsInput}`);
104+
core.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
75105

76-
// Decode and parse the buildingBlockRun input
77-
const decodedBuildingBlockRun = Buffer.from(buildingBlockRun, 'base64').toString('utf-8');
78-
buildingBlockRunJson = JSON.parse(decodedBuildingBlockRun);
106+
// Load token
107+
const { token, tokenFilePath } = loadTokenFromFile();
108+
109+
// Load building block run
110+
let buildingBlockRunJson: BuildingBlockRun;
111+
if (buildingBlockRunUrl) {
112+
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token);
113+
} else {
114+
const buildingBlockRun = github.context.payload.inputs.buildingBlockRun;
115+
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun);
79116
}
80117

81118
// Extract common data from buildingBlockRunJson
82-
bbRunUuid = buildingBlockRunJson.metadata.uuid;
83-
baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
84-
inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
119+
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
120+
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
121+
const inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
85122

86123
core.debug(`Base URL: ${baseUrl}`);
87124
core.debug(`BB Run UUID: ${bbRunUuid}`);
@@ -106,26 +143,6 @@ async function run() {
106143
const steps = JSON.parse(stepsInput);
107144
core.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
108145

109-
// Use the well-known token file location
110-
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
111-
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
112-
113-
core.debug(`Using token file path: ${tokenFilePath}`);
114-
115-
// Read token from file
116-
if (!fs.existsSync(tokenFilePath)) {
117-
throw new Error(`Token file does not exist at ${tokenFilePath}`);
118-
}
119-
120-
const tokenData = JSON.parse(fs.readFileSync(tokenFilePath, 'utf8'));
121-
const token = tokenData.token;
122-
123-
if (!token) {
124-
throw new Error('Token not found in token file');
125-
}
126-
127-
core.debug(`Token: ${token}`);
128-
129146
// Prepare the request payload and headers
130147
const requestPayload = {
131148
source: {

0 commit comments

Comments
 (0)