Skip to content
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
29 changes: 28 additions & 1 deletion app/Coding/Wiki.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,6 +83,7 @@ public function createWikiByZip(string $token, string $projectName, array $uploa
* @param string $jobId
* @return mixed
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws Exception
*/
public function getImportJobStatus(string $token, string $projectName, string $jobId)
{
Expand All@@ -99,9 +100,35 @@ public function getImportJobStatus(string $token, string $projectName, string $j
],
]);
$result = json_decode($response->getBody(), true);
if (isset($result['Response']['Error']['Message'])){
throw new Exception($result['Response']['Error']['Message']);
}
return $result['Response']['Data'];
}

public function getImportJobStatusWithRetry(string $token, string $projectName, string $jobId, int $retry = 10)
{
$waitingTimes = 0;
while (true){
// HACK 如果上传成功立即查询,会报错:invoke function
sleep(1);
try{
$jobStatus = $this->getImportJobStatus($token, $projectName, $jobId);
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waitingTimes < $retry){
$waitingTimes++;
continue;
}
return $jobStatus;
} catch (Exception $e){
if ($waitingTimes < 10){
$waitingTimes++;
continue;
}
throw $e;
}
break;
}
}
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath, int $parentId)
{
$zipFilename = basename($zipFileFullPath);
Expand DownExpand Up@@ -136,7 +163,7 @@ public function getWiki(string $token, string $projectName, int $id, int $versio
return $result['Response']['Data'];
}

public function updateWikiTitle(string $token, string $projectName, int $id, string $title): bool
public function updateTitle(string $token, string $projectName, int $id, string $title): bool
{
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
'headers' => [
Expand Down
38 changes: 14 additions & 24 deletions app/Commands/WikiImportCommand.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -197,34 +197,24 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
);
$this->info('上传成功,正在处理,任务 ID:' . $result['JobId']);
$wikiId = null;
$waitingTimes = 0;
while (true){
// HACK 如果上传成功立即查询,会报错:invoke function
sleep(1);
try{
$jobStatus = $this->codingWiki->getImportJobStatus(
$this->codingToken,
$this->codingProjectUri,
$result['JobId']
);
} catch (Exception $e){
$waitingTimes++;
continue;
}
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waitingTimes < 10){
$waitingTimes++;
continue;
}
if ($jobStatus['Status'] == 'success'){
$wikiId = intval($jobStatus['Iids'][0]);
$this->codingWiki->updateWikiTitle($this->codingToken, $this->codingProjectUri, $wikiId, $title);
}
break;
try{
$jobStatus = $this->codingWiki->getImportJobStatusWithRetry(
$this->codingToken,
$this->codingProjectUri,
$result['JobId']
);
} catch (Exception $e){
$this->error('错误:导入失败,跳过');
continue;
}
if ($jobStatus['Status'] == 'success'){
$wikiId = intval($jobStatus['Iids'][0]);
}
if (empty($wikiId)){
$this->warn('导入失败,跳过');
$this->error('错误:导入失败,跳过');
continue;
}
$this->codingWiki->updateTitle($this->codingToken, $this->codingProjectUri, $wikiId, $title);
if (!empty($subPages)){
$this->info('发现 ' . count($subPages) . ' 个子页面');
// TODO tests
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/WikiImportCommandTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -167,7 +167,7 @@ public function testHandleConfluenceHtmlSuccess()
file_get_contents($this->dataDir . 'coding/' . 'DescribeImportJobStatusResponse.json'),
true
)['Response']['Data']);
$mock->shouldReceive('updateWikiTitle')->times(4)->andReturn(true);
$mock->shouldReceive('updateTitle')->times(4)->andReturn(true);


$mockDisk = \Mockery::mock(Disk::class, [])->makePartial();
Expand DownExpand Up@@ -233,7 +233,7 @@ public function testHandleConfluenceHtmlZipSuccess()
file_get_contents($this->dataDir . 'coding/' . 'DescribeImportJobStatusResponse.json'),
true
)['Response']['Data']);
$mock->shouldReceive('updateWikiTitle')->times(5)->andReturn(true);
$mock->shouldReceive('updateTitle')->times(5)->andReturn(true);


$mockDisk = \Mockery::mock(Disk::class, [])->makePartial();
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/CodingWikiTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -243,7 +243,7 @@ public function testGetWiki()
$this->assertEquals(json_decode($responseBody, true)['Response']['Data'], $result);
}

public function testUpdateWikiTitle()
public function testupdateTitle()
{
$responseBody = file_get_contents($this->dataDir . 'coding/ModifyWikiTitleResponse.json');
$codingToken = $this->faker->md5;
Expand DownExpand Up@@ -273,7 +273,7 @@ public function testUpdateWikiTitle()
)
->willReturn(new Response(200, [], $responseBody));
$coding = new Wiki($clientMock);
$result = $coding->updateWikiTitle($codingToken, $codingProjectUri, $id, $title);
$result = $coding->updateTitle($codingToken, $codingProjectUri, $id, $title);
$this->assertTrue($result);
}
}