Skip to content

Commit bec8949

Browse files
Feature/olegz/saas 5245 download audit cmd (#400)
* SAAS-5245 #time 0m 0h Added download audit cmd * SAAS-5245 #time 0m 0h Added download audit cmd * SAAS-5245 #time 0m 0h Added download/audit to openapi * SAAS-5245 #time 0m 0h Up version * SAAS-5245 #time 0m 0h Up version
1 parent c271682 commit bec8949

File tree

4 files changed

+203
-1
lines changed

4 files changed

+203
-1
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const fs = require('fs');
2+
const moment = require('moment');
3+
const Command = require('../../Command');
4+
const getRoot = require('../root/download.cmd');
5+
const openApi = require('../../../../../openapi.json');
6+
const { sdk } = require('../../../../logic');
7+
8+
const DEFAULT_DIR = process.cwd();
9+
const DEFAULT_FILE_NAME = `codefresh_audit_${moment(new Date())
10+
.format('YYYY-MM-DD_HH-mm-ss')}.csv`;
11+
12+
const apiParameters = openApi.paths['/audit/download'].get.parameters
13+
.map(({ name, description, schema }) => ({
14+
name,
15+
description,
16+
choices: schema.enum,
17+
}));
18+
19+
function _buildRequestOptions(argv){
20+
return apiParameters.reduce((memo, { name } = {}) => {
21+
if (name in argv) {
22+
Object.assign(memo, { [name]: argv[name] });
23+
}
24+
return memo;
25+
}, {});
26+
}
27+
28+
const command = new Command({
29+
command: 'audit',
30+
parent: getRoot,
31+
description: 'Download audit',
32+
webDocs: {
33+
category: 'Audit',
34+
title: 'Download',
35+
},
36+
builder: (yargs) => {
37+
apiParameters
38+
.forEach(({ name, description, choices }) => {
39+
yargs.option(name, Object.assign({ describe: description }, choices && { choices }));
40+
});
41+
42+
return yargs
43+
.option('file', {
44+
describe: 'The name of the file and the directory in which the audit will be saved',
45+
default: `${DEFAULT_DIR}/${DEFAULT_FILE_NAME}`,
46+
})
47+
.example('codefresh download audit --file /var/www/audit.csv')
48+
.example('--entity account --rs_status 200 --sortType DESK --sortField createdAt');
49+
},
50+
handler: async (argv) => {
51+
const requestOptions = _buildRequestOptions(argv);
52+
const content = await sdk.audit.download(requestOptions);
53+
fs.writeFileSync(argv.file, content, 'utf-8');
54+
},
55+
});
56+
57+
module.exports = command;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const Command = require('../../Command');
2+
const yargs = require('yargs');
3+
4+
const download = new Command({
5+
root: true,
6+
command: 'download',
7+
description: 'Download resources',
8+
usage: 'Codefresh download <resource>',
9+
webDocs: {
10+
title: 'Download',
11+
weight: 70,
12+
},
13+
handler: async () => {
14+
yargs.showHelp();
15+
},
16+
});
17+
18+
module.exports = download;

openapi.json

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,133 @@
285285
}
286286
],
287287
"paths": {
288+
"/audit/download": {
289+
"get": {
290+
"responses": {
291+
"200": {
292+
"$ref": "#/components/responses/Json"
293+
}
294+
},
295+
"tags": [
296+
"audit",
297+
"download"
298+
],
299+
"operationId": "audit-download",
300+
"parameters": [
301+
{
302+
"name": "userName",
303+
"in": "query",
304+
"schema": {
305+
"type": "string"
306+
},
307+
"description": "filter by user name"
308+
},
309+
{
310+
"name": "userId",
311+
"in": "query",
312+
"schema": {
313+
"type": "string"
314+
},
315+
"description": "filter by codefresh user id"
316+
},
317+
{
318+
"name": "entity",
319+
"in": "query",
320+
"schema": {
321+
"type": "string"
322+
},
323+
"description": "filter by api entity, example: pipeline, account"
324+
},
325+
{
326+
"name": "entityId",
327+
"in": "query",
328+
"schema": {
329+
"type": "string"
330+
},
331+
"description": "filter by api entity id, this value different for each action, example: email, user id"
332+
},
333+
{
334+
"name": "action",
335+
"in": "query",
336+
"schema": {
337+
"type": "string"
338+
},
339+
"description": "filter by api action, get possible values on /api/audit/entitiesList, you will see only actions which was happen in your account"
340+
},
341+
{
342+
"name": "rs_status",
343+
"in": "query",
344+
"schema": {
345+
"type": "integer"
346+
},
347+
"description": "filter by response status code"
348+
},
349+
{
350+
"name": "showTriggers",
351+
"in": "query",
352+
"schema": {
353+
"type": "string"
354+
},
355+
"description": "returns only actions related to receiving triggers"
356+
},
357+
{
358+
"name": "search",
359+
"in": "query",
360+
"schema": {
361+
"type": "string"
362+
},
363+
"description": "filter by searching substring in request body or query or params or headers or url"
364+
},
365+
{
366+
"name": "from",
367+
"in": "query",
368+
"schema": {
369+
"type": "integer"
370+
},
371+
"description": "filter by date, value is number of milliseconds since 1970"
372+
},
373+
{
374+
"name": "to",
375+
"in": "query",
376+
"schema": {
377+
"type": "integer"
378+
},
379+
"description": "filter by date, value is number of milliseconds since 1970"
380+
},
381+
{
382+
"name": "sortField",
383+
"in": "query",
384+
"schema": {
385+
"type": "string",
386+
"enum": ["id", "createdAt", "updatedAt"]
387+
},
388+
"description": "name of sorting field, values: id, createdAt, updatedAt"
389+
},
390+
{
391+
"name": "sortType",
392+
"in": "query",
393+
"schema": {
394+
"type": "string",
395+
"enum": ["ASK", "DESK"]
396+
},
397+
"description": "sorting type, values: ASK, DESK"
398+
}
399+
],
400+
"summary": "Download",
401+
"x-sdk-interface": "audit.download",
402+
"x-endpoint": {
403+
"auth": {
404+
"middleware": [
405+
"auth.isAuthenticated"
406+
]
407+
},
408+
"postMiddleware": [
409+
"global.iseMiddleware"
410+
],
411+
"handler": "audit.download"
412+
}
413+
}
414+
},
288415
"/abac": {
289416
"get": {
290417
"responses": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.41.13",
3+
"version": "0.42.0",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)