2019-11-01 12:16:53 +03:00
"use strict" ;
2022-08-04 12:31:37 +03:00
var _ _createBinding = ( this && this . _ _createBinding ) || ( Object . create ? ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
Object . defineProperty ( o , k2 , { enumerable : true , get : function ( ) { return m [ k ] ; } } ) ;
} ) : ( function ( o , m , k , k2 ) {
if ( k2 === undefined ) k2 = k ;
o [ k2 ] = m [ k ] ;
} ) ) ;
var _ _setModuleDefault = ( this && this . _ _setModuleDefault ) || ( Object . create ? ( function ( o , v ) {
Object . defineProperty ( o , "default" , { enumerable : true , value : v } ) ;
} ) : function ( o , v ) {
o [ "default" ] = v ;
} ) ;
var _ _importStar = ( this && this . _ _importStar ) || function ( mod ) {
if ( mod && mod . _ _esModule ) return mod ;
var result = { } ;
if ( mod != null ) for ( var k in mod ) if ( k !== "default" && Object . hasOwnProperty . call ( mod , k ) ) _ _createBinding ( result , mod , k ) ;
_ _setModuleDefault ( result , mod ) ;
return result ;
} ;
2019-11-01 12:16:53 +03:00
var _ _awaiter = ( this && this . _ _awaiter ) || function ( thisArg , _arguments , P , generator ) {
2022-08-04 12:31:37 +03:00
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
2019-11-01 12:16:53 +03:00
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
function fulfilled ( value ) { try { step ( generator . next ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
function rejected ( value ) { try { step ( generator [ "throw" ] ( value ) ) ; } catch ( e ) { reject ( e ) ; } }
2022-08-04 12:31:37 +03:00
function step ( result ) { result . done ? resolve ( result . value ) : adopt ( result . value ) . then ( fulfilled , rejected ) ; }
2019-11-01 12:16:53 +03:00
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
} ) ;
} ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2022-08-04 12:31:37 +03:00
exports . run = void 0 ;
2019-11-01 12:16:53 +03:00
const core = _ _importStar ( require ( "@actions/core" ) ) ;
const crypto = _ _importStar ( require ( "crypto" ) ) ;
const path = _ _importStar ( require ( "path" ) ) ;
const AuthorizerFactory _1 = require ( "azure-actions-webclient/AuthorizerFactory" ) ;
const AzureMySqlActionHelper _1 = _ _importDefault ( require ( "./AzureMySqlActionHelper" ) ) ;
const AzureMySqlAction _1 = _ _importDefault ( require ( "./AzureMySqlAction" ) ) ;
const FirewallManager _1 = _ _importDefault ( require ( "./FirewallManager" ) ) ;
const AzureMySqlResourceManager _1 = _ _importDefault ( require ( "./AzureMySqlResourceManager" ) ) ;
const MySqlConnectionStringBuilder _1 = _ _importDefault ( require ( "./MySqlConnectionStringBuilder" ) ) ;
2020-09-22 05:12:14 +03:00
const MySqlUtils _1 = _ _importDefault ( require ( "./MySqlUtils" ) ) ;
2019-11-01 12:16:53 +03:00
let userAgentPrefix = ! ! process . env . AZURE _HTTP _USER _AGENT ? ` ${ process . env . AZURE _HTTP _USER _AGENT } ` : "" ;
function run ( ) {
return _ _awaiter ( this , void 0 , void 0 , function * ( ) {
let firewallManager ;
try {
// Set user agent variable
let usrAgentRepo = crypto . createHash ( 'sha256' ) . update ( ` ${ process . env . GITHUB _REPOSITORY } ` ) . digest ( 'hex' ) ;
let actionName = 'AzureMySqlAction' ;
let userAgentString = ( ! ! userAgentPrefix ? ` ${ userAgentPrefix } + ` : '' ) + ` GITHUBACTIONS_ ${ actionName } _ ${ usrAgentRepo } ` ;
core . exportVariable ( 'AZURE_HTTP_USER_AGENT' , userAgentString ) ;
let inputs = getInputs ( ) ;
let azureMySqlAction = new AzureMySqlAction _1 . default ( inputs ) ;
2020-09-22 05:12:14 +03:00
const runnerIPAddress = yield MySqlUtils _1 . default . detectIPAddress ( inputs . serverName , inputs . connectionString ) ;
if ( runnerIPAddress ) {
let azureResourceAuthorizer = yield AuthorizerFactory _1 . AuthorizerFactory . getAuthorizer ( ) ;
let azureMySqlResourceManager = yield AzureMySqlResourceManager _1 . default . getResourceManager ( inputs . serverName , azureResourceAuthorizer ) ;
firewallManager = new FirewallManager _1 . default ( azureMySqlResourceManager ) ;
yield firewallManager . addFirewallRule ( runnerIPAddress ) ;
}
2019-11-01 12:16:53 +03:00
yield azureMySqlAction . execute ( ) ;
}
catch ( error ) {
core . setFailed ( error . message ) ;
}
finally {
if ( firewallManager ) {
yield firewallManager . removeFirewallRule ( ) ;
}
// Reset AZURE_HTTP_USER_AGENT
core . exportVariable ( 'AZURE_HTTP_USER_AGENT' , userAgentPrefix ) ;
}
} ) ;
}
exports . run = run ;
function getInputs ( ) {
let serverName = core . getInput ( 'server-name' , { required : true } ) ;
2020-10-29 10:02:25 +03:00
// Support both auth methods. Passing all parameters (login,pwd,db) or the legacy connection string
const connectionString = core . getInput ( 'connection-string' , { required : false } ) ;
const username = core . getInput ( 'username' , { required : false } ) ;
if ( username && username !== '' && connectionString && connectionString !== '' ) {
throw new Error ( 'Cannot specify both username and connection string' ) ;
}
let connectionStringBuilder ;
if ( username && username !== '' ) {
connectionStringBuilder = {
server : serverName ,
userId : username ,
password : core . getInput ( 'password' , { required : true } ) ,
database : core . getInput ( 'database' , { required : false } )
} ;
}
else if ( ! connectionString || connectionString === '' ) {
throw new Error ( 'Need to specify either username and password or connection-string' ) ;
}
else { // use deprecated connection string
connectionStringBuilder = new MySqlConnectionStringBuilder _1 . default ( connectionString ) ;
// validate that the server name input matches the connection string server name
if ( serverName . toLowerCase ( ) !== connectionStringBuilder . server . toLowerCase ( ) ) {
throw new Error ( 'Server name mismatch error. The server name provided in the action input does not match the server name provided in the connection string.' ) ;
}
}
const sqlFile = AzureMySqlActionHelper _1 . default . resolveFilePath ( core . getInput ( 'sql-file' , { required : true } ) ) ;
2019-11-01 12:16:53 +03:00
if ( path . extname ( sqlFile ) . toLowerCase ( ) !== '.sql' ) {
throw new Error ( ` Invalid sql file path provided as input ${ sqlFile } ` ) ;
}
2020-10-29 10:02:25 +03:00
const additionalArguments = core . getInput ( 'arguments' ) ;
2019-11-01 12:16:53 +03:00
return {
serverName : serverName ,
connectionString : connectionStringBuilder ,
sqlFile : sqlFile ,
additionalArguments : additionalArguments
} ;
}
run ( ) ;