Skip to content

Commit 4cca678

Browse files
committed
feat: #23 confluence page tree
1 parent 4ffe4c2 commit 4cca678

File tree

6 files changed

+48
-13
lines changed

6 files changed

+48
-13
lines changed

app/Coding.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,15 @@ public function getImportJobStatus(string $token, string $projectName, string $j
145145
],
146146
]);
147147
$result = json_decode($response->getBody(), true);
148-
if (isset($result['Response']['Data']['Status'])) {
149-
return $result['Response']['Data']['Status'];
148+
if (isset($result['Response']['Data'])) {
149+
return $result['Response']['Data'];
150150
} else {
151151
// TODO exception message
152152
return new \Exception('failed');
153153
}
154154
}
155155

156-
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath)
156+
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath, int $parentId)
157157
{
158158
$zipFilename = basename($zipFileFullPath);
159159
$uploadToken = $this->createUploadToken(
@@ -163,7 +163,7 @@ public function createWikiByUploadZip(string $token, string $projectName, string
163163
);
164164
$this->upload($uploadToken, $zipFileFullPath);
165165
return $this->createWikiByZip($token, $projectName, $uploadToken, [
166-
'ParentIid' => 0,
166+
'ParentIid' => $parentId,
167167
'FileName' => $zipFilename,
168168
]);
169169
}

app/Commands/WikiImportCommand.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private function handleConfluenceHtml(): int
180180
return 0;
181181
}
182182

183-
private function uploadConfluencePages(string $dataPath, array $tree, array $titles): void
183+
private function uploadConfluencePages(string $dataPath, array $tree, array $titles, int $parentId = 0): void
184184
{
185185
foreach ($tree as $page => $subPages) {
186186
$this->info('标题:' . $titles[$page]);
@@ -190,13 +190,35 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
190190
$result = $this->coding->createWikiByUploadZip(
191191
$this->codingToken,
192192
$this->codingProjectUri,
193-
$zipFilePath
193+
$zipFilePath,
194+
$parentId,
194195
);
195196
$this->info('上传成功,正在处理,任务 ID:' . $result['JobId']);
196-
// TODO 指定 parent
197+
$wikiId = null;
198+
$waiting_times = 0;
199+
while (true) {
200+
$jobStatus = $this->coding->getImportJobStatus(
201+
$this->codingToken,
202+
$this->codingProjectUri,
203+
$result['JobId']
204+
);
205+
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waiting_times < 10) {
206+
$waiting_times++;
207+
sleep(1);
208+
continue;
209+
}
210+
if ($jobStatus['Status'] == 'success') {
211+
$wikiId = intval($jobStatus['Iids'][0]);
212+
}
213+
break;
214+
}
215+
if (empty($wikiId)) {
216+
$this->warn('导入失败,跳过');
217+
continue;
218+
}
197219
if (!empty($subPages)) {
198220
$this->info('发现 ' . count($subPages) . ' 个子页面');
199-
$this->uploadConfluencePages($dataPath, $subPages, $titles);
221+
$this->uploadConfluencePages($dataPath, $subPages, $titles, $parentId);
200222
}
201223
}
202224
}

app/Commands/WikiUploadCommand.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class WikiUploadCommand extends Command
1616
*/
1717
protected $signature = 'wiki:upload
1818
{file : Zip 文件需包含 1 个 Markdown 文件及全部引用图片,Markdown 文件名将作为文档标题,图片使用相对路径}
19+
{--parent_id=0 : 父页面 ID}
1920
{--coding_token= : CODING 令牌}
2021
{--coding_team_domain= : CODING 团队域名,如 xxx.coding.net 即填写 xxx}
2122
{--coding_project_uri= : CODING 项目标识,如 xxx.coding.net/p/yyy 即填写 yyy}
@@ -42,7 +43,13 @@ public function handle(Coding $coding): int
4243
$this->error("文件不存在:$filePath");
4344
return 1;
4445
}
45-
$result = $this->coding->createWikiByUploadZip($this->codingToken, $this->codingProjectUri, $filePath);
46+
$parentId = intval($this->option('parent_id'));
47+
$result = $this->coding->createWikiByUploadZip(
48+
$this->codingToken,
49+
$this->codingProjectUri,
50+
$filePath,
51+
$parentId
52+
);
4653
$this->info('上传成功,正在处理,任务 ID:' . $result['JobId']);
4754

4855
return 0;

tests/Feature/WikiImportCommandTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function testHandleConfluenceHtmlSuccess()
162162
file_get_contents($this->dataDir . 'coding/' . 'CreateWikiByZipResponse.json'),
163163
true
164164
)['Response']);
165+
$mock->shouldReceive('getImportJobStatus')->times(4)->andReturn(json_decode(
166+
file_get_contents($this->dataDir . 'coding/' . 'DescribeImportJobStatusResponse.json'),
167+
true
168+
)['Response']['Data']);
165169

166170
$this->artisan('wiki:import')
167171
->expectsQuestion('数据来源?', 'Confluence')

tests/Unit/CodingTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public function testGetImportJobStatus()
148148
->willReturn(new Response(200, [], $responseBody));
149149
$coding = new Coding($clientMock);
150150
$result = $coding->getImportJobStatus($codingToken, $codingProjectUri, $jobId);
151-
$this->assertEquals('success', $result);
151+
$this->assertEquals('success', $result['Status']);
152+
$this->assertEquals([27], $result['Iids']);
152153
}
153154

154155
public function testCreateWikiByZip()
@@ -158,7 +159,7 @@ public function testCreateWikiByZip()
158159
$codingProjectUri = $this->faker->slug;
159160

160161
$data = [
161-
'ParentIid' => 0,
162+
'ParentIid' => $this->faker->randomNumber(),
162163
'FileName' => $this->faker->word,
163164
];
164165
$clientMock = $this->getMockBuilder(Client::class)->getMock();
@@ -203,7 +204,7 @@ public function testCreateWikiByUploadZip()
203204
)['Response']);
204205

205206
$filePath = $this->faker->filePath();
206-
$result = $mock->createWikiByUploadZip('token', 'project', $filePath);
207+
$result = $mock->createWikiByUploadZip('token', 'project', $filePath, $this->faker->randomNumber());
207208
$this->assertArrayHasKey('JobId', $result);
208209
}
209210
}

tests/data/coding/DescribeImportJobStatusResponse.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"Response" : {
33
"Data": {
44
"JobId": "123456ad-f123-4ac2-9586-42ebe5d1234d",
5-
"Status": "success"
5+
"Status": "success",
6+
"Iids": [27]
67
},
78
"RequestId": "78d6ecf8-9123-574f-8da9-23bb44c467f1"
89
}

0 commit comments

Comments
 (0)