-
Notifications
You must be signed in to change notification settings - Fork 23
/
Common.js
219 lines (192 loc) · 5.18 KB
/
Common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const core = require( '@actions/core' );
if( typeof wTools === 'undefined' )
require( '../node_modules/Joined.s' );
const _ = wTools;
let GithubActionsParser = null;
// let ChildProcess = null;
//
function remotePathFromActionName( name )
{
return _.git.path.parse( `https://github.com/${ _.strReplace( name, '@', '!' ) }` );
}
//
function actionClone( localPath, remotePath )
{
if( !_.fileProvider.fileExists( localPath ) )
{
const con = _.take( null );
con.then( () =>
{
return _.git.repositoryClone
({
remotePath,
localPath,
sync : 0,
attemptLimit : 4,
attemptDelay : 500,
attemptDelayMultiplier : 4,
});
});
con.then( () =>
{
if( remotePath.tag !== 'master' )
return _.git.tagLocalChange
({
localPath,
tag : remotePath.tag,
sync : 0
});
return true;
});
return con;
}
return null;
}
//
function actionConfigRead( actionDir )
{
let configPath = _.path.join( actionDir, 'action.yml' );
if( !_.fileProvider.fileExists( configPath ) )
configPath = _.path.join( actionDir, 'action.yaml' )
_.assert( _.fileProvider.fileExists( configPath ), 'Expects action path `action.yml` or `action.yaml`' );
return _.fileProvider.fileRead
({
filePath : configPath,
encoding : 'yaml',
});
}
//
function actionOptionsParse( src )
{
const result = Object.create( null );
for( let i = 0 ; i < src.length ; i++ )
{
const splits = _.strStructureParse({ src : src[ i ], toNumberMaybe : 0 });
for( let key in splits )
if( splits[ key ] === '|' && i + 1 < src.length )
{
let keySpacesNumber = src[ i ].search( /\S/ );
let spacesNumber = src[ i + 1 ].search( /\S/ );
if( spacesNumber > keySpacesNumber )
{
i += 1;
splits[ key ] = "";
let multilineSplits = splits;
let multileneKey= key;
let multilineKeyIs = true;
while( multilineKeyIs && i < src.length )
{
if( src[ i ].search( /\S/ ) >= spacesNumber )
{
multilineSplits[ multileneKey ] += `\n${ src[ i ].substring( spacesNumber ) }`;
i += 1;
}
else
{
multilineSplits[ multileneKey ] = multilineSplits[ multileneKey ].substring( 1 );
_.map.extend( result, multilineSplits );
multilineKeyIs = false;
i -= 1;
}
}
if( i === src.length && multilineKeyIs )
{
multilineSplits[ multileneKey ] = multilineSplits[ multileneKey ].substring( 1 );
_.map.extend( result, multilineSplits );
}
}
else
{
_.map.extend( result, splits );
}
}
else
{
_.map.extend( result, splits );
}
}
return result;
}
//
function envOptionsFrom( options, inputs )
{
const result = Object.create( null );
for( let key in options )
result[ `INPUT_${key.replace(/ /g, '_').toUpperCase()}` ] = options[ key ];
if( inputs )
{
for( let key in inputs )
if( !( key in options ) && inputs[ key ].default !== undefined )
{
let value = inputs[ key ].default;
if( _.str.is( value ) )
if( value.startsWith( '${{' ) && value.endsWith( '}}' ) )
{
if( GithubActionsParser === null )
GithubActionsParser = require( 'github-actions-parser' );
value = GithubActionsParser.evaluateExpression( value, { get : getContext } );
}
result[ `INPUT_${key.replace(/ /g, '_').toUpperCase()}` ] = value;
}
}
return result;
/* */
function getContext( contextName )
{
if( contextName === 'env' )
{
let envContext = JSON.parse( core.getInput( 'env_context' ) );
if( _.map.keys( envContext ).length === 0 )
return process.env;
return envContext;
}
else if( contextName === 'github' )
{
let githubContext = JSON.parse( core.getInput( 'github_context' ) );
githubContext = githubContextUpdate( githubContext );
return githubContext;
}
else if( contextName === 'job' )
{
const jobContext = JSON.parse( core.getInput( 'job_context' ) );
return jobContext;
}
else if( contextName === 'matrix' )
{
const matrixContext = JSON.parse( core.getInput( 'matrix_context' ) );
return matrixContext;
}
_.assert( false, `The requested context "${ contextName }" does not supported by action. Please, open an issue with the request for the feature.` );
}
/* */
function githubContextUpdate( githubContext )
{
const remoteActionPath = remotePathFromActionName( process.env.RETRY_ACTION );
const localActionPath = _.path.nativize( _.path.join( __dirname, '../../../', remoteActionPath.repo ) );
githubContext.action_path = localActionPath;
githubContext.action_ref = remoteActionPath.tag;
return githubContext;
}
}
//
function envOptionsSetup( options )
{
for( let key in options )
{
core.exportVariable( key, options[ key ] );
process.env[ key ] = options[ key ];
}
}
// --
// export
// --
const Self =
{
remotePathFromActionName,
actionClone,
actionConfigRead,
actionOptionsParse,
envOptionsFrom,
envOptionsSetup,
};
module.exports = Self;