- Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.ts
97 lines (80 loc) · 4.08 KB
/
main.ts
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
import*ascorefrom'@actions/core';
import*ascryptofrom'crypto';
import*aspathfrom'path';
import{AuthorizerFactory}from'azure-actions-webclient/AuthorizerFactory';
importAzureMySqlActionHelperfrom'./AzureMySqlActionHelper';
importAzureMySqlAction,{IActionInputs}from"./AzureMySqlAction";
importFirewallManagerfrom'./FirewallManager';
importAzureMySqlResourceManagerfrom'./AzureMySqlResourceManager';
importMySqlConnectionStringBuilderfrom'./MySqlConnectionStringBuilder';
importMySqlUtilsfrom'./MySqlUtils';
import{MySqlConnectionString}from'./MySqlConnectionString';
letuserAgentPrefix=!!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : "";
exportasyncfunctionrun(){
letfirewallManager;
try{
// Set user agent variable
letusrAgentRepo=crypto.createHash('sha256').update(`${process.env.GITHUB_REPOSITORY}`).digest('hex');
letactionName='AzureMySqlAction';
letuserAgentString=(!!userAgentPrefix ? `${userAgentPrefix}+` : '')+`GITHUBACTIONS_${actionName}_${usrAgentRepo}`;
core.exportVariable('AZURE_HTTP_USER_AGENT',userAgentString);
letinputs=getInputs();
letazureMySqlAction=newAzureMySqlAction(inputs);
construnnerIPAddress=awaitMySqlUtils.detectIPAddress(inputs.serverName,inputs.connectionString);
if(runnerIPAddress){
letazureResourceAuthorizer=awaitAuthorizerFactory.getAuthorizer();
letazureMySqlResourceManager=awaitAzureMySqlResourceManager.getResourceManager(inputs.serverName,azureResourceAuthorizer);
firewallManager=newFirewallManager(azureMySqlResourceManager);
awaitfirewallManager.addFirewallRule(runnerIPAddress);
}
awaitazureMySqlAction.execute();
}
catch(error){
core.setFailed(error.message);
}
finally{
if(firewallManager){
awaitfirewallManager.removeFirewallRule();
}
// Reset AZURE_HTTP_USER_AGENT
core.exportVariable('AZURE_HTTP_USER_AGENT',userAgentPrefix);
}
}
functiongetInputs(): IActionInputs{
letserverName=core.getInput('server-name',{required: true});
// Support both auth methods. Passing all parameters (login,pwd,db) or the legacy connection string
constconnectionString=core.getInput('connection-string',{required: false});
constusername=core.getInput('username',{required: false});
if(username&&username!==''&&connectionString&&connectionString!==''){
thrownewError('Cannot specify both username and connection string')
}
letconnectionStringBuilder: MySqlConnectionString
if(username&&username!==''){
connectionStringBuilder={
server: serverName,
userId: username,
password: core.getInput('password',{required: true}),
database: core.getInput('database',{required: false})
}
}elseif(!connectionString||connectionString===''){
thrownewError('Need to specify either username and password or connection-string')
}else{// use deprecated connection string
connectionStringBuilder=newMySqlConnectionStringBuilder(connectionString);
// validate that the server name input matches the connection string server name
if(serverName.toLowerCase()!==connectionStringBuilder.server.toLowerCase()){
thrownewError('Server name mismatch error. The server name provided in the action input does not match the server name provided in the connection string.');
}
}
constsqlFile=AzureMySqlActionHelper.resolveFilePath(core.getInput('sql-file',{required: true}));
if(path.extname(sqlFile).toLowerCase()!=='.sql'){
thrownewError(`Invalid sql file path provided as input ${sqlFile}`);
}
constadditionalArguments=core.getInput('arguments');
return{
serverName: serverName,
connectionString: connectionStringBuilder,
sqlFile: sqlFile,
additionalArguments: additionalArguments
};
}
run();