Skip to content

Issue 78 iteration #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions app/Coding/Iteration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Coding;

use Carbon\Carbon;

class Iteration extends Base
{
public function create($token, $projectName, $data)
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => 'CreateIteration',
'ProjectName' => $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) . ' 迭代';
}
}
6 changes: 2 additions & 4 deletions app/Coding/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Coding;

use Exception;

class Project extends Base
{
public function getIssueTypes($token, $projectName)
Expand All @@ -14,10 +12,10 @@ public function getIssueTypes($token, $projectName)
'Authorization' => "token ${token}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'json' => [
'Action' => 'DescribeProjectIssueTypeList',
'ProjectName' => $projectName,
]),
],
]);
$result = json_decode($response->getBody(), true);
return $result['Response']['IssueTypes'];
Expand Down
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);
}
}
2 changes: 1 addition & 1 deletion tests/Feature/WikiImportCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected function setUp(): void
{
parent::setUp();
$this->createWikiResponse = json_decode(
file_get_contents($this->dataDir . 'coding/createWikiResponse.json'),
file_get_contents($this->dataDir . 'coding/CreateWikiResponse.json'),
true
)['Response']['Data'];
}
Expand Down
59 changes: 59 additions & 0 deletions tests/Unit/CodingIterationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Tests\Unit;

use App\Coding\Issue;
use App\Coding\Iteration;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use Tests\TestCase;

class CodingIterationTest extends TestCase
{
public function testCreateSuccess()
{
$responseBody = file_get_contents($this->dataDir . 'coding/CreateIterationResponse.json');
$codingToken = $this->faker->md5;
$codingProjectUri = $this->faker->slug;
$data = [
'Name' => $this->faker->title,
];

$clientMock = $this->getMockBuilder(Client::class)->getMock();
$clientMock->expects($this->once())
->method('request')
->with(
'POST',
'https://e.coding.net/open-api',
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => "token ${codingToken}",
'Content-Type' => 'application/json'
],
'json' => array_merge([
'Action' => 'CreateIteration',
'ProjectName' => $codingProjectUri,
], $data)
]
)
->willReturn(new Response(200, [], $responseBody));
$coding = new Iteration($clientMock);
$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);
}
}
4 changes: 2 additions & 2 deletions tests/Unit/CodingWikiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function setUp(): void

public function testCreateWiki()
{
$responseBody = file_get_contents($this->dataDir . 'coding/createWikiResponse.json');
$responseBody = file_get_contents($this->dataDir . 'coding/CreateWikiResponse.json');
$codingToken = $this->faker->md5;
$codingProjectUri = $this->faker->slug;
$article = [
Expand Down Expand Up @@ -74,7 +74,7 @@ public function testCreateWiki()

public function testCreateUploadToken()
{
$responseBody = file_get_contents($this->dataDir . 'coding/createUploadTokenResponse.json');
$responseBody = file_get_contents($this->dataDir . 'coding/CreateUploadTokenResponse.json');
$codingToken = $this->faker->md5;
$codingProjectUri = $this->faker->slug;
$fileName = $this->faker->word;
Expand Down
24 changes: 24 additions & 0 deletions tests/data/coding/CreateIterationResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"Response" : {
"Iteration" : {
"Assignee" : 0,
"Code" : 2746,
"CompletedCount" : 0,
"CompletedPercent" : 0,
"Completer" : 0,
"CreatedAt" : 1634697259529,
"Creator" : 183478,
"Deleter" : 0,
"EndAt" : -28800000,
"Goal" : "",
"Name" : "it by cli",
"ProcessingCount" : 0,
"StartAt" : -28800000,
"Starter" : 0,
"Status" : "WAIT_PROCESS",
"UpdatedAt" : 1634697259529,
"WaitProcessCount" : 0
},
"RequestId" : "58777aa6-e6e4-155a-c99f-415a33615ca6"
}
}