azdev-automation
Version:
Azure DevOps automation framework enables access control automation of projects, pipelines and repositories configuration in Azure DevOps Services
110 lines (109 loc) • 4.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurationReader = void 0;
const ajv_1 = __importDefault(require("ajv"));
const fs_1 = require("fs");
class ConfigurationReader {
constructor(artifactFactory, logger) {
this.debugLogger = logger.extend(this.constructor.name);
this.artifactFactory = artifactFactory;
}
async read() {
const debug = this.debugLogger.extend(this.read.name);
const projects = await this.parse(this.artifactFactory.configuration);
const projectPermissions = await this.parse(this.artifactFactory.projectPermissions);
const buildPermissions = await this.parse(this.artifactFactory.buildPermissions);
const releasePermissions = await this.parse(this.artifactFactory.releasePermissions);
const repositoryPermissions = await this.parse(this.artifactFactory.repositoryPermissions);
const workPermissions = await this.parse(this.artifactFactory.workPermissions);
for (const project of projects) {
debug(`Reading <${project.name}> project configuration`);
if (project.permissions.project) {
const policyName = project.permissions.project.toString();
project.permissions.project = this.getProjectPermission(policyName, projectPermissions);
}
if (project.permissions.build) {
const policyName = project.permissions.build.toString();
project.permissions.build = this.getBuildPermission(policyName, buildPermissions);
}
if (project.permissions.release) {
const policyName = project.permissions.release.toString();
project.permissions.release = this.getReleasePermission(policyName, releasePermissions);
}
if (project.permissions.repository) {
const policyName = project.permissions.repository.toString();
project.permissions.repository = this.getRepositoryPermission(policyName, repositoryPermissions);
}
if (project.permissions.work) {
const policyName = project.permissions.work.toString();
project.permissions.work = this.getWorkPermission(policyName, workPermissions);
}
}
debug(projects);
return projects;
}
async parse(config) {
const debug = this.debugLogger.extend(this.parse.name);
const validator = await this.readSchema(config.schema);
const result = await this.readJson(config.path);
const valid = await validator(result);
if (!valid) {
throw new Error(`Invalid JSON schema: ${validator.errors}`);
}
return result;
}
async readSchema(path) {
const debug = this.debugLogger.extend(this.readSchema.name);
debug(`Reading <${path}> schema`);
const validator = new ajv_1.default({ allErrors: true });
const json = (0, fs_1.readFileSync)(path, "utf8");
const schema = JSON.parse(json);
return validator.compile(schema);
}
async readJson(path) {
const debug = this.debugLogger.extend(this.readJson.name);
debug(`Reading <${path}> file`);
const json = (0, fs_1.readFileSync)(path, "utf8");
const result = JSON.parse(json);
return result;
}
getProjectPermission(name, policies) {
const result = policies.filter((p) => p.name === name);
if (!result.length) {
throw new Error(`Project permissions policy <${name}> not found`);
}
return result[0];
}
getBuildPermission(name, policies) {
const result = policies.filter((p) => p.name === name);
if (!result.length) {
throw new Error(`Build permissions policy <${name}> not found`);
}
return result[0];
}
getReleasePermission(name, policies) {
const result = policies.filter((p) => p.name === name);
if (!result.length) {
throw new Error(`Release permissions policy <${name}> not found`);
}
return result[0];
}
getRepositoryPermission(name, policies) {
const result = policies.filter((p) => p.name === name);
if (!result.length) {
throw new Error(`Repository permissions policy <${name}> not found`);
}
return result[0];
}
getWorkPermission(name, policies) {
const result = policies.filter((p) => p.name === name);
if (!result.length) {
throw new Error(`Work permissions policy <${name}> not found`);
}
return result[0];
}
}
exports.ConfigurationReader = ConfigurationReader;