-
-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathJiraImport.php
More file actions
342 lines (306 loc) · 16.8 KB
/
JiraImport.php
File metadata and controls
342 lines (306 loc) · 16.8 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
namespace App\Filament\Pages;
use BackedEnum;
use App\Helpers\JiraHelper;
use App\Jobs\ImportJiraTicketsJob;
use Filament\Schemas\Components\Section;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\CheckboxList;
use Filament\Schemas\Components\Grid;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Wizard;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use App\Settings\JiraSettings;
class JiraImport extends Page implements HasForms
{
use InteractsWithForms, JiraHelper;
protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-cloud-arrow-down';
protected string $view = 'filament.pages.jira-import';
protected static ?string $slug = 'jira-import';
protected static ?int $navigationSort = 2;
protected $listeners = [
'updateJiraProjects',
'updateJiraTickets'
];
public $host;
public $username;
public $token;
private $loadingProjects;
private $projects;
public $selected_projects;
private $loadingTickets;
private $tickets;
public $selected_tickets;
public $data = [];
public $ticketsDataApi;
public $epicKeys = [];
public $taskKeys = [];
public function mount(): void
{
$settings = app(JiraSettings::class);
$this->form->fill([
'host' => $settings->host,
'username' => $settings->username,
'token' => $settings->token,
]);
}
public static function shouldRegisterNavigation(): bool
{
return auth()->user()->can('Import from Jira');
}
public function getSubheading(): string|Htmlable|null
{
return __('Use this section to login into your jira account and import tickets to this application');
}
public static function getNavigationLabel(): string
{
return __('Jira import');
}
public static function getNavigationGroup(): ?string
{
return __('Settings');
}
public function getFormSchema(): array
{
return [
Section::make()
->schema([
Wizard::make([
Wizard\Step::make(__('Jira login'))
->schema([
Placeholder::make('info')
->extraAttributes([
'class' => 'bg-primary-500 rounded-lg border border-primary-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->content(__('Your Jira credentials are used to communicate with the Jira REST API. They will be saved (with the API token encrypted) so you do not need to re-enter them each time.')),
Grid::make()
->schema([
TextInput::make('host')
->label(__('Host'))
->helperText(__('The url used to access your jira account'))
->required(),
TextInput::make('username')
->label(__('Username'))
->helperText(__('Your jira account username'))
->required(),
TextInput::make('token')
->label(__('API Token'))
->helperText(__('Your jira account API Token'))
->password()
->required(),
]),
])
->afterValidation(function () {
$settings = app(JiraSettings::class);
$settings->host = $this->host;
$settings->username = $this->username;
$settings->token = $this->token;
$settings->save();
$this->loadingProjects = true;
$this->dispatch('updateJiraProjects');
}),
Wizard\Step::make(__('Jira projects'))
->schema([
Placeholder::make('hint')
->extraAttributes([
'class' => 'bg-primary-500 rounded-lg border border-primary-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => !$this->loadingProjects && $this->projects)
->content(__('Choose your jira projects to import')),
Placeholder::make('loading')
->extraAttributes([
'class' => 'bg-warning-500 rounded-lg border border-warning-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => $this->loadingProjects)
->content(__('Loading projects, please wait...')),
Placeholder::make('info')
->extraAttributes([
'class' => 'bg-danger-500 rounded-lg border border-danger-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => !$this->loadingProjects && !$this->projects)
->content(__('Your jira credentials are incorrect, please go to previous step and re-enter your jira credentials')),
CheckboxList::make('selected_projects')
->label(__('Jira projects'))
->required()
->visible(fn() => $this->projects)
->options(function () {
$list = [];
if ($this->projects) {
foreach ($this->projects as $project) {
$list[$project->key] = new HtmlString(
"<div class='w-full flex flex-col gap-1'>"
. "<div class='w-full flex items-center gap-1'>"
. "<img src='" . $project->avatarUrls->{'16x16'} . "' class='rounded-full w-8 h-8 shadow' />"
. "<span class='font-medium text-gray-700 text-base'>" . $project->name . "</span>"
. "<div class='text-gray-700 text-xs font-light'><span class='font-medium uppercase'>/</span> " . $project->key . "</div>"
. "</div>"
. "</div>"
);
}
}
return $list;
}),
])
->afterValidation(function () {
$this->loadingTickets = true;
$this->dispatch('updateJiraTickets');
}),
Wizard\Step::make(__('Jira tickets'))
->schema(function () {
$fields = [];
$fields[] = Placeholder::make('hint')
->extraAttributes([
'class' => 'bg-primary-500 rounded-lg border border-primary-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => !$this->loadingTickets && $this->tickets)
->content(__('Choose your jira projects to import'));
$fields[] = Placeholder::make('loading')
->extraAttributes([
'class' => 'bg-warning-500 rounded-lg border border-warning-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => $this->loadingTickets)
->content(__('Loading tickets, please wait...'));
if (!$this->loadingTickets && $this->tickets) {
$fields[] = Placeholder::make('selection_buttons')
->hiddenLabel()
->content(new HtmlString(
"<div class='flex items-center gap-2'>"
. "<button type='button' wire:click='selectAllEpics' class='px-3 py-1.5 rounded-md text-xs font-bold bg-purple-600 text-white shadow-sm hover:bg-purple-700'>" . __('Select All Epics') . "</button>"
. "<button type='button' wire:click='selectAllTasks' class='px-3 py-1.5 rounded-md text-xs font-bold bg-primary-600 text-white shadow-sm hover:bg-primary-700'>" . __('Select All Tasks') . "</button>"
. "<button type='button' wire:click='deselectAll' class='px-3 py-1.5 rounded-md text-xs font-bold bg-gray-400 text-white shadow-sm hover:bg-gray-500'>" . __('Deselect All') . "</button>"
. "</div>"
));
}
if (!$this->loadingTickets) {
if ($this->tickets) {
foreach ($this->tickets as $projectKey => $ticket) {
if ($ticket['total'] > 0) {
$fields[] = Placeholder::make('tickets_' . Str::slug($projectKey))
->label(__('Tickets for the project:') . ' ' . $projectKey)
->extraAttributes([
'style' => 'margin-bottom: -15px;'
])
->content('');
foreach ($ticket['issues'] as $issue) {
$fields[] = Checkbox::make('data.' . Str::slug($projectKey) . '_' . Str::slug($issue['code']))
->label(function () use ($issue) {
$epicBadge = !empty($issue['isEpic'])
? "<span class='inline-flex items-center px-2.5 py-1 rounded-md text-xs font-bold bg-purple-600 text-white shadow-sm'>EPIC</span> "
: '';
return new HtmlString(
"<div class='w-full flex flex-col gap-1'>"
. "<div class='w-full flex items-center gap-1'>"
. $epicBadge
. "<div class='text-gray-700 text-xs font-light'><span class='font-medium uppercase'>" . $issue['code'] . "</span> " . $issue['name'] . "</div>"
. "</div>"
. "</div>"
);
});
}
} else {
$fields[] = Placeholder::make('no_tickets_' . Str::slug($projectKey))
->label(__('Tickets for the project:') . ' ' . $projectKey)
->content(__('No tickets found!'));
}
}
} else {
$fields[] = Placeholder::make('info')
->extraAttributes([
'class' => 'bg-warning-500 rounded-lg border border-warning-600 text-white font-medium text-sm py-3 px-4'
])
->hiddenLabel()
->visible(fn() => !$this->projects)
->content(__('No tickets found!'));
}
}
return $fields;
}),
])
->submitAction(new HtmlString("<button type='submit' class='px-3 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded'>" . __('Import') . "</button>")),
]),
];
}
public function import(): void
{
if ($this->data && sizeof($this->data)) {
$tickets = [];
foreach (array_keys($this->data) as $item) {
$url = $this->ticketsDataApi[$item] ?? null;
if ($url) {
$ticket = $this->getJiraTicketDetails($this->host, $this->username, $this->token, $url);
if ($ticket) {
$tickets[] = $ticket;
}
}
}
dispatch(new ImportJiraTicketsJob($tickets, auth()->user()));
Notification::make()
->success()
->title(__('The importation job is started, when finished you will be notified'))
->send();
$this->redirect(route('filament.admin.pages.jira-import'));
} else {
Notification::make()
->warning()
->title(__('Please choose at least a jira ticket to import'))
->send();
}
}
public function updateJiraProjects(): void
{
$client = $this->connectToJira($this->host, $this->username, $this->token);
$this->projects = $this->getJiraProjects($client);
$this->loadingProjects = false;
}
public function updateJiraTickets(): void
{
$this->ticketsDataApi = [];
$this->epicKeys = [];
$this->taskKeys = [];
$client = $this->connectToJira($this->host, $this->username, $this->token);
$this->tickets = $this->getJiraTicketsByProject($client, $this->selected_projects);
if ($this->tickets) {
foreach ($this->tickets as $projectKey => $ticket) {
foreach ($ticket['issues'] as $issue) {
$key = Str::slug($projectKey) . '_' . Str::slug($issue['code']);
$this->ticketsDataApi[$key] = $issue['data']->self;
if (!empty($issue['isEpic'])) {
$this->epicKeys[] = $key;
} else {
$this->taskKeys[] = $key;
}
}
}
}
$this->loadingTickets = false;
}
public function selectAllEpics(): void
{
foreach ($this->epicKeys as $key) {
$this->data[$key] = true;
}
}
public function selectAllTasks(): void
{
foreach ($this->taskKeys as $key) {
$this->data[$key] = true;
}
}
public function deselectAll(): void
{
$this->data = [];
}
}