UNPKG

@criticalmanufacturing/dev-i18n-transform

Version:
445 lines (444 loc) 20.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 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); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; exports.__esModule = true; //#region Imports var azdev = require("azure-devops-node-api"); var fs = require("fs"); var argv = require("minimist")(process.argv); // Import config var config; if (argv["config"] === undefined || argv["config"] === null) { config = require("../i18n-import.config.json"); } else { config = require(argv["config"]); } //#endregion //#region Constants var url = config.sourceControl.url; var project = config.sourceControl.project; var token = config.sourceControl.token || process.env.AccessToken || null; var workItemTitle = config.sourceControl.workItem.title; var workItemType = config.sourceControl.workItem.type; var workItemStateToIgnore = config.sourceControl.workItem.stateToIgnore; //#endregion var AzDevOpsManagement = /** @class */ (function () { function AzDevOpsManagement() { this.header = { "Content-Type": "application/json-patch+json" }; //#endregion } //#region Public Methods /** * Get connection to Azure DevOps */ AzDevOpsManagement.prototype.getConnection = function () { return __awaiter(this, void 0, void 0, function () { var tokenHandler, connection, gitConnection, workItemConnection, iterationConnection, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 4, , 5]); if (token === null) { throw new Error("Missing access token."); } tokenHandler = azdev.getHandlerFromToken(token); connection = new azdev.WebApi(url, tokenHandler, { ignoreSslError: true }); return [4 /*yield*/, connection.getGitApi()]; case 1: gitConnection = _a.sent(); return [4 /*yield*/, connection.getWorkItemTrackingApi()]; case 2: workItemConnection = _a.sent(); return [4 /*yield*/, connection.getWorkApi()]; case 3: iterationConnection = _a.sent(); return [2 /*return*/, { git: gitConnection, workItem: workItemConnection, iteration: iterationConnection }]; case 4: err_1 = _a.sent(); console.log(err_1); return [2 /*return*/, null]; case 5: return [2 /*return*/]; } }); }); }; /** * Get current iteration of team * @param iterationConnection (connection) */ AzDevOpsManagement.prototype.getCurrentIteration = function (iterationConnection) { return __awaiter(this, void 0, void 0, function () { var currentIteration, err_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, iterationConnection.getTeamIterations({ project: project, team: config.sourceControl.team }, "current") .then(function (output) { return output; })]; case 1: currentIteration = _a.sent(); return [2 /*return*/, { id: currentIteration[0].id, path: currentIteration[0].path }]; case 2: err_2 = _a.sent(); console.log(err_2); return [2 /*return*/, null]; case 3: return [2 /*return*/]; } }); }); }; /** * Check if already exists a user story with title = userStoryName * @param iterationConnection (connection) * @param currentIteration (path of current iteration) */ AzDevOpsManagement.prototype.checkIfUSAlreadyExist = function (workItemConnection, currentIteration) { return __awaiter(this, void 0, void 0, function () { var wiql, results, err_3; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); wiql = { query: "SELECT [System.Id],[System.Title] FROM WorkItems WHERE [System.TeamProject] = '" + project + "' AND [System.WorkItemType] = '" + workItemType + "' AND [System.IterationPath] = '" + currentIteration + "' AND [System.Title] = '" + workItemTitle + "' AND [System.State] NOT CONTAINS '" + workItemStateToIgnore + "'" }; return [4 /*yield*/, workItemConnection.queryByWiql(wiql).then(function (output) { return output; })]; case 1: results = _a.sent(); if (results.workItems.length > 0) { return [2 /*return*/, results.workItems[0].id]; } return [3 /*break*/, 3]; case 2: err_3 = _a.sent(); console.log(err_3); return [3 /*break*/, 3]; case 3: return [2 /*return*/, null]; } }); }); }; /** * Create a new User Story * @param workItemConnection (connection) * @param currentIteration (path of current iteration) */ AzDevOpsManagement.prototype.createUserStory = function (workItemConnection, currentIteration) { return __awaiter(this, void 0, void 0, function () { var userStoryJSON, userStory, err_4; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); userStoryJSON = this.createUserStoryJSON(currentIteration); return [4 /*yield*/, workItemConnection.createWorkItem(this.header, userStoryJSON, project, workItemType)]; case 1: userStory = _a.sent(); return [2 /*return*/, userStory.id]; case 2: err_4 = _a.sent(); console.log(err_4); return [2 /*return*/, null]; case 3: return [2 /*return*/]; } }); }); }; /** * Create commit and push to specified repository * @param gitConnection (connection) * @param key (package of config.packages) * @param path (array with paths changed) * @param repo (repository to make the commit and push) */ AzDevOpsManagement.prototype.createCommitsAndPush = function (gitConnection, key, path, repo) { return __awaiter(this, void 0, void 0, function () { var arrayChanges, push, data, oldObjectId, objectId, commitCriteria, index, commits, change, commitToRepository, pushCommits, err_5; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 7, , 8]); arrayChanges = []; push = void 0; data = void 0; oldObjectId = void 0; objectId = void 0; commitCriteria = { itemVersion: { version: config.sourceControl.branch.target }, $skip: 0, $top: 1 }; index = 0; _a.label = 1; case 1: if (!(index < path.length)) return [3 /*break*/, 4]; if (!path[index].match(key)) return [3 /*break*/, 3]; data = fs.readFileSync(path[index]).toString(); return [4 /*yield*/, gitConnection.getCommits(repo, commitCriteria, project)]; case 2: commits = _a.sent(); oldObjectId = commits.map(function (commit) { return commit.commitId; }); objectId = oldObjectId[0]; change = this.createChanges(path[index], key, data); arrayChanges.push(change); _a.label = 3; case 3: index++; return [3 /*break*/, 1]; case 4: if (!(arrayChanges.length > 0)) return [3 /*break*/, 6]; commitToRepository = this.createCommitJSON(arrayChanges); pushCommits = this.createPushJSON(commitToRepository, objectId); return [4 /*yield*/, gitConnection.createPush(pushCommits, repo, project)]; case 5: push = _a.sent(); _a.label = 6; case 6: return [2 /*return*/, push.pushedBy.id]; case 7: err_5 = _a.sent(); console.log(err_5); return [2 /*return*/, null]; case 8: return [2 /*return*/]; } }); }); }; /** * Create pull request * @param gitConnection (connection) * @param repo (repository to create a pull request) * @param userStoryId (id of user story to associate) */ AzDevOpsManagement.prototype.createPR = function (gitConnection, repo, userStoryId) { return __awaiter(this, void 0, void 0, function () { var pullRequestCriteria, pullRequests, lastMergeTargetCommit, gitPullRequestToCreate, createPRequest; return __generator(this, function (_a) { switch (_a.label) { case 0: pullRequestCriteria = { targetRefName: "refs/heads/ " + config.sourceControl.branch.target }; return [4 /*yield*/, gitConnection.getPullRequests(repo, pullRequestCriteria, project)]; case 1: pullRequests = _a.sent(); lastMergeTargetCommit = pullRequests.map(function (request) { return request.lastMergeTargetCommit.commitId; }); gitPullRequestToCreate = this.createPullRequestJSON(lastMergeTargetCommit[0], repo, userStoryId); return [4 /*yield*/, gitConnection.createPullRequest(gitPullRequestToCreate, repo, project, true)]; case 2: createPRequest = _a.sent(); return [2 /*return*/, createPRequest.pullRequestId]; } }); }); }; /** * Update pull request * @param gitConnection (connection) * @param pullRequestId (id of pull request to be updated) * @param repo (repository where is located the pull request to be updated) * @param azDevOpsUserId (id of Azure DevOps user) */ AzDevOpsManagement.prototype.updatePR = function (gitConnection, pullRequestId, repo, azDevOpsUserId) { return __awaiter(this, void 0, void 0, function () { var gitPullRequestToUpdate, updatePRequest; return __generator(this, function (_a) { switch (_a.label) { case 0: gitPullRequestToUpdate = this.updatePullRequestJSON(azDevOpsUserId); // Create pull request reviewer and approve return [4 /*yield*/, gitConnection.createPullRequestReviewer({ vote: 10 }, repo, pullRequestId, azDevOpsUserId, project)]; case 1: // Create pull request reviewer and approve _a.sent(); return [4 /*yield*/, gitConnection.updatePullRequest(gitPullRequestToUpdate, repo, pullRequestId, project)]; case 2: updatePRequest = _a.sent(); return [2 /*return*/, updatePRequest.pullRequestId]; } }); }); }; //#endregion //#region Private Methods /** * JSON to create a new user story * @param currentIteration (path of current iteration) */ AzDevOpsManagement.prototype.createUserStoryJSON = function (currentIteration) { var userStoryJSON = [ { "op": "add", "from": null, "path": "/fields/System.Title", "value": workItemTitle }, { "op": "add", "from": null, "path": "/fields/System.IterationPath", "value": currentIteration } ]; return userStoryJSON; }; /** * JSON to identify changes * @param path (path changed) * @param key (package in config.packages) * @param data (content of path changed) */ AzDevOpsManagement.prototype.createChanges = function (path, key, data) { var i18nPackages = Object.keys(config.packages.i18n); var resourcesPackages = Object.keys(config.packages.resources); var parameter; if (i18nPackages.includes(key)) { parameter = config.packages.i18n[key].repository; } else if (resourcesPackages.includes(key)) { parameter = key; path = path.replace(/\\/gm, "/"); } var regexString = ".*PARAMETER"; regexString = regexString.replace("PARAMETER", parameter); var regExpression = new RegExp(regexString); path = path.replace(regExpression, ""); var change = { "changeType": 2, "item": { "path": path }, "newContent": { "content": data, "contentType": 0 } }; return change; }; /** * JSON to create a new commit * @param changesArray (array with changes) */ AzDevOpsManagement.prototype.createCommitJSON = function (changesArray) { var commit = { "comment": config.sourceControl.commit.comment, "changes": changesArray }; return commit; }; /** * JSON to create a new push * @param commits (commit to make push) * @param oldObjectId (id of last commit) */ AzDevOpsManagement.prototype.createPushJSON = function (commits, oldObjectId) { var push = { "refUpdates": [ { "name": "refs/heads/" + config.sourceControl.branch.source, "oldObjectId": oldObjectId } ], "commits": [commits] }; return push; }; /** * JSON to create a new pull request * @param lastMergeTargetCommit (id of last commit merged) * @param repo (repository to create a pull request) * @param userStoryId (id of user story to associate) */ AzDevOpsManagement.prototype.createPullRequestJSON = function (lastMergeTargetCommit, repo, userStoryId) { var gitPullRequestToCreate = { lastMergeTargetCommit: { commitId: lastMergeTargetCommit }, createdBy: { "id": config.sourceControl.user.id, "displayName": config.sourceControl.user.displayName }, title: config.sourceControl.pullRequest.title, description: config.sourceControl.pullRequest.description, repository: { id: repo, project: { name: project, url: url }, url: url + "/" + repo }, sourceRefName: "refs/heads/" + config.sourceControl.branch.source, targetRefName: "refs/heads/" + config.sourceControl.branch.target, workItemRefs: [{ id: "" + userStoryId }] }; return gitPullRequestToCreate; }; /** * JSON to update the pull request * Set auto-complete * @param azDevOpsUserId (id of person to set pull request to be auto-completed and approve pull request) */ AzDevOpsManagement.prototype.updatePullRequestJSON = function (azDevOpsUserId) { var gitPullRequestToUpdate = { autoCompleteSetBy: { id: azDevOpsUserId }, completionOptions: { bypassPolicy: false, bypassReason: "", deleteSourceBranch: true, mergeCommitMessage: config.sourceControl.pullRequest.mergeCommitMessage, mergeStrategy: 1, transitionWorkItems: false } }; return gitPullRequestToUpdate; }; return AzDevOpsManagement; }()); exports.AzDevOpsManagement = AzDevOpsManagement;