-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAppsAndBranches.ts
126 lines (114 loc) · 3.57 KB
/
getAppsAndBranches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import axios from 'axios';
import inquirer from 'inquirer'
import { IModel } from "mendixmodelsdk";
// interfaces for processing rest requests
interface App {
AppId: string;
Name: string;
Url: string;
ProjectId: string;
}
interface BranchItem {
name: string,
latestCommit: {
date: Date,
message: string,
mendixVersion: string
}
}
interface Branch {
items: BranchItem[]
}
export async function getBranches(appId: string): Promise<Branch> {
let res = await axios.get(`https://repository.api.mendix.com/v1/repositories/${appId}/branches`, {
'headers': {
'Authorization': 'MxToken ' + process.env.MENDIX_TOKEN
}
})
if (res && res.data) {
return res.data;
} else {
// trunk is always available
return ({ items: [{ name: 'trunk', latestCommit: { date: new Date(), message: '', mendixVersion: '' } }] });
}
}
export async function getApps(): Promise<App[]> {
let request = await axios.get('https://deploy.mendix.com/api/1/apps', {
'headers': {
'Mendix-ApiKey': process.env.MENDIX_API_KEY,
'Mendix-Username': process.env.MENDIX_API_USER
}
})
return request.data;
}
/*
Check whether the environment constants are filled in
open the Mendix pages for instructions.
*/
export function keysAndTokensOk() {
let result = true;
if (process.env.MENDIX_API_KEY == null || process.env.MENDIX_API_USER == null) {
console.warn("Please setup your api key, environment variables (MENDIX_API_KEY, MENDIX_API_USER)");
open('https://docs.mendix.com/apidocs-mxsdk/apidocs/authentication/');
result = false;
}
if (process.env.MENDIX_TOKEN == null) {
console.warn("Please setup your Mendix personal access token");
open('https://docs.mendix.com/apidocs-mxsdk/mxsdk/setup-your-pat/');
result = false;
}
return result;
}
export async function askApp() {
let apps = await getApps();
// create a list of app names
const choices = apps.map(app => app.Name);
// setup the question
const question = [{
type: 'list',
name: 'app',
message: 'For which project do you want to create microflows?',
choices: choices
}];
// ask for project
let response = await inquirer.prompt(question);
// get the project id by name
let projectId = apps.filter(app => app.Name == response.app)[0].ProjectId;
return projectId;
}
/*
* If there are multiple branches, ask which branch
*/
export async function askBranch(projectId: string) {
const branches = await getBranches(projectId)
let branch = 'trunk';
if (branches.items.length > 1) {
const branchchoices = branches.items.map(item => item.name);
// setup the question
const questionbranch = [{
type: 'list',
name: 'branch',
message: 'For which branch?',
choices: branchchoices
}];
let response = await inquirer.prompt(questionbranch);
branch = response.branch;
}
return branch;
}
/*
* Ask for which module
*/
export async function askModule(model: IModel, targetModule: string) {
const questionmodels = [{
type: 'list',
name: 'name',
message: 'For which module?',
choices: model.allDomainModels()
.filter(module => module.containerAsModule.name != targetModule)
.map(module => module.containerAsModule.name)
.sort()
}];
const moduleresponse = await inquirer.prompt(questionmodels);
return moduleresponse;
}