From f81a8ffb2f19af51add1cd36f6954a679c048115 Mon Sep 17 00:00:00 2001 From: made nindya Date: Wed, 12 Dec 2018 13:46:29 +0700 Subject: [PATCH] Update cmd pull with revision --- components/bots/bot.ts | 74 ++++++++++++++++++++++++-------------- lib/components/bots/bot.js | 69 ++++++++++++++++++++--------------- service.yml | 6 ++-- 3 files changed, 90 insertions(+), 59 deletions(-) diff --git a/components/bots/bot.ts b/components/bots/bot.ts index be6a727..dd0e1b8 100644 --- a/components/bots/bot.ts +++ b/components/bots/bot.ts @@ -380,38 +380,58 @@ export default class Bot extends Component { throw new Error("Sync only accept promises"); } - public async pull(name: string, version: string, options: JsonObject) { - let isGettingBot = false; + public async pull(revision: string, options: JsonObject) { + + let projectId; + let bots; + let botDesc; + try { - const { data, response } = await this.helper.toPromise(this.api.botApi, this.api.botApi.botsGet, {}); - let found = false; - let selectedBot: any; - for (const bot of data.items) { - const botName = bot.name; - if (botName === name) { - found = true; - selectedBot = bot; - break; - } - } - if (found) { - // Get specific bot version - isGettingBot = true; - const botId = selectedBot.id + ":" + version; - const getBot = await this.helper.toPromise(this.api.botApi, this.api.botApi.botsBotIdGet, botId); - const botDesc = getBot.data; - this.helper.dumpYaml("./bot.yml", botDesc); - console.log(`SUCCESS PULL BOT ${name} WITH VERSION ${version}`); - } else { - console.log(`BOT NOT FOUND`); + projectId = this.getProject(); + } catch (e) { + console.log(this.helper.wrapError(e)); + return; + } + try { + const { response: { body } } = await this.helper.toPromise( + this.api.botApi, this.api.botApi.projectsProjectIdBotRevisionsGet, projectId); + bots = body.data; + + } catch (e) { + console.log("INVALID PROJECT"); + return; + } + try { + if (!revision) { + revision = bots[0].revision; } + const { response: { body } } = await this.helper.toPromise( + this.api.botApi, this.api.botApi.projectsProjectIdBotRevisionsRevisionGet, projectId, revision); + botDesc = body; + } catch (e) { - if (isGettingBot) { - console.log(`CANNOT PULL BOT ${name} WITH VERSION ${version}`); - } else { - console.log(this.helper.wrapError(e)); + console.log("INVALID PROJECT REVISION"); + return; + } + + // remove data + delete botDesc.id; + delete botDesc.revision; + delete botDesc.changelog; + for (const flow in botDesc.flows) { + if (botDesc.flows[flow]) { + for (const state in botDesc.flows[flow].states) { + if (botDesc.flows[flow].states[state]) { + delete botDesc.flows[flow].states[state].style; + } + } } } + + console.log(`Pull bot revision ${revision.substring(0, 6)} to bot.yml`); + this.helper.dumpYaml("./bot.yml", botDesc); + + return; } private getProject() { diff --git a/lib/components/bots/bot.js b/lib/components/bots/bot.js index fb948a0..b06265e 100644 --- a/lib/components/bots/bot.js +++ b/lib/components/bots/bot.js @@ -364,42 +364,53 @@ class Bot extends merapi_1.Component { } throw new Error("Sync only accept promises"); } - pull(name, version, options) { + pull(revision, options) { return __awaiter(this, void 0, void 0, function* () { - let isGettingBot = false; + let projectId; + let bots; + let botDesc; try { - const { data, response } = yield this.helper.toPromise(this.api.botApi, this.api.botApi.botsGet, {}); - let found = false; - let selectedBot; - for (const bot of data.items) { - const botName = bot.name; - if (botName === name) { - found = true; - selectedBot = bot; - break; - } - } - if (found) { - // Get specific bot version - isGettingBot = true; - const botId = selectedBot.id + ":" + version; - const getBot = yield this.helper.toPromise(this.api.botApi, this.api.botApi.botsBotIdGet, botId); - const botDesc = getBot.data; - this.helper.dumpYaml("./bot.yml", botDesc); - console.log(`SUCCESS PULL BOT ${name} WITH VERSION ${version}`); - } - else { - console.log(`BOT NOT FOUND`); - } + projectId = this.getProject(); + } + catch (e) { + console.log(this.helper.wrapError(e)); + return; + } + try { + const { response: { body } } = yield this.helper.toPromise(this.api.botApi, this.api.botApi.projectsProjectIdBotRevisionsGet, projectId); + bots = body.data; } catch (e) { - if (isGettingBot) { - console.log(`CANNOT PULL BOT ${name} WITH VERSION ${version}`); + console.log("INVALID PROJECT"); + return; + } + try { + if (!revision) { + revision = bots[0].revision; } - else { - console.log(this.helper.wrapError(e)); + const { response: { body } } = yield this.helper.toPromise(this.api.botApi, this.api.botApi.projectsProjectIdBotRevisionsRevisionGet, projectId, revision); + botDesc = body; + } + catch (e) { + console.log("INVALID PROJECT REVISION"); + return; + } + // remove data + delete botDesc.id; + delete botDesc.revision; + delete botDesc.changelog; + for (const flow in botDesc.flows) { + if (botDesc.flows[flow]) { + for (const state in botDesc.flows[flow].states) { + if (botDesc.flows[flow].states[state]) { + delete botDesc.flows[flow].states[state].style; + } + } } } + console.log(`Pull bot revision ${revision.substring(0, 6)} to bot.yml`); + this.helper.dumpYaml("./bot.yml", botDesc); + return; }); } getProject() { diff --git a/service.yml b/service.yml index 4646d59..9bc7018 100644 --- a/service.yml +++ b/service.yml @@ -80,8 +80,8 @@ commands: handler: bot.push alias: push pull: - desc: pull bot with specified name and version - args: " " + desc: pull bot with specific revision + args: "[revision]" handler: bot.pull alias: pull discard: @@ -181,7 +181,7 @@ commands: desc: list all project handler: project.list alias: list-project - + session: type: group desc: Commands for session management