@elisra-devops/docgen-data-provider 0.4.6

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.
@@ -0,0 +1,446 @@
1
+ import { TFSServices } from "../helpers/tfs";
2
+ import TicketsDataProvider from "./TicketsDataProvider";
3
+ import logger from "../utils/logger";
4
+ export default class GitDataProvider {
5
+ orgUrl: string = "";
6
+ token: string = "";
7
+ ticketsDataProvider: TicketsDataProvider;
8
+
9
+ constructor(orgUrl: string, token: string) {
10
+ this.orgUrl = orgUrl;
11
+ this.token = token;
12
+ this.ticketsDataProvider = new TicketsDataProvider(
13
+ this.orgUrl,
14
+ this.token,
15
+ );
16
+ }
17
+ async GetTeamProjectGitReposList(
18
+ teamProject: string
19
+ ) {
20
+ logger.debug(`fetching repos list for team project - ${teamProject}`);
21
+ let url = `${this.orgUrl}/${teamProject}/_apis/git/repositories`;
22
+ return TFSServices.getItemContent(url, this.token, "get");
23
+ } //GetGitRepoFromPrId
24
+
25
+ async GetGitRepoFromRepoId(
26
+ repoId: string
27
+ ) {
28
+ logger.debug(`fetching repo data by id - ${repoId}`);
29
+ let url = `${this.orgUrl}_apis/git/repositories/${repoId}`;
30
+ return TFSServices.getItemContent(url, this.token, "get");
31
+ } //GetGitRepoFromPrId
32
+
33
+ async GetJsonFileFromGitRepo(
34
+ projectName: string,
35
+ repoName: string,
36
+ filePath: string
37
+ ) {
38
+ let url = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoName}/items?path=${filePath}&includeContent=true`;
39
+ let res = await TFSServices.getItemContent(url, this.token, "get");
40
+ let jsonObject = JSON.parse(res.content);
41
+ return jsonObject;
42
+ } //GetJsonFileFromGitRepo
43
+
44
+ async GetGitRepoFromPrId(
45
+ pullRequestId: number
46
+ ) {
47
+ let url = `${this.orgUrl}_apis/git/pullrequests/${pullRequestId}`;
48
+ let res = await TFSServices.getItemContent(url, this.token, "get");
49
+ return res;
50
+ } //GetGitRepoFromPrId
51
+
52
+ async GetPullRequestCommits(
53
+ repositoryId: string,
54
+ pullRequestId: number
55
+ ) {
56
+ let url = `${this.orgUrl}_apis/git/repositories/${repositoryId}/pullRequests/${pullRequestId}/commits`;
57
+ let res = await TFSServices.getItemContent(url, this.token, "get");
58
+ return res;
59
+ } //GetGitRepoFromPrId
60
+
61
+ async GetPullRequestsLinkedItemsInCommitRange(
62
+ projectId: string,
63
+ repositoryId: string,
64
+ commitRangeArray: any
65
+ ) {
66
+ let pullRequestsFilteredArray: any = [];
67
+ let ChangeSetsArray: any = [];
68
+ //get all pr's in git repo
69
+ let url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/pullrequests?status=completed&includeLinks=true&$top=2000}`;
70
+ logger.debug(`request url: ${url}`);
71
+ let pullRequestsArray = await TFSServices.getItemContent(
72
+ url,
73
+ this.token,
74
+ "get"
75
+ );
76
+ logger.info(
77
+ `got ${pullRequestsArray.count} pullrequests for repo: ${repositoryId}`
78
+ );
79
+ //iterate commit list to filter relavant pullrequests
80
+ pullRequestsArray.value.forEach((pr: any) => {
81
+ commitRangeArray.value.forEach((commit: any) => {
82
+ if (pr.lastMergeCommit.commitId == commit.commitId) {
83
+ pullRequestsFilteredArray.push(pr);
84
+ }
85
+ });
86
+ });
87
+ logger.info(
88
+ `filtered in commit range ${pullRequestsFilteredArray.length} pullrequests for repo: ${repositoryId}`
89
+ );
90
+ //extract linked items and append them to result
91
+ await Promise.all(
92
+ pullRequestsFilteredArray.map(async (pr: any) => {
93
+ let linkedItems: any = {};
94
+ try {
95
+ if (pr._links.workItems.href) {
96
+ //get workitems linked to pr
97
+ let url: string = pr._links.workItems.href;
98
+ linkedItems = await TFSServices.getItemContent(
99
+ url,
100
+ this.token,
101
+ "get"
102
+ );
103
+ logger.info(
104
+ `got ${linkedItems.count} items linked to pr ${pr.pullRequestId}`
105
+ );
106
+ await Promise.all(
107
+ linkedItems.value.map(async (item: any) => {
108
+ let populatedItem = await this.ticketsDataProvider.GetWorkItem(
109
+ projectId,
110
+ item.id
111
+ );
112
+ let changeSet: any = {
113
+ workItem: populatedItem,
114
+ pullrequest: pr,
115
+ };
116
+ ChangeSetsArray.push(changeSet);
117
+ })
118
+ );
119
+ }
120
+ } catch (error) {
121
+ logger.error(error);
122
+ }
123
+ })
124
+ );
125
+ return ChangeSetsArray;
126
+ } //GetPullRequestsInCommitRange
127
+
128
+ async GetItemsInCommitRange(
129
+ projectId: string,
130
+ repositoryId: string,
131
+ commitRange:any
132
+ ) {
133
+ //get all items linked to commits
134
+ let res: any = [];
135
+ let commitChangesArray: any = [];
136
+ //extract linked items and append them to result
137
+ for (const commit of commitRange.value) {
138
+ if (commit.workItems) {
139
+ for (const wi of commit.workItems) {
140
+ let populatedItem = await this.ticketsDataProvider.GetWorkItem(
141
+ projectId,
142
+ wi.id
143
+ );
144
+ let changeSet: any = { workItem: populatedItem, commit: commit };
145
+ commitChangesArray.push(changeSet);
146
+ }
147
+ }
148
+ }
149
+ //get all items and pr data from pr's in commit range - using the above function
150
+ let pullRequestsChangesArray =
151
+ await this.GetPullRequestsLinkedItemsInCommitRange(
152
+ projectId,
153
+ repositoryId,
154
+ commitRange
155
+ );
156
+ //merge commit links with pr links
157
+ logger.info(`got ${pullRequestsChangesArray.length} items from pr's and`);
158
+ res = [...commitChangesArray, ...pullRequestsChangesArray];
159
+ let workItemIds :any = []
160
+ for (let index = 0; index < res.length; index++) {
161
+ if (workItemIds.includes(res[index].workItem.id)){
162
+ res.splice(index, 1);
163
+ index--;
164
+ }
165
+ else{
166
+ workItemIds.push(res[index].workItem.id)
167
+ }
168
+ }
169
+ return res;
170
+ } //GetItemsInCommitRange
171
+
172
+ async GetPullRequestsInCommitRangeWithoutLinkedItems(
173
+ projectId: string,
174
+ repositoryId: string,
175
+ commitRangeArray: any
176
+ ) {
177
+ let pullRequestsFilteredArray: any[] = [];
178
+
179
+ // Extract the organization name from orgUrl
180
+ let orgName = this.orgUrl.split('/').filter(Boolean).pop();
181
+
182
+ // Get all PRs in the git repo
183
+ let url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/pullrequests?status=completed&includeLinks=true&$top=2000}`;
184
+ logger.debug(`request url: ${url}`);
185
+ let pullRequestsArray = await TFSServices.getItemContent(
186
+ url,
187
+ this.token,
188
+ "get"
189
+ );
190
+ logger.info(
191
+ `got ${pullRequestsArray.count} pullrequests for repo: ${repositoryId}`
192
+ );
193
+
194
+ // Iterate commit list to filter relevant pull requests
195
+ pullRequestsArray.value.forEach((pr: any) => {
196
+ commitRangeArray.value.forEach((commit: any) => {
197
+ if (pr.lastMergeCommit.commitId == commit.commitId) {
198
+ // Construct the pull request URL
199
+ const prUrl = `https://dev.azure.com/${orgName}/${projectId}/_git/${repositoryId}/pullrequest/${pr.pullRequestId}`;
200
+
201
+ // Extract only the desired properties from the PR
202
+ const prFilteredData = {
203
+ pullRequestId: pr.pullRequestId,
204
+ createdBy: pr.createdBy.displayName,
205
+ creationDate: pr.creationDate,
206
+ closedDate: pr.closedDate,
207
+ title: pr.title,
208
+ description: pr.description,
209
+ url: prUrl // Use the constructed URL here
210
+ };
211
+ pullRequestsFilteredArray.push(prFilteredData);
212
+ }
213
+ });
214
+ });
215
+ logger.info(
216
+ `filtered in commit range ${pullRequestsFilteredArray.length} pullrequests for repo: ${repositoryId}`
217
+ );
218
+
219
+ return pullRequestsFilteredArray;
220
+ } // GetPullRequestsInCommitRangeWithoutLinkedItems
221
+
222
+
223
+ async GetCommitByCommitId(
224
+ projectId: string,
225
+ repositoryId: string,
226
+ commitSha: string
227
+ ) {
228
+ let url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/commits/${commitSha}`;
229
+ return TFSServices.getItemContent(url, this.token, "get");
230
+ }
231
+
232
+ async GetCommitForPipeline(
233
+ projectId: string,
234
+ buildId: number
235
+ ) {
236
+ let url = `${this.orgUrl}${projectId}/_apis/build/builds/${buildId}`;
237
+ let res = await TFSServices.getItemContent(url, this.token, "get");
238
+ return res.sourceVersion;
239
+ } //GetCommitForPipeline
240
+
241
+ async GetItemsForPipelinesRange(
242
+ projectId: string,
243
+ fromBuildId: number,
244
+ toBuildId: number
245
+ ) {
246
+ let linkedItemsArray: any = [];
247
+ let url = `${this.orgUrl}${projectId}/_apis/build/workitems?fromBuildId=${fromBuildId}&toBuildId=${toBuildId}&$top=2000`;
248
+ let res = await TFSServices.getItemContent(url, this.token, "get");
249
+ logger.info(
250
+ `recieved ${res.count} items in build range ${fromBuildId}-${toBuildId}`
251
+ );
252
+ await Promise.all(
253
+ res.value.map(async (wi: any) => {
254
+ let populatedItem = await this.ticketsDataProvider.GetWorkItem(
255
+ projectId,
256
+ wi.id
257
+ );
258
+ let changeSet: any = { workItem: populatedItem, build: toBuildId };
259
+ linkedItemsArray.push(changeSet);
260
+ })
261
+ );
262
+ return linkedItemsArray;
263
+ } //GetCommitForPipeline
264
+
265
+
266
+ async GetCommitsInDateRange(
267
+ projectId: string,
268
+ repositoryId: string,
269
+ fromDate: string,
270
+ toDate: string,
271
+ branchName?: string
272
+ ) {
273
+ let url:string
274
+ if (typeof branchName !== 'undefined') {
275
+ url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/commits?searchCriteria.fromDate=${fromDate}&searchCriteria.toDate=${toDate}&searchCriteria.includeWorkItems=true&searchCriteria.$top=2000&searchCriteria.itemVersion.version=${branchName}`;
276
+ }
277
+ else{
278
+ url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/commits?searchCriteria.fromDate=${fromDate}&searchCriteria.toDate=${toDate}&searchCriteria.includeWorkItems=true&searchCriteria.$top=2000`;
279
+ }
280
+ return TFSServices.getItemContent(url, this.token, "get");
281
+ } //GetCommitsInDateRange
282
+
283
+ async GetCommitsInCommitRange(
284
+ projectId: string,
285
+ repositoryId: string,
286
+ fromSha: string,
287
+ toSha: string
288
+ ) {
289
+ let url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/commits?searchCriteria.fromCommitId=${fromSha}&searchCriteria.toCommitId=${toSha}&searchCriteria.includeWorkItems=true&searchCriteria.$top=2000`;
290
+ return TFSServices.getItemContent(url, this.token, "get");
291
+ } //GetCommitsInCommitRange doesen't work!!!
292
+
293
+ async CreatePullRequestComment(
294
+ projectName: string,
295
+ repoID: string,
296
+ pullRequestID: number,
297
+ threads: any
298
+ ) {
299
+ let url: string = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/pullRequests/${pullRequestID}/threads?api-version=5.0`;
300
+ let res: any = await TFSServices.getItemContent(
301
+ url,
302
+ this.token,
303
+ "post",
304
+ threads,
305
+ null
306
+ );
307
+ return res;
308
+ }
309
+
310
+ async GetPullRequestComments(
311
+ projectName: string,
312
+ repoID: string,
313
+ pullRequestID: number
314
+ ) {
315
+ let url: string = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/pullRequests/${pullRequestID}/threads`;
316
+ return TFSServices.getItemContent(
317
+ url,
318
+ this.token,
319
+ "get",
320
+ null,
321
+ null
322
+ );
323
+ }
324
+
325
+ async GetCommitsForRepo(
326
+ projectName: string,
327
+ repoID: string,
328
+ branchName?: string
329
+ ){
330
+ let url: string
331
+ if (typeof branchName !== 'undefined') {
332
+ url = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/commits?searchCriteria.$top=2000&searchCriteria.itemVersion.version=${branchName}`
333
+ }
334
+ else{
335
+ url = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/commits?searchCriteria.$top=2000`
336
+ }
337
+ let res:any = await TFSServices.getItemContent(
338
+ url,
339
+ this.token,
340
+ "get",
341
+ null,
342
+ null
343
+ );
344
+ return res;
345
+ }
346
+
347
+ async GetPullRequestsForRepo(
348
+ projectName: string,
349
+ repoID: string
350
+ ){
351
+ let url: string =
352
+ `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/pullrequests?status=completed&includeLinks=true&$top=2000}`;
353
+
354
+ let res:any = await TFSServices.getItemContent(
355
+ url,
356
+ this.token,
357
+ "get",
358
+ null,
359
+ null
360
+ );
361
+ return res;
362
+ }
363
+
364
+
365
+ async GetItemsInPullRequestRange(
366
+ projectId: string,
367
+ repositoryId: string,
368
+ pullRequestIDs: any,
369
+ ) {
370
+ let pullRequestsFilteredArray: any = [];
371
+ let ChangeSetsArray: any = [];
372
+ //get all pr's in git repo
373
+ let url = `${this.orgUrl}${projectId}/_apis/git/repositories/${repositoryId}/pullrequests?status=completed&includeLinks=true&$top=2000}`;
374
+ logger.debug(`request url: ${url}`);
375
+ let pullRequestsArray = await TFSServices.getItemContent(
376
+ url,
377
+ this.token,
378
+ "get"
379
+ );
380
+ logger.info(
381
+ `got ${pullRequestsArray.count} pullrequests for repo: ${repositoryId}`
382
+ );
383
+ //iterate commit list to filter relavant pullrequests
384
+ pullRequestsArray.value.forEach((pr: any) => {
385
+ pullRequestIDs.forEach((prId: any) => {
386
+ if (prId == pr.pullRequestId) {
387
+ pullRequestsFilteredArray.push(pr);
388
+ }
389
+ });
390
+ });
391
+ logger.info(
392
+ `filtered in prId range ${pullRequestsFilteredArray.length} pullrequests for repo: ${repositoryId}`
393
+ );
394
+ //extract linked items and append them to result
395
+ await Promise.all(
396
+ pullRequestsFilteredArray.map(async (pr: any) => {
397
+ let linkedItems: any = {};
398
+ try {
399
+ if (pr._links.workItems.href) {
400
+ //get workitems linked to pr
401
+ let url: string = pr._links.workItems.href;
402
+ linkedItems = await TFSServices.getItemContent(
403
+ url,
404
+ this.token,
405
+ "get"
406
+ );
407
+ logger.info(
408
+ `got ${linkedItems.count} items linked to pr ${pr.pullRequestId}`
409
+ );
410
+ await Promise.all(
411
+ linkedItems.value.map(async (item: any) => {
412
+ let populatedItem = await this.ticketsDataProvider.GetWorkItem(
413
+ projectId,
414
+ item.id
415
+ );
416
+ let changeSet: any = {
417
+ workItem: populatedItem,
418
+ pullrequest: pr,
419
+ };
420
+ ChangeSetsArray.push(changeSet);
421
+ })
422
+ );
423
+ }
424
+ } catch (error) {
425
+ logger.error(error);
426
+ }
427
+ })
428
+ );
429
+ return ChangeSetsArray;
430
+ }
431
+
432
+ async GetRepoBranches(
433
+ projectName: string,
434
+ repoID: string
435
+ ){
436
+ let url: string = `${this.orgUrl}${projectName}/_apis/git/repositories/${repoID}/refs?searchCriteria.$top=1000&filter=heads`
437
+ let res:any = await TFSServices.getItemContent(
438
+ url,
439
+ this.token,
440
+ "get",
441
+ null,
442
+ null
443
+ );
444
+ return res;
445
+ }
446
+ }
@@ -0,0 +1,59 @@
1
+ import { TFSServices } from "../helpers/tfs";
2
+ import logger from "../utils/logger";
3
+
4
+ export default class MangementDataProvider {
5
+ orgUrl: string = "";
6
+ token: string = "";
7
+
8
+ constructor(orgUrl: string, token: string) {
9
+ this.orgUrl = orgUrl;
10
+ this.token = token;
11
+ }
12
+
13
+ async GetCllectionLinkTypes() {
14
+ let url: string = `${this.orgUrl}_apis/wit/workitemrelationtypes`;
15
+ let res: any = await TFSServices.getItemContent(
16
+ url,
17
+ this.token,
18
+ "get",
19
+ null,
20
+ null
21
+ );
22
+ return res;
23
+ }
24
+
25
+ //get all projects
26
+ async GetProjects(): Promise<any> {
27
+ let projectUrl: string = `${this.orgUrl}_apis/projects?$top=1000`;
28
+ let projects: any = await TFSServices.getItemContent(
29
+ projectUrl,
30
+ this.token
31
+ );
32
+ return projects;
33
+ }
34
+
35
+ // get project by name return project object
36
+ async GetProjectByName(
37
+ projectName: string
38
+ ): Promise<any> {
39
+ try {
40
+ let projects: any = await this.GetProjects();
41
+ for (let i = 0; i < projects.value.length; i++) {
42
+ if (projects.value[i].name === projectName) return projects.value[i];
43
+ }
44
+ return {};
45
+ } catch (err) {
46
+ console.log(err);
47
+ return {};
48
+ }
49
+ }
50
+
51
+ // get project by id return project object
52
+ async GetProjectByID(
53
+ projectID: string
54
+ ): Promise<any> {
55
+ let projectUrl: string = `${this.orgUrl}_apis/projects/${projectID}`;
56
+ let project: any = await TFSServices.getItemContent(projectUrl, this.token);
57
+ return project;
58
+ }
59
+ }
@@ -0,0 +1,142 @@
1
+ import { TFSServices } from "../helpers/tfs";
2
+
3
+ import logger from "../utils/logger";
4
+
5
+ export default class PipelinesDataProvider {
6
+ orgUrl: string = "";
7
+ token: string = "";
8
+
9
+ constructor(orgUrl: string, token: string) {
10
+ this.orgUrl = orgUrl;
11
+ this.token = token;
12
+ }
13
+
14
+ async getPipelineFromPipelineId(projectName: string, buildId: number) {
15
+ let url = `${this.orgUrl}${projectName}/_apis/build/builds/${buildId}`;
16
+ return TFSServices.getItemContent(url, this.token, "get");
17
+ } //GetCommitForPipeline
18
+
19
+ async TriggerBuildById(
20
+ projectName: string,
21
+ buildDefanitionId: string,
22
+ parameter: any
23
+ ) {
24
+ let data = {
25
+ definition: {
26
+ id: buildDefanitionId,
27
+ },
28
+ parameters: parameter, //'{"Test":"123"}'
29
+ };
30
+ logger.info(JSON.stringify(data));
31
+ let url = `${this.orgUrl}${projectName}/_apis/build/builds?api-version=5.0`;
32
+ let res = await TFSServices.postRequest(
33
+ url,
34
+ this.token,
35
+ "post",
36
+ data,
37
+ null
38
+ );
39
+ return res;
40
+ }
41
+
42
+ async GetArtifactByBuildId(
43
+ projectName: string,
44
+ buildId: string,
45
+ artifactName: string
46
+ ): Promise<any> {
47
+ try {
48
+ logger.info(
49
+ `Get artifactory from project ${projectName},BuildId ${buildId} artifact name ${artifactName}`
50
+ );
51
+ logger.info(`Check if build ${buildId} have artifact`);
52
+ let url = `${this.orgUrl}${projectName}/_apis/build/builds/${buildId}/artifacts`;
53
+ let response = await TFSServices.getItemContent(
54
+ url,
55
+ this.token,
56
+ "Get",
57
+ null,
58
+ null
59
+ );
60
+ if (response.count == 0) {
61
+ logger.info(`No artifact for build ${buildId} was published `);
62
+ return response;
63
+ }
64
+ url = `${this.orgUrl}${projectName}/_apis/build/builds/${buildId}/artifacts?artifactName=${artifactName}`;
65
+ let res = await TFSServices.getItemContent(
66
+ url,
67
+ this.token,
68
+ "Get",
69
+ null,
70
+ null
71
+ );
72
+ logger.info(`Url for download :${res.resource.downloadUrl}`);
73
+ let result = await TFSServices.downloadZipFile(
74
+ res.resource.downloadUrl,
75
+ this.token
76
+ );
77
+ return result;
78
+ } catch (err) {
79
+ logger.error(`Error : ${err}`);
80
+ throw new Error(String(err));
81
+ }
82
+ }
83
+
84
+ async GetReleaseByReleaseId(
85
+ projectName: string,
86
+ releaseId: number
87
+ ): Promise<any> {
88
+ let url = `${this.orgUrl}${projectName}/_apis/release/releases/${releaseId}`;
89
+ url = url.replace("dev.azure.com", "vsrm.dev.azure.com");
90
+ return TFSServices.getItemContent(url, this.token, "get", null, null);
91
+ }
92
+
93
+ async GetPipelineRunHistory(projectName: string, pipelineId: string) {
94
+ let url: string = `${this.orgUrl}${projectName}/_apis/pipelines/${pipelineId}/runs`;
95
+ let res: any = await TFSServices.getItemContent(
96
+ url,
97
+ this.token,
98
+ "get",
99
+ null,
100
+ null
101
+ );
102
+ return res;
103
+ }
104
+
105
+ async GetReleaseHistory(projectName: string, definitionId: string) {
106
+ let url: string = `${this.orgUrl}${projectName}/_apis/release/releases?definitionId=${definitionId}&$top=2000`;
107
+ url = url.replace("dev.azure.com", "vsrm.dev.azure.com");
108
+ let res: any = await TFSServices.getItemContent(
109
+ url,
110
+ this.token,
111
+ "get",
112
+ null,
113
+ null
114
+ );
115
+ return res;
116
+ }
117
+
118
+ async GetAllPipelines(projectName: string) {
119
+ let url: string = `${this.orgUrl}${projectName}/_apis/pipelines?$top=2000`;
120
+ let res: any = await TFSServices.getItemContent(
121
+ url,
122
+ this.token,
123
+ "get",
124
+ null,
125
+ null
126
+ );
127
+ return res;
128
+ }
129
+
130
+ async GetAllReleaseDefenitions(projectName: string) {
131
+ let url: string = `${this.orgUrl}${projectName}/_apis/release/definitions?$top=2000`;
132
+ url = url.replace("dev.azure.com", "vsrm.dev.azure.com");
133
+ let res: any = await TFSServices.getItemContent(
134
+ url,
135
+ this.token,
136
+ "get",
137
+ null,
138
+ null
139
+ );
140
+ return res;
141
+ }
142
+ }