Skip to content

Commit

Permalink
feat: #72 create iteration when import
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Oct 21, 2021
1 parent 8312c85 commit da1e462
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 16 deletions.
48 changes: 34 additions & 14 deletions app/Commands/IssueImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Commands;

use App\Coding\Issue;
use App\Coding\Iteration;
use App\Coding\Project;
use LaravelZero\Framework\Commands\Command;
use Rap2hpoutre\FastExcel\Facades\FastExcel;
Expand Down Expand Up @@ -31,11 +32,14 @@ class IssueImportCommand extends Command
*/
protected $description = '导入事项';

protected array $iterationMap = [];
protected array $issueTypes = [];

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

Expand All @@ -45,29 +49,45 @@ public function handle(Issue $codingIssue, Project $codingProject): int
return 1;
}

$result = $codingProject->getIssueTypes($this->codingToken, $this->codingProjectUri);
$issueTypes = [];
foreach ($result as $item) {
$issueTypes[$item['Name']] = $item;
}
$rows = FastExcel::import($filePath);
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);
$iterationResult = $this->createByRow($codingProject, $codingIssue, $iteration, $row);
} catch (\Exception $e) {
$this->error('Error: ' . $e->getMessage());
return 1;
}
$this->info("https://{$this->codingTeamDomain}.coding.net/p/{$this->codingProjectUri}" .
"/all/issues/${result['Code']}");
"/all/issues/${iterationResult['Code']}");
}

return 0;
}

private function createByRow(Project $codingProject, Issue $issue, Iteration $iteration, array $row)
{
if (empty($this->issueTypes)) {
$result = $codingProject->getIssueTypes($this->codingToken, $this->codingProjectUri);
foreach ($result as $item) {
$this->issueTypes[$item['Name']] = $item;
}
}
$data = [
'Type' => $this->issueTypes[$row['事项类型']]['IssueType'],
'IssueTypeId' => $this->issueTypes[$row['事项类型']]['Id'],
'Name' => $row['标题'],
'Priority' => \App\Models\Issue::PRIORITY_MAP[$row['优先级']],
'IterationCode' => $row['所属迭代'] ? $this->getIterationCode($iteration, $row['所属迭代']) : null,
];
return $issue->create($this->codingToken, $this->codingProjectUri, $data);
}

private function getIterationCode(Iteration $iteration, string $name)
{
if (!isset($this->iterationMap[$name])) {
$result = $iteration->create($this->codingToken, $this->codingProjectUri, ['name' => $name]);
$this->iterationMap[$name] = $result['Code'];
}
return $this->iterationMap[$name];
}
}
57 changes: 55 additions & 2 deletions tests/Feature/IssueImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
namespace Tests\Feature;

use App\Coding\Issue;
use App\Coding\Iteration;
use App\Coding\Project;
use Tests\TestCase;

class IssueImportCommandTest extends TestCase
{
private string $codingToken;
private string $codingTeamDomain;
private string $codingProjectUri;

protected function setUp(): void
{
parent::setUp();
$codingToken = $this->faker->md5;
config(['coding.token' => $codingToken]);
$this->codingToken = $this->faker->md5;
config(['coding.token' => $this->codingToken]);
$this->codingTeamDomain = $this->faker->domainWord;
config(['coding.team_domain' => $this->codingTeamDomain]);
$this->codingProjectUri = $this->faker->slug;
Expand All @@ -34,6 +36,8 @@ public function testImportSuccess()

$issueMock = \Mockery::mock(Issue::class, [])->makePartial();
$this->instance(Issue::class, $issueMock);
$iterationMock = \Mockery::mock(Iteration::class, [])->makePartial();
$this->instance(Iteration::class, $iterationMock);

$response = json_decode(
file_get_contents($this->dataDir . 'coding/' . 'CreateIssueResponse.json'),
Expand All @@ -44,6 +48,11 @@ public function testImportSuccess()
$response['Code'] = $i;
$results[] = $response;
}
$iterationMock->shouldReceive('create')->times(2)->andReturn(json_decode(
file_get_contents($this->dataDir . 'coding/' . 'CreateIterationResponse.json'),
true
)['Response']['Iteration']);

$issueMock->shouldReceive('create')->times(21)->andReturn(...$results);

$this->artisan('issue:import', ['file' => $this->dataDir . 'coding/scrum-issues.csv'])
Expand All @@ -54,4 +63,48 @@ public function testImportSuccess()
->expectsOutput("https://$this->codingTeamDomain.coding.net/p/$this->codingProjectUri/all/issues/21")
->assertExitCode(0);
}

public function testImportUserStorySuccess()
{
$mock = \Mockery::mock(Project::class, [])->makePartial();
$this->instance(Project::class, $mock);

$mock->shouldReceive('getIssueTypes')->times(1)->andReturn(json_decode(
file_get_contents($this->dataDir . 'coding/' . 'DescribeProjectIssueTypeListResponse.json'),
true
)['Response']['IssueTypes']);

$issueMock = \Mockery::mock(Issue::class, [])->makePartial();
$this->instance(Issue::class, $issueMock);
$iterationMock = \Mockery::mock(Iteration::class, [])->makePartial();
$this->instance(Iteration::class, $iterationMock);

$response = json_decode(
file_get_contents($this->dataDir . 'coding/' . 'CreateIssueResponse.json'),
true
)['Response']['Issue'];
$response['Code'] = $this->faker->randomNumber();
$result = $response;
$iterationMock->shouldReceive('create')->times(1)->andReturn(json_decode(
file_get_contents($this->dataDir . 'coding/' . 'CreateIterationResponse.json'),
true
)['Response']['Iteration']);

$issueMock->shouldReceive('create')->times(1)->withArgs([
$this->codingToken,
$this->codingProjectUri,
[
'Type' => 'REQUIREMENT',
'IssueTypeId' => 213218,
'Name' => '用户可通过手机号注册账户',
'Priority' => "1",
'IterationCode' => 2746,
]
])->andReturn($result);

$this->artisan('issue:import', ['file' => $this->dataDir . 'coding/scrum-issue-5.csv'])
->expectsOutput("https://$this->codingTeamDomain.coding.net/p/$this->codingProjectUri/all/issues/" .
$result['Code'])
->assertExitCode(0);
}
}
2 changes: 2 additions & 0 deletions tests/data/coding/scrum-issue-5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ID,事项类型,标题,描述,状态,创建时间,创建人,更新时间,所属迭代,故事点,处理人,缺陷类型,优先级,截止日期,模块,标签,关注人,开始日期
5,用户故事,用户可通过手机号注册账户,,开发中,2021-10-19 11:26:37,sinkcup,2021-10-19 11:26:37,第 1 次迭代,2,sinkcup,,中,2021-10-21,,,,

0 comments on commit da1e462

Please sign in to comment.