Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update command pull with revision #76

Merged
merged 1 commit into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 47 additions & 27 deletions components/bots/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
69 changes: 40 additions & 29 deletions lib/components/bots/bot.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ commands:
handler: bot.push
alias: push
pull:
desc: pull bot with specified name and version
args: "<name> <version>"
desc: pull bot with specific revision
args: "[revision]"
handler: bot.pull
alias: pull
discard:
Expand Down Expand Up @@ -181,7 +181,7 @@ commands:
desc: list all project
handler: project.list
alias: list-project

session:
type: group
desc: Commands for session management
Expand Down