Skip to content

Commit 972cb15

Browse files
author
Oleg Sucharevich
authored
add commands to get and create workflow-data-item (#525)
* add commands to get and create workflow-data-item
1 parent c081e14 commit 972cb15

File tree

5 files changed

+288
-1
lines changed

5 files changed

+288
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const _ = require('lodash');
2+
const Promise = require('bluebird');
3+
const Command = require('../../../Command');
4+
const createRoot = require('../../root/create.cmd');
5+
const { sdk } = require('../../../../../logic');
6+
const { crudFilenameOption } = require('../../../helpers/general');
7+
8+
function builder(y) {
9+
crudFilenameOption(y);
10+
return y
11+
.option('workflow', {
12+
describe: 'Workflow ID that the Workflow-Data-Item belongs to',
13+
required: true,
14+
alias: 'wf',
15+
})
16+
.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"')
17+
}
18+
19+
async function handler({ workflow, filename }) {
20+
let payload;
21+
if (filename) { // file content
22+
payload = filename;
23+
}
24+
const res = await sdk.workflows.pushWorkflowData({
25+
payload,
26+
workflowId: workflow,
27+
});
28+
console.log(`Workflow-Data-Item ${_.get(res, '_id')} created`);
29+
return Promise.resolve();
30+
}
31+
32+
const command = new Command({
33+
command: 'workflow-data-item',
34+
aliases: ['wdi'],
35+
parent: createRoot,
36+
description: 'Create New Workflow Data Item',
37+
webDocs: {
38+
category: 'Workflow-Data',
39+
title: 'Create',
40+
},
41+
builder,
42+
handler,
43+
});
44+
45+
module.exports = command;
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const _ = require('lodash');
2+
const Promise = require('bluebird');
3+
const Command = require('../../../Command');
4+
const getRoot = require('../../root/get.cmd');
5+
const { sdk } = require('../../../../../logic');
6+
const Output = require('../../../../../output/Output');
7+
const WorkflowDataItem = require('../../../../../logic/entities/WorkflowDataItem');
8+
9+
async function _get(workflowId, workflowDataItemIds, decrypt) {
10+
const result = [];
11+
await Promise.map((workflowDataItemIds), async (id) => {
12+
const res = await sdk.workflows.getWorkflowDataItem({
13+
workflowId,
14+
id,
15+
decrypt,
16+
});
17+
result.push(WorkflowDataItem.fromResponse(res));
18+
return Promise.resolve();
19+
});
20+
return result;
21+
}
22+
23+
async function _list(workflowId) {
24+
const res = await sdk.workflows.getWorkflowData({
25+
workflowId,
26+
});
27+
return _.map(res.docs, WorkflowDataItem.fromResponse);
28+
}
29+
30+
function builder(y) {
31+
return y
32+
.positional('id', {
33+
describe: 'Workflow-Data-Item ID',
34+
})
35+
.option('workflow', {
36+
describe: 'Workflow ID that the Workflow-Data-Item belongs to',
37+
required: true,
38+
alias: 'wf',
39+
})
40+
.option('decrypt', {
41+
describe: 'When requesting spesific Workflow-Data-Item, decrypt the stored data',
42+
typoe: 'boolean',
43+
})
44+
.example('codefresh get workflow-data-item --workflow [WORKFLOW_ID]', 'Get all Workflow-Data-Items for given WORKFLOW_ID')
45+
.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');
46+
}
47+
48+
async function handler({ workflow, id, decrypt }) {
49+
let res;
50+
if (id && id.length > 0) {
51+
res = await _get(workflow, id, decrypt);
52+
} else {
53+
res = await _list(workflow);
54+
}
55+
Output.print(res);
56+
}
57+
58+
const command = new Command({
59+
command: 'workflow-data-item [id..]',
60+
aliases: ['wdi'],
61+
parent: getRoot,
62+
description: 'Get Workflow Data Item',
63+
usage: 'Passing [id] argument will cause a retrieval of a specific Workflow-Data-Item.',
64+
webDocs: {
65+
category: 'Workflow-Data',
66+
title: 'Get',
67+
},
68+
builder,
69+
handler,
70+
});
71+
72+
module.exports = command;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const _ = require('lodash');
2+
const Entity = require('./Entity');
3+
4+
class WorkflowDataItem extends Entity {
5+
constructor(data) {
6+
super();
7+
this.entityType = 'workflow-data-item';
8+
this.info = data;
9+
this.defaultColumns = [
10+
'id',
11+
'created-at',
12+
'data',
13+
];
14+
this.wideColumns = _.clone(this.defaultColumns);
15+
}
16+
17+
static _strinfigyData(input) {
18+
if (input.data === '*****') {
19+
return input;
20+
}
21+
const data = JSON.stringify(input.data);
22+
return {
23+
...input,
24+
data,
25+
};
26+
}
27+
28+
toDefault() {
29+
return WorkflowDataItem._strinfigyData(super.toDefault());
30+
}
31+
32+
toWide() {
33+
return WorkflowDataItem._strinfigyData(super.toWide());
34+
}
35+
36+
static fromResponse(response) {
37+
const id = response._id;
38+
const createdAt = _.get(response, 'metadata.createdAt');
39+
const data = _.get(response, 'data', '*****');
40+
return new WorkflowDataItem({
41+
id,
42+
'created-at': createdAt,
43+
data,
44+
});
45+
}
46+
}
47+
48+
module.exports = WorkflowDataItem;

openapi.json

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,128 @@
21932193
"x-sdk-interface": "workflows.list"
21942194
}
21952195
},
2196+
"/workflow/{workflowId}/data": {
2197+
"post": {
2198+
"operationId": "push-workflow-data",
2199+
"parameters": [
2200+
{
2201+
"description": "id of a workflowId object",
2202+
"in": "path",
2203+
"name": "workflowId",
2204+
"required": true,
2205+
"schema": {
2206+
"type": "string"
2207+
}
2208+
}
2209+
],
2210+
"responses": {
2211+
"201": {
2212+
"content": {
2213+
"application/json": {
2214+
"schema": {
2215+
"type": "object"
2216+
}
2217+
}
2218+
},
2219+
"description": "json"
2220+
}
2221+
},
2222+
"tags": [
2223+
"Builds"
2224+
],
2225+
"requestBody": {
2226+
"content": {
2227+
"application/json": {
2228+
"schema": {
2229+
"type": "object"
2230+
}
2231+
}
2232+
}
2233+
},
2234+
"summary": "Push New Workflow Data",
2235+
"x-sdk-interface": "workflows.pushWorkflowData"
2236+
},
2237+
"get": {
2238+
"operationId": "get-workflow-data",
2239+
"parameters": [
2240+
{
2241+
"description": "id of a workflowId object",
2242+
"in": "path",
2243+
"name": "workflowId",
2244+
"required": true,
2245+
"schema": {
2246+
"type": "string"
2247+
}
2248+
}
2249+
],
2250+
"responses": {
2251+
"200": {
2252+
"content": {
2253+
"application/json": {
2254+
"schema": {
2255+
"type": "object"
2256+
}
2257+
}
2258+
},
2259+
"description": "json"
2260+
}
2261+
},
2262+
"tags": [
2263+
"Builds"
2264+
],
2265+
"summary": "Get Workflow Data",
2266+
"x-sdk-interface": "workflows.getWorkflowData"
2267+
}
2268+
},
2269+
"/workflow/{workflowId}/data/{id}": {
2270+
"get": {
2271+
"operationId": "get-workflow-data-item",
2272+
"parameters": [
2273+
{
2274+
"description": "id of a workflowId object",
2275+
"in": "path",
2276+
"name": "workflowId",
2277+
"required": true,
2278+
"schema": {
2279+
"type": "string"
2280+
}
2281+
},
2282+
{
2283+
"description": "id of an object",
2284+
"in": "path",
2285+
"name": "id",
2286+
"required": true,
2287+
"schema": {
2288+
"type": "string"
2289+
}
2290+
},
2291+
{
2292+
"in": "query",
2293+
"name": "decrypt",
2294+
"schema": {
2295+
"type": "boolean"
2296+
}
2297+
}
2298+
],
2299+
"responses": {
2300+
"200": {
2301+
"content": {
2302+
"application/json": {
2303+
"schema": {
2304+
"type": "object"
2305+
}
2306+
}
2307+
},
2308+
"description": "json"
2309+
}
2310+
},
2311+
"tags": [
2312+
"Builds"
2313+
],
2314+
"summary": "Get Workflow Data Item",
2315+
"x-sdk-interface": "workflows.getWorkflowDataItem"
2316+
}
2317+
},
21962318
"/builds/{buildId}/update": {
21972319
"post": {
21982320
"parameters": [

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.69.6",
3+
"version": "0.70.0",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)