-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
152 lines (111 loc) · 3.93 KB
/
index.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
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
import {Issue, Youtrack } from 'youtrack-rest-client';
import { Command } from 'commander';
import chalk from 'chalk';
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
type Options = {
token: string;
url: string;
templateIssueId: string;
}
async function prepare ({ templateIssueId, token, url }: Options) {
const youtrack = new Youtrack({
baseUrl: url,
token: token,
});
function cloneIssue (issue: Issue) {
return youtrack.issues.create({
summary: (issue.summary || '').replace(' - Šablona', ''),
tags: issue.tags?.filter(({ name }) => !['Šablona', 'Template'].includes(name)),
// @ts-ignore
project: issue.project,
description: issue.description,
fields: issue.fields
})
}
let templateIssue: Issue | undefined;
try {
templateIssue = await youtrack.issues.byId(templateIssueId);
}
catch (error) {
if (error.statusCode !== 404) {
throw error;
}
}
// Must be on next block, because of TLS_REJECT warning
process.stdout.write('Searching for issue in youtrack... ');
if (!templateIssue) {
console.log(chalk.bgRed('NOT FOUND'));
process.exit(1);
}
console.log(chalk.bgGreen('OK'));
process.stdout.write('Check if the issue has "Template" tag... ');
if (!templateIssue.tags?.some(({ name }) => name === 'Template')) {
console.log(chalk.bgGreen('NO'));
process.exit(1);
}
console.log(chalk.bgGreen('OK'));
const projectId = templateIssueId.split('-')[0];
if (templateIssue?.id) {
process.stdout.write('Cloning parent issue... ');
const parentIssue = await cloneIssue(templateIssue);
if (parentIssue) {
const parentIssueId = `${projectId}-${parentIssue.numberInProject}`;
console.log(chalk.bgGreen(`OK (${parentIssueId})`));
process.stdout.write('Requesting child issues of the parent issue... ');
const templateSearchedChildIssues = await youtrack.issues.search(`subtask of: ${templateIssueId}`);
const templateSearchedChildIssueIds = templateSearchedChildIssues.map(({ id }) => id);
const childIssueIds: string[] = [];
console.log(chalk.bgGreen(`OK (Found ${templateSearchedChildIssues.length})`));
while (templateSearchedChildIssueIds.length) {
const templateSearchedChildIssueId = templateSearchedChildIssueIds.pop();
if (templateSearchedChildIssueId) {
process.stdout.write(`Requesting data from child issue with id "${templateSearchedChildIssueId}"... `);
// issues.search() nedava vsechna data jako tagy, musi se jit pres issues.byId()
const templateChildIssue = await youtrack.issues.byId(templateSearchedChildIssueId);
console.log(chalk.bgGreen('OK'));
process.stdout.write(`Cloning child issue with id "${templateSearchedChildIssueId}"... `);
const childIssue = await cloneIssue(templateChildIssue);
if (childIssue) {
console.log(chalk.bgGreen('OK'));
childIssueIds.push(childIssue.id!);
}
}
}
if (childIssueIds.length) {
process.stdout.write('Setting parent issue as subtask to all child issues... ');
await youtrack.issues.executeCommand({
query: `subtask of: ${parentIssueId}`,
issues: childIssueIds.map((id) => ({ id })),
})
console.log(chalk.bgGreen('OK'));
}
console.log(chalk.bgGreen('DONE.'));
}
}
}
const program = new Command();
program
.description('Script to clone templated issue tree for new employee on youtrack');
program
.command('prepare')
.description('Clone template issue tree', {
templateIssueId: 'Id of the parent issue, fe. ABC-123',
})
.arguments('<templateIssueId>', )
.requiredOption('-t, --token <string>', 'Youtrack token (Profile > Hub account > Authentication > New token)')
.requiredOption('-u, --url <string>', 'Youtrack url')
.action((...args) => {
const [templateIssueId, options] = args;
prepare({
templateIssueId,
...options,
});
});
program
.command('*')
.action((command) => {
console.error(`Unknown command "${command}"`);
program.outputHelp();
process.exit(1);
});
program.parse(process.argv);