diff --git a/lib/interface/cli/commands/workflow/workflow-data-item/create.cmd.js b/lib/interface/cli/commands/workflow/workflow-data-item/create.cmd.js new file mode 100644 index 000000000..4325ac139 --- /dev/null +++ b/lib/interface/cli/commands/workflow/workflow-data-item/create.cmd.js @@ -0,0 +1,45 @@ +const _ = require('lodash'); +const Promise = require('bluebird'); +const Command = require('../../../Command'); +const createRoot = require('../../root/create.cmd'); +const { sdk } = require('../../../../../logic'); +const { crudFilenameOption } = require('../../../helpers/general'); + +function builder(y) { + crudFilenameOption(y); + return y + .option('workflow', { + describe: 'Workflow ID that the Workflow-Data-Item belongs to', + required: true, + alias: 'wf', + }) + .example('codefresh create workflow-data-item --workflow [WORKFLOW_ID] --file ./file.json', 'Create new Workflow-Data-Iten that belongs to WORKFLOW_ID with content from "./file.json"') +} + +async function handler({ workflow, filename }) { + let payload; + if (filename) { // file content + payload = filename; + } + const res = await sdk.workflows.pushWorkflowData({ + payload, + workflowId: workflow, + }); + console.log(`Workflow-Data-Item ${_.get(res, '_id')} created`); + return Promise.resolve(); +} + +const command = new Command({ + command: 'workflow-data-item', + aliases: ['wdi'], + parent: createRoot, + description: 'Create New Workflow Data Item', + webDocs: { + category: 'Workflow-Data', + title: 'Create', + }, + builder, + handler, +}); + +module.exports = command; diff --git a/lib/interface/cli/commands/workflow/workflow-data-item/get.cmd.js b/lib/interface/cli/commands/workflow/workflow-data-item/get.cmd.js new file mode 100644 index 000000000..eacafc000 --- /dev/null +++ b/lib/interface/cli/commands/workflow/workflow-data-item/get.cmd.js @@ -0,0 +1,72 @@ +const _ = require('lodash'); +const Promise = require('bluebird'); +const Command = require('../../../Command'); +const getRoot = require('../../root/get.cmd'); +const { sdk } = require('../../../../../logic'); +const Output = require('../../../../../output/Output'); +const WorkflowDataItem = require('../../../../../logic/entities/WorkflowDataItem'); + +async function _get(workflowId, workflowDataItemIds, decrypt) { + const result = []; + await Promise.map((workflowDataItemIds), async (id) => { + const res = await sdk.workflows.getWorkflowDataItem({ + workflowId, + id, + decrypt, + }); + result.push(WorkflowDataItem.fromResponse(res)); + return Promise.resolve(); + }); + return result; +} + +async function _list(workflowId) { + const res = await sdk.workflows.getWorkflowData({ + workflowId, + }); + return _.map(res.docs, WorkflowDataItem.fromResponse); +} + +function builder(y) { + return y + .positional('id', { + describe: 'Workflow-Data-Item ID', + }) + .option('workflow', { + describe: 'Workflow ID that the Workflow-Data-Item belongs to', + required: true, + alias: 'wf', + }) + .option('decrypt', { + describe: 'When requesting spesific Workflow-Data-Item, decrypt the stored data', + typoe: 'boolean', + }) + .example('codefresh get workflow-data-item --workflow [WORKFLOW_ID]', 'Get all Workflow-Data-Items for given WORKFLOW_ID') + .example('codefresh get workflow-data-item [WORKFLOW_DATA_ITEM_ID] --decrypt --workflow [WORKFLOW_ID]', 'Get and decrypt WORKFLOW_DATA_ITEM_ID that is was reported to WORKFLOW_ID'); +} + +async function handler({ workflow, id, decrypt }) { + let res; + if (id && id.length > 0) { + res = await _get(workflow, id, decrypt); + } else { + res = await _list(workflow); + } + Output.print(res); +} + +const command = new Command({ + command: 'workflow-data-item [id..]', + aliases: ['wdi'], + parent: getRoot, + description: 'Get Workflow Data Item', + usage: 'Passing [id] argument will cause a retrieval of a specific Workflow-Data-Item.', + webDocs: { + category: 'Workflow-Data', + title: 'Get', + }, + builder, + handler, +}); + +module.exports = command; diff --git a/lib/logic/entities/WorkflowDataItem.js b/lib/logic/entities/WorkflowDataItem.js new file mode 100644 index 000000000..4c08cbf82 --- /dev/null +++ b/lib/logic/entities/WorkflowDataItem.js @@ -0,0 +1,48 @@ +const _ = require('lodash'); +const Entity = require('./Entity'); + +class WorkflowDataItem extends Entity { + constructor(data) { + super(); + this.entityType = 'workflow-data-item'; + this.info = data; + this.defaultColumns = [ + 'id', + 'created-at', + 'data', + ]; + this.wideColumns = _.clone(this.defaultColumns); + } + + static _strinfigyData(input) { + if (input.data === '*****') { + return input; + } + const data = JSON.stringify(input.data); + return { + ...input, + data, + }; + } + + toDefault() { + return WorkflowDataItem._strinfigyData(super.toDefault()); + } + + toWide() { + return WorkflowDataItem._strinfigyData(super.toWide()); + } + + static fromResponse(response) { + const id = response._id; + const createdAt = _.get(response, 'metadata.createdAt'); + const data = _.get(response, 'data', '*****'); + return new WorkflowDataItem({ + id, + 'created-at': createdAt, + data, + }); + } +} + +module.exports = WorkflowDataItem; diff --git a/openapi.json b/openapi.json index 645882150..4d0cb7956 100644 --- a/openapi.json +++ b/openapi.json @@ -2193,6 +2193,128 @@ "x-sdk-interface": "workflows.list" } }, + "/workflow/{workflowId}/data": { + "post": { + "operationId": "push-workflow-data", + "parameters": [ + { + "description": "id of a workflowId object", + "in": "path", + "name": "workflowId", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "201": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + }, + "description": "json" + } + }, + "tags": [ + "Builds" + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + } + }, + "summary": "Push New Workflow Data", + "x-sdk-interface": "workflows.pushWorkflowData" + }, + "get": { + "operationId": "get-workflow-data", + "parameters": [ + { + "description": "id of a workflowId object", + "in": "path", + "name": "workflowId", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + }, + "description": "json" + } + }, + "tags": [ + "Builds" + ], + "summary": "Get Workflow Data", + "x-sdk-interface": "workflows.getWorkflowData" + } + }, + "/workflow/{workflowId}/data/{id}": { + "get": { + "operationId": "get-workflow-data-item", + "parameters": [ + { + "description": "id of a workflowId object", + "in": "path", + "name": "workflowId", + "required": true, + "schema": { + "type": "string" + } + }, + { + "description": "id of an object", + "in": "path", + "name": "id", + "required": true, + "schema": { + "type": "string" + } + }, + { + "in": "query", + "name": "decrypt", + "schema": { + "type": "boolean" + } + } + ], + "responses": { + "200": { + "content": { + "application/json": { + "schema": { + "type": "object" + } + } + }, + "description": "json" + } + }, + "tags": [ + "Builds" + ], + "summary": "Get Workflow Data Item", + "x-sdk-interface": "workflows.getWorkflowDataItem" + } + }, "/builds/{buildId}/update": { "post": { "parameters": [ diff --git a/package.json b/package.json index 22874281e..dfcefe84a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "codefresh", - "version": "0.69.6", + "version": "0.70.0", "description": "Codefresh command line utility", "main": "index.js", "preferGlobal": true,