Skip to content

Commit 6542075

Browse files
committed
chore: #57 output errors in the end
1 parent 5120502 commit 6542075

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

app/Commands/WikiImportCommand.php

+29-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class WikiImportCommand extends Command
1818

1919
protected Disk $codingDisk;
2020
protected Wiki $codingWiki;
21+
protected array $errors = [];
22+
2123
/**
2224
* The signature of the command.
2325
*
@@ -81,12 +83,21 @@ public function handle(Disk $codingDisk, Wiki $codingWiki, \App\Confluence $conf
8183
}
8284
switch ($dataType) {
8385
case 'HTML':
84-
return $this->handleConfluenceHtml();
86+
$this->handleConfluenceHtml();
87+
break;
8588
case 'API':
86-
return $this->handleConfluenceApi();
89+
$this->handleConfluenceApi();
90+
break;
8791
default:
8892
break;
8993
}
94+
if (!empty($this->errors)) {
95+
$this->info('报错信息汇总:');
96+
}
97+
foreach ($this->errors as $error) {
98+
$this->error($error);
99+
}
100+
return count($this->errors);
90101
}
91102

92103
private function createWiki($data)
@@ -139,7 +150,9 @@ private function handleConfluenceHtml(): int
139150
$htmlDir = $this->unzipConfluenceHtml();
140151
$filePath = $htmlDir . 'index.html';
141152
if (!file_exists($filePath)) {
142-
$this->error("文件不存在:$filePath");
153+
$message = "文件不存在:$filePath";
154+
$this->error($message);
155+
$this->errors[] = $message;
143156
return 1;
144157
}
145158
try {
@@ -187,6 +200,13 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
187200
$dataPath,
188201
$attachments
189202
);
203+
foreach ($codingAttachments as $attachmentPath => $codingAttachment) {
204+
if (empty($codingAttachment)) {
205+
$message = '错误:文件上传失败 ' . $attachmentPath;
206+
$this->error($message);
207+
$this->errors[] = $message;
208+
}
209+
}
190210
$markdown = $this->codingWiki->replaceAttachments($markdown, $codingAttachments);
191211
$mdFilename = substr($page, 0, -5) . '.md';
192212
if ($this->option('save-markdown')) {
@@ -208,14 +228,18 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
208228
$result['JobId']
209229
);
210230
} catch (Exception $e) {
211-
$this->error('错误:导入失败,跳过');
231+
$message = '错误:导入失败,跳过 ' . $title . ' ' . $page;
232+
$this->error($message);
233+
$this->errors[] = $message;
212234
continue;
213235
}
214236
if ($jobStatus['Status'] == 'success') {
215237
$wikiId = intval($jobStatus['Iids'][0]);
216238
}
217239
if (empty($wikiId)) {
218-
$this->error('错误:导入失败,跳过');
240+
$message = '错误:导入失败,跳过 ' . $title . ' ' . $page;
241+
$this->error($message);
242+
$this->errors[] = $message;
219243
continue;
220244
}
221245
$this->codingWiki->updateTitle($this->codingToken, $this->codingProjectUri, $wikiId, $title);

tests/Feature/WikiImportCommandTest.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public function testHandleShow()
121121
->assertExitCode(0);
122122
}
123123

124-
public function testHandleConfluenceHtmlFileNotExist()
124+
public function testHandleConfluenceHtmlDirNotExist()
125125
{
126126
$codingToken = $this->faker->md5;
127127
config(['coding.token' => $codingToken]);
@@ -133,15 +133,27 @@ public function testHandleConfluenceHtmlFileNotExist()
133133
$this->artisan('wiki:import')
134134
->expectsQuestion('数据来源?', 'Confluence')
135135
->expectsQuestion('数据类型?', 'HTML')
136-
->expectsQuestion('空间导出的 HTML zip 文件路径', '~/Downloads/')
137-
->expectsOutput('文件不存在:~/Downloads/index.html')
136+
->expectsQuestion('空间导出的 HTML zip 文件路径', '/dev/null/')
137+
->expectsOutput('文件不存在:/dev/null/index.html')
138+
->expectsOutput('报错信息汇总:')
139+
->expectsOutput('文件不存在:/dev/null/index.html')
138140
->assertExitCode(1);
141+
}
142+
143+
public function testHandleConfluenceHtmlFileNotExist()
144+
{
145+
$codingToken = $this->faker->md5;
146+
config(['coding.token' => $codingToken]);
147+
$codingTeamDomain = $this->faker->domainWord;
148+
config(['coding.team_domain' => $codingTeamDomain]);
149+
$codingProjectUri = $this->faker->slug;
150+
config(['coding.project_uri' => $codingProjectUri]);
139151

140152
$this->artisan('wiki:import')
141153
->expectsQuestion('数据来源?', 'Confluence')
142154
->expectsQuestion('数据类型?', 'HTML')
143-
->expectsQuestion('空间导出的 HTML zip 文件路径', '~/Downloads/index.html')
144-
->expectsOutput('文件不存在:~/Downloads/index.html')
155+
->expectsQuestion('空间导出的 HTML zip 文件路径', '/dev/null/index.html')
156+
->expectsOutput('文件不存在:/dev/null/index.html')
145157
->assertExitCode(1);
146158
}
147159

0 commit comments

Comments
 (0)