|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Commands; |
| 4 | + |
| 5 | +use App\Coding\Issue; |
| 6 | +use App\Coding\Project; |
| 7 | +use App\Imports\IssuesImport; |
| 8 | +use LaravelZero\Framework\Commands\Command; |
| 9 | +use Maatwebsite\Excel\Facades\Excel; |
| 10 | + |
| 11 | +class IssueImportCommand extends Command |
| 12 | +{ |
| 13 | + use WithCoding; |
| 14 | + |
| 15 | + /** |
| 16 | + * The signature of the command. |
| 17 | + * |
| 18 | + * @var string |
| 19 | + */ |
| 20 | + protected $signature = 'issue:import |
| 21 | + {file : 文件(支持格式:csv)} |
| 22 | + {--type= : 类型(使用英文),如 DEFECT(缺陷)、REQUIREMENT(需求)、MISSION(任务)、EPIC(史诗)、SUB_TASK(子任务)} |
| 23 | + {--coding_token= : CODING 令牌} |
| 24 | + {--coding_team_domain= : CODING 团队域名,如 xxx.coding.net 即填写 xxx} |
| 25 | + {--coding_project_uri= : CODING 项目标识,如 xxx.coding.net/p/yyy 即填写 yyy} |
| 26 | + '; |
| 27 | + |
| 28 | + /** |
| 29 | + * The description of the command. |
| 30 | + * |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + protected $description = '导入事项'; |
| 34 | + |
| 35 | + /** |
| 36 | + * Execute the console command. |
| 37 | + * |
| 38 | + */ |
| 39 | + public function handle(Issue $codingIssue, Project $codingProject): int |
| 40 | + { |
| 41 | + $this->setCodingApi(); |
| 42 | + |
| 43 | + $filePath = $this->argument('file'); |
| 44 | + if (!file_exists($filePath)) { |
| 45 | + $this->error("文件不存在:$filePath"); |
| 46 | + return 1; |
| 47 | + } |
| 48 | + |
| 49 | + $result = $codingProject->getIssueTypes($this->codingToken, $this->codingProjectUri); |
| 50 | + $issueTypes = []; |
| 51 | + foreach ($result as $item) { |
| 52 | + $issueTypes[$item['Name']] = $item; |
| 53 | + } |
| 54 | + $rows = Excel::toArray(new IssuesImport(), $filePath)[0]; |
| 55 | + foreach ($rows as $row) { |
| 56 | + $data = [ |
| 57 | + 'Type' => $issueTypes[$row['事项类型']]['IssueType'], |
| 58 | + 'IssueTypeId' => $issueTypes[$row['事项类型']]['Id'], |
| 59 | + 'Name' => $row['标题'], |
| 60 | + 'Priority' => \App\Models\Issue::PRIORITY_MAP[$row['优先级']], |
| 61 | + ]; |
| 62 | + try { |
| 63 | + $result = $codingIssue->create($this->codingToken, $this->codingProjectUri, $data); |
| 64 | + } catch (\Exception $e) { |
| 65 | + $this->error('Error: ' . $e->getMessage()); |
| 66 | + return 1; |
| 67 | + } |
| 68 | + $this->info("https://{$this->codingTeamDomain}.coding.net/p/{$this->codingProjectUri}" . |
| 69 | + "/all/issues/${result['Code']}"); |
| 70 | + } |
| 71 | + |
| 72 | + return 0; |
| 73 | + } |
| 74 | +} |
0 commit comments