@clickview/ship-it 0.0.40 → 0.0.42-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (125) hide show
  1. package/Dockerfile +33 -33
  2. package/README.md +48 -48
  3. package/config.json +19 -19
  4. package/dist/I3/I3 Notes Audit.csv +127 -0
  5. package/dist/J1/J1 Notes Audit.csv +38 -0
  6. package/dist/J2/J2 Notes Audit.csv +52 -0
  7. package/dist/L1/L1 Notes Audit.csv +139 -0
  8. package/lib/config.json +20 -20
  9. package/lib/src/api-clients/git-lab/GitLabApiClient.js +564 -557
  10. package/lib/src/api-clients/git-lab/GitLabMapper.js +122 -122
  11. package/lib/src/api-clients/index.js +20 -20
  12. package/lib/src/api-clients/slack/SlackApiClient.js +206 -206
  13. package/lib/src/api-clients/team-city/TeamCityApiClient.js +100 -100
  14. package/lib/src/api-clients/trello/TrelloApiClient.js +145 -145
  15. package/lib/src/commands/BumpPackagesCommand.js +76 -76
  16. package/lib/src/commands/ConfigureProjectsCommand.js +84 -84
  17. package/lib/src/commands/CreateBranchCommand.js +100 -100
  18. package/lib/src/commands/CreateMergeRequestCommand.js +107 -107
  19. package/lib/src/commands/CreateNotesAuditCommand.js +84 -84
  20. package/lib/src/commands/CreateReleaseNotesCommand.js +95 -95
  21. package/lib/src/commands/CreateTrelloCardsCommand.js +76 -77
  22. package/lib/src/commands/InstallPackagesCommand.js +76 -76
  23. package/lib/src/commands/ListProjectsCommand.js +76 -76
  24. package/lib/src/commands/ReleaseCommand.js +136 -136
  25. package/lib/src/commands/StartProjectBuildsCommand.js +76 -76
  26. package/lib/src/commands/StatusCommand.js +93 -93
  27. package/lib/src/commands/TagMasterCommand.js +76 -76
  28. package/lib/src/commands/ValidateMergeRequestCommand.js +76 -76
  29. package/lib/src/commands/VersionCommand.js +76 -76
  30. package/lib/src/commands/index.js +31 -31
  31. package/lib/src/constants/CommandTypeMapping.js +21 -21
  32. package/lib/src/constants/GitLabMemberIds.js +6 -6
  33. package/lib/src/constants/MonorepoPackageJsonFiles.js +41 -41
  34. package/lib/src/constants/MonorepoProjectNames.js +16 -16
  35. package/lib/src/constants/ProjectNames.js +69 -69
  36. package/lib/src/constants/SlackUserEmailList.js +13 -13
  37. package/lib/src/constants/TeamCityBuildIDs.js +46 -46
  38. package/lib/src/constants/index.js +19 -19
  39. package/lib/src/errors/ShipItError.js +32 -32
  40. package/lib/src/errors/index.js +17 -17
  41. package/lib/src/index.js +18 -18
  42. package/lib/src/interfaces/Config.js +2 -2
  43. package/lib/src/interfaces/MergeRequestState.js +2 -2
  44. package/lib/src/interfaces/ProjectNames.js +2 -2
  45. package/lib/src/interfaces/ShipItCommand.js +2 -2
  46. package/lib/src/interfaces/ShipItTask.js +2 -2
  47. package/lib/src/interfaces/VersionType.js +2 -2
  48. package/lib/src/interfaces/command-cli-options/DockerMilestoneCommandCliOptions.js +2 -0
  49. package/lib/src/interfaces/command-cli-options/MilestoneCommandCliOptions.js +2 -2
  50. package/lib/src/interfaces/command-cli-options/ReleaseCommandCliOptions.js +2 -2
  51. package/lib/src/interfaces/command-cli-options/StatusCommandCliOptions.js +2 -2
  52. package/lib/src/interfaces/command-cli-options/index.js +19 -19
  53. package/lib/src/interfaces/command-options/BaseCommandOptions.js +2 -2
  54. package/lib/src/interfaces/command-options/BumpPackagesCommandOptions.js +2 -2
  55. package/lib/src/interfaces/command-options/ConfigureProjectsOptions.js +2 -2
  56. package/lib/src/interfaces/command-options/DockerMilestoneCommandOptions.js +2 -2
  57. package/lib/src/interfaces/command-options/MilestoneCommandOptions.js +2 -2
  58. package/lib/src/interfaces/command-options/ReleaseCommandOptions.js +2 -2
  59. package/lib/src/interfaces/command-options/StatusCommandOptions.js +2 -2
  60. package/lib/src/interfaces/command-options/ValidateMRCommandOptions.js +2 -2
  61. package/lib/src/interfaces/command-options/index.js +23 -23
  62. package/lib/src/interfaces/index.js +26 -26
  63. package/lib/src/interfaces/models/ApprovalRule.js +2 -2
  64. package/lib/src/interfaces/models/ApprovalRuleConfiguration.js +2 -2
  65. package/lib/src/interfaces/models/Branch.js +2 -2
  66. package/lib/src/interfaces/models/Commit.js +2 -2
  67. package/lib/src/interfaces/models/CommitFile.js +2 -2
  68. package/lib/src/interfaces/models/CreateApprovalRuleRequest.js +2 -2
  69. package/lib/src/interfaces/models/File.js +2 -2
  70. package/lib/src/interfaces/models/Group.js +2 -2
  71. package/lib/src/interfaces/models/Label.js +2 -2
  72. package/lib/src/interfaces/models/MergeRequest.js +2 -2
  73. package/lib/src/interfaces/models/Milestone.js +2 -2
  74. package/lib/src/interfaces/models/Project.js +2 -2
  75. package/lib/src/interfaces/models/ProjectConfig.js +9 -9
  76. package/lib/src/interfaces/models/ProtectedBranch.js +2 -2
  77. package/lib/src/interfaces/models/Release.js +2 -2
  78. package/lib/src/interfaces/models/Tag.js +2 -2
  79. package/lib/src/interfaces/models/index.js +32 -32
  80. package/lib/src/interfaces/view-models/MilestoneProject.js +2 -2
  81. package/lib/src/interfaces/view-models/index.js +17 -17
  82. package/lib/src/inversify.config.js +79 -79
  83. package/lib/src/services/GitLabService.js +553 -544
  84. package/lib/src/services/SlackService.js +142 -142
  85. package/lib/src/services/TeamCityService.js +37 -37
  86. package/lib/src/services/TrelloService.js +84 -84
  87. package/lib/src/services/index.js +20 -20
  88. package/lib/src/startup/AddMilestoneCommand.js +88 -96
  89. package/lib/src/startup/GetAction.js +68 -68
  90. package/lib/src/startup/SetupCommands.js +191 -180
  91. package/lib/src/tasks/BumpPackagesTask.js +230 -230
  92. package/lib/src/tasks/BumpVersionTxtTask.js +110 -110
  93. package/lib/src/tasks/CommitPackageVersionsTask.js +116 -0
  94. package/lib/src/tasks/CreateBranchTask.js +104 -104
  95. package/lib/src/tasks/CreateMergeRequestTask.js +126 -118
  96. package/lib/src/tasks/CreateNotesAuditTask.js +140 -140
  97. package/lib/src/tasks/CreateReleaseNotesTask.js +144 -144
  98. package/lib/src/tasks/InstallPackagesTask.js +409 -415
  99. package/lib/src/tasks/ListProjectsTask.js +87 -87
  100. package/lib/src/tasks/PopulateTrelloBoardTask.js +140 -140
  101. package/lib/src/tasks/StartMonorepoBuildTask.js +109 -109
  102. package/lib/src/tasks/StartProjectBuildsTask.js +164 -170
  103. package/lib/src/tasks/TagMasterTask.js +196 -189
  104. package/lib/src/tasks/ValidateCommitsTask.js +210 -143
  105. package/lib/src/tasks/ValidateDescriptionsTask.js +178 -178
  106. package/lib/src/tasks/ValidateMergeRequestTask.js +115 -115
  107. package/lib/src/tasks/ValidateTask.js +419 -419
  108. package/lib/src/tasks/VersionTask.js +98 -98
  109. package/lib/src/tasks/configure-projects/AddApprovalRulesTask.js +199 -182
  110. package/lib/src/tasks/configure-projects/CreateToolingBranchTask.js +138 -136
  111. package/lib/src/tasks/configure-projects/EnsureApprovalRulesTask.js +106 -106
  112. package/lib/src/tasks/configure-projects/index.js +18 -18
  113. package/lib/src/tasks/configure-projects/primitives/BaseConfigureProjectsTask.js +98 -98
  114. package/lib/src/tasks/index.js +34 -34
  115. package/lib/src/types.js +60 -60
  116. package/lib/src/utils/BranchNames.js +11 -11
  117. package/lib/src/utils/Commits.js +82 -55
  118. package/lib/src/utils/Csv.js +54 -54
  119. package/lib/src/utils/Descriptions.js +274 -274
  120. package/lib/src/utils/GroupIds.js +6 -6
  121. package/lib/src/utils/Logger.js +62 -62
  122. package/lib/src/utils/ProjectIds.js +48 -47
  123. package/lib/src/utils/Versions.js +20 -20
  124. package/lib/src/utils/index.js +22 -22
  125. package/package.json +50 -50
