Skip to content

Commit a6cfe2f

Browse files
author
Jeff Young
committed
Minor cleanup
1 parent 32730cc commit a6cfe2f

File tree

1 file changed

+42
-45
lines changed

1 file changed

+42
-45
lines changed

Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,24 @@ class Credential {
2121
function getRequest(url: string, cred: Credential, strictSSL: boolean): Q.Promise<any> {
2222
const defer = Q.defer<any>();
2323

24-
request
25-
.get({url: url, strictSSL: strictSSL}, (err, res, body) => {
26-
if (res && body && res.statusCode === 200) {
27-
defer.resolve(JSON.parse(body));
28-
} else {
29-
if (res && res.statusCode) {
30-
tl.debug(tl.loc('ServerCallErrorCode', res.statusCode));
31-
}
32-
if (body) {
33-
tl.debug(body);
24+
request.get({url: url, strictSSL: strictSSL}, (err, res, body) => {
25+
if (res && body && res.statusCode === 200) {
26+
defer.resolve(JSON.parse(body));
27+
} else {
28+
if (res && res.statusCode) {
29+
tl.debug(tl.loc('ServerCallErrorCode', res.statusCode));
30+
}
31+
if (body) {
32+
tl.debug(body);
33+
}
34+
defer.reject(new Error(tl.loc('ServerCallFailed')));
3435
}
35-
defer.reject(new Error(tl.loc('ServerCallFailed')));
36-
}
37-
})
38-
.auth(cred.mUsername, cred.mPassword, true)
39-
.on('error', (err) => {
40-
//TODO: Do we even need an 'error' handler here if we're just re-throwing?
41-
defer.reject(new Error(err.message));
42-
});
36+
})
37+
.auth(cred.mUsername, cred.mPassword, true)
38+
.on('error', (err) => {
39+
//TODO: Do we even need an 'error' handler here if we're just re-throwing?
40+
defer.reject(new Error(err.message));
41+
});
4342

4443
return defer.promise;
4544
}
@@ -48,20 +47,19 @@ function getLastSuccessful(serverEndpointUrl: string, jobName: string, cred: Cre
4847
const defer = Q.defer<number>();
4948
const lastSuccessfulUrl: string = `${serverEndpointUrl}/job/${jobName}/api/json?tree=lastSuccessfulBuild[id,displayname]`;
5049

51-
getRequest(lastSuccessfulUrl, cred, strictSSL)
52-
.then((result) => {
53-
if (result && result['lastSuccessfulBuild']) {
54-
let lastSuccessfulBuildId = result['lastSuccessfulBuild']['id'];
55-
if (lastSuccessfulBuildId) {
56-
defer.resolve(lastSuccessfulBuildId);
50+
getRequest(lastSuccessfulUrl, cred, strictSSL).then((result) => {
51+
if (result && result['lastSuccessfulBuild']) {
52+
const lastSuccessfulBuildId = result['lastSuccessfulBuild']['id'];
53+
if (lastSuccessfulBuildId) {
54+
defer.resolve(lastSuccessfulBuildId);
55+
} else {
56+
defer.reject(new Error(tl.loc('CouldNotGetLastSuccessfuilBuildNumber')));
57+
}
5758
} else {
5859
defer.reject(new Error(tl.loc('CouldNotGetLastSuccessfuilBuildNumber')));
5960
}
60-
} else {
61-
defer.reject(new Error(tl.loc('CouldNotGetLastSuccessfuilBuildNumber')));
62-
}
63-
})
64-
.fail((err) => { defer.reject(err); });
61+
})
62+
.fail((err) => { defer.reject(err); });
6563

6664
return defer.promise;
6765
}
@@ -70,24 +68,23 @@ function getArtifactsRelativePaths(serverEndpointUrl: string, jobName: string, j
7068
const defer = Q.defer<string[]>();
7169
const artifactQueryUrl: string = `${serverEndpointUrl}/job/${jobName}/${jobBuildId}/api/json?tree=artifacts[*]`;
7270

73-
getRequest(artifactQueryUrl, cred, strictSSL)
74-
.then((result) => {
75-
if (result && result['artifacts']) {
76-
let artifacts = result['artifacts'];
77-
if (artifacts.length === 0) {
78-
defer.reject(new Error(tl.loc('CouldNotFindArtifacts', jobName, jobBuildId)));
71+
getRequest(artifactQueryUrl, cred, strictSSL).then((result) => {
72+
if (result && result['artifacts']) {
73+
const artifacts = result['artifacts'];
74+
if (artifacts.length === 0) {
75+
defer.reject(new Error(tl.loc('CouldNotFindArtifacts', jobName, jobBuildId)));
76+
} else {
77+
const artifactsRelativePaths = result['artifacts'].map((artifact) => {
78+
return artifact['relativePath'];
79+
});
80+
defer.resolve(artifactsRelativePaths);
81+
}
7982
} else {
80-
let artifactsRelativePaths = result['artifacts'].map((artifact) => {
81-
return artifact['relativePath'];
82-
});
83-
defer.resolve(artifactsRelativePaths);
83+
// no artifacts for this job
84+
defer.reject(new Error(tl.loc('CouldNotFindArtifacts', jobName, jobBuildId)));
8485
}
85-
} else {
86-
// no artifacts for this job
87-
defer.reject(new Error(tl.loc('CouldNotFindArtifacts', jobName, jobBuildId)));
88-
}
89-
})
90-
.fail((err) => { defer.reject(err); });
86+
})
87+
.fail((err) => { defer.reject(err); });
9188

9289
return defer.promise;
9390
}

0 commit comments

Comments
 (0)