Skip to content

Commit 055da60

Browse files
refactor: extract input extraction and register source logic into functions
- Extract extractInputs() to handle input parsing and GitHub output setting - Extract registerSource() to handle the POST request to meshStack API - Extract buildRequestPayload() to construct the request payload - Extract buildRequestHeaders() to construct the request headers - Add TypeScript interfaces for better type safety: - BuildingBlockInput: represents individual building block inputs - RequestSource: represents the source section of the request payload - RequestPayload: represents the complete request payload - RequestHeaders: represents all HTTP headers with proper index signature - ExtractedInputs: type-safe dictionary for extracted inputs - Simplify run() to orchestrate the helper functions - All tests pass
1 parent 40a48a6 commit 055da60

File tree

2 files changed

+179
-110
lines changed

2 files changed

+179
-110
lines changed

dist/main/index.js

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32339,6 +32339,66 @@ async function loadBuildingBlockRunFromUrl(url, token) {
3233932339
throw fetchError;
3234032340
}
3234132341
}
32342+
function extractInputs(buildingBlockRun) {
32343+
core.debug('Extracting inputs from building block run');
32344+
const inputs = buildingBlockRun.spec.buildingBlock.spec.inputs;
32345+
const extractedInputs = {};
32346+
inputs.forEach((input) => {
32347+
const value = inputs.find((i) => i.key === input.key)?.value;
32348+
if (value) {
32349+
extractedInputs[input.key] = value;
32350+
}
32351+
});
32352+
core.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
32353+
// Write each extracted input to GITHUB_OUTPUT
32354+
for (const [key, value] of Object.entries(extractedInputs)) {
32355+
core.setOutput(key, value);
32356+
}
32357+
return extractedInputs;
32358+
}
32359+
function buildRequestHeaders(token) {
32360+
return {
32361+
'Content-Type': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32362+
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32363+
'Authorization': `Bearer ${token}`
32364+
};
32365+
}
32366+
function buildRequestPayload(steps) {
32367+
return {
32368+
source: {
32369+
id: 'github',
32370+
externalRunId: github.context.runId,
32371+
externalRunUrl: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
32372+
},
32373+
steps: steps
32374+
};
32375+
}
32376+
async function registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath) {
32377+
core.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
32378+
core.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
32379+
try {
32380+
const response = await axios_1.default.post(`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`, requestPayload, {
32381+
headers: requestHeaders
32382+
});
32383+
core.setOutput('response', response.data);
32384+
core.setOutput('token_file', tokenFilePath);
32385+
}
32386+
catch (registerError) {
32387+
if (axios_1.default.isAxiosError(registerError)) {
32388+
if (registerError.response) {
32389+
core.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
32390+
core.error(`Status code: ${registerError.response.status}`);
32391+
}
32392+
else {
32393+
core.error(`Register source error message: ${registerError.message}`);
32394+
}
32395+
}
32396+
else {
32397+
core.error(`Unexpected error: ${registerError}`);
32398+
}
32399+
throw registerError;
32400+
}
32401+
}
3234232402
async function run() {
3234332403
try {
3234432404
const stepsInput = core.getInput('steps');
@@ -32359,65 +32419,18 @@ async function run() {
3235932419
// Extract common data from buildingBlockRunJson
3236032420
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
3236132421
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
32362-
const inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
3236332422
core.debug(`Base URL: ${baseUrl}`);
3236432423
core.debug(`BB Run UUID: ${bbRunUuid}`);
32365-
// Extract additional inputs
32366-
const extractedInputs = {};
32367-
inputs.forEach((input) => {
32368-
const value = buildingBlockRunJson.spec.buildingBlock.spec.inputs.find((i) => i.key === input.key)?.value;
32369-
if (value) {
32370-
extractedInputs[input.key] = value;
32371-
}
32372-
});
32373-
core.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
32374-
// Write each extracted input to GITHUB_OUTPUT
32375-
for (const [key, value] of Object.entries(extractedInputs)) {
32376-
core.setOutput(key, value);
32377-
}
32424+
// Extract inputs and write to outputs
32425+
extractInputs(buildingBlockRunJson);
3237832426
// Parse the JSON steps input
3237932427
const steps = JSON.parse(stepsInput);
3238032428
core.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
3238132429
// Prepare the request payload and headers
32382-
const requestPayload = {
32383-
source: {
32384-
id: 'github',
32385-
externalRunId: github.context.runId,
32386-
externalRunUrl: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
32387-
},
32388-
steps: steps
32389-
};
32390-
const requestHeaders = {
32391-
'Content-Type': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32392-
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
32393-
'Authorization': `Bearer ${token}`
32394-
};
32395-
// Log the request payload and headers
32396-
core.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
32397-
core.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
32430+
const requestPayload = buildRequestPayload(steps);
32431+
const requestHeaders = buildRequestHeaders(token);
3239832432
// Register the source
32399-
try {
32400-
const response = await axios_1.default.post(`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`, requestPayload, {
32401-
headers: requestHeaders
32402-
});
32403-
core.setOutput('response', response.data);
32404-
core.setOutput('token_file', tokenFilePath);
32405-
}
32406-
catch (registerError) {
32407-
if (axios_1.default.isAxiosError(registerError)) {
32408-
if (registerError.response) {
32409-
core.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
32410-
core.error(`Status code: ${registerError.response.status}`);
32411-
}
32412-
else {
32413-
core.error(`Register source error message: ${registerError.message}`);
32414-
}
32415-
}
32416-
else {
32417-
core.error(`Unexpected error: ${registerError}`);
32418-
}
32419-
throw registerError;
32420-
}
32433+
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath);
3242132434
}
3242232435
catch (error) {
3242332436
if (error instanceof Error) {

src/index.ts

Lines changed: 114 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ interface TokenData {
99
token: string;
1010
}
1111

12+
interface BuildingBlockInput {
13+
key: string;
14+
value: string;
15+
type: string;
16+
isSensitive: boolean;
17+
isEnvironment: boolean;
18+
}
19+
1220
interface BuildingBlockRun {
1321
metadata: {
1422
uuid: string;
1523
};
1624
spec: {
1725
buildingBlock: {
1826
spec: {
19-
inputs: any[];
27+
inputs: BuildingBlockInput[];
2028
};
2129
};
2230
};
@@ -27,6 +35,28 @@ interface BuildingBlockRun {
2735
};
2836
}
2937

38+
interface RequestSource {
39+
id: string;
40+
externalRunId: number;
41+
externalRunUrl: string;
42+
}
43+
44+
interface RequestPayload {
45+
source: RequestSource;
46+
steps: any[];
47+
}
48+
49+
interface RequestHeaders {
50+
[key: string]: string;
51+
'Content-Type': string;
52+
'Accept': string;
53+
'Authorization': string;
54+
}
55+
56+
interface ExtractedInputs {
57+
[key: string]: string;
58+
}
59+
3060
function loadTokenFromFile(): { token: string; tokenFilePath: string } {
3161
const tempDir = process.env.RUNNER_TEMP || os.tmpdir();
3262
const tokenFilePath = path.join(tempDir, 'meshstack_token.json');
@@ -95,6 +125,84 @@ async function loadBuildingBlockRunFromUrl(
95125
}
96126
}
97127

128+
function extractInputs(buildingBlockRun: BuildingBlockRun): ExtractedInputs {
129+
core.debug('Extracting inputs from building block run');
130+
131+
const inputs = buildingBlockRun.spec.buildingBlock.spec.inputs;
132+
const extractedInputs: ExtractedInputs = {};
133+
134+
inputs.forEach((input: BuildingBlockInput) => {
135+
const value = inputs.find((i: BuildingBlockInput) => i.key === input.key)?.value;
136+
if (value) {
137+
extractedInputs[input.key] = value;
138+
}
139+
});
140+
141+
core.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
142+
143+
// Write each extracted input to GITHUB_OUTPUT
144+
for (const [key, value] of Object.entries(extractedInputs)) {
145+
core.setOutput(key, value);
146+
}
147+
148+
return extractedInputs;
149+
}
150+
151+
function buildRequestHeaders(token: string): RequestHeaders {
152+
return {
153+
'Content-Type': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
154+
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
155+
'Authorization': `Bearer ${token}`
156+
};
157+
}
158+
159+
function buildRequestPayload(steps: any[]): RequestPayload {
160+
return {
161+
source: {
162+
id: 'github',
163+
externalRunId: github.context.runId,
164+
externalRunUrl: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
165+
},
166+
steps: steps
167+
};
168+
}
169+
170+
async function registerSource(
171+
baseUrl: string,
172+
bbRunUuid: string,
173+
requestPayload: RequestPayload,
174+
requestHeaders: RequestHeaders,
175+
tokenFilePath: string
176+
): Promise<void> {
177+
core.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
178+
core.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
179+
180+
try {
181+
const response = await axios.post(
182+
`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`,
183+
requestPayload,
184+
{
185+
headers: requestHeaders
186+
}
187+
);
188+
189+
core.setOutput('response', response.data);
190+
core.setOutput('token_file', tokenFilePath);
191+
} catch (registerError) {
192+
if (axios.isAxiosError(registerError)) {
193+
if (registerError.response) {
194+
core.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
195+
core.error(`Status code: ${registerError.response.status}`);
196+
} else {
197+
core.error(`Register source error message: ${registerError.message}`);
198+
}
199+
} else {
200+
core.error(`Unexpected error: ${registerError}`);
201+
}
202+
throw registerError;
203+
}
204+
}
205+
98206
async function run() {
99207
try {
100208
const stepsInput = core.getInput('steps');
@@ -118,75 +226,23 @@ async function run() {
118226
// Extract common data from buildingBlockRunJson
119227
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
120228
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
121-
const inputs = buildingBlockRunJson.spec.buildingBlock.spec.inputs;
122229

123230
core.debug(`Base URL: ${baseUrl}`);
124231
core.debug(`BB Run UUID: ${bbRunUuid}`);
125232

126-
// Extract additional inputs
127-
const extractedInputs: { [key: string]: string } = {};
128-
inputs.forEach((input: { key: string }) => {
129-
const value = buildingBlockRunJson.spec.buildingBlock.spec.inputs.find((i: { key: string }) => i.key === input.key)?.value;
130-
if (value) {
131-
extractedInputs[input.key] = value;
132-
}
133-
});
134-
135-
core.debug(`Extracted Inputs: ${JSON.stringify(extractedInputs)}`);
136-
137-
// Write each extracted input to GITHUB_OUTPUT
138-
for (const [key, value] of Object.entries(extractedInputs)) {
139-
core.setOutput(key, value);
140-
}
233+
// Extract inputs and write to outputs
234+
extractInputs(buildingBlockRunJson);
141235

142236
// Parse the JSON steps input
143237
const steps = JSON.parse(stepsInput);
144238
core.debug(`Parsed Steps: ${JSON.stringify(steps)}`);
145239

146240
// Prepare the request payload and headers
147-
const requestPayload = {
148-
source: {
149-
id: 'github',
150-
externalRunId: github.context.runId,
151-
externalRunUrl: `https://github.com/${github.context.repo.owner}/${github.context.repo.repo}/actions/runs/${github.context.runId}`
152-
},
153-
steps: steps
154-
};
155-
const requestHeaders = {
156-
'Content-Type': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
157-
'Accept': 'application/vnd.meshcloud.api.meshbuildingblockrun.v1.hal+json',
158-
'Authorization': `Bearer ${token}`
159-
};
160-
161-
// Log the request payload and headers
162-
core.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
163-
core.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
241+
const requestPayload = buildRequestPayload(steps);
242+
const requestHeaders = buildRequestHeaders(token);
164243

165244
// Register the source
166-
try {
167-
const response = await axios.post(
168-
`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`,
169-
requestPayload,
170-
{
171-
headers: requestHeaders
172-
}
173-
);
174-
175-
core.setOutput('response', response.data);
176-
core.setOutput('token_file', tokenFilePath);
177-
} catch (registerError) {
178-
if (axios.isAxiosError(registerError)) {
179-
if (registerError.response) {
180-
core.error(`Register source error response: ${JSON.stringify(registerError.response.data)}`);
181-
core.error(`Status code: ${registerError.response.status}`);
182-
} else {
183-
core.error(`Register source error message: ${registerError.message}`);
184-
}
185-
} else {
186-
core.error(`Unexpected error: ${registerError}`);
187-
}
188-
throw registerError;
189-
}
245+
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath);
190246
} catch (error) {
191247
if (error instanceof Error) {
192248
core.setFailed(error.message);

0 commit comments

Comments
 (0)