Skip to content

Commit 9393399

Browse files
committed
adding lib
1 parent 6a76542 commit 9393399

File tree

11 files changed

+815
-0
lines changed

11 files changed

+815
-0
lines changed

lib/Constants.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.PsqlConstants = exports.FirewallConstants = exports.FileConstants = void 0;
4+
class FileConstants {
5+
}
6+
exports.FileConstants = FileConstants;
7+
FileConstants.singleParentDirRegex = /.*(\.sql){1}$/g;
8+
class FirewallConstants {
9+
}
10+
exports.FirewallConstants = FirewallConstants;
11+
FirewallConstants.ipv4MatchPattern = /\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/;
12+
class PsqlConstants {
13+
}
14+
exports.PsqlConstants = PsqlConstants;
15+
PsqlConstants.SELECT_1 = "SELECT 1";
16+
PsqlConstants.extractPasswordRegex = /(?<key>password)=(?<val>[^\s]*)/g;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
var __importDefault = (this && this.__importDefault) || function (mod) {
3+
return (mod && mod.__esModule) ? mod : { "default": mod };
4+
};
5+
Object.defineProperty(exports, "__esModule", { value: true });
6+
exports.FileListCreator = void 0;
7+
const SingleParentDir_1 = __importDefault(require("./SingleParentDir"));
8+
const Constants_1 = require("../Constants");
9+
class FileListCreator {
10+
static getFileList(filePathRegex) {
11+
if (Constants_1.FileConstants.singleParentDirRegex.test(filePathRegex)) {
12+
const singleParentDir = new SingleParentDir_1.default(filePathRegex);
13+
this.fileList = singleParentDir.getFileList();
14+
}
15+
return this.fileList;
16+
}
17+
}
18+
exports.FileListCreator = FileListCreator;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10+
Object.defineProperty(o, "default", { enumerable: true, value: v });
11+
}) : function(o, v) {
12+
o["default"] = v;
13+
});
14+
var __importStar = (this && this.__importStar) || function (mod) {
15+
if (mod && mod.__esModule) return mod;
16+
var result = {};
17+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18+
__setModuleDefault(result, mod);
19+
return result;
20+
};
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
const path = __importStar(require("path"));
23+
const core = __importStar(require("@actions/core"));
24+
const glob = __importStar(require("glob"));
25+
class SingleParentDir {
26+
constructor(filePathRegex) {
27+
this.filePathRegex = filePathRegex;
28+
}
29+
/**
30+
* Get list of files to execute in lexicographical order from the given filePathRegex
31+
* Supported filePathRegex are:
32+
* <file>.sql
33+
* <folder>/<regex>.sql
34+
* <folder1>/<folder2>/<folder3>/<regex>.sql
35+
*/
36+
getFileList() {
37+
core.debug(`Getting list of files to execute`);
38+
const basedir = process.env.GITHUB_WORKSPACE;
39+
if (!basedir) {
40+
throw new Error("GITHUB_WORKSPACE env variable is empty");
41+
}
42+
;
43+
let listOfMatchedFiles = glob.sync(this.filePathRegex, {});
44+
core.debug(`Matching list of files: ${listOfMatchedFiles}`);
45+
listOfMatchedFiles.sort();
46+
listOfMatchedFiles = listOfMatchedFiles.map((fileName) => path.join(basedir, fileName));
47+
console.log("List of files to be executed in order: " + listOfMatchedFiles);
48+
return listOfMatchedFiles;
49+
}
50+
}
51+
exports.default = SingleParentDir;

lib/PsqlFilesExecutor.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
const PsqlToolRunner_1 = __importDefault(require("./Utils/PsqlUtils/PsqlToolRunner"));
16+
const FileListCreator_1 = require("./FileListCreator/FileListCreator");
17+
class PsqlFilesExecutor {
18+
constructor(connectionString, filePath, args) {
19+
this.connectionString = connectionString;
20+
this.plsqlFilePath = filePath;
21+
this.args = args;
22+
this.extractedFiles = FileListCreator_1.FileListCreator.getFileList(this.plsqlFilePath);
23+
}
24+
static getPsqlFilesExecutor(connectionString, filePath, args) {
25+
if (!this.psqlFileExecutor) {
26+
this.psqlFileExecutor = new PsqlFilesExecutor(connectionString, filePath, args);
27+
}
28+
return this.psqlFileExecutor;
29+
}
30+
execute() {
31+
return __awaiter(this, void 0, void 0, function* () {
32+
let error = "";
33+
const options = {
34+
listeners: {
35+
stderr: (data) => {
36+
error += data.toString();
37+
}
38+
}
39+
};
40+
yield PsqlToolRunner_1.default.init();
41+
for (const file of this.extractedFiles) {
42+
console.log(`Executing file: ${file}`);
43+
yield PsqlToolRunner_1.default.executePsqlFile(this.connectionString, file, this.args, options);
44+
if (error) {
45+
throw new Error(`error in file: ${file}\n${error}`);
46+
}
47+
}
48+
});
49+
}
50+
}
51+
exports.default = PsqlFilesExecutor;

