@elisra-devops/docgen-data-provider 1.5.0 → 1.6.0

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.
@@ -1,26 +1,119 @@
1
- import { TFSServices } from "../helpers/tfs";
1
+ import { PipelineRun, Repository, ResourceRepository } from '../models/tfs-data';
2
+ import { TFSServices } from '../helpers/tfs';
2
3
 
3
- import logger from "../utils/logger";
4
+ import logger from '../utils/logger';
5
+ import GitDataProvider from './GitDataProvider';
4
6
 
5
7
  export default class PipelinesDataProvider {
6
- orgUrl: string = "";
7
- token: string = "";
8
+ orgUrl: string = '';
9
+ token: string = '';
8
10
 
9
11
  constructor(orgUrl: string, token: string) {
10
12
  this.orgUrl = orgUrl;
11
13
  this.token = token;
12
14
  }
13
15
 
16
+ public async findPreviousPipeline(
17
+ projectName: string,
18
+ pipeLineId: string,
19
+ toPipelineRunId: number,
20
+ toPipeline: any,
21
+ searchPrevPipelineFromDifferentCommit: boolean
22
+ ) {
23
+ // get pipeline runs:
24
+ const pipelineRuns = await this.GetPipelineRunHistory(projectName, pipeLineId);
25
+
26
+ for (const pipelineRun of pipelineRuns.value) {
27
+ if (pipelineRun.id >= toPipelineRunId) {
28
+ continue;
29
+ }
30
+ if (pipelineRun.result !== 'succeeded') {
31
+ continue;
32
+ }
33
+
34
+ const fromPipeline = await this.getPipelineFromPipelineId(projectName, Number(pipeLineId));
35
+ if (!fromPipeline.resources.repositories) {
36
+ continue;
37
+ }
38
+ const fromPipelineRepositories = fromPipeline.resources.repositories;
39
+ logger.debug(`from pipeline repositories ${JSON.stringify(fromPipelineRepositories)}`);
40
+ const toPipelineRepositories = toPipeline.resources.repositories;
41
+ logger.debug(`to pipeline repositories ${JSON.stringify(toPipelineRepositories)}`);
42
+
43
+ const fromPipeLineSelfRepo =
44
+ '__designer_repo' in fromPipelineRepositories
45
+ ? fromPipelineRepositories['__designer_repo']
46
+ : 'self' in fromPipelineRepositories
47
+ ? fromPipelineRepositories['self']
48
+ : undefined;
49
+ const toPipeLineSelfRepo =
50
+ '__designer_repo' in toPipelineRepositories
51
+ ? toPipelineRepositories['__designer_repo']
52
+ : 'self' in toPipelineRepositories
53
+ ? toPipelineRepositories['self']
54
+ : undefined;
55
+ if (
56
+ fromPipeLineSelfRepo.repository.id === toPipeLineSelfRepo.repository.id &&
57
+ fromPipeLineSelfRepo.version === toPipeLineSelfRepo.version
58
+ ) {
59
+ if (searchPrevPipelineFromDifferentCommit) {
60
+ continue;
61
+ }
62
+ }
63
+
64
+ if (
65
+ fromPipeLineSelfRepo.repository.id === toPipeLineSelfRepo.repository.id &&
66
+ fromPipeLineSelfRepo.refName !== toPipeLineSelfRepo.refName
67
+ ) {
68
+ continue;
69
+ }
70
+
71
+ return pipelineRun.id;
72
+ }
73
+ return undefined;
74
+ }
75
+
76
+ public async getPipelineResourceRepositoriesFromObject(
77
+ inPipeline: PipelineRun,
78
+ gitDataProviderInstance: GitDataProvider
79
+ ) {
80
+ const resourceRepositories: Set<any> = new Set();
81
+
82
+ if (!inPipeline.resources.repositories) {
83
+ return resourceRepositories;
84
+ }
85
+ const repositories = inPipeline.resources.repositories;
86
+ for (const prop in repositories) {
87
+ const resourceRepo = repositories[prop];
88
+ if (resourceRepo.repository.type !== 'azureReposGit') {
89
+ continue;
90
+ }
91
+ const repoId = resourceRepo.repository.id;
92
+
93
+ const repo: Repository = await gitDataProviderInstance.GetGitRepoFromRepoId(repoId);
94
+ const resourceRepository: ResourceRepository = {
95
+ repoName: repo.name,
96
+ repoSha1: resourceRepo.version,
97
+ url: repo.url,
98
+ };
99
+ if (!resourceRepositories.has(resourceRepository)) {
100
+ resourceRepositories.add(resourceRepository);
101
+ }
102
+ }
103
+ return [...resourceRepositories];
104
+ }
105
+
14
106
  async getPipelineFromPipelineId(projectName: string, buildId: number) {
15
107
  let url = `${this.orgUrl}${projectName}/_apis/build/builds/${buildId}`;
16
- return TFSServices.getItemContent(url, this.token, "get");
108
+ return TFSServices.getItemContent(url, this.token, 'get');
17
109
  } //GetCommitForPipeline
18
110
 
19
- async TriggerBuildById(
20
- projectName: string,
21
- buildDefanitionId: string,
22
- parameter: any
23
- ) {
111
+ async getPipelineRunBuildById(projectName: string, pipelineId: number, runId: number) {
112
+ let url = `${this.orgUrl}${projectName}/_apis/pipelines/${pipelineId}/runs/${runId}`;
113
+ return TFSServices.getItemContent(url, this.token);
114
+ }
115
+
116
+ async TriggerBuildById(projectName: string, buildDefanitionId: string, parameter: any) {
24
117
  let data = {
25
118
  definition: {
26
119
  id: buildDefanitionId,
@@ -29,51 +122,26 @@ export default class PipelinesDataProvider {
29
122
  };
30
123
  logger.info(JSON.stringify(data));
31
124
  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
- );
125
+ let res = await TFSServices.postRequest(url, this.token, 'post', data, null);
39
126
  return res;
40
127
  }
41
128
 
42
- async GetArtifactByBuildId(
43
- projectName: string,
44
- buildId: string,
45
- artifactName: string
46
- ): Promise<any> {
129
+ async GetArtifactByBuildId(projectName: string, buildId: string, artifactName: string): Promise<any> {
47
130
  try {
48
131
  logger.info(
49
132
  `Get artifactory from project ${projectName},BuildId ${buildId} artifact name ${artifactName}`
50
133
  );
51
134
  logger.info(`Check if build ${buildId} have artifact`);
52
135
  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
- );
136
+ let response = await TFSServices.getItemContent(url, this.token, 'Get', null, null);
60
137
  if (response.count == 0) {
61
138
  logger.info(`No artifact for build ${buildId} was published `);
62
139
  return response;
63
140
  }
64
141
  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
- );
142
+ let res = await TFSServices.getItemContent(url, this.token, 'Get', null, null);
72
143
  logger.info(`Url for download :${res.resource.downloadUrl}`);
73
- let result = await TFSServices.downloadZipFile(
74
- res.resource.downloadUrl,
75
- this.token
76
- );
144
+ let result = await TFSServices.downloadZipFile(res.resource.downloadUrl, this.token);
77
145
  return result;
78
146
  } catch (err) {
79
147
  logger.error(`Error : ${err}`);
@@ -81,62 +149,41 @@ export default class PipelinesDataProvider {
81
149
  }
82
150
  }
83
151
 
84
- async GetReleaseByReleaseId(
85
- projectName: string,
86
- releaseId: number
87
- ): Promise<any> {
152
+ async GetReleaseByReleaseId(projectName: string, releaseId: number): Promise<any> {
88
153
  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);
154
+ url = url.replace('dev.azure.com', 'vsrm.dev.azure.com');
155
+ return TFSServices.getItemContent(url, this.token, 'get', null, null);
91
156
  }
92
157
 
93
158
  async GetPipelineRunHistory(projectName: string, pipelineId: string) {
94
159
  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
- );
160
+ let res: any = await TFSServices.getItemContent(url, this.token, 'get', null, null);
161
+ //Filter successful builds only
162
+ let { value } = res;
163
+ if (value) {
164
+ const successfulRunHistory = value.filter((run: any) => run.result === 'succeeded');
165
+ return { count: successfulRunHistory.length, value: successfulRunHistory };
166
+ }
102
167
  return res;
103
168
  }
104
169
 
105
170
  async GetReleaseHistory(projectName: string, definitionId: string) {
106
171
  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
- );
172
+ url = url.replace('dev.azure.com', 'vsrm.dev.azure.com');
173
+ let res: any = await TFSServices.getItemContent(url, this.token, 'get', null, null);
115
174
  return res;
116
175
  }
117
176
 
118
177
  async GetAllPipelines(projectName: string) {
119
178
  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
- );
179
+ let res: any = await TFSServices.getItemContent(url, this.token, 'get', null, null);
127
180
  return res;
128
181
  }
129
182
 
130
183
  async GetAllReleaseDefenitions(projectName: string) {
131
184
  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
- );
185
+ url = url.replace('dev.azure.com', 'vsrm.dev.azure.com');
186
+ let res: any = await TFSServices.getItemContent(url, this.token, 'get', null, null);
140
187
  return res;
141
188
  }
142
189
  }