Skip to content

Commit 2283681

Browse files
refactor(app/Generators): sanitize output in GithubCopilotCliGenerator
- Add a private method `sanitize` to the `GithubCopilotCliGenerator` class - The `sanitize` method takes a string parameter `output` - Inside the `sanitize` method, the `output` string is sanitized - The sanitized output is returned as a string - The `sanitize` method uses the `str` function to manipulate the `output` string - The `match` method is called on the `str` object to extract a JSON substring from the `output` string - The `replaceMatches` method is called on the `str` object to remove control characters from the JSON substring - The sanitized JSON substring is returned as the result of the `sanitize` method - The `sanitize` method is called in the `GithubCopilotCliGenerator` class to sanitize the output before returning it
1 parent 85e6765 commit 2283681

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

app/Generators/GithubCopilotCliGenerator.php

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function generate(string $prompt): string
5050
Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
5151
})->getOutput();
5252

53+
return $this->sanitize($output);
54+
}
55+
56+
private function sanitize(string $output): string
57+
{
5358
return (string) str($output)
5459
->match('/\{.*\}/s')
5560
// ->replaceMatches('/[[:cntrl:]]/mu', '')

tests/Unit/Generators/GithubCopilotCliGeneratorTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,20 @@
2828
config('ai-commit')->set('generators.github_copilot_cli.binary', 'github-copilot-cli');
2929
expect(app(GeneratorManager::class)->driver('github_copilot_cli'))->generate('error');
3030
})->group(__DIR__, __FILE__)->throws(ProcessFailedException::class);
31+
32+
it('can sanitize output to JSON', function (): void {
33+
$output = <<<'OUTPUT'
34+
{
35+
"subject": "fix(app/Generators): update GithubCopilotCliGenerator to include binary command",
36+
"body": "- Change the command array in the `resolve` function call to include `[\'binary\', \'copilot\', \'explain\', $prompt]` as the command\\n- Update the `mustRun` function
37+
callback to handle output formatting\\n- Add debug statements to output the generated `$output` variable and perform a `dd()` call\\n- Return the generated `$output` variable"
38+
}
39+
OUTPUT;
40+
41+
expect($output)->not->toBeJson()
42+
->and(
43+
(function (string $output): string {
44+
return $this->sanitize($output);
45+
})->call(app(GeneratorManager::class)->driver('github_copilot_cli'), $output)
46+
)->toBeJson();
47+
})->group(__DIR__, __FILE__);

0 commit comments

Comments
 (0)