modalLoaded = true; } public function mount($editPid) { $editPid = '15689'; // remove this when real $this->patchId = $editPid; $this->loadPatchData(); } public function getChangedFiles() { $this->fileChangesTemp = []; $client = new Client([ 'headers' => [ 'Authorization' => 'Bearer ' . env('GITLAB_API_TOKEN'), 'Accept' => 'application/json', ], 'verify' => false, ]); try { $response = $client->get(env('GITLAB_API_URL') . "/projects/{$this->selectedProject}/repository/compare", [ 'query' => [ 'from' => $this->startCommit, 'to' => $this->endCommit, ], ]); $data = json_decode($response->getBody(), true); foreach ($data['diffs'] as $file) { $this->fileChangesTemp[] = $file['new_path']; } $this->fileGitChanges = $this->buildTreeEdit($data['diffs']); } catch (\Throwable $th) { $data = ''; } } private function buildTreeEdit($files) { $tree = []; foreach ($files as $file) { $path = explode('/', $file['new_path']); $current = &$tree; foreach ($path as $part) { if (!isset($current[$part])) { $current[$part] = []; } $current = &$current[$part]; } // $current['id'] = $file['fid']; } return $tree; } public function reloadComponent($id) { $this->patchId = $id; $this->loadPatchData(); $this->render(); } public function loadPatchData() { $patch = ConfSmartUpdate::findOrFail($this->patchId); $this->PATCHNAME = $patch->PATCHNAME; $this->PDATE = $patch->PDATE; $this->PHP_VERSION = $patch->PHP_VERSION; $this->PLEVEL = $patch->PLEVEL; $this->PCODE = $patch->PCODE; $this->MAJOR_VERSION = $patch->MAJOR_VERSION; $this->PDESC = $patch->PDESC; $this->Remark = $patch->Remark; $this->POWNER = $patch->POWNER; $this->PAPPROVEDATE = $patch->PAPPROVEDATE; $this->PTYPE = $patch->PTYPE; $this->PATCHCODE = $patch->PATCHCODE; $this->UNINSTALL = $patch->UNINSTALL; $this->PATCHCODE_SERVER = $patch->PATCHCODE_SERVER; $filePath = TabPatchFile::where("ptid", $this->patchId)->get()->toArray(); $this->filePatch = $filePath; $this->filePatchChanges = $this->buildTree($filePath); } public function save() { $this->isProcessing = true; // $this->validate([ // 'PATCHNAME' => 'required|string|max:255', // 'PDATE' => 'required|date', // 'PHP_VERSION' => 'required|integer', // 'PLEVEL' => 'required|string|max:255', // 'PCODE' => 'required|string|max:255', // 'MAJOR_VERSION' => 'required|string|max:255', // 'PDESC' => 'required|string|max:255', // 'Remark' => 'required|string|max:255', // 'POWNER' => 'required|string|max:255', // 'PAPPROVEDATE' => 'required|date', // 'PTYPE' => 'required|string|max:255', // 'PATCHCODE' => 'required|string', // 'UNINSTALL' => 'required|string|max:255', // 'PATCHCODE_SERVER' => 'required|string', // ]); $confSmartUpdate = ConfSmartUpdate::findOrFail($this->patchId); $confSmartUpdate->PATCHNAME = $this->PATCHNAME; $confSmartUpdate->PDATE = $this->PDATE; $confSmartUpdate->PHP_VERSION_ID = $this->PHP_VERSION; $confSmartUpdate->PLEVEL = $this->PLEVEL; $confSmartUpdate->PCODE = $this->PCODE; $confSmartUpdate->MAJOR_VERSION = $this->MAJOR_VERSION; $confSmartUpdate->PDESC = $this->PDESC; $confSmartUpdate->Remark = $this->Remark; $confSmartUpdate->POWNER = $this->POWNER; $confSmartUpdate->PAPPROVEDATE = $this->PAPPROVEDATE; $confSmartUpdate->PTYPE = $this->PTYPE; $confSmartUpdate->PATCHCODE = $this->PATCHCODE; $confSmartUpdate->UNINSTALL = $this->UNINSTALL; $confSmartUpdate->PATCHCODE_SERVER = $this->PATCHCODE_SERVER; $confSmartUpdate->save(); $totalFiles = count($this->fileChangesTemp); $successCount = 0; // $this->fileChangesTemp = 1000000; if (count($this->fileChangesTemp) > 0) { for ($i=0; $i < $this->fileChangesTemp; $i++) { $progress = (($i + 1) / $this->fileChangesTemp) * 100; $this->progressSave = $i; // Emit an event to update progress on the frontend // $this->emit('progressUpdated', $progress); // Use dispatch browser event to trigger frontend update // $this->dispatchBrowserEvent('progressUpdated', ['progress' => $progress]); } foreach ($this->fileChangesTemp as $index => $file) { $filedata = $this->getFileContentFromGit($file, $this->endCommit); $filePath = $this->formatFilePath($file); $filepath = new TabPatchFile; $filepath->ptid = $confSmartUpdate->PID; $filepath->file_name = $filePath; $filepath->file_data = base64_encode($filedata); if ($filepath->save()) { $successCount++; } $progress = (($index + 1) / $totalFiles) * 100; $this->progressSave = $progress; // Emit an event to update progress on the frontend $this->emit('progressUpdated', $progress); // Use dispatch browser event to trigger frontend update $this->dispatchBrowserEvent('progressUpdated', ['progress' => $progress]); } } // session()->flash('message', 'Patch details and file changes updated successfully.'); // $this->reset(['fileChangesTemp', 'fileGitChanges']); // $this->emit('reloadComponent', $this->patchId); } private function formatFilePath($file) { if (strpos($file, 'SPN/') !== false) { return str_replace("SPN/", "../SPN/", $file); } else if (strpos($file, 'spn/') !== false) { return str_replace("spn/", "../spn/", $file); } else { return str_replace("IE5DEV.shippingnet", ".", $file); } } private function getFileContentFromGit($filePath, $commit) { $token = env('GITLAB_API_TOKEN'); $client = new Client([ 'base_uri' => 'https://idemo.netbay.co.th/gitlab/api/v4/', 'headers' => [ 'Authorization' => "Bearer $token", 'Accept' => 'application/json', ], 'verify' => false, ]); try { $response = $client->get("projects/$this->selectedProject/repository/files/" . urlencode($filePath) . "/raw", [ 'query' => ['ref' => $commit] ]); $statusCode = $response->getStatusCode(); $content = $response->getBody()->getContents(); if ($statusCode == 200) { return $content; } else { throw new \Exception("Failed to fetch file. Status code: $statusCode"); } } catch (RequestException $e) { throw $e; } catch (\Exception $e) { throw $e; } } private function buildTree($files) { $tree = []; foreach ($files as $file) { if (strpos($file['file_name'], "../SPN/") !== false) { $filePath = str_replace("../SPN/", "SPN/", $file['file_name']); } else if (strpos($file['file_name'], "../spn/") !== false) { $filePath = str_replace("../spn/", "spn/", $file['file_name']); } else { $filePath = "IE5DEV.shippingnet" . substr($file['file_name'], 1); } $pathParts = explode('/', $filePath); $current = &$tree; foreach ($pathParts as $part) { if (!isset($current[$part])) { $current[$part] = []; } $current = &$current[$part]; } $current['id'] = $file['fid'] ?? 0; } return $tree; } public function loadPage($page) { $this->emit('menuChanged', $page); } public function deletePatchFile($patchFileId) { $pathFile = TabPatchFile::where("fid", $patchFileId)->first(); $patchId = $pathFile->ptid; $pathFile = TabPatchFile::where("fid", $patchFileId)->delete(); $message = "Deleted File ID : " . json_encode($patchFileId) . " Successfully"; // $this->message = $message; $this->emit('reloadComponent', $patchId); } public function render() { return view('livewire.pages.patch.patch-edit'); } }