-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
127 lines (99 loc) · 3.82 KB
/
main.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
// Core modules
const readline = require('readline');
// Custom modules
const { LoginJira, ValidateJiraSession, LoadSession, GetJiraUser } = require('./jiraApi');
const { PromptLogin, PromptUniqueValues, DisplayFileList, RetryAsyncFunction } = require('./uiPrompts');
const { CreateIssuesFromTemplate, ExtractUniquePlaceholders } = require('./issueProcessor');
const { GetTemplates } = require('./templates');
const config = require('./config');
// Function to listen for ESC key press
function setupEscListener() {
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.name === 'escape') {
console.log('\nProcess cancelled.');
process.exit();
}
});
}
async function GetJiraSession() {
let sessionCookie = LoadSession();
if (sessionCookie && await ValidateJiraSession(sessionCookie)) {
return sessionCookie;
}
let attempts = 3;
while (attempts > 0) {
const { username, password } = await PromptLogin();
try {
sessionCookie = await LoginJira(username, password);
return sessionCookie;
} catch (error) {
console.error(error.message);
attempts -= 1;
console.log(`Attempts remaining: ${attempts}`);
}
}
throw new Error('Failed to log in after multiple attempts.');
}
async function SelectTemplates(sessionCookie) {
const templates = GetTemplates();
if (templates.length === 0) {
console.error('No templates available to select.');
return;
}
const selectedFile = await DisplayFileList(templates);
console.log('Selected template:', selectedFile.name);
if (!selectedFile || !selectedFile.data) {
console.error('Invalid file format: Data object is missing.');
return;
}
const { Prompt: prompt, Mappings: mappings, Structure: structure } = selectedFile.data;
config.debug(prompt);
let attempts = 3;
try {
await RetryAsyncFunction(
() => BuildIssues(mappings, structure, sessionCookie),
attempts
);
console.log('Issues created successfully');
} catch (error) {
console.error('Function failed after maximum retries:', error);
}
}
async function BuildIssues(mappings, structure, sessionCookie) {
// Initialize issueKeysByRefId within the function
const issueKeysByRefId = new Map();
// Prompt the user for unique values
const uniquePlaceholders = ExtractUniquePlaceholders(mappings, structure);
const uniqueValues = await PromptUniqueValues(uniquePlaceholders);
// Process issues based on the selected template, passing issueKeysByRefId
try {
await CreateIssuesFromTemplate(mappings, structure, sessionCookie, issueKeysByRefId, uniqueValues);
console.log('Issues created successfully');
} catch (error) {
console.error('There was an error creating the Issue:', error);
throw error; // Propagate the error to allow retry logic to catch it
}
}
async function main() {
setupEscListener(); // Set up the ESC key listener
console.log('Welcome! Connecting to: ' + config.JIRA_URL);
try {
const sessionCookie = await GetJiraSession();
// Get and display Jira user information
try {
const userInfo = await GetJiraUser(sessionCookie);
console.log('Success! Authenticated as:', userInfo.displayName);
} catch (error) {
console.error('Error retrieving user information:', error.message);
return; // Exit if unable to retrieve user info
}
// Display and process templates
await SelectTemplates(sessionCookie);
} catch (error) {
console.error('Error:', error.message);
}
}
// Call the main function
main();