@@ -1,544 +1,553 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17
- return c > 3 && r && Object.defineProperty(target, key, r), r;
18
- };
19
- var __metadata = (this && this.__metadata) || function (k, v) {
20
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
21
- };
22
- var __param = (this && this.__param) || function (paramIndex, decorator) {
23
- return function (target, key) { decorator(target, key, paramIndex); }
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (_) try {
41
- 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;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
- var __importDefault = (this && this.__importDefault) || function (mod) {
62
- return (mod && mod.__esModule) ? mod : { "default": mod };
63
- };
64
- Object.defineProperty(exports, "__esModule", { value: true });
65
- exports.GitLabService = void 0;
66
- require("reflect-metadata");
67
- var inversify_1 = require("inversify");
68
- var lodash_1 = __importDefault(require("lodash"));
69
- var semver_1 = __importDefault(require("semver"));
70
- var types_1 = require("../types");
71
- var api_clients_1 = require("../api-clients");
72
- var errors_1 = require("../errors");
73
- var utils_1 = require("../utils");
74
- var ProjectConfig_1 = require("../interfaces/models/ProjectConfig");
75
- /**
76
- * These are projects that will try to use tags to pull in extra projects
77
- */
78
- var MONOREPO_IDS = [
79
- utils_1.ProjectIds.MONOREPO_PROJECT_ID
80
- ];
81
- var GitLabService = /** @class */ (function () {
82
- function GitLabService(apiClient) {
83
- this.projectConfigCache = {};
84
- this.apiClient = apiClient;
85
- }
86
- GitLabService.prototype.getProjects = function (options, _a) {
87
- var _b, _c;
88
- var _d = _a === void 0 ? {} : _a, _e = _d.invalidateCache, invalidateCache = _e === void 0 ? false : _e;
89
- return __awaiter(this, void 0, void 0, function () {
90
- var allMergeRequests, mergeRequests, projectIds, projects, mappedProjects, _i, mappedProjects_1, mappedProject, extraProjects;
91
- return __generator(this, function (_f) {
92
- switch (_f.label) {
93
- case 0:
94
- // If invalidateCache is false and projects are cached, return the cached projects
95
- if (!invalidateCache && this.projects)
96
- return [2 /*return*/, this.projects];
97
- return [4 /*yield*/, this.apiClient.getMergeRequests(options)];
98
- case 1:
99
- allMergeRequests = _f.sent();
100
- mergeRequests = allMergeRequests.filter(function (mr) { return mr.targetBranch !== 'master'; });
101
- if (!(mergeRequests === null || mergeRequests === void 0 ? void 0 : mergeRequests.length))
102
- throw new errors_1.ShipItError('Could not find any merge requests for the specified options');
103
- projectIds = lodash_1.default.uniqBy(mergeRequests, function (m) { return m.projectId; }).map(function (m) { return m.projectId; });
104
- return [4 /*yield*/, this.apiClient.getProjectsByIds(projectIds)];
105
- case 2:
106
- projects = _f.sent();
107
- if (!(projects === null || projects === void 0 ? void 0 : projects.length) || projects.length !== projectIds.length)
108
- throw new errors_1.ShipItError('Could not find all projects for the found merge requests');
109
- mappedProjects = projects.map(function (p) { return (__assign(__assign({}, p), { mergeRequests: mergeRequests.filter(function (mr) { return mr.projectId === p.id; }) })); });
110
- _i = 0, mappedProjects_1 = mappedProjects;
111
- _f.label = 3;
112
- case 3:
113
- if (!(_i < mappedProjects_1.length)) return [3 /*break*/, 6];
114
- mappedProject = mappedProjects_1[_i];
115
- return [4 /*yield*/, this.getExtraProjects(mappedProject, projectIds)];
116
- case 4:
117
- extraProjects = _f.sent();
118
- Array.prototype.push.apply(mappedProjects, extraProjects.map(function (p) { return (__assign(__assign({}, p), { mergeRequests: [] })); }));
119
- _f.label = 5;
120
- case 5:
121
- _i++;
122
- return [3 /*break*/, 3];
123
- case 6:
124
- /**
125
- * We need to filter our relevant projects after we've added extra projects via tags
126
- */
127
- if ((_b = options.projectIds) === null || _b === void 0 ? void 0 : _b.length) {
128
- mappedProjects = mappedProjects.filter(function (p) {
129
- var _a;
130
- return (_a = options.projectIds) === null || _a === void 0 ? void 0 : _a.some(function (pid) { return p.id.toString() === pid; });
131
- });
132
- }
133
- if ((_c = options.omittedProjectIds) === null || _c === void 0 ? void 0 : _c.length) {
134
- mappedProjects = mappedProjects.filter(function (p) {
135
- var _a;
136
- return !((_a = options.omittedProjectIds) === null || _a === void 0 ? void 0 : _a.some(function (pid) { return p.id.toString() === pid; }));
137
- });
138
- }
139
- return [2 /*return*/, (this.projects = mappedProjects)];
140
- }
141
- });
142
- });
143
- };
144
- GitLabService.prototype.getAllProjects = function () {
145
- return this.apiClient.getAllProjects();
146
- };
147
- GitLabService.prototype.getAllGroupProjects = function (groupId) {
148
- return __awaiter(this, void 0, void 0, function () {
149
- return __generator(this, function (_a) {
150
- return [2 /*return*/, this.apiClient.getAllGroupProjects(groupId)];
151
- });
152
- });
153
- };
154
- /**
155
- * Fetches any projects that are not already included via MRs by checking the project labels.
156
- * We only do this for a whitelist of projects
157
- */
158
- GitLabService.prototype.getExtraProjects = function (project, existingProjectIds) {
159
- return __awaiter(this, void 0, void 0, function () {
160
- var labels, mrLabelNames, mrLabels, projectIds, extraProjectIds;
161
- return __generator(this, function (_a) {
162
- switch (_a.label) {
163
- case 0:
164
- if (!MONOREPO_IDS.includes(project.id.toString()))
165
- return [2 /*return*/, []];
166
- return [4 /*yield*/, this.getProjectLabels(project.id)];
167
- case 1:
168
- labels = _a.sent();
169
- mrLabelNames = lodash_1.default.chain(project.mergeRequests)
170
- .map(function (m) { return m.labels; })
171
- .flatten()
172
- .uniq()
173
- .value();
174
- mrLabels = lodash_1.default.intersection(labels.map(function (l) { return l.name; }), mrLabelNames);
175
- projectIds = mrLabels.map(function (m) { var _a; return (_a = labels.find(function (l) { return l.name === m; })) === null || _a === void 0 ? void 0 : _a.description; });
176
- extraProjectIds = lodash_1.default.difference(projectIds, existingProjectIds.map(function (p) { return p.toString(); }));
177
- return [4 /*yield*/, this.apiClient.getProjectsByIds(extraProjectIds)];
178
- case 2:
179
- // fetches and returns the extra projects
180
- return [2 /*return*/, _a.sent()];
181
- }
182
- });
183
- });
184
- };
185
- /**
186
- * Gets labels that have a projectId as their description
187
- */
188
- GitLabService.prototype.getProjectLabels = function (projectId) {
189
- return __awaiter(this, void 0, void 0, function () {
190
- var labels;
191
- return __generator(this, function (_a) {
192
- switch (_a.label) {
193
- case 0: return [4 /*yield*/, this.apiClient.getLabels(projectId)];
194
- case 1:
195
- labels = _a.sent();
196
- return [2 /*return*/, labels.filter(function (l) {
197
- if (!l.description)
198
- return false;
199
- return !isNaN(+l.description);
200
- })];
201
- }
202
- });
203
- });
204
- };
205
- GitLabService.prototype.getProject = function (projectId) {
206
- return __awaiter(this, void 0, void 0, function () {
207
- var projects;
208
- return __generator(this, function (_a) {
209
- switch (_a.label) {
210
- case 0: return [4 /*yield*/, this.apiClient.getProjectsByIds([projectId])];
211
- case 1:
212
- projects = _a.sent();
213
- if (!projects.length)
214
- throw new errors_1.ShipItError("Could not find project projectId: ".concat(projectId));
215
- return [2 /*return*/, projects[0]];
216
- }
217
- });
218
- });
219
- };
220
- GitLabService.prototype.getCommitsForRelease = function (projectId, dateSince) {
221
- return __awaiter(this, void 0, void 0, function () {
222
- return __generator(this, function (_a) {
223
- switch (_a.label) {
224
- case 0: return [4 /*yield*/, this.apiClient.getCommitsForRelease(projectId, dateSince)];
225
- case 1: return [2 /*return*/, _a.sent()];
226
- }
227
- });
228
- });
229
- };
230
- GitLabService.prototype.getBranch = function (projectId, branchName) {
231
- return __awaiter(this, void 0, void 0, function () {
232
- return __generator(this, function (_a) {
233
- switch (_a.label) {
234
- case 0: return [4 /*yield*/, this.apiClient.getBranch(projectId, branchName)];
235
- case 1: return [2 /*return*/, _a.sent()];
236
- }
237
- });
238
- });
239
- };
240
- GitLabService.prototype.createBranch = function (project, branchFrom, branchName) {
241
- return __awaiter(this, void 0, void 0, function () {
242
- return __generator(this, function (_a) {
243
- switch (_a.label) {
244
- case 0: return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, branchFrom)];
245
- case 1: return [2 /*return*/, _a.sent()];
246
- }
247
- });
248
- });
249
- };
250
- GitLabService.prototype._createBranch = function (project, branchFrom, branchName, cherryPick) {
251
- return __awaiter(this, void 0, void 0, function () {
252
- var newBranch, i, mr;
253
- return __generator(this, function (_a) {
254
- switch (_a.label) {
255
- case 0:
256
- if (!cherryPick) return [3 /*break*/, 6];
257
- return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, 'master')];
258
- case 1:
259
- newBranch = _a.sent();
260
- i = 0;
261
- _a.label = 2;
262
- case 2:
263
- if (!(i < project.mergeRequests.length)) return [3 /*break*/, 5];
264
- mr = project.mergeRequests[i];
265
- if (!mr.mergeCommit)
266
- throw new errors_1.ShipItError("Could not find the merge commit sha for MR: ".concat(mr.title, " in projectId: ").concat(project.id));
267
- return [4 /*yield*/, this.apiClient.cherryPick(project.id, mr.mergeCommit, branchName)];
268
- case 3:
269
- _a.sent();
270
- _a.label = 4;
271
- case 4:
272
- i++;
273
- return [3 /*break*/, 2];
274
- case 5: return [2 /*return*/, newBranch];
275
- case 6: return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, branchFrom)];
276
- case 7: return [2 /*return*/, _a.sent()];
277
- }
278
- });
279
- });
280
- };
281
- GitLabService.prototype.getMergeRequest = function (projectId, branchName, state) {
282
- return this.apiClient.getMergeRequest(projectId, branchName, 'master', state);
283
- };
284
- GitLabService.prototype.getMergeRequestById = function (projectId, mergeRequestId) {
285
- if (isNaN(+mergeRequestId))
286
- throw new errors_1.ShipItError("MergeRequest Id must be a number: ".concat(mergeRequestId));
287
- return this.apiClient.getMergeRequestById(projectId, +mergeRequestId);
288
- };
289
- GitLabService.prototype.createMergeRequest = function (projectId, branchName, targetBranch, title, description, milestoneName) {
290
- return __awaiter(this, void 0, void 0, function () {
291
- var milestone;
292
- return __generator(this, function (_a) {
293
- switch (_a.label) {
294
- case 0: return [4 /*yield*/, this.getMilestone(milestoneName, projectId)];
295
- case 1:
296
- milestone = _a.sent();
297
- return [2 /*return*/, this.apiClient.createMergeRequest(projectId, branchName, targetBranch, title, description, milestone)];
298
- }
299
- });
300
- });
301
- };
302
- GitLabService.prototype.AcceptMergeRequest = function (projectId, mergerequestIId) {
303
- return this.apiClient.AcceptMergeRequest(projectId, mergerequestIId);
304
- };
305
- GitLabService.prototype.getMilestone = function (milestoneName, projectId) {
306
- return this.apiClient.getMilestone(milestoneName, projectId);
307
- };
308
- GitLabService.prototype.getReleaseBranchNameMilestone = function (projectId, milestone) {
309
- return __awaiter(this, void 0, void 0, function () {
310
- var releaseMR;
311
- return __generator(this, function (_a) {
312
- switch (_a.label) {
313
- case 0: return [4 /*yield*/, this.apiClient.getReleaseMergeRequest(projectId, milestone)];
314
- case 1:
315
- releaseMR = _a.sent();
316
- if (!releaseMR)
317
- return [2 /*return*/, null];
318
- return [2 /*return*/, releaseMR.sourceBranch];
319
- }
320
- });
321
- });
322
- };
323
- GitLabService.prototype.getMilestoneByBranchName = function (projectId, branchName) {
324
- var _a, _b;
325
- return __awaiter(this, void 0, void 0, function () {
326
- var mergeRequest;
327
- return __generator(this, function (_c) {
328
- switch (_c.label) {
329
- case 0: return [4 /*yield*/, this.getMergeRequest(projectId, branchName, 'opened')];
330
- case 1:
331
- mergeRequest = _c.sent();
332
- if (!mergeRequest)
333
- throw new errors_1.ShipItError("No merge request in project \"monorepo\" with branch name \"".concat(branchName, "\" could be found."));
334
- if (!mergeRequest.milestone)
335
- throw new errors_1.ShipItError("No milestone could be found on merge request \"".concat(mergeRequest.title, "\". Please attach a milestone then try again: ").concat(mergeRequest.url));
336
- return [2 /*return*/, (_b = (_a = mergeRequest.milestone) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : ''];
337
- }
338
- });
339
- });
340
- };
341
- GitLabService.prototype.bumpVersionTxt = function (projectId, branchName, newVersion) {
342
- return __awaiter(this, void 0, void 0, function () {
343
- var versionTxt, buff, text;
344
- return __generator(this, function (_a) {
345
- switch (_a.label) {
346
- case 0: return [4 /*yield*/, this.apiClient.getFile(projectId, 'version.txt', branchName)];
347
- case 1:
348
- versionTxt = _a.sent();
349
- if (!versionTxt)
350
- return [2 /*return*/, false];
351
- buff = Buffer.from(versionTxt.content, 'base64');
352
- text = buff.toString('utf-8');
353
- if (text === newVersion)
354
- return [2 /*return*/, false];
355
- return [4 /*yield*/, this.apiClient.updateFile(projectId, 'version.txt', branchName, newVersion, 'Bumping version.txt')];
356
- case 2:
357
- _a.sent();
358
- return [2 /*return*/, true];
359
- }
360
- });
361
- });
362
- };
363
- GitLabService.prototype.getFile = function (projectId, branchName, file) {
364
- return __awaiter(this, void 0, void 0, function () {
365
- return __generator(this, function (_a) {
366
- switch (_a.label) {
367
- case 0: return [4 /*yield*/, this.apiClient.getFile(projectId, file, branchName)];
368
- case 1: return [2 /*return*/, _a.sent()];
369
- }
370
- });
371
- });
372
- };
373
- GitLabService.prototype.updateFile = function (projectId, filePath, branchName, content, commit) {
374
- return this.apiClient.updateFile(projectId, filePath, branchName, content, commit);
375
- };
376
- GitLabService.prototype.commit = function (projectId, branchName, commitMessage, files) {
377
- return __awaiter(this, void 0, void 0, function () {
378
- return __generator(this, function (_a) {
379
- switch (_a.label) {
380
- case 0: return [4 /*yield*/, this.apiClient.commit(projectId, branchName, files, commitMessage)];
381
- case 1:
382
- _a.sent();
383
- return [2 /*return*/];
384
- }
385
- });
386
- });
387
- };
388
- GitLabService.prototype.getTag = function (projectId, tagName) {
389
- return __awaiter(this, void 0, void 0, function () {
390
- return __generator(this, function (_a) {
391
- switch (_a.label) {
392
- case 0: return [4 /*yield*/, this.apiClient.getTag(projectId, tagName)];
393
- case 1: return [2 /*return*/, _a.sent()];
394
- }
395
- });
396
- });
397
- };
398
- GitLabService.prototype.createTag = function (projectId, tagName, sha) {
399
- return __awaiter(this, void 0, void 0, function () {
400
- return __generator(this, function (_a) {
401
- switch (_a.label) {
402
- case 0: return [4 /*yield*/, this.apiClient.createTag(projectId, tagName, sha)];
403
- case 1: return [2 /*return*/, _a.sent()];
404
- }
405
- });
406
- });
407
- };
408
- GitLabService.prototype.createRelease = function (projectId, release) {
409
- return __awaiter(this, void 0, void 0, function () {
410
- return __generator(this, function (_a) {
411
- switch (_a.label) {
412
- case 0: return [4 /*yield*/, this.apiClient.createRelease(projectId, release)];
413
- case 1: return [2 /*return*/, _a.sent()];
414
- }
415
- });
416
- });
417
- };
418
- /**
419
- * We use the latest valid semver tag in master to determine what the current version
420
- * of a project is
421
- */
422
- GitLabService.prototype.getCurrentMinorVersionTag = function (projectId) {
423
- return __awaiter(this, void 0, void 0, function () {
424
- var tags, tagNames, sortedTagNames, latestMinorVersion, currentTag;
425
- return __generator(this, function (_a) {
426
- switch (_a.label) {
427
- case 0: return [4 /*yield*/, this.apiClient.getLatestTags(projectId)];
428
- case 1:
429
- tags = _a.sent();
430
- if (!(tags === null || tags === void 0 ? void 0 : tags.length))
431
- throw new errors_1.ShipItError("Project ".concat(projectId, " does not have any tags"));
432
- tagNames = tags.map(function (t) { return t.name; }).filter(function (t) { return semver_1.default.valid(t); });
433
- if (!tagNames.length)
434
- return [2 /*return*/, null];
435
- sortedTagNames = semver_1.default.sort(tagNames).reverse();
436
- latestMinorVersion = sortedTagNames.find(function (tag) { return tag.endsWith('.0'); });
437
- currentTag = tags.find(function (t) { return t.name === latestMinorVersion; });
438
- if (!currentTag)
439
- return [2 /*return*/, null];
440
- return [2 /*return*/, currentTag];
441
- }
442
- });
443
- });
444
- };
445
- GitLabService.prototype.hasSomeTags = function (projectId) {
446
- return __awaiter(this, void 0, void 0, function () {
447
- var tags;
448
- return __generator(this, function (_a) {
449
- switch (_a.label) {
450
- case 0: return [4 /*yield*/, this.apiClient.getLatestTags(projectId)];
451
- case 1:
452
- tags = _a.sent();
453
- return [2 /*return*/, !!(tags === null || tags === void 0 ? void 0 : tags.length)];
454
- }
455
- });
456
- });
457
- };
458
- GitLabService.prototype.getNextBranchInfo = function (projectId, version) {
459
- return __awaiter(this, void 0, void 0, function () {
460
- var tag, nextVersion, branchName, branch;
461
- return __generator(this, function (_a) {
462
- switch (_a.label) {
463
- case 0: return [4 /*yield*/, this.getCurrentMinorVersionTag(projectId)];
464
- case 1:
465
- tag = _a.sent();
466
- if (!tag)
467
- throw new errors_1.ShipItError("Could not find the latest semver tag for projectId: ".concat(projectId));
468
- nextVersion = utils_1.Versions.getNextVersion(tag.name, version);
469
- branchName = version === 'patch' ?
470
- utils_1.BranchNames.getHotfix(nextVersion) :
471
- utils_1.BranchNames.getRelease(nextVersion);
472
- return [4 /*yield*/, this.getBranch(projectId, branchName)];
473
- case 2:
474
- branch = _a.sent();
475
- return [2 /*return*/, {
476
- nextVersion: nextVersion,
477
- branchName: branchName,
478
- branch: branch,
479
- tag: tag
480
- }];
481
- }
482
- });
483
- });
484
- };
485
- GitLabService.prototype.getProjectConfig = function (projectId, branchName) {
486
- return __awaiter(this, void 0, void 0, function () {
487
- var cacheKey, configFileName, configFile, buff, text, json, projectConfig;
488
- return __generator(this, function (_a) {
489
- switch (_a.label) {
490
- case 0:
491
- cacheKey = projectId + ':' + branchName;
492
- if (this.projectConfigCache[cacheKey])
493
- return [2 /*return*/, this.projectConfigCache[cacheKey]];
494
- configFileName = '.ship-it.config';
495
- return [4 /*yield*/, this.apiClient.getFile(projectId, configFileName, branchName)];
496
- case 1:
497
- configFile = _a.sent();
498
- if (!configFile)
499
- return [2 /*return*/, ProjectConfig_1.DefaultProjectConfig];
500
- try {
501
- buff = Buffer.from(configFile.content, 'base64');
502
- text = buff.toString('utf-8');
503
- json = JSON.parse(text);
504
- projectConfig = lodash_1.default.merge({}, ProjectConfig_1.DefaultProjectConfig, json);
505
- this.projectConfigCache[cacheKey] = projectConfig;
506
- return [2 /*return*/, projectConfig];
507
- }
508
- catch (_b) {
509
- throw new errors_1.ShipItError("Failed to parse ".concat(configFileName));
510
- }
511
- return [2 /*return*/];
512
- }
513
- });
514
- });
515
- };
516
- GitLabService.prototype.updateMilestone = function (projectId, mergeRequestID, newMilestoneId) {
517
- return this.apiClient.updateMilestone(projectId, mergeRequestID, newMilestoneId);
518
- };
519
- GitLabService.prototype.getApprovalRuleConfiguration = function (projectId) {
520
- return this.apiClient.getApprovalRuleConfiguration(projectId);
521
- };
522
- GitLabService.prototype.updateApprovalRuleConfiguration = function (projectId, configuration) {
523
- return this.apiClient.updateApprovalRuleConfiguration(projectId, configuration);
524
- };
525
- GitLabService.prototype.getApprovalRules = function (projectId) {
526
- return this.apiClient.getApprovalRules(projectId);
527
- };
528
- GitLabService.prototype.createApprovalRule = function (projectId, rule) {
529
- return this.apiClient.createApprovalRule(projectId, rule);
530
- };
531
- GitLabService.prototype.updateApprovalRule = function (projectId, approvalRuleId, rule) {
532
- return this.apiClient.updateApprovalRule(projectId, approvalRuleId, rule);
533
- };
534
- GitLabService.prototype.getProtectedBranches = function (projectId) {
535
- return this.apiClient.getProtectedBranches(projectId);
536
- };
537
- GitLabService = __decorate([
538
- (0, inversify_1.injectable)(),
539
- __param(0, (0, inversify_1.inject)(types_1.API_CLIENTS.GitLabApiClient)),
540
- __metadata("design:paramtypes", [api_clients_1.GitLabApiClient])
541
- ], GitLabService);
542
- return GitLabService;
543
- }());
544
- exports.GitLabService = GitLabService;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
14
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
15
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
16
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
17
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
18
+ };
19
+ var __metadata = (this && this.__metadata) || function (k, v) {
20
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
21
+ };
22
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
23
+ return function (target, key) { decorator(target, key, paramIndex); }
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __generator = (this && this.__generator) || function (thisArg, body) {
35
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
+ function verb(n) { return function (v) { return step([n, v]); }; }
38
+ function step(op) {
39
+ if (f) throw new TypeError("Generator is already executing.");
40
+ while (_) try {
41
+ 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;
42
+ if (y = 0, t) op = [op[0] & 2, t.value];
43
+ switch (op[0]) {
44
+ case 0: case 1: t = op; break;
45
+ case 4: _.label++; return { value: op[1], done: false };
46
+ case 5: _.label++; y = op[1]; op = [0]; continue;
47
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
+ default:
49
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
+ if (t[2]) _.ops.pop();
54
+ _.trys.pop(); continue;
55
+ }
56
+ op = body.call(thisArg, _);
57
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
+ }
60
+ };
61
+ var __importDefault = (this && this.__importDefault) || function (mod) {
62
+ return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ exports.GitLabService = void 0;
66
+ require("reflect-metadata");
67
+ var inversify_1 = require("inversify");
68
+ var lodash_1 = __importDefault(require("lodash"));
69
+ var semver_1 = __importDefault(require("semver"));
70
+ var types_1 = require("../types");
71
+ var api_clients_1 = require("../api-clients");
72
+ var errors_1 = require("../errors");
73
+ var utils_1 = require("../utils");
74
+ var ProjectConfig_1 = require("../interfaces/models/ProjectConfig");
75
+ /**
76
+ * These are projects that will try to use tags to pull in extra projects
77
+ */
78
+ var MONOREPO_IDS = [
79
+ utils_1.ProjectIds.MONOREPO_PROJECT_ID
80
+ ];
81
+ var GitLabService = /** @class */ (function () {
82
+ function GitLabService(apiClient) {
83
+ this.projectConfigCache = {};
84
+ this.apiClient = apiClient;
85
+ }
86
+ GitLabService.prototype.getProjects = function (milestone, projectIds, omittedProjectIds, _a) {
87
+ if (projectIds === void 0) { projectIds = []; }
88
+ if (omittedProjectIds === void 0) { omittedProjectIds = []; }
89
+ var _b = _a === void 0 ? {} : _a, _c = _b.invalidateCache, invalidateCache = _c === void 0 ? false : _c;
90
+ return __awaiter(this, void 0, void 0, function () {
91
+ var allMergeRequests, mergeRequests, releaseProjectIds, projects, mappedProjects, _i, mappedProjects_1, mappedProject, extraProjects;
92
+ return __generator(this, function (_d) {
93
+ switch (_d.label) {
94
+ case 0:
95
+ // If invalidateCache is false and projects are cached, return the cached projects
96
+ if (!invalidateCache && this.projects)
97
+ return [2 /*return*/, this.projects];
98
+ return [4 /*yield*/, this.apiClient.getMergeRequests(milestone, projectIds)];
99
+ case 1:
100
+ allMergeRequests = _d.sent();
101
+ mergeRequests = allMergeRequests.filter(function (mr) { return mr.targetBranch !== 'master'; });
102
+ if (!(mergeRequests === null || mergeRequests === void 0 ? void 0 : mergeRequests.length))
103
+ throw new errors_1.ShipItError('Could not find any merge requests for the specified options');
104
+ releaseProjectIds = lodash_1.default.uniqBy(mergeRequests, function (m) { return m.projectId; }).map(function (m) { return m.projectId; });
105
+ return [4 /*yield*/, this.apiClient.getProjectsByIds(releaseProjectIds)];
106
+ case 2:
107
+ projects = _d.sent();
108
+ if (!(projects === null || projects === void 0 ? void 0 : projects.length) || projects.length !== releaseProjectIds.length)
109
+ throw new errors_1.ShipItError('Could not find all projects for the found merge requests');
110
+ mappedProjects = projects.map(function (p) { return (__assign(__assign({}, p), { mergeRequests: mergeRequests.filter(function (mr) { return mr.projectId === p.id; }) })); });
111
+ _i = 0, mappedProjects_1 = mappedProjects;
112
+ _d.label = 3;
113
+ case 3:
114
+ if (!(_i < mappedProjects_1.length)) return [3 /*break*/, 6];
115
+ mappedProject = mappedProjects_1[_i];
116
+ return [4 /*yield*/, this.getExtraProjects(mappedProject, releaseProjectIds)];
117
+ case 4:
118
+ extraProjects = _d.sent();
119
+ Array.prototype.push.apply(mappedProjects, extraProjects.map(function (p) { return (__assign(__assign({}, p), { mergeRequests: [] })); }));
120
+ _d.label = 5;
121
+ case 5:
122
+ _i++;
123
+ return [3 /*break*/, 3];
124
+ case 6:
125
+ /**
126
+ * We need to filter our relevant projects after we've added extra projects via tags
127
+ */
128
+ if (projectIds === null || projectIds === void 0 ? void 0 : projectIds.length) {
129
+ mappedProjects = mappedProjects.filter(function (p) {
130
+ return projectIds === null || projectIds === void 0 ? void 0 : projectIds.some(function (pid) { return p.id.toString() === pid; });
131
+ });
132
+ }
133
+ if (omittedProjectIds === null || omittedProjectIds === void 0 ? void 0 : omittedProjectIds.length) {
134
+ mappedProjects = mappedProjects.filter(function (p) {
135
+ return !(omittedProjectIds === null || omittedProjectIds === void 0 ? void 0 : omittedProjectIds.some(function (pid) { return p.id.toString() === pid; }));
136
+ });
137
+ }
138
+ return [2 /*return*/, (this.projects = mappedProjects)];
139
+ }
140
+ });
141
+ });
142
+ };
143
+ GitLabService.prototype.getAllProjects = function () {
144
+ return this.apiClient.getAllProjects();
145
+ };
146
+ GitLabService.prototype.getAllGroupProjects = function (groupId) {
147
+ return __awaiter(this, void 0, void 0, function () {
148
+ return __generator(this, function (_a) {
149
+ return [2 /*return*/, this.apiClient.getAllGroupProjects(groupId)];
150
+ });
151
+ });
152
+ };
153
+ /**
154
+ * Fetches any projects that are not already included via MRs by checking the project labels.
155
+ * We only do this for a whitelist of projects
156
+ */
157
+ GitLabService.prototype.getExtraProjects = function (project, existingProjectIds) {
158
+ return __awaiter(this, void 0, void 0, function () {
159
+ var labels, mrLabelNames, mrLabels, projectIds, extraProjectIds;
160
+ return __generator(this, function (_a) {
161
+ switch (_a.label) {
162
+ case 0:
163
+ if (!MONOREPO_IDS.includes(project.id.toString()))
164
+ return [2 /*return*/, []];
165
+ return [4 /*yield*/, this.getProjectLabels(project.id)];
166
+ case 1:
167
+ labels = _a.sent();
168
+ mrLabelNames = lodash_1.default.chain(project.mergeRequests)
169
+ .map(function (m) { return m.labels; })
170
+ .flatten()
171
+ .uniq()
172
+ .value();
173
+ mrLabels = lodash_1.default.intersection(labels.map(function (l) { return l.name; }), mrLabelNames);
174
+ projectIds = mrLabels.map(function (m) { var _a; return (_a = labels.find(function (l) { return l.name === m; })) === null || _a === void 0 ? void 0 : _a.description; });
175
+ extraProjectIds = lodash_1.default.difference(projectIds, existingProjectIds.map(function (p) { return p.toString(); }));
176
+ return [4 /*yield*/, this.apiClient.getProjectsByIds(extraProjectIds)];
177
+ case 2:
178
+ // fetches and returns the extra projects
179
+ return [2 /*return*/, _a.sent()];
180
+ }
181
+ });
182
+ });
183
+ };
184
+ /**
185
+ * Gets labels that have a projectId as their description
186
+ */
187
+ GitLabService.prototype.getProjectLabels = function (projectId) {
188
+ return __awaiter(this, void 0, void 0, function () {
189
+ var labels;
190
+ return __generator(this, function (_a) {
191
+ switch (_a.label) {
192
+ case 0: return [4 /*yield*/, this.apiClient.getLabels(projectId)];
193
+ case 1:
194
+ labels = _a.sent();
195
+ return [2 /*return*/, labels.filter(function (l) {
196
+ if (!l.description)
197
+ return false;
198
+ return !isNaN(+l.description);
199
+ })];
200
+ }
201
+ });
202
+ });
203
+ };
204
+ GitLabService.prototype.getProject = function (projectId) {
205
+ return __awaiter(this, void 0, void 0, function () {
206
+ var projects;
207
+ return __generator(this, function (_a) {
208
+ switch (_a.label) {
209
+ case 0: return [4 /*yield*/, this.apiClient.getProjectsByIds([projectId])];
210
+ case 1:
211
+ projects = _a.sent();
212
+ if (!projects.length)
213
+ throw new errors_1.ShipItError("Could not find project projectId: ".concat(projectId));
214
+ return [2 /*return*/, projects[0]];
215
+ }
216
+ });
217
+ });
218
+ };
219
+ GitLabService.prototype.getCommitsForBranch = function (branch, projectId, dateSince) {
220
+ return __awaiter(this, void 0, void 0, function () {
221
+ return __generator(this, function (_a) {
222
+ switch (_a.label) {
223
+ case 0: return [4 /*yield*/, this.apiClient.getCommitsForBranch(branch, projectId, dateSince)];
224
+ case 1: return [2 /*return*/, _a.sent()];
225
+ }
226
+ });
227
+ });
228
+ };
229
+ GitLabService.prototype.getBranch = function (projectId, branchName) {
230
+ return __awaiter(this, void 0, void 0, function () {
231
+ return __generator(this, function (_a) {
232
+ switch (_a.label) {
233
+ case 0: return [4 /*yield*/, this.apiClient.getBranch(projectId, branchName)];
234
+ case 1: return [2 /*return*/, _a.sent()];
235
+ }
236
+ });
237
+ });
238
+ };
239
+ GitLabService.prototype.createBranch = function (project, branchFrom, branchName) {
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ return __generator(this, function (_a) {
242
+ switch (_a.label) {
243
+ case 0: return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, branchFrom)];
244
+ case 1: return [2 /*return*/, _a.sent()];
245
+ }
246
+ });
247
+ });
248
+ };
249
+ GitLabService.prototype._createBranch = function (project, branchFrom, branchName, cherryPick) {
250
+ return __awaiter(this, void 0, void 0, function () {
251
+ var newBranch, i, mr;
252
+ return __generator(this, function (_a) {
253
+ switch (_a.label) {
254
+ case 0:
255
+ if (!cherryPick) return [3 /*break*/, 6];
256
+ return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, 'master')];
257
+ case 1:
258
+ newBranch = _a.sent();
259
+ i = 0;
260
+ _a.label = 2;
261
+ case 2:
262
+ if (!(i < project.mergeRequests.length)) return [3 /*break*/, 5];
263
+ mr = project.mergeRequests[i];
264
+ if (!mr.mergeCommit)
265
+ throw new errors_1.ShipItError("Could not find the merge commit sha for MR: ".concat(mr.title, " in projectId: ").concat(project.id));
266
+ return [4 /*yield*/, this.apiClient.cherryPick(project.id, mr.mergeCommit, branchName)];
267
+ case 3:
268
+ _a.sent();
269
+ _a.label = 4;
270
+ case 4:
271
+ i++;
272
+ return [3 /*break*/, 2];
273
+ case 5: return [2 /*return*/, newBranch];
274
+ case 6: return [4 /*yield*/, this.apiClient.createBranch(project.id, branchName, branchFrom)];
275
+ case 7: return [2 /*return*/, _a.sent()];
276
+ }
277
+ });
278
+ });
279
+ };
280
+ GitLabService.prototype.getMergeRequest = function (projectId, branchName, state) {
281
+ return this.apiClient.getMergeRequest(projectId, branchName, 'master', state);
282
+ };
283
+ GitLabService.prototype.getMergeRequestById = function (projectId, mergeRequestId) {
284
+ if (isNaN(+mergeRequestId))
285
+ throw new errors_1.ShipItError("MergeRequest Id must be a number: ".concat(mergeRequestId));
286
+ return this.apiClient.getMergeRequestById(projectId, +mergeRequestId);
287
+ };
288
+ GitLabService.prototype.createMergeRequest = function (options) {
289
+ return __awaiter(this, void 0, void 0, function () {
290
+ var projectId, sourceBranch, targetBranch, title, description, milestoneName, removeSourceBranch, milestone;
291
+ return __generator(this, function (_a) {
292
+ switch (_a.label) {
293
+ case 0:
294
+ projectId = options.projectId, sourceBranch = options.sourceBranch, targetBranch = options.targetBranch, title = options.title, description = options.description, milestoneName = options.milestoneName, removeSourceBranch = options.removeSourceBranch;
295
+ return [4 /*yield*/, this.getMilestone(milestoneName, projectId)];
296
+ case 1:
297
+ milestone = _a.sent();
298
+ return [2 /*return*/, this.apiClient.createMergeRequest({
299
+ projectId: projectId,
300
+ sourceBranch: sourceBranch,
301
+ targetBranch: targetBranch,
302
+ title: title,
303
+ description: description,
304
+ milestone: milestone,
305
+ removeSourceBranch: removeSourceBranch
306
+ })];
307
+ }
308
+ });
309
+ });
310
+ };
311
+ GitLabService.prototype.AcceptMergeRequest = function (projectId, mergerequestIId) {
312
+ return this.apiClient.AcceptMergeRequest(projectId, mergerequestIId);
313
+ };
314
+ GitLabService.prototype.getMilestone = function (milestoneName, projectId) {
315
+ return this.apiClient.getMilestone(milestoneName, projectId);
316
+ };
317
+ GitLabService.prototype.getReleaseBranchNameMilestone = function (projectId, milestone) {
318
+ return __awaiter(this, void 0, void 0, function () {
319
+ var releaseMR;
320
+ return __generator(this, function (_a) {
321
+ switch (_a.label) {
322
+ case 0: return [4 /*yield*/, this.apiClient.getReleaseMergeRequest(projectId, milestone)];
323
+ case 1:
324
+ releaseMR = _a.sent();
325
+ if (!releaseMR)
326
+ return [2 /*return*/, null];
327
+ return [2 /*return*/, releaseMR.sourceBranch];
328
+ }
329
+ });
330
+ });
331
+ };
332
+ GitLabService.prototype.getMilestoneByBranchName = function (projectId, branchName) {
333
+ var _a, _b;
334
+ return __awaiter(this, void 0, void 0, function () {
335
+ var mergeRequest;
336
+ return __generator(this, function (_c) {
337
+ switch (_c.label) {
338
+ case 0: return [4 /*yield*/, this.getMergeRequest(projectId, branchName, 'opened')];
339
+ case 1:
340
+ mergeRequest = _c.sent();
341
+ if (!mergeRequest)
342
+ throw new errors_1.ShipItError("No merge request in project \"monorepo\" with branch name \"".concat(branchName, "\" could be found."));
343
+ if (!mergeRequest.milestone)
344
+ throw new errors_1.ShipItError("No milestone could be found on merge request \"".concat(mergeRequest.title, "\". Please attach a milestone then try again: ").concat(mergeRequest.url));
345
+ return [2 /*return*/, (_b = (_a = mergeRequest.milestone) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : ''];
346
+ }
347
+ });
348
+ });
349
+ };
350
+ GitLabService.prototype.bumpVersionTxt = function (projectId, branchName, newVersion) {
351
+ return __awaiter(this, void 0, void 0, function () {
352
+ var versionTxt, buff, text;
353
+ return __generator(this, function (_a) {
354
+ switch (_a.label) {
355
+ case 0: return [4 /*yield*/, this.apiClient.getFile(projectId, 'version.txt', branchName)];
356
+ case 1:
357
+ versionTxt = _a.sent();
358
+ if (!versionTxt)
359
+ return [2 /*return*/, false];
360
+ buff = Buffer.from(versionTxt.content, 'base64');
361
+ text = buff.toString('utf-8');
362
+ if (text === newVersion)
363
+ return [2 /*return*/, false];
364
+ return [4 /*yield*/, this.apiClient.updateFile(projectId, 'version.txt', branchName, newVersion, 'Bumping version.txt')];
365
+ case 2:
366
+ _a.sent();
367
+ return [2 /*return*/, true];
368
+ }
369
+ });
370
+ });
371
+ };
372
+ GitLabService.prototype.getFile = function (projectId, branchName, file) {
373
+ return __awaiter(this, void 0, void 0, function () {
374
+ return __generator(this, function (_a) {
375
+ switch (_a.label) {
376
+ case 0: return [4 /*yield*/, this.apiClient.getFile(projectId, file, branchName)];
377
+ case 1: return [2 /*return*/, _a.sent()];
378
+ }
379
+ });
380
+ });
381
+ };
382
+ GitLabService.prototype.updateFile = function (projectId, filePath, branchName, content, commit) {
383
+ return this.apiClient.updateFile(projectId, filePath, branchName, content, commit);
384
+ };
385
+ GitLabService.prototype.commit = function (projectId, branchName, commitMessage, files) {
386
+ return __awaiter(this, void 0, void 0, function () {
387
+ return __generator(this, function (_a) {
388
+ switch (_a.label) {
389
+ case 0: return [4 /*yield*/, this.apiClient.commit(projectId, branchName, files, commitMessage)];
390
+ case 1:
391
+ _a.sent();
392
+ return [2 /*return*/];
393
+ }
394
+ });
395
+ });
396
+ };
397
+ GitLabService.prototype.getTag = function (projectId, tagName) {
398
+ return __awaiter(this, void 0, void 0, function () {
399
+ return __generator(this, function (_a) {
400
+ switch (_a.label) {
401
+ case 0: return [4 /*yield*/, this.apiClient.getTag(projectId, tagName)];
402
+ case 1: return [2 /*return*/, _a.sent()];
403
+ }
404
+ });
405
+ });
406
+ };
407
+ GitLabService.prototype.createTag = function (projectId, tagName, sha) {
408
+ return __awaiter(this, void 0, void 0, function () {
409
+ return __generator(this, function (_a) {
410
+ switch (_a.label) {
411
+ case 0: return [4 /*yield*/, this.apiClient.createTag(projectId, tagName, sha)];
412
+ case 1: return [2 /*return*/, _a.sent()];
413
+ }
414
+ });
415
+ });
416
+ };
417
+ GitLabService.prototype.createRelease = function (projectId, release) {
418
+ return __awaiter(this, void 0, void 0, function () {
419
+ return __generator(this, function (_a) {
420
+ switch (_a.label) {
421
+ case 0: return [4 /*yield*/, this.apiClient.createRelease(projectId, release)];
422
+ case 1: return [2 /*return*/, _a.sent()];
423
+ }
424
+ });
425
+ });
426
+ };
427
+ /**
428
+ * We use the latest valid semver tag in master to determine what the current version
429
+ * of a project is
430
+ */
431
+ GitLabService.prototype.getCurrentMinorVersionTag = function (projectId) {
432
+ return __awaiter(this, void 0, void 0, function () {
433
+ var tags, tagNames, sortedTagNames, latestMinorVersion, currentTag;
434
+ return __generator(this, function (_a) {
435
+ switch (_a.label) {
436
+ case 0: return [4 /*yield*/, this.apiClient.getLatestTags(projectId)];
437
+ case 1:
438
+ tags = _a.sent();
439
+ if (!(tags === null || tags === void 0 ? void 0 : tags.length))
440
+ throw new errors_1.ShipItError("Project ".concat(projectId, " does not have any tags"));
441
+ tagNames = tags.map(function (t) { return t.name; }).filter(function (t) { return semver_1.default.valid(t); });
442
+ if (!tagNames.length)
443
+ return [2 /*return*/, null];
444
+ sortedTagNames = semver_1.default.sort(tagNames).reverse();
445
+ latestMinorVersion = sortedTagNames.find(function (tag) { return tag.endsWith('.0'); });
446
+ currentTag = tags.find(function (t) { return t.name === latestMinorVersion; });
447
+ if (!currentTag)
448
+ return [2 /*return*/, null];
449
+ return [2 /*return*/, currentTag];
450
+ }
451
+ });
452
+ });
453
+ };
454
+ GitLabService.prototype.hasSomeTags = function (projectId) {
455
+ return __awaiter(this, void 0, void 0, function () {
456
+ var tags;
457
+ return __generator(this, function (_a) {
458
+ switch (_a.label) {
459
+ case 0: return [4 /*yield*/, this.apiClient.getLatestTags(projectId)];
460
+ case 1:
461
+ tags = _a.sent();
462
+ return [2 /*return*/, !!(tags === null || tags === void 0 ? void 0 : tags.length)];
463
+ }
464
+ });
465
+ });
466
+ };
467
+ GitLabService.prototype.getNextBranchInfo = function (projectId, version) {
468
+ return __awaiter(this, void 0, void 0, function () {
469
+ var tag, nextVersion, branchName, branch;
470
+ return __generator(this, function (_a) {
471
+ switch (_a.label) {
472
+ case 0: return [4 /*yield*/, this.getCurrentMinorVersionTag(projectId)];
473
+ case 1:
474
+ tag = _a.sent();
475
+ if (!tag)
476
+ throw new errors_1.ShipItError("Could not find the latest semver tag for projectId: ".concat(projectId));
477
+ nextVersion = utils_1.Versions.getNextVersion(tag.name, version);
478
+ branchName = version === 'patch' ?
479
+ utils_1.BranchNames.getHotfix(nextVersion) :
480
+ utils_1.BranchNames.getRelease(nextVersion);
481
+ return [4 /*yield*/, this.getBranch(projectId, branchName)];
482
+ case 2:
483
+ branch = _a.sent();
484
+ return [2 /*return*/, {
485
+ nextVersion: nextVersion,
486
+ branchName: branchName,
487
+ branch: branch,
488
+ tag: tag
489
+ }];
490
+ }
491
+ });
492
+ });
493
+ };
494
+ GitLabService.prototype.getProjectConfig = function (projectId, branchName) {
495
+ return __awaiter(this, void 0, void 0, function () {
496
+ var cacheKey, configFileName, configFile, buff, text, json, projectConfig;
497
+ return __generator(this, function (_a) {
498
+ switch (_a.label) {
499
+ case 0:
500
+ cacheKey = projectId + ':' + branchName;
501
+ if (this.projectConfigCache[cacheKey])
502
+ return [2 /*return*/, this.projectConfigCache[cacheKey]];
503
+ configFileName = '.ship-it.config';
504
+ return [4 /*yield*/, this.apiClient.getFile(projectId, configFileName, branchName)];
505
+ case 1:
506
+ configFile = _a.sent();
507
+ if (!configFile)
508
+ return [2 /*return*/, ProjectConfig_1.DefaultProjectConfig];
509
+ try {
510
+ buff = Buffer.from(configFile.content, 'base64');
511
+ text = buff.toString('utf-8');
512
+ json = JSON.parse(text);
513
+ projectConfig = lodash_1.default.merge({}, ProjectConfig_1.DefaultProjectConfig, json);
514
+ this.projectConfigCache[cacheKey] = projectConfig;
515
+ return [2 /*return*/, projectConfig];
516
+ }
517
+ catch (_b) {
518
+ throw new errors_1.ShipItError("Failed to parse ".concat(configFileName));
519
+ }
520
+ return [2 /*return*/];
521
+ }
522
+ });
523
+ });
524
+ };
525
+ GitLabService.prototype.updateMilestone = function (projectId, mergeRequestID, newMilestoneId) {
526
+ return this.apiClient.updateMilestone(projectId, mergeRequestID, newMilestoneId);
527
+ };
528
+ GitLabService.prototype.getApprovalRuleConfiguration = function (projectId) {
529
+ return this.apiClient.getApprovalRuleConfiguration(projectId);
530
+ };
531
+ GitLabService.prototype.updateApprovalRuleConfiguration = function (projectId, configuration) {
532
+ return this.apiClient.updateApprovalRuleConfiguration(projectId, configuration);
533
+ };
534
+ GitLabService.prototype.getApprovalRules = function (projectId) {
535
+ return this.apiClient.getApprovalRules(projectId);
536
+ };
537
+ GitLabService.prototype.createApprovalRule = function (projectId, rule) {
538
+ return this.apiClient.createApprovalRule(projectId, rule);
539
+ };
540
+ GitLabService.prototype.updateApprovalRule = function (projectId, approvalRuleId, rule) {
541
+ return this.apiClient.updateApprovalRule(projectId, approvalRuleId, rule);
542
+ };
543
+ GitLabService.prototype.getProtectedBranches = function (projectId) {
544
+ return this.apiClient.getProtectedBranches(projectId);
545
+ };
546
+ GitLabService = __decorate([
547
+ (0, inversify_1.injectable)(),
548
+ __param(0, (0, inversify_1.inject)(types_1.API_CLIENTS.GitLabApiClient)),
549
+ __metadata("design:paramtypes", [api_clients_1.GitLabApiClient])
550
+ ], GitLabService);
551
+ return GitLabService;
552
+ }());
553
+ exports.GitLabService = GitLabService;