Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1251841: JenkinsDownloadArtifacts task fails with "ExtractionFail… #7188

Merged
merged 2 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,42 @@ export class JenkinsRestClient {
return defer.promise;
}

public HasAssociatedArtifacts(artifactQueryUrl: string): Q.Promise<boolean> {
let defer = Q.defer<boolean>();
this.GetClient().get(artifactQueryUrl).then((response: HttpClientResponse) => {
response.readBody().then((body: string) => {
if (!!body && response.message.statusCode === 200) {
let jsonResult;
try {
jsonResult = JSON.parse(body);

if (!!jsonResult.artifacts && jsonResult.artifacts.length > 0) {
defer.resolve(true);
}
else {
defer.resolve(false);
}
} catch (error) {
defer.reject(error);
}
}
else {
if (response.message.statusCode) {
console.log(tl.loc('ServerCallErrorCode', response.message.statusCode));
}

if (body) {
tl.debug(body);
}

defer.reject(new Error(tl.loc('ServerCallFailed')));
}
})
});

return defer.promise;
}

private IsValidBuildId(buildId: number, multiBranchName: string, isMultiBranch: boolean): boolean {
if (isNaN(buildId)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@
"loc.messages.ContinuationTokenExistsFetchingRemainingFiles": "Continuation token exists, trying to fetch the list of remaining files.",
"loc.messages.GetArtifactItemsNotSupported": "Get artifact items not supported, invalid code path",
"loc.messages.CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode": "Could not fetch access token for Managed Service Principal. Please configure Managed Service Identity (MSI) for virtual machine 'https://aka.ms/azure-msi-docs'. Status code: %s, status message: %s",
"loc.messages.CouldNotFetchAccessTokenforMSIStatusCode": "Could not fetch access token for Managed Service Principal. Status code: %s, status message: %s"
"loc.messages.CouldNotFetchAccessTokenforMSIStatusCode": "Could not fetch access token for Managed Service Principal. Status code: %s, status message: %s",
"loc.messages.NoAssociatedArtifacts": "No associated artifacts found"
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{}');
}

if (url === "http://url//job/myfreestyleproject//20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{}');
}

if (url === "http://url//job/myfreestyleproject//20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url.indexOf('allBuilds[number]') !== -1) {
return helper.GetSuccessExpectedResult('{"allBuilds":[{"number":22},{"number":21},{"number":20},{"number":18},{"number":15},{"number":14},{"number":13}]}');
}

if (url === "http://url//job/testmultibranchproject//job/master/20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/folder1/job/folder2/job/testmultibranchproject//api/json") {
return helper.GetSuccessExpectedResult('{ "_class": "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" }');
};

if (url === "http://url//job/folder1/job/folder2/job/testmultibranchproject//job/master/20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{}');
}

if (url === "http://url//job/myfreestyleproject//15/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{}');
}

if (url === "http://url//job/myfreestyleproject//15/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/myfreestyleproject//api/json") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleProject" }');
}
if (url === "http://url//job/myfreestyleproject//20/api/json?tree=artifacts[*]") {
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ helper.RegisterHttpClientMock(tr, (url: string) => {
if (url === "http://url/job/testmultibranchproject//api/json") {
return helper.GetSuccessExpectedResult('{ "_class": "org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject" }');
};

if (url === "http://url//job/testmultibranchproject//job/master/20/api/json?tree=artifacts[*]"){
return helper.GetSuccessExpectedResult('{ "_class": "hudson.model.FreeStyleBuild", "artifacts": [ "abc" ] }');
}
});

tr.run();
69 changes: 38 additions & 31 deletions Tasks/JenkinsDownloadArtifacts/jenkinsdownloadartifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,42 +158,49 @@ async function doWork() {
};

var handler = new handlers.BasicCredentialHandler(username, password);
if (!itemPattern || itemPattern === '**') {
const archiveUrl: string = `${serverEndpointUrl}/${jenkinsJobDetails.jobUrlInfix}/${jenkinsJobDetails.multiBranchPipelineUrlInfix}/${jenkinsJobDetails.buildId}/artifact/*zip*/`
var zipLocation = path.join(localPathRoot, "archive.zip");
await getZipFromUrl(archiveUrl, strictSSL, zipLocation, handler);

var unzipPromise = unzip(zipLocation, localPathRoot);
unzipPromise.catch((error) => {
throw error;
});

await unzipPromise;

if (tl.exist(zipLocation)) {
tl.rmRF(zipLocation);
}
var hasArtifacts = await jenkinsClient.HasAssociatedArtifacts(artifactQueryUrl);

var archivePath = path.join(localPathRoot, "archive");
var tempPath = path.join(localPathRoot, uuidv4());
fsExtra.move(archivePath, tempPath)
.then(() => {
fsExtra.copy(tempPath, localPathRoot)
.then(() => {
fsExtra.remove(tempPath).catch((error) => {
throw error;
});
})
.catch((error) => {
throw error;
});
})
.catch((error) => {
if (hasArtifacts) {
if (!itemPattern || itemPattern === '**') {
const archiveUrl: string = `${serverEndpointUrl}/${jenkinsJobDetails.jobUrlInfix}/${jenkinsJobDetails.multiBranchPipelineUrlInfix}/${jenkinsJobDetails.buildId}/artifact/*zip*/`
var zipLocation = path.join(localPathRoot, "archive.zip");
await getZipFromUrl(archiveUrl, strictSSL, zipLocation, handler);

var unzipPromise = unzip(zipLocation, localPathRoot);
unzipPromise.catch((error) => {
throw error;
});

await unzipPromise;

if (tl.exist(zipLocation)) {
tl.rmRF(zipLocation);
}

var archivePath = path.join(localPathRoot, "archive");
var tempPath = path.join(localPathRoot, uuidv4());
fsExtra.move(archivePath, tempPath)
.then(() => {
fsExtra.copy(tempPath, localPathRoot)
.then(() => {
fsExtra.remove(tempPath).catch((error) => {
throw error;
});
})
.catch((error) => {
throw error;
});
})
.catch((error) => {
throw error;
});
}
else {
await getArtifactsFromUrl(artifactQueryUrl, strictSSL, localPathRoot, itemPattern, handler, variables);
}
}
else {
await getArtifactsFromUrl(artifactQueryUrl, strictSSL, localPathRoot, itemPattern, handler, variables);
console.log(tl.loc('NoAssociatedArtifacts'));
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tasks/JenkinsDownloadArtifacts/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
"ContinuationTokenExistsFetchingRemainingFiles": "Continuation token exists, trying to fetch the list of remaining files.",
"GetArtifactItemsNotSupported": "Get artifact items not supported, invalid code path",
"CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode": "Could not fetch access token for Managed Service Principal. Please configure Managed Service Identity (MSI) for virtual machine 'https://aka.ms/azure-msi-docs'. Status code: %s, status message: %s",
"CouldNotFetchAccessTokenforMSIStatusCode": "Could not fetch access token for Managed Service Principal. Status code: %s, status message: %s"
"CouldNotFetchAccessTokenforMSIStatusCode": "Could not fetch access token for Managed Service Principal. Status code: %s, status message: %s",
"NoAssociatedArtifacts": "No associated artifacts found"
}
}
3 changes: 2 additions & 1 deletion Tasks/JenkinsDownloadArtifacts/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@
"ContinuationTokenExistsFetchingRemainingFiles": "ms-resource:loc.messages.ContinuationTokenExistsFetchingRemainingFiles",
"GetArtifactItemsNotSupported": "ms-resource:loc.messages.GetArtifactItemsNotSupported",
"CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode": "ms-resource:loc.messages.CouldNotFetchAccessTokenforMSIDueToMSINotConfiguredProperlyStatusCode",
"CouldNotFetchAccessTokenforMSIStatusCode": "ms-resource:loc.messages.CouldNotFetchAccessTokenforMSIStatusCode"
"CouldNotFetchAccessTokenforMSIStatusCode": "ms-resource:loc.messages.CouldNotFetchAccessTokenforMSIStatusCode",
"NoAssociatedArtifacts": "ms-resource:loc.messages.NoAssociatedArtifacts"
}
}