lib/Utils/ActionInputs.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10+
Object.defineProperty(o, "default", { enumerable: true, value: v });
11+
}) : function(o, v) {
12+
o["default"] = v;
13+
});
14+
var __importStar = (this && this.__importStar) || function (mod) {
15+
if (mod && mod.__esModule) return mod;
16+
var result = {};
17+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18+
__setModuleDefault(result, mod);
19+
return result;
20+
};
21+
Object.defineProperty(exports, "__esModule", { value: true });
22+
exports.ActionInputs = void 0;
23+
const core = __importStar(require("@actions/core"));
24+
const Constants_1 = require("../Constants");
25+
class ActionInputs {
26+
constructor() {
27+
this._serverName = core.getInput('server-name', { required: true });
28+
console.log(`this.servername: ${this._serverName}`);
29+
this._connectionString = core.getInput('connection-string', { required: true }).split("psql")[1].trim();
30+
this._plsqlFile = core.getInput('plsql-file', { required: true });
31+
this._args = core.getInput('arguments');
32+
this.parseConnectionString();
33+
}
34+
static getActionInputs() {
35+
if (!this.actionInputs) {
36+
this.actionInputs = new ActionInputs();
37+
}
38+
return this.actionInputs;
39+
}
40+
parseConnectionString() {
41+
const password = this.getPassword();
42+
if (!password) {
43+
throw new Error(`Password not found in connection string`);
44+
}
45+
core.setSecret(password);
46+
}
47+
get connectionString() {
48+
return this._connectionString;
49+
}
50+
get plsqlFile() {
51+
return this._plsqlFile;
52+
}
53+
get args() {
54+
return this._args;
55+
}
56+
getPassword() {
57+
let password = '';
58+
let matchingGroup = Constants_1.PsqlConstants.extractPasswordRegex.exec(this.connectionString);
59+
if (matchingGroup) {
60+
for (let match of matchingGroup) {
61+
password = match;
62+
}
63+
}
64+
return password;
65+
}
66+
;
67+
get serverName() {
68+
return this._serverName;
69+
}
70+
}
71+
exports.ActionInputs = ActionInputs;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10+
Object.defineProperty(o, "default", { enumerable: true, value: v });
11+
}) : function(o, v) {
12+
o["default"] = v;
13+
});
14+
var __importStar = (this && this.__importStar) || function (mod) {
15+
if (mod && mod.__esModule) return mod;
16+
var result = {};
17+
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18+
__setModuleDefault(result, mod);
19+
return result;
20+
};
21+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23+
return new (P || (P = Promise))(function (resolve, reject) {
24+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
25+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
27+
step((generator = generator.apply(thisArg, _arguments || [])).next());
28+
});
29+
};
30+
Object.defineProperty(exports, "__esModule", { value: true });
31+
const core = __importStar(require("@actions/core"));
32+
class FirewallManager {
33+
constructor(azurePsqlResourceManager) {
34+
this._resourceManager = azurePsqlResourceManager;
35+
}
36+
get resourceManager() {
37+
return this._resourceManager;
38+
}
39+
addFirewallRule(ipAddress) {
40+
return __awaiter(this, void 0, void 0, function* () {
41+
if (!ipAddress) {
42+
core.debug(`Client has access to PSQL server. Skip adding firewall exception.`);
43+
return;
44+
}
45+
console.log(`Client does not have access to PSQL server. Adding firewall exception for client's IP address.`);
46+
this._firewallRule = yield this._resourceManager.addFirewallRule(ipAddress, ipAddress);
47+
core.debug(JSON.stringify(this._firewallRule));
48+
console.log(`Successfully added firewall rule ${this._firewallRule.name}.`);
49+
});
50+
}
51+
removeFirewallRule() {
52+
return __awaiter(this, void 0, void 0, function* () {
53+
if (this._firewallRule) {
54+
console.log(`Removing firewall rule '${this._firewallRule.name}'.`);
55+
yield this._resourceManager.removeFirewallRule(this._firewallRule);
56+
console.log('Successfully removed firewall rule.');
57+
}
58+
else {
59+
core.debug('No firewall exception was added.');
60+
}
61+
});
62+
}
63+
}
64+
exports.default = FirewallManager;

0 commit comments

Comments
 (0)