Skip to content

Commit c1607b2

Browse files
Refactor GitHub context handling in registerBuildingBlockSource
1 parent 7f62ef8 commit c1607b2

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

action.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ inputs:
55
description: 'The steps to register'
66
required: true
77
type: string
8-
buildingBlockRunUrl:
9-
description: 'URL to fetch the building block run object from (alternative to buildingBlockRun input via payload)'
10-
required: false
11-
type: string
128
outputs:
139
token_file:
1410
description: 'Path to the file containing the authentication token'

dist/main/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32374,11 +32374,11 @@ function buildRequestPayload(steps, githubContext) {
3237432374
steps: steps
3237532375
};
3237632376
}
32377-
async function registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath, coreAdapter) {
32377+
async function registerSource(buildingBlockRunUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter) {
3237832378
coreAdapter.debug(`Request Payload: ${JSON.stringify(requestPayload)}`);
3237932379
coreAdapter.debug(`Request Headers: ${JSON.stringify(requestHeaders)}`);
3238032380
try {
32381-
const response = await axios_1.default.post(`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`, requestPayload, {
32381+
const response = await axios_1.default.post(`${buildingBlockRunUrl}/status/source`, requestPayload, {
3238232382
headers: requestHeaders
3238332383
});
3238432384
coreAdapter.setOutput('response', response.data);
@@ -32403,25 +32403,26 @@ async function registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders
3240332403
async function runRegisterSource(coreAdapter = core, githubContext = github) {
3240432404
try {
3240532405
const stepsInput = coreAdapter.getInput('steps');
32406-
const buildingBlockRunUrl = coreAdapter.getInput('buildingBlockRunUrl');
32406+
// Extract buildingBlockRunUrl and buildingBlockRun from GitHub event payload
32407+
const buildingBlockRunUrl = githubContext.context.payload.inputs?.buildingBlockRunUrl;
32408+
const buildingBlockRun = githubContext.context.payload.inputs?.buildingBlockRun;
3240732409
coreAdapter.debug(`Steps Input: ${stepsInput}`);
3240832410
coreAdapter.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
32411+
coreAdapter.debug(`Building Block Run: ${buildingBlockRun}`);
3240932412
// Load token
3241032413
const { token, tokenFilePath } = loadTokenFromFile(coreAdapter);
32411-
// Load building block run
32414+
// Load building block run and determine the run URL
3241232415
let buildingBlockRunJson;
32416+
let runUrl;
3241332417
if (buildingBlockRunUrl) {
3241432418
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token, coreAdapter);
32419+
runUrl = buildingBlockRunUrl;
3241532420
}
3241632421
else {
32417-
const buildingBlockRun = githubContext.context.payload.inputs.buildingBlockRun;
3241832422
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun, coreAdapter);
32423+
runUrl = buildingBlockRunJson._links.self.href;
3241932424
}
32420-
// Extract common data from buildingBlockRunJson
32421-
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
32422-
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
32423-
coreAdapter.debug(`Base URL: ${baseUrl}`);
32424-
coreAdapter.debug(`BB Run UUID: ${bbRunUuid}`);
32425+
coreAdapter.debug(`Building Block Run URL: ${runUrl}`);
3242532426
// Extract inputs and write to outputs
3242632427
extractInputs(buildingBlockRunJson, coreAdapter);
3242732428
// Parse the JSON steps input
@@ -32431,7 +32432,7 @@ async function runRegisterSource(coreAdapter = core, githubContext = github) {
3243132432
const requestPayload = buildRequestPayload(steps, githubContext);
3243232433
const requestHeaders = buildRequestHeaders(token);
3243332434
// Register the source
32434-
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
32435+
await registerSource(runUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
3243532436
}
3243632437
catch (error) {
3243732438
if (error instanceof Error) {

src/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ interface BuildingBlockRun {
5959
meshstackBaseUrl: {
6060
href: string;
6161
};
62+
self: {
63+
href: string;
64+
};
6265
};
6366
}
6467

@@ -196,8 +199,7 @@ function buildRequestPayload(steps: any[], githubContext: GithubContextAdapter):
196199
}
197200

198201
async function registerSource(
199-
baseUrl: string,
200-
bbRunUuid: string,
202+
buildingBlockRunUrl: string,
201203
requestPayload: RequestPayload,
202204
requestHeaders: RequestHeaders,
203205
tokenFilePath: string,
@@ -208,7 +210,7 @@ async function registerSource(
208210

209211
try {
210212
const response = await axios.post(
211-
`${baseUrl}/api/meshobjects/meshbuildingblockruns/${bbRunUuid}/status/source`,
213+
`${buildingBlockRunUrl}/status/source`,
212214
requestPayload,
213215
{
214216
headers: requestHeaders
@@ -238,29 +240,31 @@ export async function runRegisterSource(
238240
): Promise<void> {
239241
try {
240242
const stepsInput = coreAdapter.getInput('steps');
241-
const buildingBlockRunUrl = coreAdapter.getInput('buildingBlockRunUrl');
243+
244+
// Extract buildingBlockRunUrl and buildingBlockRun from GitHub event payload
245+
const buildingBlockRunUrl = githubContext.context.payload.inputs?.buildingBlockRunUrl;
246+
const buildingBlockRun = githubContext.context.payload.inputs?.buildingBlockRun;
242247

243248
coreAdapter.debug(`Steps Input: ${stepsInput}`);
244249
coreAdapter.debug(`Building Block Run URL: ${buildingBlockRunUrl}`);
250+
coreAdapter.debug(`Building Block Run: ${buildingBlockRun}`);
245251

246252
// Load token
247253
const { token, tokenFilePath } = loadTokenFromFile(coreAdapter);
248254

249-
// Load building block run
255+
// Load building block run and determine the run URL
250256
let buildingBlockRunJson: BuildingBlockRun;
257+
let runUrl: string;
258+
251259
if (buildingBlockRunUrl) {
252260
buildingBlockRunJson = await loadBuildingBlockRunFromUrl(buildingBlockRunUrl, token, coreAdapter);
261+
runUrl = buildingBlockRunUrl;
253262
} else {
254-
const buildingBlockRun = githubContext.context.payload.inputs.buildingBlockRun;
255263
buildingBlockRunJson = loadBuildingBlockRunFromBase64(buildingBlockRun, coreAdapter);
264+
runUrl = buildingBlockRunJson._links.self.href;
256265
}
257266

258-
// Extract common data from buildingBlockRunJson
259-
const bbRunUuid = buildingBlockRunJson.metadata.uuid;
260-
const baseUrl = buildingBlockRunJson._links.meshstackBaseUrl.href;
261-
262-
coreAdapter.debug(`Base URL: ${baseUrl}`);
263-
coreAdapter.debug(`BB Run UUID: ${bbRunUuid}`);
267+
coreAdapter.debug(`Building Block Run URL: ${runUrl}`);
264268

265269
// Extract inputs and write to outputs
266270
extractInputs(buildingBlockRunJson, coreAdapter);
@@ -274,7 +278,7 @@ export async function runRegisterSource(
274278
const requestHeaders = buildRequestHeaders(token);
275279

276280
// Register the source
277-
await registerSource(baseUrl, bbRunUuid, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
281+
await registerSource(runUrl, requestPayload, requestHeaders, tokenFilePath, coreAdapter);
278282
} catch (error) {
279283
if (error instanceof Error) {
280284
coreAdapter.setFailed(error.message);

0 commit comments

Comments
 (0)