@@ -5,83 +5,120 @@ import * as fs from 'fs';
55import * as path from 'path' ;
66import * 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