Skip to content

Commit

Permalink
feat: #78 cli create iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkcup committed Oct 20, 2021
1 parent 240f90b commit 38a4ca4
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 9 deletions.
8 changes: 8 additions & 0 deletions app/Coding/Iteration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Coding;

use Carbon\Carbon;

class Iteration extends Base
{
public function create($token, $projectName, $data)
Expand All @@ -20,4 +22,10 @@ public function create($token, $projectName, $data)
$result = json_decode($response->getBody(), true);
return $result['Response']['Iteration'];
}

public static function generateName(Carbon $startAt, Carbon $endAt): string
{
$endFormat = $startAt->year == $endAt->year ? 'm/d' : 'Y/m/d';
return $startAt->format('Y/m/d') . '-' . $endAt->format($endFormat) . ' 迭代';
}
}
64 changes: 64 additions & 0 deletions app/Commands/IterationCreateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Commands;

use App\Coding\Iteration;
use Carbon\Carbon;
use LaravelZero\Framework\Commands\Command;

class IterationCreateCommand extends Command
{
use WithCoding;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'iteration:create
{--start_at= : 开始时间,格式:2021-10-20}
{--end_at= : 结束时间,格式:2021-10-30}
{--name= : 标题}
{--goal= : 目标}
{--assignee= : 处理人 ID}
{--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(Iteration $iteration): int
{
$this->setCodingApi();

$data = [];
$startAt = Carbon::parse($this->option('start_at') ?? $this->ask('开始时间:', Carbon::today()->toDateString()));
$data['StartAt'] = $startAt->toDateString();
$endAt = Carbon::parse($this->option('end_at') ?? $this->ask(
'结束时间:',
Carbon::today()->addDays(14)->toDateString()
));
$data['EndAt'] = $endAt->toDateString();
$data['Name'] = $this->option('name') ?? $this->ask('标题:', Iteration::generateName($startAt, $endAt));
$data['Goal'] = $this->option('goal');
$data['Assignee'] = $this->option('assignee');

$result = $iteration->create($this->codingToken, $this->codingProjectUri, $data);

$this->info('创建成功');
$this->info("https://{$this->codingTeamDomain}.coding.net/p/{$this->codingProjectUri}" .
"/iterations/${result['Code']}/issues");

return 0;
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"laravel-fans/confluence": "^0.1.1",
"laravel-zero/framework": "^8.8",
"league/html-to-markdown": "^5.0",
"nesbot/carbon": "^2.53",
"sinkcup/laravel-filesystem-cos-updated": "^4.0"
},
"require-dev": {
Expand Down
19 changes: 10 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions tests/Feature/IterationCreateCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Tests\Feature;

use App\Coding\Iteration;
use Carbon\Carbon;
use Tests\TestCase;

class IterationCreateCommandTest extends TestCase
{
private string $teamDomain;
private string $projectUri;

protected function setUp(): void
{
parent::setUp();
$codingToken = $this->faker->md5;
config(['coding.token' => $codingToken]);
$this->teamDomain = $this->faker->domainWord;
config(['coding.team_domain' => $this->teamDomain]);
$this->projectUri = $this->faker->slug;
config(['coding.project_uri' => $this->projectUri]);
}

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

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

$startAt = $this->faker->date();
$endAt = Carbon::parse($startAt)->addDays($this->faker->randomNumber())->toDateString();
$this->artisan('iteration:create', [
'--goal' => $this->faker->text(),
'--assignee' => $this->faker->randomNumber(),
])
->expectsQuestion('开始时间:', $startAt)
->expectsQuestion('结束时间:', $endAt)
->expectsQuestion('标题:', $startAt . '~' . $endAt . ' 迭代')
->expectsOutput('创建成功')
->expectsOutput("https://$this->teamDomain.coding.net/p/$this->projectUri/iterations/2746/issues")
->assertExitCode(0);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/CodingIterationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Coding\Issue;
use App\Coding\Iteration;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;
Expand Down Expand Up @@ -42,4 +43,17 @@ public function testCreateSuccess()
$result = $coding->create($codingToken, $codingProjectUri, $data);
$this->assertEquals(json_decode($responseBody, true)['Response']['Iteration'], $result);
}

public function testGenerateName()
{
$startAt = Carbon::parse('2021-10-20');
$endAt = Carbon::parse('2021-10-30');
$result = Iteration::generateName($startAt, $endAt);
$this->assertEquals("2021/10/20-10/30 迭代", $result);

$startAt = Carbon::parse('2021-12-27');
$endAt = Carbon::parse('2022-01-07');
$result = Iteration::generateName($startAt, $endAt);
$this->assertEquals("2021/12/27-2022/01/07 迭代", $result);
}
}

0 comments on commit 38a4ca4

Please sign in to comment.