Skip to content

Commit fed35d8

Browse files
committed
feat: #72 cli import issues
1 parent aa8e0fa commit fed35d8

File tree

11 files changed

+1746
-146
lines changed

11 files changed

+1746
-146
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/.vscode
66
/.vagrant
77
.phpunit.result.cache
8+
/database/database.sqlite

app/Commands/IssueImportCommand.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

app/Imports/IssuesImport.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Imports;
4+
5+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
6+
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
7+
8+
class IssuesImport implements WithHeadingRow
9+
{
10+
public function __construct()
11+
{
12+
HeadingRowFormatter::default('none');
13+
}
14+
}

app/Models/Issue.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Issue extends Model
9+
{
10+
use HasFactory;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'type',
19+
'name',
20+
'priority',
21+
];
22+
23+
public const PRIORITY_MAP = [
24+
'' => '0',
25+
'' => '1',
26+
'' => '2',
27+
'紧急' => '3',
28+
];
29+
}

composer.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,18 @@
2222
"ext-json": "*",
2323
"ext-libxml": "*",
2424
"ext-zip": "*",
25+
"illuminate/database": "^8.40",
2526
"illuminate/log": "^8.0",
27+
"illuminate/translation": "^8.64",
28+
"illuminate/validation": "^8.64",
2629
"laravel-fans/confluence": "^0.1.1",
2730
"laravel-zero/framework": "^8.8",
2831
"league/html-to-markdown": "^5.0",
32+
"maatwebsite/excel": "^3.1",
2933
"sinkcup/laravel-filesystem-cos-updated": "^4.0"
3034
},
3135
"require-dev": {
32-
"fakerphp/faker": "^1.14",
36+
"fakerphp/faker": "^1.9.1",
3337
"mockery/mockery": "^1.4.3",
3438
"phpmd/phpmd": "^2.10",
3539
"phpunit/phpunit": "^9.5",

0 commit comments

Comments
 (0)