Skip to content

Commit

Permalink
feat: #72 cli import issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Oct 20, 2021
1 parent 38a4ca4 commit 52b2a5d
Show file tree
Hide file tree
Showing 11 changed files with 2,450 additions and 684 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/.vscode
/.vagrant
.phpunit.result.cache
/database/database.sqlite
74 changes: 74 additions & 0 deletions app/Commands/IssueImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Commands;

use App\Coding\Issue;
use App\Coding\Project;
use App\Imports\IssuesImport;
use LaravelZero\Framework\Commands\Command;
use Maatwebsite\Excel\Facades\Excel;

class IssueImportCommand extends Command
{
use WithCoding;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'issue:import
{file : 文件(支持格式:csv)}
{--type= : 类型(使用英文),如 DEFECT(缺陷)、REQUIREMENT(需求)、MISSION(任务)、EPIC(史诗)、SUB_TASK(子任务)}
{--coding_token= : CODING 令牌}
{--coding_team_domain= : CODING 团队域名,如 xxx.coding.net 即填写 xxx}
{--coding_project_uri= : CODING 项目标识,如 xxx.coding.net/p/yyy 即填写 yyy}
';

/**
* The description of the command.
*
* @var string
*/
protected $description = '导入事项';

/**
* Execute the console command.
*
*/
public function handle(Issue $codingIssue, Project $codingProject): int
{
$this->setCodingApi();

$filePath = $this->argument('file');
if (!file_exists($filePath)) {
$this->error("文件不存在:$filePath");
return 1;
}

$result = $codingProject->getIssueTypes($this->codingToken, $this->codingProjectUri);
$issueTypes = [];
foreach ($result as $item) {
$issueTypes[$item['Name']] = $item;
}
$rows = Excel::toArray(new IssuesImport(), $filePath)[0];
foreach ($rows as $row) {
$data = [
'Type' => $issueTypes[$row['事项类型']]['IssueType'],
'IssueTypeId' => $issueTypes[$row['事项类型']]['Id'],
'Name' => $row['标题'],
'Priority' => \App\Models\Issue::PRIORITY_MAP[$row['优先级']],
];
try {
$result = $codingIssue->create($this->codingToken, $this->codingProjectUri, $data);
} catch (\Exception $e) {
$this->error('Error: ' . $e->getMessage());
return 1;
}
$this->info("https://{$this->codingTeamDomain}.coding.net/p/{$this->codingProjectUri}" .
"/all/issues/${result['Code']}");
}

return 0;
}
}
14 changes: 14 additions & 0 deletions app/Imports/IssuesImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

class IssuesImport implements WithHeadingRow
{
public function __construct()
{
HeadingRowFormatter::default('none');
}
}
29 changes: 29 additions & 0 deletions app/Models/Issue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Issue extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'type',
'name',
'priority',
];

public const PRIORITY_MAP = [
'' => '0',
'' => '1',
'' => '2',
'紧急' => '3',
];
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@
"ext-json": "*",
"ext-libxml": "*",
"ext-zip": "*",
"illuminate/database": "^8.40",
"illuminate/log": "^8.0",
"illuminate/translation": "^8.64",
"illuminate/validation": "^8.64",
"laravel-fans/confluence": "^0.1.1",
"laravel-zero/framework": "^8.8",
"league/html-to-markdown": "^5.0",
"maatwebsite/excel": "^3.1",
"nesbot/carbon": "^2.53",
"sinkcup/laravel-filesystem-cos-updated": "^4.0"
},
"require-dev": {
"fakerphp/faker": "^1.14",
"fakerphp/faker": "^1.9.1",
"mockery/mockery": "^1.4.3",
"phpmd/phpmd": "^2.10",
"phpunit/phpunit": "^9.5",
Expand Down
Loading

0 comments on commit 52b2a5d

Please sign in to comment.