@clickview/ship-it 0.0.40 → 0.0.41

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 (124) 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 +561 -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 +77 -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/MilestoneCommandCliOptions.js +2 -2
  49. package/lib/src/interfaces/command-cli-options/ReleaseCommandCliOptions.js +2 -2
  50. package/lib/src/interfaces/command-cli-options/StatusCommandCliOptions.js +2 -2
  51. package/lib/src/interfaces/command-cli-options/index.js +19 -19
  52. package/lib/src/interfaces/command-options/BaseCommandOptions.js +2 -2
  53. package/lib/src/interfaces/command-options/BumpPackagesCommandOptions.js +2 -2
  54. package/lib/src/interfaces/command-options/ConfigureProjectsOptions.js +2 -2
  55. package/lib/src/interfaces/command-options/DockerMilestoneCommandOptions.js +2 -2
  56. package/lib/src/interfaces/command-options/MilestoneCommandOptions.js +2 -2
  57. package/lib/src/interfaces/command-options/ReleaseCommandOptions.js +2 -2
  58. package/lib/src/interfaces/command-options/StatusCommandOptions.js +2 -2
  59. package/lib/src/interfaces/command-options/ValidateMRCommandOptions.js +2 -2
  60. package/lib/src/interfaces/command-options/index.js +23 -23
  61. package/lib/src/interfaces/index.js +26 -26
  62. package/lib/src/interfaces/models/ApprovalRule.js +2 -2
  63. package/lib/src/interfaces/models/ApprovalRuleConfiguration.js +2 -2
  64. package/lib/src/interfaces/models/Branch.js +2 -2
  65. package/lib/src/interfaces/models/Commit.js +2 -2
  66. package/lib/src/interfaces/models/CommitFile.js +2 -2
  67. package/lib/src/interfaces/models/CreateApprovalRuleRequest.js +2 -2
  68. package/lib/src/interfaces/models/File.js +2 -2
  69. package/lib/src/interfaces/models/Group.js +2 -2
  70. package/lib/src/interfaces/models/Label.js +2 -2
  71. package/lib/src/interfaces/models/MergeRequest.js +2 -2
  72. package/lib/src/interfaces/models/Milestone.js +2 -2
  73. package/lib/src/interfaces/models/Project.js +2 -2
  74. package/lib/src/interfaces/models/ProjectConfig.js +9 -9
  75. package/lib/src/interfaces/models/ProtectedBranch.js +2 -2
  76. package/lib/src/interfaces/models/Release.js +2 -2
  77. package/lib/src/interfaces/models/Tag.js +2 -2
  78. package/lib/src/interfaces/models/index.js +32 -32
  79. package/lib/src/interfaces/view-models/MilestoneProject.js +2 -2
  80. package/lib/src/interfaces/view-models/index.js +17 -17
  81. package/lib/src/inversify.config.js +79 -79
  82. package/lib/src/services/GitLabService.js +544 -544
  83. package/lib/src/services/SlackService.js +142 -142
  84. package/lib/src/services/TeamCityService.js +37 -37
  85. package/lib/src/services/TrelloService.js +84 -84
  86. package/lib/src/services/index.js +20 -20
  87. package/lib/src/startup/AddMilestoneCommand.js +96 -96
  88. package/lib/src/startup/GetAction.js +68 -68
  89. package/lib/src/startup/SetupCommands.js +180 -180
  90. package/lib/src/tasks/BumpPackagesTask.js +230 -230
  91. package/lib/src/tasks/BumpVersionTxtTask.js +110 -110
  92. package/lib/src/tasks/CommitPackageVersionsTask.js +116 -0
  93. package/lib/src/tasks/CreateBranchTask.js +104 -104
  94. package/lib/src/tasks/CreateMergeRequestTask.js +118 -118
  95. package/lib/src/tasks/CreateNotesAuditTask.js +140 -140
  96. package/lib/src/tasks/CreateReleaseNotesTask.js +144 -144
  97. package/lib/src/tasks/InstallPackagesTask.js +415 -415
  98. package/lib/src/tasks/ListProjectsTask.js +87 -87
  99. package/lib/src/tasks/PopulateTrelloBoardTask.js +140 -140
  100. package/lib/src/tasks/StartMonorepoBuildTask.js +109 -109
  101. package/lib/src/tasks/StartProjectBuildsTask.js +170 -170
  102. package/lib/src/tasks/TagMasterTask.js +189 -189
  103. package/lib/src/tasks/ValidateCommitsTask.js +143 -143
  104. package/lib/src/tasks/ValidateDescriptionsTask.js +178 -178
  105. package/lib/src/tasks/ValidateMergeRequestTask.js +115 -115
  106. package/lib/src/tasks/ValidateTask.js +419 -419
  107. package/lib/src/tasks/VersionTask.js +98 -98
  108. package/lib/src/tasks/configure-projects/AddApprovalRulesTask.js +199 -182
  109. package/lib/src/tasks/configure-projects/CreateToolingBranchTask.js +138 -136
  110. package/lib/src/tasks/configure-projects/EnsureApprovalRulesTask.js +106 -106
  111. package/lib/src/tasks/configure-projects/index.js +18 -18
  112. package/lib/src/tasks/configure-projects/primitives/BaseConfigureProjectsTask.js +98 -98
  113. package/lib/src/tasks/index.js +34 -34
  114. package/lib/src/types.js +60 -60
  115. package/lib/src/utils/BranchNames.js +11 -11
  116. package/lib/src/utils/Commits.js +55 -55
  117. package/lib/src/utils/Csv.js +54 -54
  118. package/lib/src/utils/Descriptions.js +274 -274
  119. package/lib/src/utils/GroupIds.js +6 -6
  120. package/lib/src/utils/Logger.js +62 -62
  121. package/lib/src/utils/ProjectIds.js +47 -47
  122. package/lib/src/utils/Versions.js +20 -20
  123. package/lib/src/utils/index.js +22 -22
  124. package/package.json +50 -50
@@ -1,544 +1,544 @@
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 (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;