Skip to content

Commit

Permalink
refactor shared code
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Emslie committed Aug 13, 2020
1 parent bd6c726 commit 32f35a3
Show file tree
Hide file tree
Showing 25 changed files with 1,686 additions and 757 deletions.
164 changes: 7 additions & 157 deletions Tasks/DownloadArtifactsNexus/DownloadArtifactsNexusV0_2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import tl = require('azure-pipelines-task-lib/task');
import shell = require("shelljs");
import path = require("path");
import fs = require('fs');
import * as xml2js from 'xml2js';
import * as xpath from "xml2js-xpath";
import { IhttpHelper } from './IhttpHelper';
import { httpHelper } from './httpHelper';
const nexus : IhttpHelper = new httpHelper();
import n = require('nexus-v2');
const nexus = new n.nexus();

async function run() {
console.log(`Downloading artifact.`);
Expand Down Expand Up @@ -47,7 +44,7 @@ async function run() {
const group: string | undefined = tl.getInput("group", true);
const artifact: string | undefined = tl.getInput("artifact", true);
const baseVersion: string | undefined = tl.getInput("version", true);
const packaging: string | undefined = tl.getInput("packaging", false);
const packaging: string | undefined = tl.getInput("packaging", true);
const classifier: string | undefined = tl.getInput("classifier", false);
const extension: string | undefined = tl.getInput("extension", false);
const downloadPath: string | undefined = tl.getInput("downloadPath", false);
Expand Down Expand Up @@ -89,15 +86,7 @@ async function run() {
}

tl.debug(`HostUri set to '${hostUri}'`);

if(packaging)
{
console.log(`Using Packaging ${packaging}.`);
}
else
{
console.log('Packaging has not been supplied.');
}
console.log(`Using Packaging ${packaging}.`);

if(extension)
{
Expand All @@ -120,12 +109,12 @@ async function run() {
// Do we have packaging and extension? if not lets download all files.
// https://help.sonatype.com/repomanager3/repository-manager-concepts/components%2C-repositories%2C-and-repository-formats
if(!extension)
{
await downloadAssets(hostUri.href, auth, acceptUntrustedCerts, repository, group, artifact, baseVersion, classifier, packaging);
{
await nexus.downloadAssets(hostUri.href, auth, acceptUntrustedCerts, repository, group, artifact, baseVersion, packaging, classifier);
}
else
{
await downloadAsset(hostUri.href, auth, acceptUntrustedCerts, repository, group, artifact, baseVersion, extension, classifier, packaging);
await nexus.downloadAsset(hostUri.href, auth, acceptUntrustedCerts, repository, group, artifact, baseVersion, extension, packaging, classifier);
}
}
catch (err) {
Expand All @@ -134,144 +123,5 @@ async function run() {
console.log(`Downloading artifact completed.`);
}

async function downloadAsset(nexusUrl: string, auth: tl.EndpointAuthorization, acceptUntrustedCerts: boolean, repository : string, group : string, artifact : string, baseVersion : string, extension : string, classifier? : string, packaging? : string) : Promise<void> {
// Build the final download uri
// https://support.sonatype.com/hc/en-us/articles/213465488
// https://repository.sonatype.org/nexus-restlet1x-plugin/default/docs/path__artifact_maven_redirect.html
let hostUri = new URL(nexusUrl);
let requestPath : string = `/service/local/artifact/maven/redirect`;

// Handle root path
if(hostUri.pathname !== "/")
{
requestPath = path.join(hostUri.pathname, requestPath);
}
hostUri.pathname = requestPath;

// Query Parameters
hostUri.searchParams.append("r", repository);
hostUri.searchParams.append("g", group);
hostUri.searchParams.append("a", artifact);
hostUri.searchParams.append("v", baseVersion);

if(packaging)
{
hostUri.searchParams.append("p", packaging);
}

// Do we have a extension
if (extension) {
hostUri.searchParams.append("e", extension);
}

// Do we have a classifier
if (classifier) {
hostUri.searchParams.append("c", classifier);
}

console.log(`Download asset using '${hostUri}'.`);
// Execute the request
await executeRequest(hostUri, auth, acceptUntrustedCerts);
console.log(`Completed download asset using '${hostUri}'.`);
}

async function downloadAssets(nexusUrl: string, auth: tl.EndpointAuthorization, acceptUntrustedCerts: boolean, repository : string, group : string, artifact : string, baseVersion : string, classifier? : string, packaging? : string) : Promise<void> {
// Build the final search uri
// https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/path__lucene_search.html
// https://nexusrepov2vm1.azure-zone.net:8443/nexus/service/local/lucene/search?repositoryId=releases&g=org.apache.maven&a=maven-artifact&v=3.6.3
// https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/el_ns0_searchNGResponse.html
// https://repository.sonatype.org/nexus-indexer-lucene-plugin/default/docs/ns0.xsd
let hostUri = new URL(nexusUrl);
let lucenePath : string = `/service/local/lucene/search`;

// Handle root path
if(hostUri.pathname !== "/")
{
lucenePath = path.join(hostUri.pathname, lucenePath);
}
hostUri.pathname = lucenePath;
// Query Parameters
hostUri.searchParams.append("repositoryId", repository);
hostUri.searchParams.append("g", group);
hostUri.searchParams.append("a", artifact);
hostUri.searchParams.append("v", baseVersion);
// Do we have a classifier
if (classifier) {
hostUri.searchParams.append("c", classifier);
}
if(packaging)
{
hostUri.searchParams.append("p", packaging);
}
console.log(`Search for asset using '${hostUri}'.`);

// perform lucene Search
var responseContent = await executeRequest(hostUri, auth, acceptUntrustedCerts);
tl.debug(`Response Content '${responseContent}'.`);
var extensions : string[] = await parseExtensions(responseContent);

// Download each asset
for(var extension in extensions){
await downloadAsset(nexusUrl, auth, acceptUntrustedCerts, repository, group, artifact, baseVersion, extensions[extension], classifier);
}

console.log(`Completed search for asset using '${hostUri}'.`);
}

async function parseExtensions(xml: string) : Promise<string[]> {
return new Promise((resolve, reject) => {
xml2js.parseString(xml, { explicitArray : false }, function (err, result) {
if(err)
{
console.log(`Failed to parse response XML.`);
reject(err);
}
else
{
if(result.searchNGResponse.totalCount == 1)
{
resolve(xpath.find(result.searchNGResponse.data.artifact.artifactHits, "//extension"));
}
else
{
let message = `Search result XML contains multiple artifactHits.`;
console.log(message);
reject(message);
}
}
});
});
}

async function executeRequest(hostUri: URL, auth: tl.EndpointAuthorization, acceptUntrustedCerts: boolean) : Promise<string> {
var responseContent: string;
try {
if (hostUri.protocol === "https:") {
if (auth.scheme === "UsernamePassword") {
responseContent = await nexus.execute_https(hostUri, acceptUntrustedCerts, auth.parameters["username"], auth.parameters["password"]);
}

else {
responseContent = await nexus.execute_https(hostUri, acceptUntrustedCerts);
}
}

else {
if (auth.scheme === "UsernamePassword") {
responseContent = await nexus.execute_http(hostUri, auth.parameters["username"], auth.parameters["password"]);
}

else {
responseContent = await nexus.execute_http(hostUri);
}
}
} catch (inner_err) {
console.log(`Failed to execute request '${hostUri}'.`);
throw inner_err;
}
return responseContent;
}


run();

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"azure-pipelines-task-lib": "^2.9.3",
"fs": "0.0.1-security",
"path": "^0.12.7",
"shelljs": "^0.8.3",
"xml2js": "^0.4.23",
"xml2js-xpath": "^0.11.0"
"shelljs": "^0.8.3"
},
"devDependencies": {
"@types/node": "^12.12.15",
Expand Down

This file was deleted.

Loading

0 comments on commit 32f35a3

Please sign in to comment.