-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_labels.js
172 lines (156 loc) · 6.01 KB
/
get_labels.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const fs = require('fs');
const readline = require('readline');
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let MIRO_ORG_ID;
let API_TOKEN;
// Function to ask a question
function askQuestion(question, validator, callback) {
rl.question(question, (answer) => {
if (validator(answer)) {
callback(answer);
} else {
console.log('Invalid input. Please try again.');
askQuestion(question, validator, callback); // Re-ask the question
}
});
}
// Array of questions with corresponding validators
const questions = [
{
question: 'Enter your Miro Organization ID: ',
validator: (answer) => !isNaN(parseFloat(answer)) && isFinite(answer)
},
{
question: 'Enter your Miro REST API Token: ',
validator: (answer) => typeof answer === 'string'
}
// Add more questions with validators as needed
];
// Function to ask multiple questions recursively
async function askQuestions(index) {
if (index >= questions.length) {
// End of questions
console.log('Thank you for answering the questions!');
await init(MIRO_ORG_ID, API_TOKEN);
rl.close();
return;
}
const { question, validator } = questions[index];
askQuestion(question, validator, (answer) => {
if (question === 'Enter your Miro Organization ID: ') {
MIRO_ORG_ID = answer.toString();
}
else if (question === 'Enter your Miro REST API Token: ') {
API_TOKEN = answer.toString();;
}
askQuestions(index + 1); // Ask the next question
});
}
// Start asking questions
askQuestions(0);
async function callAPI(url, options) {
async function manageErrors(response) {
if(!response.ok){
const parsedResponse = await response.json();
const responseError = {
status: response.status,
statusText: response.statusText,
requestUrl: response.url,
errorDetails: parsedResponse
};
throw(responseError);
}
return response;
}
const response = await fetch(url, options)
.then(manageErrors)
.then((res) => {
if (res.ok) {
const rateLimitRemaining = res.headers.get('X-RateLimit-Remaining');
return res[res.status == 204 ? 'text' : 'json']().then((data) => ({ status: res.status, rate_limit_remaining: rateLimitRemaining, body: data }));
}
})
.catch((error) => {
console.error('Error:', error);
return error;
});
return response;
}
function jsonToCsv(jsonData) {
if (jsonData) {
let csv = '';
// Get the headers
let headers = Object.keys(jsonData[Object.keys(jsonData)[0]]);
csv += headers.join(',') + '\n';
// Helper function to escape CSV special characters
const escapeCSV = (value) => {
if (typeof value === 'string') {
if (value.includes('"')) {
value = value.replace(/"/g, '""');
}
}
value = `"${value}"`;
return value;
};
// Add the data
Object.keys(jsonData).forEach(function(row) {
let data = headers.map(header => escapeCSV(jsonData[row][header])).join(',');
csv += data + '\n';
});
return csv;
}
}
async function init(orgId, token) {
const apiUrl = `https://api.miro.com/v2/orgs/${orgId}/data-classification-settings`;
const reqHeaders = {
'cache-control': 'no-cache, no-store',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token
};
const reqGetOptions = {
method: 'GET',
headers: reqHeaders,
body: null
};
try {
const getClassificationLabels = await callAPI(apiUrl, reqGetOptions);
console.log('============== CLASSIFICATION LABELS - BEGIN ===================');
console.log(JSON.stringify(getClassificationLabels.body, null, 2));
console.log('=============== CLASSIFICATION LABELS - END ===================');
if (getClassificationLabels.status === 200) {
const getClassificationLabelsArray = [];
for(let i=0; i < getClassificationLabels.body.labels.length; i++) {
var label = {
label_id: getClassificationLabels.body.labels[i].id,
label_name: getClassificationLabels.body.labels[i].name,
is_default: getClassificationLabels.body.labels[i].default,
description: getClassificationLabels.body.labels[i].description ? getClassificationLabels.body.labels[i].description : '',
order_number: getClassificationLabels.body.labels[i].orderNumber,
type: getClassificationLabels.body.labels[i].type
};
getClassificationLabelsArray.push(label);
}
const directory = 'board_classification_labels';
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
let content;
let filePath;
content = jsonToCsv(getClassificationLabelsArray);
filePath = 'board_classification_labels/classification_labels.csv';
fs.writeFileSync(filePath, content);
content = JSON.stringify(getClassificationLabelsArray, null, 2);
filePath = 'board_classification_labels/classification_labels.json';
fs.writeFileSync(filePath, content);
console.log('# Next steps:\n# 1. Review the classification labels from the list above (or open the "classification_labels.csv" file within the folder "board_classification_labels" in the directory where this script lives)\n# 2. Identify the label you want to use to classify the unclassified boards (you will be asked for the ID of the desired label on step 3)\n# 3. Run: node classification.js');
console.log('===========================================');
}
}
catch(error) {
console.log(error);
}
}