-
Notifications
You must be signed in to change notification settings - Fork 526
/
BbCreationApi.js
200 lines (168 loc) · 6.81 KB
/
BbCreationApi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { capabilityAugmenter, Fetch, UrlConfig, Utils, AppConfig } from '@jenkins-cd/blueocean-core-js';
import { Enum } from '../../flow2/Enum';
const ERROR_FIELD_SCM_CREDENTIAL = 'credentialId';
const CODE_VALIDATION_FAILED = 400;
const ERROR_FIELD_CODE_CONFLICT = 'ALREADY_EXISTS';
const ERROR_FIELD_SCM_URI = 'scmConfig.uri';
export const ListOrganizationsOutcome = new Enum({
SUCCESS: 'success',
INVALID_CREDENTIAL_ID: 'invalid_credential_id',
ERROR: 'error',
});
export const CreateMbpOutcome = new Enum({
SUCCESS: 'success',
INVALID_URI: 'invalid_uri',
INVALID_CREDENTIAL: 'invalid_credential',
INVALID_NAME: 'invalid_name',
ERROR: 'error',
});
function hasErrorFieldName(errors, fieldName) {
return errors.filter(err => err.field === fieldName).length > 0;
}
function hasErrorFieldCode(errors, code) {
return errors.filter(err => err.code === code).length > 0;
}
export class BbCreationApi {
constructor(scmId, fetch) {
this._fetch = fetch || Fetch.fetchJSON;
this.organization = AppConfig.getOrganizationName();
this.scmId = scmId;
this.partialLoadedOrganizations = [];
}
listOrganizations(credentialId, apiUrl, pagedOrgsStart = 0, pageSize = 100) {
const path = UrlConfig.getJenkinsRootURL();
const orgsUrl = Utils.cleanSlashes(
`${path}/blue/rest/organizations/${this.organization}/scm/${
this.scmId
}/organizations/?credentialId=${credentialId}&start=${pagedOrgsStart}&limit=100&apiUrl=${apiUrl}`,
false
);
return this._fetch(orgsUrl)
.then(orgs => capabilityAugmenter.augmentCapabilities(orgs))
.then(orgs => this._listOrganizationsSuccess(orgs, credentialId, apiUrl, pagedOrgsStart), error => this._listOrganizationsFailure(error));
}
_listOrganizationsSuccess(organizations, credentialId, apiUrl, pagedOrgsStart) {
this.partialLoadedOrganizations = this.partialLoadedOrganizations.concat(organizations);
if (organizations.length >= 100) {
//if we got 100 or more orgs, we need to check the next page to see if there are any more orgs
return this.listOrganizations(credentialId, apiUrl, pagedOrgsStart + 100);
} else {
return {
outcome: 'SUCCESS',
organizations: this.partialLoadedOrganizations,
};
}
}
_listOrganizationsFailure(error) {
const { code, message, errors } = error.responseBody;
if (code === 400) {
if (hasErrorFieldName(errors, ERROR_FIELD_SCM_CREDENTIAL)) {
return {
outcome: ListOrganizationsOutcome.INVALID_CREDENTIAL_ID,
};
}
}
return {
outcome: ListOrganizationsOutcome.ERROR,
error: message,
};
}
listRepositories(credentialId, apiUrl, organizationName, pageNumber = 1, pageSize = 100) {
const path = UrlConfig.getJenkinsRootURL();
const reposUrl = Utils.cleanSlashes(
`${path}/blue/rest/organizations/${this.organization}/scm/${this.scmId}/organizations/${organizationName}/repositories/` +
`?credentialId=${credentialId}&pageNumber=${pageNumber}&pageSize=${pageSize}&apiUrl=${apiUrl}`
);
return this._fetch(reposUrl).then(response => capabilityAugmenter.augmentCapabilities(response));
}
createMbp(credentialId, scmId, apiUrl, itemName, bbOrganizationKey, repoName, creatorClass) {
const path = UrlConfig.getJenkinsRootURL();
const createUrl = Utils.cleanSlashes(`${path}/blue/rest/organizations/${this.organization}/pipelines/`);
const requestBody = this._buildRequestBody(credentialId, scmId, apiUrl, itemName, bbOrganizationKey, repoName, creatorClass);
const fetchOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
};
return this._fetch(createUrl, { fetchOptions })
.then(data => capabilityAugmenter.augmentCapabilities(data))
.then(pipeline => this._createMbpSuccess(pipeline), error => this._createMbpFailure(error));
}
checkPipelineNameAvailable(name) {
const path = UrlConfig.getRestBaseURL();
const checkUrl = Utils.cleanSlashes(`${path}/organizations/${this.organization}/pipelines/${name}`);
const fetchOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
return this._fetch(checkUrl, { fetchOptions }).then(() => false, () => true);
}
_createMbpSuccess(pipeline) {
return {
outcome: CreateMbpOutcome.SUCCESS,
pipeline,
};
}
_createMbpFailure(error) {
const { code, errors } = error.responseBody;
if (code === CODE_VALIDATION_FAILED) {
if (errors.length === 1 && hasErrorFieldCode(errors, ERROR_FIELD_CODE_CONFLICT)) {
return {
outcome: CreateMbpOutcome.INVALID_NAME,
};
}
if (hasErrorFieldName(errors, ERROR_FIELD_SCM_URI)) {
return {
outcome: CreateMbpOutcome.INVALID_URI,
};
} else if (hasErrorFieldName(errors, ERROR_FIELD_SCM_CREDENTIAL)) {
return {
outcome: CreateMbpOutcome.INVALID_CREDENTIAL,
};
}
}
return {
outcome: CreateMbpOutcome.ERROR,
error: error.responseBody,
};
}
_buildRequestBody(credentialId, scmId, apiUrl, itemName, organizationName, repoName, creatorClass) {
return {
name: itemName,
$class: creatorClass,
scmConfig: {
id: scmId,
credentialId,
uri: apiUrl,
config: {
repoOwner: organizationName,
repository: repoName,
},
},
};
}
findBranches(pipelineName) {
const path = UrlConfig.getJenkinsRootURL();
const pipelineUrl = Utils.cleanSlashes(`${path}/blue/rest/organizations/${this.organization}/pipelines/${pipelineName}/`);
return this._fetch(pipelineUrl)
.then(response => capabilityAugmenter.augmentCapabilities(response))
.then(pipeline => this._findBranchesSuccess(pipeline), error => this._findBranchesFailure(error));
}
_findBranchesSuccess(pipeline) {
return {
isFound: pipeline.getTotalNumberOfBranches > 0,
hasError: false,
pipeline,
};
}
_findBranchesFailure(error) {
return {
hasError: true,
error,
};
}
}