Last active
April 26, 2017 07:30
-
-
Save pcan/39d09fcbe17e690e0ee3170c95f02c4c to your computer and use it in GitHub Desktop.
NodeJS script for Nexus 3 massive artifact deletion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var server = 'localhost:8081'; | |
var client = new NexusClient(server); | |
client.login('user', 'password') | |
.then(() => client.deleteArtifacts('maven-releases', 'it.pcan.test', '0.10.0').then(response => console.log(response.body))) | |
.then(() => client.deleteArtifacts('maven-releases', 'it.pcan.test', '0.10.1').then(response => console.log(response.body))) | |
.then(() => client.deleteArtifacts('maven-releases', 'it.pcan.test', '0.10.2').then(response => console.log(response.body))); | |
function NexusClient(server) { | |
var hostAndPort = server.split(':'); | |
if(hostAndPort.length != 2) { | |
throw new Error('Server must be `hostname:port`.'); | |
} | |
var host = hostAndPort[0]; | |
var port = parseInt(hostAndPort[1], 10); | |
var tid = 0; | |
Object.defineProperties(this, { | |
login: readOnlyField(login), | |
getRepositories: readOnlyField(getRepositories), | |
getArtifacts: readOnlyField(getArtifacts), | |
deleteArtifacts: readOnlyField(deleteArtifacts), | |
}); | |
var cookie; | |
function login(username, password) { | |
var loginOptions = buildOptions(host, port, '/service/rapture/session'); | |
loginOptions.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; | |
var reqString = 'username=' + encodeURIComponent(new Buffer(username).toString('base64')) + '&' + 'password=' + encodeURIComponent(new Buffer(password).toString('base64')); | |
var promise = serviceCall(loginOptions, reqString); | |
promise.then((response) => { | |
cookie = response.headers['set-cookie'][0]; | |
}) | |
return promise; | |
} | |
function getRepositories() { | |
var options = buildRequestOptions(); | |
return serviceCall(options, { | |
"action": "coreui_Repository", | |
"method": "readReferences", | |
"data": [{ | |
"page": 1, | |
"start": 0, | |
"limit": 25, | |
"filter": [{ | |
"property": "applyPermissions", | |
"value": true | |
}] | |
}], | |
"type": "rpc", | |
tid: tid++ | |
}); | |
} | |
function getArtifacts(repoName) { | |
var options = buildRequestOptions(); | |
return serviceCall(options, { | |
"action": "coreui_Component", | |
"method": "read", | |
"data": [{ | |
"page": 1, | |
"start": 0, | |
"limit": 350, | |
"sort": [{ | |
"property": "name", | |
"direction": "ASC" | |
}], | |
"filter": [{ | |
"property": "repositoryName", | |
"value": repoName | |
}] | |
}], | |
"type": "rpc", | |
"tid": tid++ | |
}); | |
} | |
function deleteArtifacts(repoName, groupId, version) { | |
return getArtifacts(repoName).then(response => { | |
var artifacts = response.body.result.data; | |
var request = artifacts | |
.filter( a => a.group.startsWith(groupId) && a.version.startsWith(version)) | |
.map(a => ({ | |
"action": "coreui_Component", | |
"method": "deleteComponent", | |
"data": [a.id, repoName], | |
"type": "rpc", | |
"tid": tid++ | |
})); | |
if(request.length > 0) { | |
var options = buildRequestOptions(); | |
return serviceCall(options, request); | |
} else { | |
return Promise.resolve({body:{}}); | |
} | |
}); | |
} | |
function buildRequestOptions(path) { | |
return buildOptions(host, 8081, path || '/service/extdirect', cookie); | |
} | |
function readOnlyField(field) { | |
return { | |
value: field, | |
writable: false | |
}; | |
} | |
function serviceCall(options, data) { | |
return new Promise((resolve, reject) => { | |
data = (typeof data === 'object') ? JSON.stringify(data) : data; | |
var http = require('http'); | |
var req = http.request(options, (res) => { | |
if (res.statusCode < 200 || res.statusCode > 299) { | |
reject(new Error('Failed to load resource, status code: ' + res.statusCode)); | |
} | |
var body = []; | |
res.on('data', (chunk) => { | |
body.push(chunk); | |
}); | |
res.on('end', () => { | |
body = body.join(''); | |
resolve({ | |
statusCode: res.statusCode, | |
headers: res.headers, | |
body: body.length > 1 ? JSON.parse(body) : '' | |
}); | |
}); | |
}); | |
req.on('error', (err) => { | |
reject(err); | |
}); | |
if (data) { | |
req.write(data); | |
} | |
req.end(); | |
}); | |
} | |
function buildOptions(hostname, port, path, cookie) { | |
var host = hostname + ':' + port; | |
var origin = 'http://' + host; | |
var options = { | |
method: 'POST', | |
hostname: hostname, | |
port: port, | |
path: path, | |
headers: { | |
Host: host, | |
Origin: origin, | |
Referer: origin + '/', | |
'X-Nexus-UI': 'true', | |
'X-Requested-With': 'XMLHttpRequest', | |
'Content-Type': 'application/json' | |
} | |
} | |
if(cookie) { | |
options.headers.Cookie = cookie; | |
} | |
return options; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment