forked from bkinnightskytw/code_work_spawner
fix: `tea` CLI call hang in windows by replaced it with direct http API called
This commit is contained in:
parent
b14bbe5d0a
commit
041225b2aa
|
|
@ -79,3 +79,8 @@ Start from [`examples/simple-github.yaml`](simple-github.yaml) for GitHub,
|
||||||
[`examples/simple-openproject.yaml`](simple-openproject.yaml) for OpenProject.
|
[`examples/simple-openproject.yaml`](simple-openproject.yaml) for OpenProject.
|
||||||
This repo uses `dataDir`, `worktreeDir`, and `projects`, and uses
|
This repo uses `dataDir`, `worktreeDir`, and `projects`, and uses
|
||||||
`worktreeDir` for bug-verification worktrees.
|
`worktreeDir` for bug-verification worktrees.
|
||||||
|
|
||||||
|
## tea + windows
|
||||||
|
|
||||||
|
The `tea` CLI has known severe issues on Windows due to lipgloss/v2 query console color. We had bypass it by read config file of `tea` directly, but only token login is supported. And need to config a default login through `tea login default <LOGIN>`.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,92 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
|
import '../config/app_logger.dart';
|
||||||
|
import 'package:executable/executable.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
|
||||||
class _ResolvedCommand {
|
class ResolvedExternalCommand {
|
||||||
_ResolvedCommand({
|
ResolvedExternalCommand({
|
||||||
required this.executable,
|
required this.executable,
|
||||||
required this.arguments,
|
required this.arguments,
|
||||||
required this.usesBashShim,
|
required this.usesBashShim,
|
||||||
|
required this.runInShell,
|
||||||
});
|
});
|
||||||
|
|
||||||
final String executable;
|
final String executable;
|
||||||
final List<String> arguments;
|
final List<String> arguments;
|
||||||
final bool usesBashShim;
|
final bool usesBashShim;
|
||||||
|
final bool runInShell;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ResolvedCommand _resolveCommand(String executable, List<String> arguments) {
|
final AppLogger _processLauncherLogger = AppLogger(
|
||||||
final extension = p.extension(executable).toLowerCase();
|
'code_work_spawner.process_launcher',
|
||||||
|
);
|
||||||
|
|
||||||
|
ResolvedExternalCommand resolveExternalCommandForCurrentPlatform(
|
||||||
|
String executable,
|
||||||
|
List<String> arguments, {
|
||||||
|
bool? isWindows,
|
||||||
|
}) {
|
||||||
|
final isWindowsPlatform = isWindows ?? Platform.isWindows;
|
||||||
|
final localExecutablePath = _resolveLocalExecutablePath(executable);
|
||||||
|
final resolvedExecutablePath =
|
||||||
|
localExecutablePath ?? _resolveExecutablePathOnPath(executable);
|
||||||
|
final effectiveExecutable = resolvedExecutablePath ?? executable;
|
||||||
|
final extension = p.extension(effectiveExecutable).toLowerCase();
|
||||||
final shouldUseBash = extension.isEmpty || extension == '.sh';
|
final shouldUseBash = extension.isEmpty || extension == '.sh';
|
||||||
|
|
||||||
if (Platform.isWindows && shouldUseBash && File(executable).existsSync()) {
|
if (isWindowsPlatform && shouldUseBash && localExecutablePath != null) {
|
||||||
final bashScriptPath = _toBashPath(executable);
|
final bashScriptPath = _toBashPath(localExecutablePath);
|
||||||
return _ResolvedCommand(
|
_processLauncherLogger.debug(
|
||||||
|
'resolved WSL bash shim executable=$localExecutablePath '
|
||||||
|
'bash_script=$bashScriptPath '
|
||||||
|
'args=${_renderLauncherArguments(arguments)}',
|
||||||
|
);
|
||||||
|
return ResolvedExternalCommand(
|
||||||
executable: 'wsl.exe',
|
executable: 'wsl.exe',
|
||||||
arguments: ['--exec', 'bash', bashScriptPath, ...arguments],
|
arguments: ['--exec', 'bash', bashScriptPath, ...arguments],
|
||||||
usesBashShim: true,
|
usesBashShim: true,
|
||||||
|
runInShell: false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _ResolvedCommand(
|
return ResolvedExternalCommand(
|
||||||
executable: executable,
|
executable: effectiveExecutable,
|
||||||
arguments: arguments,
|
arguments: arguments,
|
||||||
usesBashShim: false,
|
usesBashShim: false,
|
||||||
|
// Only batch files require shell mediation on Windows. Native executables
|
||||||
|
// such as Scoop shims should run directly to avoid hanging shell wrappers.
|
||||||
|
runInShell: isWindowsPlatform && _requiresWindowsShell(extension),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool _requiresWindowsShell(String extension) {
|
||||||
|
return extension == '.bat' || extension == '.cmd';
|
||||||
|
}
|
||||||
|
|
||||||
|
String? _resolveLocalExecutablePath(String executable) {
|
||||||
|
final localFile = File(executable);
|
||||||
|
if (!localFile.existsSync()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return localFile.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
String? _resolveExecutablePathOnPath(String executable) {
|
||||||
|
if (_looksLikePathReference(executable)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return Executable(executable).findSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _looksLikePathReference(String executable) {
|
||||||
|
return executable.contains(r'\') ||
|
||||||
|
executable.contains('/') ||
|
||||||
|
executable.startsWith('.');
|
||||||
|
}
|
||||||
|
|
||||||
String _toBashPath(String windowsPath) {
|
String _toBashPath(String windowsPath) {
|
||||||
final normalized = windowsPath.replaceAll('\\', '/');
|
final normalized = windowsPath.replaceAll('\\', '/');
|
||||||
final drivePrefixMatch = RegExp(r'^([A-Za-z]):/(.*)$').firstMatch(normalized);
|
final drivePrefixMatch = RegExp(r'^([A-Za-z]):/(.*)$').firstMatch(normalized);
|
||||||
|
|
@ -52,20 +105,251 @@ Future<ProcessResult> runExternalCommand(
|
||||||
String? workingDirectory,
|
String? workingDirectory,
|
||||||
Map<String, String>? environment,
|
Map<String, String>? environment,
|
||||||
bool includeParentEnvironment = true,
|
bool includeParentEnvironment = true,
|
||||||
}) {
|
Duration? timeout,
|
||||||
final resolved = _resolveCommand(executable, arguments);
|
}) async {
|
||||||
|
final resolved = resolveExternalCommandForCurrentPlatform(
|
||||||
|
executable,
|
||||||
|
arguments,
|
||||||
|
);
|
||||||
final effectiveEnvironment = _effectiveEnvironmentForResolvedCommand(
|
final effectiveEnvironment = _effectiveEnvironmentForResolvedCommand(
|
||||||
environment: environment,
|
environment: environment,
|
||||||
includeParentEnvironment: includeParentEnvironment,
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
usesBashShim: resolved.usesBashShim,
|
usesBashShim: resolved.usesBashShim,
|
||||||
);
|
);
|
||||||
return Process.run(
|
final mode = Platform.isWindows && !resolved.usesBashShim
|
||||||
|
? 'windows-wrapper'
|
||||||
|
: 'process-run';
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'launch mode=$mode executable=${resolved.executable} '
|
||||||
|
'requested_executable=$executable run_in_shell=${resolved.runInShell} '
|
||||||
|
'uses_bash_shim=${resolved.usesBashShim} '
|
||||||
|
'working_directory=${workingDirectory ?? "(inherit)"} '
|
||||||
|
'timeout_ms=${timeout?.inMilliseconds ?? 0} '
|
||||||
|
'args=${_renderLauncherArguments(resolved.arguments)}',
|
||||||
|
);
|
||||||
|
if (Platform.isWindows && !resolved.usesBashShim) {
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'process_run in runExternalCommand - windows wrapper',
|
||||||
|
);
|
||||||
|
return _runExternalCommandViaWindowsWrapper(
|
||||||
|
executable: resolved.executable,
|
||||||
|
arguments: resolved.arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
environment: effectiveEnvironment,
|
||||||
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
|
timeout: timeout,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
final stopwatch = Stopwatch()..start();
|
||||||
|
try {
|
||||||
|
_processLauncherLogger.debug('process_run in runExternalCommand');
|
||||||
|
final result = timeout == null
|
||||||
|
? await Process.run(
|
||||||
resolved.executable,
|
resolved.executable,
|
||||||
resolved.arguments,
|
resolved.arguments,
|
||||||
workingDirectory: workingDirectory,
|
workingDirectory: workingDirectory,
|
||||||
environment: effectiveEnvironment,
|
environment: effectiveEnvironment,
|
||||||
includeParentEnvironment: includeParentEnvironment,
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
|
runInShell: resolved.runInShell,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
)
|
||||||
|
: await Process.run(
|
||||||
|
resolved.executable,
|
||||||
|
resolved.arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
environment: effectiveEnvironment,
|
||||||
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
|
runInShell: resolved.runInShell,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
).timeout(
|
||||||
|
timeout,
|
||||||
|
onTimeout: () {
|
||||||
|
throw TimeoutException(
|
||||||
|
'Command timed out after ${timeout.inMilliseconds}ms.',
|
||||||
|
timeout,
|
||||||
);
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
stopwatch.stop();
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'finished mode=$mode exit_code=${result.exitCode} '
|
||||||
|
'duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'stdout_len=${result.stdout.toString().length} '
|
||||||
|
'stderr_len=${result.stderr.toString().length}',
|
||||||
|
);
|
||||||
|
return result;
|
||||||
|
} on TimeoutException {
|
||||||
|
stopwatch.stop();
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'timeout mode=$mode duration_ms=${stopwatch.elapsedMilliseconds}',
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ProcessResult> _runExternalCommandViaWindowsWrapper({
|
||||||
|
required String executable,
|
||||||
|
required List<String> arguments,
|
||||||
|
required String? workingDirectory,
|
||||||
|
required Map<String, String>? environment,
|
||||||
|
required bool includeParentEnvironment,
|
||||||
|
required Duration? timeout,
|
||||||
|
}) async {
|
||||||
|
const timeoutExitCode = 124;
|
||||||
|
final sandbox = await Directory.systemTemp.createTemp(
|
||||||
|
'cws-process-launcher-',
|
||||||
|
);
|
||||||
|
final stdoutPath = p.join(sandbox.path, 'stdout.txt');
|
||||||
|
final stderrPath = p.join(sandbox.path, 'stderr.txt');
|
||||||
|
final scriptPath = p.join(sandbox.path, 'run.ps1');
|
||||||
|
final script = File(scriptPath);
|
||||||
|
|
||||||
|
_processLauncherLogger.debug('after fopen');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await script.writeAsString(
|
||||||
|
_buildWindowsWrapperScript(
|
||||||
|
executable: executable,
|
||||||
|
arguments: arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
stdoutPath: stdoutPath,
|
||||||
|
stderrPath: stderrPath,
|
||||||
|
timeout: timeout,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
final stopwatch = Stopwatch()..start();
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'wrapper start executable=$executable '
|
||||||
|
'script=${script.path} stdout_path=$stdoutPath stderr_path=$stderrPath',
|
||||||
|
);
|
||||||
|
final wrapperResult = await Process.run(
|
||||||
|
'powershell.exe',
|
||||||
|
<String>[
|
||||||
|
'-NoProfile',
|
||||||
|
'-NonInteractive',
|
||||||
|
'-ExecutionPolicy',
|
||||||
|
'Bypass',
|
||||||
|
'-File',
|
||||||
|
script.path,
|
||||||
|
],
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
environment: environment,
|
||||||
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
);
|
||||||
|
stopwatch.stop();
|
||||||
|
final stdoutText = await _readWrapperOutput(stdoutPath);
|
||||||
|
final stderrText = await _readWrapperOutput(stderrPath);
|
||||||
|
_processLauncherLogger.debug(
|
||||||
|
'wrapper finished exit_code=${wrapperResult.exitCode} '
|
||||||
|
'duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'stdout_len=${stdoutText.length} stderr_len=${stderrText.length} '
|
||||||
|
'wrapper_stdout_len=${wrapperResult.stdout.toString().length} '
|
||||||
|
'wrapper_stderr_len=${wrapperResult.stderr.toString().length}',
|
||||||
|
);
|
||||||
|
if (wrapperResult.exitCode == timeoutExitCode) {
|
||||||
|
throw TimeoutException(
|
||||||
|
'Command timed out after ${timeout!.inMilliseconds}ms.',
|
||||||
|
timeout,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return ProcessResult(
|
||||||
|
wrapperResult.pid,
|
||||||
|
wrapperResult.exitCode,
|
||||||
|
stdoutText,
|
||||||
|
stderrText.isEmpty ? wrapperResult.stderr : stderrText,
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
await sandbox.delete(recursive: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String _buildWindowsWrapperScript({
|
||||||
|
required String executable,
|
||||||
|
required List<String> arguments,
|
||||||
|
required String? workingDirectory,
|
||||||
|
required String stdoutPath,
|
||||||
|
required String stderrPath,
|
||||||
|
required Duration? timeout,
|
||||||
|
}) {
|
||||||
|
final timeoutMilliseconds = timeout?.inMilliseconds ?? 0;
|
||||||
|
return '''
|
||||||
|
\$ErrorActionPreference = 'Stop'
|
||||||
|
\$stdoutPath = ${_powerShellSingleQuoted(stdoutPath)}
|
||||||
|
\$stderrPath = ${_powerShellSingleQuoted(stderrPath)}
|
||||||
|
try {
|
||||||
|
\$commandPath = ${_powerShellSingleQuoted(executable)}
|
||||||
|
|
||||||
|
\$startProcess = @{
|
||||||
|
FilePath = \$commandPath
|
||||||
|
ArgumentList = ${_renderPowerShellArray(arguments)}
|
||||||
|
PassThru = \$true
|
||||||
|
WindowStyle = 'Hidden'
|
||||||
|
RedirectStandardOutput = \$stdoutPath
|
||||||
|
RedirectStandardError = \$stderrPath
|
||||||
|
}
|
||||||
|
if (${workingDirectory == null ? '\$false' : '\$true'}) {
|
||||||
|
\$startProcess.WorkingDirectory = ${_powerShellSingleQuoted(workingDirectory ?? '')}
|
||||||
|
}
|
||||||
|
\$process = Start-Process @startProcess
|
||||||
|
|
||||||
|
\$timedOut = \$false
|
||||||
|
if ($timeoutMilliseconds -gt 0) {
|
||||||
|
if (-not \$process.WaitForExit($timeoutMilliseconds)) {
|
||||||
|
\$timedOut = \$true
|
||||||
|
try {
|
||||||
|
Stop-Process -Id \$process.Id -Force
|
||||||
|
} catch {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
\$process.WaitForExit()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (\$timedOut) {
|
||||||
|
exit 124
|
||||||
|
}
|
||||||
|
|
||||||
|
exit \$process.ExitCode
|
||||||
|
} catch {
|
||||||
|
Set-Content -LiteralPath \$stderrPath -Value \$_.ToString() -Encoding utf8
|
||||||
|
exit 127
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> _readWrapperOutput(String path) async {
|
||||||
|
final file = File(path);
|
||||||
|
if (!await file.exists()) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
return file.readAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
String _renderPowerShellArray(List<String> arguments) {
|
||||||
|
final renderedArguments = arguments.map(_powerShellSingleQuoted).join(', ');
|
||||||
|
return '@($renderedArguments)';
|
||||||
|
}
|
||||||
|
|
||||||
|
String _powerShellSingleQuoted(String value) {
|
||||||
|
return "'${value.replaceAll("'", "''")}'";
|
||||||
|
}
|
||||||
|
|
||||||
|
String _renderLauncherArguments(List<String> arguments) {
|
||||||
|
return arguments.map(_summarizeLauncherArgument).join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
String _summarizeLauncherArgument(String value) {
|
||||||
|
if (value.startsWith('body=')) {
|
||||||
|
return 'body=<redacted>';
|
||||||
|
}
|
||||||
|
if (value.length <= 120) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return '${value.substring(0, 117)}...';
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Process> startExternalCommand(
|
Future<Process> startExternalCommand(
|
||||||
|
|
@ -77,7 +361,10 @@ Future<Process> startExternalCommand(
|
||||||
bool runInShell = false,
|
bool runInShell = false,
|
||||||
ProcessStartMode mode = ProcessStartMode.normal,
|
ProcessStartMode mode = ProcessStartMode.normal,
|
||||||
}) {
|
}) {
|
||||||
final resolved = _resolveCommand(executable, arguments);
|
final resolved = resolveExternalCommandForCurrentPlatform(
|
||||||
|
executable,
|
||||||
|
arguments,
|
||||||
|
);
|
||||||
final effectiveEnvironment = _effectiveEnvironmentForResolvedCommand(
|
final effectiveEnvironment = _effectiveEnvironmentForResolvedCommand(
|
||||||
environment: environment,
|
environment: environment,
|
||||||
includeParentEnvironment: includeParentEnvironment,
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
|
|
@ -89,7 +376,7 @@ Future<Process> startExternalCommand(
|
||||||
workingDirectory: workingDirectory,
|
workingDirectory: workingDirectory,
|
||||||
environment: effectiveEnvironment,
|
environment: effectiveEnvironment,
|
||||||
includeParentEnvironment: includeParentEnvironment,
|
includeParentEnvironment: includeParentEnvironment,
|
||||||
runInShell: runInShell,
|
runInShell: runInShell || resolved.runInShell,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:github/github.dart';
|
import 'package:github/github.dart';
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:yaml/yaml.dart';
|
||||||
|
|
||||||
import '../config/app_environment.dart';
|
import '../config/app_environment.dart';
|
||||||
|
import '../config/app_logger.dart';
|
||||||
import '../config/config_schema.dart';
|
import '../config/config_schema.dart';
|
||||||
import '../core/process_launcher.dart';
|
import '../core/process_launcher.dart';
|
||||||
import 'models.dart';
|
import 'models.dart';
|
||||||
|
|
@ -15,12 +18,17 @@ class IssueTrackerClient {
|
||||||
this.glabCommand = 'glab',
|
this.glabCommand = 'glab',
|
||||||
this.teaCommand = 'tea',
|
this.teaCommand = 'tea',
|
||||||
this.opCommand = 'op',
|
this.opCommand = 'op',
|
||||||
});
|
Duration? commandTimeout,
|
||||||
|
AppLogger? logger,
|
||||||
|
}) : commandTimeout = commandTimeout ?? const Duration(seconds: 15),
|
||||||
|
_logger = logger ?? AppLogger('code_work_spawner.issue_tracker');
|
||||||
|
|
||||||
final String ghCommand;
|
final String ghCommand;
|
||||||
final String glabCommand;
|
final String glabCommand;
|
||||||
final String teaCommand;
|
final String teaCommand;
|
||||||
final String opCommand;
|
final String opCommand;
|
||||||
|
final Duration commandTimeout;
|
||||||
|
final AppLogger _logger;
|
||||||
final Map<IssueTrackerProvider, Future<String?>> _authenticatedLoginFutures =
|
final Map<IssueTrackerProvider, Future<String?>> _authenticatedLoginFutures =
|
||||||
<IssueTrackerProvider, Future<String?>>{};
|
<IssueTrackerProvider, Future<String?>>{};
|
||||||
final Map<String, Future<_OpenProjectProjectRef>> _openProjectProjectFutures =
|
final Map<String, Future<_OpenProjectProjectRef>> _openProjectProjectFutures =
|
||||||
|
|
@ -81,18 +89,14 @@ class IssueTrackerClient {
|
||||||
{'per_page': '100'},
|
{'per_page': '100'},
|
||||||
),
|
),
|
||||||
]),
|
]),
|
||||||
IssueTrackerProvider.gitea => await _runTeaJson([
|
IssueTrackerProvider.gitea => await _runGiteaJson(
|
||||||
'api',
|
method: 'GET',
|
||||||
'-X',
|
path: _buildApiPath(
|
||||||
'GET',
|
|
||||||
_buildApiPath(
|
|
||||||
issue.requiredRepoSlug,
|
issue.requiredRepoSlug,
|
||||||
['issues', '${issue.number}', 'comments'],
|
['issues', '${issue.number}', 'comments'],
|
||||||
{'limit': '100'},
|
{'limit': '100'},
|
||||||
),
|
),
|
||||||
'-r',
|
),
|
||||||
issue.requiredRepoSlug.fullName,
|
|
||||||
]),
|
|
||||||
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
path: '/api/v3/work_packages/${issue.number}/activities',
|
path: '/api/v3/work_packages/${issue.number}/activities',
|
||||||
|
|
@ -157,20 +161,15 @@ class IssueTrackerClient {
|
||||||
'-f',
|
'-f',
|
||||||
'body=$body',
|
'body=$body',
|
||||||
]),
|
]),
|
||||||
IssueTrackerProvider.gitea => await _runTeaJson([
|
IssueTrackerProvider.gitea => await _runGiteaJson(
|
||||||
'api',
|
method: 'POST',
|
||||||
'-X',
|
path: _buildApiPath(parseRepositorySlug(trackerProject), [
|
||||||
'POST',
|
|
||||||
_buildApiPath(parseRepositorySlug(trackerProject), [
|
|
||||||
'issues',
|
'issues',
|
||||||
'$issueNumber',
|
'$issueNumber',
|
||||||
'comments',
|
'comments',
|
||||||
]),
|
]),
|
||||||
'-f',
|
body: <String, Object?>{'body': body},
|
||||||
'body=$body',
|
),
|
||||||
'-r',
|
|
||||||
trackerProject,
|
|
||||||
]),
|
|
||||||
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
path: '/api/v3/work_packages/$issueNumber/activities',
|
path: '/api/v3/work_packages/$issueNumber/activities',
|
||||||
|
|
@ -241,7 +240,7 @@ class IssueTrackerClient {
|
||||||
return switch (provider) {
|
return switch (provider) {
|
||||||
IssueTrackerProvider.github => 'gh',
|
IssueTrackerProvider.github => 'gh',
|
||||||
IssueTrackerProvider.gitlab => 'glab',
|
IssueTrackerProvider.gitlab => 'glab',
|
||||||
IssueTrackerProvider.gitea => 'tea',
|
IssueTrackerProvider.gitea => 'gitea',
|
||||||
IssueTrackerProvider.openproject => opCommand,
|
IssueTrackerProvider.openproject => opCommand,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -250,43 +249,23 @@ class IssueTrackerClient {
|
||||||
required RepositorySlug repo,
|
required RepositorySlug repo,
|
||||||
required String url,
|
required String url,
|
||||||
}) async {
|
}) async {
|
||||||
final result = await _runCommand(teaCommand, [
|
final json = await _runGiteaJson(
|
||||||
'webhooks',
|
method: 'POST',
|
||||||
'create',
|
path: _buildApiPath(repo, ['hooks']),
|
||||||
'--type',
|
body: <String, Object?>{
|
||||||
'gitea',
|
'type': 'gitea',
|
||||||
'--events',
|
'config': <String, Object?>{'url': url, 'content_type': 'json'},
|
||||||
'issues,issue_comment',
|
'events': <String>['issues', 'issue_comment'],
|
||||||
'--active',
|
'active': true,
|
||||||
'-o',
|
},
|
||||||
'json',
|
|
||||||
'-r',
|
|
||||||
repo.fullName,
|
|
||||||
url,
|
|
||||||
]);
|
|
||||||
final stdoutText = result.stdout.toString().trim();
|
|
||||||
if (stdoutText.isEmpty) {
|
|
||||||
throw FormatException(
|
|
||||||
'tea webhooks create returned an empty response for repo '
|
|
||||||
'${repo.fullName}.',
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
final json = jsonDecode(stdoutText);
|
|
||||||
if (json is Map<String, dynamic>) {
|
if (json is Map<String, dynamic>) {
|
||||||
return readIntField(json, 'id');
|
return readIntField(json, 'id');
|
||||||
}
|
}
|
||||||
} on FormatException {
|
|
||||||
final webhookId = _extractWebhookIdFromTeaOutput(stdoutText);
|
|
||||||
if (webhookId != null) {
|
|
||||||
return webhookId;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw FormatException(
|
throw FormatException(
|
||||||
'tea webhooks create returned an unexpected response for repo '
|
'Gitea API webhook create returned an unexpected response for repo '
|
||||||
'${repo.fullName}. stdout=${_summarizeOutput(stdoutText)}',
|
'${repo.fullName}.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -362,14 +341,10 @@ class IssueTrackerClient {
|
||||||
required RepositorySlug repo,
|
required RepositorySlug repo,
|
||||||
required int webhookId,
|
required int webhookId,
|
||||||
}) async {
|
}) async {
|
||||||
await _runTeaJson([
|
await _runGiteaJson(
|
||||||
'webhooks',
|
method: 'DELETE',
|
||||||
'delete',
|
path: _buildApiPath(repo, ['hooks', '$webhookId']),
|
||||||
'--confirm',
|
);
|
||||||
'-r',
|
|
||||||
repo.fullName,
|
|
||||||
'$webhookId',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteGitLabWebhook({
|
Future<void> deleteGitLabWebhook({
|
||||||
|
|
@ -404,6 +379,14 @@ class IssueTrackerClient {
|
||||||
|
|
||||||
final issues = <GitHubIssueSummary>[];
|
final issues = <GitHubIssueSummary>[];
|
||||||
for (var page = 1; true; page += 1) {
|
for (var page = 1; true; page += 1) {
|
||||||
|
_traceRequest(
|
||||||
|
provider: IssueTrackerProvider.github,
|
||||||
|
requestKind: 'search-issues',
|
||||||
|
trackerProject: repoList.map((repo) => repo.fullName).join(','),
|
||||||
|
page: page,
|
||||||
|
since: since,
|
||||||
|
requestPath: 'search/issues',
|
||||||
|
);
|
||||||
final json = await _runGhJson([
|
final json = await _runGhJson([
|
||||||
'api',
|
'api',
|
||||||
'-X',
|
'-X',
|
||||||
|
|
@ -453,6 +436,14 @@ class IssueTrackerClient {
|
||||||
'page': '$page',
|
'page': '$page',
|
||||||
if (since != null) 'updated_after': since.toUtc().toIso8601String(),
|
if (since != null) 'updated_after': since.toUtc().toIso8601String(),
|
||||||
};
|
};
|
||||||
|
_traceRequest(
|
||||||
|
provider: IssueTrackerProvider.gitlab,
|
||||||
|
requestKind: 'list-issues',
|
||||||
|
trackerProject: trackerProject,
|
||||||
|
page: page,
|
||||||
|
since: since,
|
||||||
|
requestPath: _buildGitLabApiPath(trackerProject, ['issues'], query),
|
||||||
|
);
|
||||||
final json = await _runGlabJson([
|
final json = await _runGlabJson([
|
||||||
'api',
|
'api',
|
||||||
'-X',
|
'-X',
|
||||||
|
|
@ -490,23 +481,25 @@ class IssueTrackerClient {
|
||||||
'page': '$page',
|
'page': '$page',
|
||||||
if (since != null) 'since': since.toUtc().toIso8601String(),
|
if (since != null) 'since': since.toUtc().toIso8601String(),
|
||||||
};
|
};
|
||||||
final json = await _runTeaJson([
|
final requestPath = _buildApiPath(repo, ['issues'], query);
|
||||||
'api',
|
_traceRequest(
|
||||||
'-X',
|
provider: IssueTrackerProvider.gitea,
|
||||||
'GET',
|
requestKind: 'list-issues',
|
||||||
_buildApiPath(repo, ['issues'], query),
|
trackerProject: repo.fullName,
|
||||||
'-r',
|
page: page,
|
||||||
repo.fullName,
|
since: since,
|
||||||
]);
|
requestPath: requestPath,
|
||||||
|
);
|
||||||
|
final json = await _runGiteaJson(method: 'GET', path: requestPath);
|
||||||
if (json is Map<String, dynamic>) {
|
if (json is Map<String, dynamic>) {
|
||||||
throw FormatException(
|
throw FormatException(
|
||||||
'tea api returned an error while listing Gitea issues for '
|
'Gitea API returned an error while listing issues for '
|
||||||
'${repo.fullName}: ${_describeJsonObject(json)}',
|
'${repo.fullName}: ${_describeJsonObject(json)}',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (json != null && json is! List<dynamic>) {
|
if (json != null && json is! List<dynamic>) {
|
||||||
throw FormatException(
|
throw FormatException(
|
||||||
'tea api returned ${json.runtimeType} while listing Gitea issues '
|
'Gitea API returned ${json.runtimeType} while listing issues '
|
||||||
'for ${repo.fullName}; expected a JSON array.',
|
'for ${repo.fullName}; expected a JSON array.',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -579,7 +572,10 @@ class IssueTrackerClient {
|
||||||
final json = switch (provider) {
|
final json = switch (provider) {
|
||||||
IssueTrackerProvider.github => await _runGhJson(['api', 'user']),
|
IssueTrackerProvider.github => await _runGhJson(['api', 'user']),
|
||||||
IssueTrackerProvider.gitlab => await _runGlabJson(['api', 'user']),
|
IssueTrackerProvider.gitlab => await _runGlabJson(['api', 'user']),
|
||||||
IssueTrackerProvider.gitea => await _runTeaJson(['api', 'user']),
|
IssueTrackerProvider.gitea => await _runGiteaJson(
|
||||||
|
method: 'GET',
|
||||||
|
path: '/user',
|
||||||
|
),
|
||||||
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
IssueTrackerProvider.openproject => await _runOpenProjectJson(
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
path: '/api/v3/users/me',
|
path: '/api/v3/users/me',
|
||||||
|
|
@ -604,8 +600,102 @@ class IssueTrackerClient {
|
||||||
return _runJson(glabCommand, arguments);
|
return _runJson(glabCommand, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Object?> _runTeaJson(List<String> arguments) {
|
Future<Object?> _runGiteaJson({
|
||||||
return _runJson(teaCommand, arguments);
|
required String method,
|
||||||
|
required String path,
|
||||||
|
Map<String, String>? queryParameters,
|
||||||
|
Object? body,
|
||||||
|
}) async {
|
||||||
|
final config = await _loadGiteaConfig();
|
||||||
|
final client = HttpClient();
|
||||||
|
if (config.insecure) {
|
||||||
|
client.badCertificateCallback = (_, _, _) => true;
|
||||||
|
}
|
||||||
|
final uri = _buildGiteaUri(
|
||||||
|
config.host,
|
||||||
|
path,
|
||||||
|
queryParameters: queryParameters,
|
||||||
|
);
|
||||||
|
final stopwatch = Stopwatch()..start();
|
||||||
|
_logger.debug(
|
||||||
|
'gitea request start timeout_ms=${commandTimeout.inMilliseconds} '
|
||||||
|
'method=$method uri=$uri',
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
final result =
|
||||||
|
await (() async {
|
||||||
|
final request = await client.openUrl(method, uri);
|
||||||
|
request.headers.set(HttpHeaders.acceptHeader, 'application/json');
|
||||||
|
request.headers.set(
|
||||||
|
HttpHeaders.authorizationHeader,
|
||||||
|
'token ${config.token}',
|
||||||
|
);
|
||||||
|
if (body != null) {
|
||||||
|
request.headers.set(
|
||||||
|
HttpHeaders.contentTypeHeader,
|
||||||
|
'application/json',
|
||||||
|
);
|
||||||
|
request.write(jsonEncode(body));
|
||||||
|
}
|
||||||
|
final response = await request.close();
|
||||||
|
final responseBody = await utf8.decodeStream(response);
|
||||||
|
return (response: response, body: responseBody);
|
||||||
|
})().timeout(
|
||||||
|
commandTimeout,
|
||||||
|
onTimeout: () {
|
||||||
|
throw TimeoutException(
|
||||||
|
'Gitea API request timed out after ${commandTimeout.inMilliseconds}ms.',
|
||||||
|
commandTimeout,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.debug(
|
||||||
|
'gitea request finished duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'status_code=${result.response.statusCode} method=$method uri=$uri',
|
||||||
|
);
|
||||||
|
final trimmed = result.body.trim();
|
||||||
|
final parsedJson = trimmed.isEmpty ? null : jsonDecode(trimmed);
|
||||||
|
if (result.response.statusCode < 200 ||
|
||||||
|
result.response.statusCode >= 300) {
|
||||||
|
if (parsedJson is Map<String, dynamic>) {
|
||||||
|
throw FormatException(
|
||||||
|
'Gitea API request failed '
|
||||||
|
'(${result.response.statusCode} ${result.response.reasonPhrase}): '
|
||||||
|
'${_describeJsonObject(parsedJson)}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
throw HttpException(
|
||||||
|
'Gitea API request failed '
|
||||||
|
'(${result.response.statusCode} ${result.response.reasonPhrase}): '
|
||||||
|
'${_summarizeOutput(result.body)}',
|
||||||
|
uri: uri,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return parsedJson;
|
||||||
|
} on TimeoutException catch (error, stackTrace) {
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.error(
|
||||||
|
'gitea request timeout duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'timeout_ms=${commandTimeout.inMilliseconds} method=$method uri=$uri',
|
||||||
|
error: error,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.error(
|
||||||
|
'gitea request failed duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'method=$method uri=$uri',
|
||||||
|
error: error,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
} finally {
|
||||||
|
client.close(force: true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Object?> _runOpenProjectJson({
|
Future<Object?> _runOpenProjectJson({
|
||||||
|
|
@ -659,12 +749,31 @@ class IssueTrackerClient {
|
||||||
final result = await _runCommand(command, arguments);
|
final result = await _runCommand(command, arguments);
|
||||||
final stdoutText = result.stdout.toString().trim();
|
final stdoutText = result.stdout.toString().trim();
|
||||||
if (stdoutText.isEmpty) {
|
if (stdoutText.isEmpty) {
|
||||||
|
_traceCommandResult(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
state: 'empty-stdout',
|
||||||
|
);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return jsonDecode(stdoutText);
|
final json = jsonDecode(stdoutText);
|
||||||
|
_traceCommandResult(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
state: 'json-parsed',
|
||||||
|
detail: _describeJsonPayload(json),
|
||||||
|
);
|
||||||
|
return json;
|
||||||
} on FormatException catch (error) {
|
} on FormatException catch (error) {
|
||||||
|
_traceCommandResult(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
state: 'json-parse-failed',
|
||||||
|
detail:
|
||||||
|
'${error.message} stdout=${_summarizeOutput(stdoutText, maxLength: 120)}',
|
||||||
|
);
|
||||||
throw FormatException(
|
throw FormatException(
|
||||||
'Failed to parse JSON from `$command ${arguments.join(' ')}`: '
|
'Failed to parse JSON from `$command ${arguments.join(' ')}`: '
|
||||||
'${error.message}. stdout=${_summarizeOutput(stdoutText)}',
|
'${error.message}. stdout=${_summarizeOutput(stdoutText)}',
|
||||||
|
|
@ -676,7 +785,41 @@ class IssueTrackerClient {
|
||||||
String command,
|
String command,
|
||||||
List<String> arguments,
|
List<String> arguments,
|
||||||
) async {
|
) async {
|
||||||
final result = await runExternalCommand(command, arguments);
|
final renderedCommand = _renderCommand(command, arguments);
|
||||||
|
final stopwatch = Stopwatch()..start();
|
||||||
|
_logger.debug(
|
||||||
|
'command start timeout_ms=${commandTimeout.inMilliseconds} command=$renderedCommand',
|
||||||
|
);
|
||||||
|
late final ProcessResult result;
|
||||||
|
try {
|
||||||
|
result = await runExternalCommand(
|
||||||
|
command,
|
||||||
|
arguments,
|
||||||
|
timeout: commandTimeout,
|
||||||
|
);
|
||||||
|
} on TimeoutException catch (error, stackTrace) {
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.error(
|
||||||
|
'command timeout duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'timeout_ms=${commandTimeout.inMilliseconds} command=$renderedCommand',
|
||||||
|
error: error,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.error(
|
||||||
|
'command failed duration_ms=${stopwatch.elapsedMilliseconds} command=$renderedCommand',
|
||||||
|
error: error,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
|
stopwatch.stop();
|
||||||
|
_logger.debug(
|
||||||
|
'command finished duration_ms=${stopwatch.elapsedMilliseconds} '
|
||||||
|
'exit_code=${result.exitCode} command=$renderedCommand',
|
||||||
|
);
|
||||||
if (result.exitCode != 0) {
|
if (result.exitCode != 0) {
|
||||||
throw ProcessException(
|
throw ProcessException(
|
||||||
command,
|
command,
|
||||||
|
|
@ -688,12 +831,19 @@ class IssueTrackerClient {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int? _extractWebhookIdFromTeaOutput(String stdoutText) {
|
String _renderCommand(String command, List<String> arguments) {
|
||||||
final match = RegExp(
|
final parts = <String>[command, ...arguments];
|
||||||
r'Webhook created successfully \(ID:\s*(\d+)\)',
|
return parts.map(_shellQuote).join(' ');
|
||||||
caseSensitive: false,
|
}
|
||||||
).firstMatch(stdoutText);
|
|
||||||
return match == null ? null : int.parse(match.group(1)!);
|
String _shellQuote(String value) {
|
||||||
|
if (value.isEmpty) {
|
||||||
|
return '""';
|
||||||
|
}
|
||||||
|
if (!value.contains(RegExp(r'[\s"]'))) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return '"${value.replaceAll('"', r'\"')}"';
|
||||||
}
|
}
|
||||||
|
|
||||||
String _describeJsonObject(Map<String, dynamic> json) {
|
String _describeJsonObject(Map<String, dynamic> json) {
|
||||||
|
|
@ -722,6 +872,47 @@ class IssueTrackerClient {
|
||||||
return '${singleLine.substring(0, maxLength)}...';
|
return '${singleLine.substring(0, maxLength)}...';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String _describeJsonPayload(Object? json) {
|
||||||
|
return switch (json) {
|
||||||
|
List<dynamic> list => 'type=list length=${list.length}',
|
||||||
|
Map<String, dynamic> map =>
|
||||||
|
'type=object keys=${map.keys.take(6).join(",")}',
|
||||||
|
null => 'type=null',
|
||||||
|
_ => 'type=${json.runtimeType}',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void _traceRequest({
|
||||||
|
required IssueTrackerProvider provider,
|
||||||
|
required String requestKind,
|
||||||
|
required String trackerProject,
|
||||||
|
required int page,
|
||||||
|
required DateTime? since,
|
||||||
|
required String requestPath,
|
||||||
|
}) {
|
||||||
|
_logger.debug(
|
||||||
|
'trace request provider=${provider.name} kind=$requestKind '
|
||||||
|
'tracker_project=$trackerProject page=$page '
|
||||||
|
'since=${since?.toUtc().toIso8601String() ?? "(none)"} '
|
||||||
|
'path=$requestPath',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _traceCommandResult({
|
||||||
|
required String command,
|
||||||
|
required List<String> arguments,
|
||||||
|
required String state,
|
||||||
|
String? detail,
|
||||||
|
}) {
|
||||||
|
final parts = <String>[
|
||||||
|
'trace command_result state=$state',
|
||||||
|
'command=$command',
|
||||||
|
'args=${arguments.map(_shellQuote).join(" ")}',
|
||||||
|
if (detail != null && detail.isNotEmpty) 'detail=$detail',
|
||||||
|
];
|
||||||
|
_logger.debug(parts.join(' '));
|
||||||
|
}
|
||||||
|
|
||||||
String _buildApiPath(
|
String _buildApiPath(
|
||||||
RepositorySlug repo,
|
RepositorySlug repo,
|
||||||
List<String> tailSegments, [
|
List<String> tailSegments, [
|
||||||
|
|
@ -761,6 +952,186 @@ class IssueTrackerClient {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Uri _buildGiteaUri(
|
||||||
|
Uri host,
|
||||||
|
String path, {
|
||||||
|
Map<String, String>? queryParameters,
|
||||||
|
}) {
|
||||||
|
final relativeUri = Uri.parse(path);
|
||||||
|
final normalizedPath = relativeUri.path.startsWith('/')
|
||||||
|
? relativeUri.path
|
||||||
|
: '/${relativeUri.path}';
|
||||||
|
final effectiveQueryParameters =
|
||||||
|
queryParameters ??
|
||||||
|
(relativeUri.hasQuery ? relativeUri.queryParameters : null);
|
||||||
|
return host.replace(
|
||||||
|
path: p.posix.normalize(
|
||||||
|
'${host.path.endsWith('/') ? host.path.substring(0, host.path.length - 1) : host.path}/api/v1$normalizedPath',
|
||||||
|
),
|
||||||
|
queryParameters:
|
||||||
|
effectiveQueryParameters == null || effectiveQueryParameters.isEmpty
|
||||||
|
? null
|
||||||
|
: effectiveQueryParameters,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<_GiteaConfig> _loadGiteaConfig() async {
|
||||||
|
final environment = AppEnvironment.variables;
|
||||||
|
final host =
|
||||||
|
environment['CWS_GITEA_HOST']?.trim() ??
|
||||||
|
environment['GITEA_HOST']?.trim();
|
||||||
|
final token =
|
||||||
|
environment['CWS_GITEA_TOKEN']?.trim() ??
|
||||||
|
environment['GITEA_TOKEN']?.trim();
|
||||||
|
final insecureValue =
|
||||||
|
environment['CWS_GITEA_INSECURE']?.trim() ??
|
||||||
|
environment['GITEA_INSECURE']?.trim();
|
||||||
|
if (host != null && host.isNotEmpty && token != null && token.isNotEmpty) {
|
||||||
|
return _GiteaConfig(
|
||||||
|
host: Uri.parse(host),
|
||||||
|
token: token,
|
||||||
|
insecure: _parseBooleanFlag(insecureValue),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final loginName =
|
||||||
|
environment['CWS_GITEA_LOGIN']?.trim() ??
|
||||||
|
environment['GITEA_LOGIN']?.trim() ??
|
||||||
|
environment['TEA_LOGIN']?.trim();
|
||||||
|
final configPath = _teaConfigPath(environment);
|
||||||
|
final file = File(configPath);
|
||||||
|
if (!await file.exists()) {
|
||||||
|
throw const FileSystemException(
|
||||||
|
'Gitea config not found. Set CWS_GITEA_HOST/CWS_GITEA_TOKEN or configure tea first.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final document = loadYaml(await file.readAsString());
|
||||||
|
final logins =
|
||||||
|
(document is YamlMap ? document['logins'] : null) as YamlList?;
|
||||||
|
if (logins == null || logins.isEmpty) {
|
||||||
|
throw FileSystemException(
|
||||||
|
'No tea logins found in Gitea config at $configPath.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final loginMaps = logins
|
||||||
|
.whereType<YamlMap>()
|
||||||
|
.map((entry) => Map<String, Object?>.from(entry))
|
||||||
|
.toList(growable: false);
|
||||||
|
final selectedLogin = _selectTeaLogin(loginMaps, loginName);
|
||||||
|
final loginHost = selectedLogin['url']?.toString().trim();
|
||||||
|
final loginToken = selectedLogin['token']?.toString().trim();
|
||||||
|
if (loginHost == null ||
|
||||||
|
loginHost.isEmpty ||
|
||||||
|
loginToken == null ||
|
||||||
|
loginToken.isEmpty) {
|
||||||
|
throw FileSystemException(
|
||||||
|
'Selected tea login in $configPath is missing url or token.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return _GiteaConfig(
|
||||||
|
host: Uri.parse(loginHost),
|
||||||
|
token: loginToken,
|
||||||
|
insecure: _parseBooleanFlag(selectedLogin['insecure']?.toString()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object?> _selectTeaLogin(
|
||||||
|
List<Map<String, Object?>> logins,
|
||||||
|
String? requestedLoginName,
|
||||||
|
) {
|
||||||
|
if (requestedLoginName != null && requestedLoginName.isNotEmpty) {
|
||||||
|
final matchedLogin = logins
|
||||||
|
.where((login) {
|
||||||
|
final name = login['name']?.toString().trim();
|
||||||
|
return name != null &&
|
||||||
|
name.toLowerCase() == requestedLoginName.toLowerCase();
|
||||||
|
})
|
||||||
|
.toList(growable: false);
|
||||||
|
if (matchedLogin.length == 1) {
|
||||||
|
return matchedLogin.single;
|
||||||
|
}
|
||||||
|
throw FileSystemException(
|
||||||
|
'tea login "$requestedLoginName" was not found or was ambiguous.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final defaultLogins = logins
|
||||||
|
.where((login) {
|
||||||
|
final value = login['default'];
|
||||||
|
return value == true || value?.toString().toLowerCase() == 'true';
|
||||||
|
})
|
||||||
|
.toList(growable: false);
|
||||||
|
if (defaultLogins.length == 1) {
|
||||||
|
return defaultLogins.single;
|
||||||
|
}
|
||||||
|
if (logins.length == 1) {
|
||||||
|
return logins.single;
|
||||||
|
}
|
||||||
|
throw const FileSystemException(
|
||||||
|
'Multiple tea logins found. Set CWS_GITEA_LOGIN, GITEA_LOGIN, or TEA_LOGIN to choose one.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _teaConfigPath(Map<String, String> environment) {
|
||||||
|
final explicitPath =
|
||||||
|
environment['CWS_GITEA_CONFIG_PATH']?.trim() ??
|
||||||
|
environment['TEA_CONFIG_PATH']?.trim();
|
||||||
|
if (explicitPath != null && explicitPath.isNotEmpty) {
|
||||||
|
return explicitPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
final candidates = <String>[];
|
||||||
|
final localAppData = environment['LOCALAPPDATA']?.trim();
|
||||||
|
if (localAppData != null && localAppData.isNotEmpty) {
|
||||||
|
candidates.add(p.join(localAppData, 'tea', 'config.yml'));
|
||||||
|
}
|
||||||
|
final xdgConfigHome = environment['XDG_CONFIG_HOME']?.trim();
|
||||||
|
if (xdgConfigHome != null && xdgConfigHome.isNotEmpty) {
|
||||||
|
candidates.add(p.join(xdgConfigHome, 'tea', 'config.yml'));
|
||||||
|
}
|
||||||
|
final userProfile = environment['USERPROFILE']?.trim();
|
||||||
|
if (userProfile != null && userProfile.isNotEmpty) {
|
||||||
|
candidates.add(
|
||||||
|
p.join(userProfile, 'AppData', 'Local', 'tea', 'config.yml'),
|
||||||
|
);
|
||||||
|
candidates.add(p.join(userProfile, '.tea', 'tea.yml'));
|
||||||
|
}
|
||||||
|
final home = environment['HOME']?.trim();
|
||||||
|
if (home != null && home.isNotEmpty) {
|
||||||
|
candidates.add(p.join(home, '.config', 'tea', 'config.yml'));
|
||||||
|
candidates.add(p.join(home, '.tea', 'tea.yml'));
|
||||||
|
}
|
||||||
|
if (candidates.isEmpty) {
|
||||||
|
throw const FileSystemException(
|
||||||
|
'HOME is not set and LOCALAPPDATA/XDG_CONFIG_HOME/USERPROFILE are unset. Cannot locate tea config.',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (final candidate in candidates) {
|
||||||
|
if (File(candidate).existsSync()) {
|
||||||
|
return candidate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return candidates.first;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool _parseBooleanFlag(String? value) {
|
||||||
|
if (value == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
switch (value.trim().toLowerCase()) {
|
||||||
|
case '1':
|
||||||
|
case 'true':
|
||||||
|
case 'yes':
|
||||||
|
case 'on':
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Future<_OpenProjectConfig> _loadOpenProjectConfig() async {
|
Future<_OpenProjectConfig> _loadOpenProjectConfig() async {
|
||||||
final environment = AppEnvironment.variables;
|
final environment = AppEnvironment.variables;
|
||||||
final host = environment['OP_CLI_HOST']?.trim();
|
final host = environment['OP_CLI_HOST']?.trim();
|
||||||
|
|
@ -864,6 +1235,18 @@ class _OpenProjectConfig {
|
||||||
final String token;
|
final String token;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class _GiteaConfig {
|
||||||
|
const _GiteaConfig({
|
||||||
|
required this.host,
|
||||||
|
required this.token,
|
||||||
|
required this.insecure,
|
||||||
|
});
|
||||||
|
|
||||||
|
final Uri host;
|
||||||
|
final String token;
|
||||||
|
final bool insecure;
|
||||||
|
}
|
||||||
|
|
||||||
class _OpenProjectProjectRef {
|
class _OpenProjectProjectRef {
|
||||||
const _OpenProjectProjectRef({required this.id, required this.name});
|
const _OpenProjectProjectRef({required this.id, required this.name});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ dependencies:
|
||||||
git: ^2.3.2
|
git: ^2.3.2
|
||||||
nyxx: ^6.8.1
|
nyxx: ^6.8.1
|
||||||
dart_eval: ^0.8.4
|
dart_eval: ^0.8.4
|
||||||
|
executable: ^1.4.1
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
build_runner: ^2.6.0
|
build_runner: ^2.6.0
|
||||||
|
|
|
||||||
|
|
@ -234,24 +234,21 @@ projects:
|
||||||
await initGitRepo(repoDir);
|
await initGitRepo(repoDir);
|
||||||
|
|
||||||
// And Gitea issue data containing a comment with an explicit assistant mention.
|
// And Gitea issue data containing a comment with an explicit assistant mention.
|
||||||
final teaScript = File(p.join(sandbox.path, 'tea'));
|
final giteaServer = await FakeGiteaApiServer.start(
|
||||||
final teaLog = File(p.join(sandbox.path, 'tea-log.jsonl'));
|
|
||||||
final postedBody = File(p.join(sandbox.path, 'posted-body.md'));
|
|
||||||
await writeFakeTeaScript(
|
|
||||||
teaScript: teaScript,
|
|
||||||
issueListResponsesByRepo: {
|
issueListResponsesByRepo: {
|
||||||
'owner/sample': [
|
'owner/sample': [
|
||||||
{
|
{
|
||||||
'index': 12,
|
'number': 12,
|
||||||
'title': 'Need architecture help',
|
'title': 'Need architecture help',
|
||||||
'body': 'Please review the API layering.',
|
'body': 'Please review the API layering.',
|
||||||
'state': 'open',
|
'state': 'open',
|
||||||
'url': 'https://gitea.example.test/owner/sample/issues/12',
|
'html_url': 'https://gitea.example.test/owner/sample/issues/12',
|
||||||
'updated_at': '2026-04-04T12:00:00Z',
|
'updated_at': '2026-04-04T12:00:00Z',
|
||||||
'poster': {'login': 'reporter'},
|
'user': {'login': 'reporter'},
|
||||||
'labels': [
|
'labels': [
|
||||||
{'name': 'help'},
|
{'name': 'help'},
|
||||||
],
|
],
|
||||||
|
'pull_request': null,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
@ -261,22 +258,28 @@ projects:
|
||||||
{
|
{
|
||||||
'id': 50,
|
'id': 50,
|
||||||
'body': '@helper can you plan the architecture?',
|
'body': '@helper can you plan the architecture?',
|
||||||
'url':
|
'html_url':
|
||||||
'https://gitea.example.test/owner/sample/issues/12#issuecomment-50',
|
'https://gitea.example.test/owner/sample/issues/12#issuecomment-50',
|
||||||
'created_at': '2026-04-04T12:00:00Z',
|
'created_at': '2026-04-04T12:00:00Z',
|
||||||
'updated_at': '2026-04-04T12:00:00Z',
|
'updated_at': '2026-04-04T12:00:00Z',
|
||||||
'poster': {'login': 'reporter'},
|
'user': {'login': 'reporter'},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
postCommentIssueNumbersByRepo: {
|
postCommentResponsesByRepo: {
|
||||||
'owner/sample': {12},
|
'owner/sample': {
|
||||||
|
12: {
|
||||||
|
'id': 701,
|
||||||
|
'body': 'posted',
|
||||||
|
'html_url':
|
||||||
|
'https://gitea.example.test/owner/sample/issues/12#issuecomment-701',
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
teaLog: teaLog,
|
|
||||||
postedBodyFile: postedBody,
|
|
||||||
viewerLogin: 'tea-octocat',
|
viewerLogin: 'tea-octocat',
|
||||||
);
|
);
|
||||||
|
addTearDown(giteaServer.close);
|
||||||
|
|
||||||
// And a responder that emits a planning reply.
|
// And a responder that emits a planning reply.
|
||||||
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
||||||
|
|
@ -307,37 +310,44 @@ projects:
|
||||||
repo: owner/sample
|
repo: owner/sample
|
||||||
path: ${repoDir.path}
|
path: ${repoDir.path}
|
||||||
defaultBranch: main
|
defaultBranch: main
|
||||||
|
''');
|
||||||
|
await File(p.join(sandbox.path, '.env')).writeAsString('''
|
||||||
|
CWS_GITEA_HOST=${giteaServer.baseUrl}
|
||||||
|
CWS_GITEA_TOKEN=test-token
|
||||||
''');
|
''');
|
||||||
final app = await IssueAssistantApp.open(
|
final app = await IssueAssistantApp.open(
|
||||||
config: await AppConfig.load(configFile.path),
|
config: await AppConfig.load(configFile.path),
|
||||||
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
||||||
teaCommand: teaScript.path,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// When the app processes one polling cycle.
|
// When the app processes one polling cycle.
|
||||||
await app.runOnce();
|
await app.runOnce();
|
||||||
await app.close();
|
await app.close();
|
||||||
|
|
||||||
// Then it reads issue comments and posts exactly one reply through tea for that issue.
|
// Then it reads issue comments and posts exactly one reply through the Gitea API for that issue.
|
||||||
final logLines = await teaLog.readAsLines();
|
|
||||||
expect(
|
expect(
|
||||||
logLines
|
giteaServer.requestPaths
|
||||||
.where(
|
.where(
|
||||||
(line) => line.contains('repos/owner/sample/issues?state=open'),
|
(path) => path.contains('/api/v1/repos/owner/sample/issues?'),
|
||||||
)
|
)
|
||||||
.length,
|
.length,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
expect(
|
expect(
|
||||||
logLines
|
giteaServer.requestPaths
|
||||||
.where(
|
.where(
|
||||||
(line) => line.contains('repos/owner/sample/issues/12/comments'),
|
(path) => path.contains(
|
||||||
|
'/api/v1/repos/owner/sample/issues/12/comments',
|
||||||
|
),
|
||||||
)
|
)
|
||||||
.length,
|
.length,
|
||||||
2,
|
2,
|
||||||
);
|
);
|
||||||
final postedComment = await postedBody.readAsString();
|
final postedComment =
|
||||||
expect(postedComment, contains('via `tea`'));
|
(jsonDecode(giteaServer.postedBodies.single)
|
||||||
|
as Map<String, dynamic>)['body']
|
||||||
|
as String;
|
||||||
|
expect(postedComment, contains('via `gitea`'));
|
||||||
expect(postedComment, contains('@tea-octocat'));
|
expect(postedComment, contains('@tea-octocat'));
|
||||||
expect(
|
expect(
|
||||||
postedComment,
|
postedComment,
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,108 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:code_work_spawner/src/core/process_launcher.dart';
|
||||||
|
import 'package:path/path.dart' as p;
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
/// ```gherkin
|
||||||
|
/// Feature: Process launcher command resolution
|
||||||
|
///
|
||||||
|
/// As a maintainer running the app on Windows
|
||||||
|
/// I want native executables to avoid unnecessary shell wrappers
|
||||||
|
/// So that tracker CLIs do not hang before returning their output
|
||||||
|
/// ```
|
||||||
|
group('resolveExternalCommandForCurrentPlatform', () {
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Execute Windows native executables directly
|
||||||
|
/// Given a Windows executable path for a native CLI
|
||||||
|
/// When the process launcher resolves the command
|
||||||
|
/// Then it keeps the original executable path
|
||||||
|
/// And it does not force the command to run in a shell
|
||||||
|
/// ```
|
||||||
|
test('Execute Windows native executables directly', () {
|
||||||
|
// Given a Windows executable path for a native CLI.
|
||||||
|
const executable = r'C:\Users\bensung\scoop\shims\tea.exe';
|
||||||
|
|
||||||
|
// When the process launcher resolves the command.
|
||||||
|
final resolved = resolveExternalCommandForCurrentPlatform(
|
||||||
|
executable,
|
||||||
|
const <String>['--version'],
|
||||||
|
isWindows: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then it keeps the original executable path.
|
||||||
|
expect(resolved.executable, executable);
|
||||||
|
|
||||||
|
// And it does not force the command to run in a shell.
|
||||||
|
expect(resolved.runInShell, isFalse);
|
||||||
|
expect(resolved.usesBashShim, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Keep Windows batch files on the shell path
|
||||||
|
/// Given a Windows command script path
|
||||||
|
/// When the process launcher resolves the command
|
||||||
|
/// Then it leaves the script path unchanged
|
||||||
|
/// And it marks the command to run in a shell
|
||||||
|
/// ```
|
||||||
|
test('Keep Windows batch files on the shell path', () {
|
||||||
|
// Given a Windows command script path.
|
||||||
|
const executable = r'C:\tools\helper.cmd';
|
||||||
|
|
||||||
|
// When the process launcher resolves the command.
|
||||||
|
final resolved = resolveExternalCommandForCurrentPlatform(
|
||||||
|
executable,
|
||||||
|
const <String>['sync'],
|
||||||
|
isWindows: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then it leaves the script path unchanged.
|
||||||
|
expect(resolved.executable, executable);
|
||||||
|
|
||||||
|
// And it marks the command to run in a shell.
|
||||||
|
expect(resolved.runInShell, isTrue);
|
||||||
|
expect(resolved.usesBashShim, isFalse);
|
||||||
|
});
|
||||||
|
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Wrap local extensionless scripts through WSL bash on Windows
|
||||||
|
/// Given a local extensionless script file on Windows
|
||||||
|
/// When the process launcher resolves the command
|
||||||
|
/// Then it swaps the executable to wsl.exe
|
||||||
|
/// And it forwards the script path as a bash path
|
||||||
|
/// ```
|
||||||
|
test(
|
||||||
|
'Wrap local extensionless scripts through WSL bash on Windows',
|
||||||
|
() async {
|
||||||
|
// Given a local extensionless script file on Windows.
|
||||||
|
final sandbox = await Directory.systemTemp.createTemp(
|
||||||
|
'cws-process-launcher-',
|
||||||
|
);
|
||||||
|
final script = File(p.join(sandbox.path, 'fake-tea'));
|
||||||
|
await script.writeAsString('#!/usr/bin/env bash\nprintf \'[]\\n\'\n');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// When the process launcher resolves the command.
|
||||||
|
final resolved = resolveExternalCommandForCurrentPlatform(
|
||||||
|
script.path,
|
||||||
|
const <String>['api'],
|
||||||
|
isWindows: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then it swaps the executable to wsl.exe.
|
||||||
|
expect(resolved.executable, 'wsl.exe');
|
||||||
|
expect(resolved.usesBashShim, isTrue);
|
||||||
|
|
||||||
|
// And it forwards the script path as a bash path.
|
||||||
|
expect(resolved.runInShell, isFalse);
|
||||||
|
expect(resolved.arguments.first, '--exec');
|
||||||
|
expect(resolved.arguments[1], 'bash');
|
||||||
|
expect(resolved.arguments[2], startsWith('/mnt/'));
|
||||||
|
} finally {
|
||||||
|
await sandbox.delete(recursive: true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -21,12 +21,12 @@ void registerIssueAssistantAppRunGosmeeTests() {
|
||||||
/// Scenario: Reply to a mention received through tea/gosmee
|
/// Scenario: Reply to a mention received through tea/gosmee
|
||||||
/// Given a temporary project checkout that can be used as the local repo path
|
/// Given a temporary project checkout that can be used as the local repo path
|
||||||
/// And a gosmee script that forwards one Gitea issue_comment webhook event
|
/// And a gosmee script that forwards one Gitea issue_comment webhook event
|
||||||
/// And a tea script that can manage webhooks and post issue comments
|
/// And a Gitea API fixture that can manage webhooks and post issue comments
|
||||||
/// And a responder that emits a planning reply
|
/// And a responder that emits a planning reply
|
||||||
/// And an orchestrator-style config that uses tea/gosmee
|
/// And an orchestrator-style config that uses tea/gosmee
|
||||||
/// When the long-running app starts and receives the forwarded webhook
|
/// When the long-running app starts and receives the forwarded webhook
|
||||||
/// Then it reads issue comments and posts exactly one reply for that issue
|
/// Then it reads issue comments and posts exactly one reply for that issue
|
||||||
/// And the tea webhook lifecycle commands are invoked for the Gitea project
|
/// And the Gitea webhook lifecycle API is invoked for the project
|
||||||
/// ```
|
/// ```
|
||||||
test('Reply to a mention received through tea/gosmee', () async {
|
test('Reply to a mention received through tea/gosmee', () async {
|
||||||
// Given a temporary project checkout that can be used as the local repo path.
|
// Given a temporary project checkout that can be used as the local repo path.
|
||||||
|
|
@ -70,62 +70,26 @@ void registerIssueAssistantAppRunGosmeeTests() {
|
||||||
gosmeeLog: gosmeeLog,
|
gosmeeLog: gosmeeLog,
|
||||||
);
|
);
|
||||||
|
|
||||||
// And a tea script that can manage webhooks and post issue comments.
|
// And a Gitea API fixture for issue reads and replies.
|
||||||
final teaScript = File(p.join(sandbox.path, 'tea'));
|
final giteaServer = await FakeGiteaApiServer.start(
|
||||||
final teaLog = File(p.join(sandbox.path, 'tea-log.jsonl'));
|
issueListResponsesByRepo: {
|
||||||
final postedBody = File(p.join(sandbox.path, 'posted-body.md'));
|
'owner/sample': const <Map<String, Object?>>[],
|
||||||
await teaScript.writeAsString('''#!/usr/bin/env bash
|
},
|
||||||
set -euo pipefail
|
commentResponsesByRepo: {
|
||||||
printf '%s\n' "\$*" >> "${bashScriptPath(teaLog.path)}"
|
'owner/sample': {12: comments},
|
||||||
|
},
|
||||||
if [[ "\$1" == "api" && "\$2" == "user" ]]; then
|
postCommentResponsesByRepo: {
|
||||||
cat <<'JSON'
|
'owner/sample': {
|
||||||
${jsonEncode({'login': 'tea-octocat'})}
|
12: {
|
||||||
JSON
|
'id': 801,
|
||||||
exit 0
|
'body': 'posted',
|
||||||
fi
|
'html_url': 'https://gitea.example.test/comment/801',
|
||||||
|
},
|
||||||
if [[ "\$1" == "api" && "\$4" == repos/owner/sample/issues\\?state=open* ]]; then
|
},
|
||||||
cat <<'JSON'
|
},
|
||||||
[]
|
viewerLogin: 'tea-octocat',
|
||||||
JSON
|
);
|
||||||
exit 0
|
addTearDown(giteaServer.close);
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "api" && "\$4" == "repos/owner/sample/issues/12/comments?limit=100" ]]; then
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode(comments)}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "api" && "\$4" == "repos/owner/sample/issues/12/comments" ]]; then
|
|
||||||
for arg in "\$@"; do
|
|
||||||
if [[ "\$arg" == body=* ]]; then
|
|
||||||
printf '%s' "\${arg#body=}" > "${bashScriptPath(postedBody.path)}"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode({'id': 801, 'url': 'https://gitea.example.test/comment/801', 'body': 'posted'})}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "webhooks" && "\$2" == "create" ]]; then
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode({'id': 81})}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "webhooks" && "\$2" == "delete" ]]; then
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "unexpected tea args: \$*" >&2
|
|
||||||
exit 1
|
|
||||||
''');
|
|
||||||
await makeScriptExecutable(teaScript);
|
|
||||||
|
|
||||||
// And a responder that emits a planning reply.
|
// And a responder that emits a planning reply.
|
||||||
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
||||||
|
|
@ -156,18 +120,21 @@ projects:
|
||||||
type: tea/gosmee
|
type: tea/gosmee
|
||||||
repo: owner/sample
|
repo: owner/sample
|
||||||
path: ${repoDir.path}
|
path: ${repoDir.path}
|
||||||
|
''');
|
||||||
|
await File(p.join(sandbox.path, '.env')).writeAsString('''
|
||||||
|
CWS_GITEA_HOST=${giteaServer.baseUrl}
|
||||||
|
CWS_GITEA_TOKEN=test-token
|
||||||
''');
|
''');
|
||||||
final app = await IssueAssistantApp.open(
|
final app = await IssueAssistantApp.open(
|
||||||
config: await AppConfig.load(configFile.path),
|
config: await AppConfig.load(configFile.path),
|
||||||
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
||||||
gosmeeCommand: gosmeeScript.path,
|
gosmeeCommand: gosmeeScript.path,
|
||||||
teaCommand: teaScript.path,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// When the long-running app starts and receives the forwarded webhook.
|
// When the long-running app starts and receives the forwarded webhook.
|
||||||
final runFuture = app.run();
|
final runFuture = app.run();
|
||||||
for (var attempt = 0; attempt < 200; attempt += 1) {
|
for (var attempt = 0; attempt < 200; attempt += 1) {
|
||||||
if (await postedBody.exists()) {
|
if (giteaServer.postedBodies.isNotEmpty) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||||
|
|
@ -176,38 +143,31 @@ projects:
|
||||||
await runFuture;
|
await runFuture;
|
||||||
|
|
||||||
// Then it reads issue comments and posts exactly one reply for that issue.
|
// Then it reads issue comments and posts exactly one reply for that issue.
|
||||||
expect(await postedBody.exists(), isTrue);
|
expect(giteaServer.postedBodies, hasLength(1));
|
||||||
final postedComment = await postedBody.readAsString();
|
final postedComment =
|
||||||
|
(jsonDecode(giteaServer.postedBodies.single)
|
||||||
|
as Map<String, dynamic>)['body']
|
||||||
|
as String;
|
||||||
expect(
|
expect(
|
||||||
postedComment,
|
postedComment,
|
||||||
contains('Plan:\n- consume gosmee issue_comment events'),
|
contains('Plan:\n- consume gosmee issue_comment events'),
|
||||||
);
|
);
|
||||||
expect(postedComment, contains('@tea-octocat'));
|
expect(postedComment, contains('@tea-octocat'));
|
||||||
|
|
||||||
// And the tea webhook lifecycle commands are invoked for the Gitea project.
|
// And the Gitea webhook lifecycle API is invoked for the project.
|
||||||
final teaLogLines = await teaLog.readAsLines();
|
expect(giteaServer.createdWebhooks, hasLength(1));
|
||||||
expect(
|
expect(
|
||||||
teaLogLines.any(
|
(giteaServer.createdWebhooks.single['config'] as Map)['url'],
|
||||||
(line) => line.contains(
|
'https://gosmee.example.test/sample',
|
||||||
'webhooks create --type gitea --events issues,issue_comment --active -o json -r owner/sample https://gosmee.example.test/sample',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
isTrue,
|
|
||||||
);
|
|
||||||
expect(
|
|
||||||
teaLogLines.any(
|
|
||||||
(line) =>
|
|
||||||
line.contains('webhooks delete --confirm -r owner/sample 81'),
|
|
||||||
),
|
|
||||||
isTrue,
|
|
||||||
);
|
);
|
||||||
|
expect(giteaServer.deletedWebhookIds, contains(81));
|
||||||
});
|
});
|
||||||
|
|
||||||
/// ```gherkin
|
/// ```gherkin
|
||||||
/// Scenario: Reconcile a missed Gitea gosmee event through polling
|
/// Scenario: Reconcile a missed Gitea gosmee event through polling
|
||||||
/// Given a temporary project checkout that can be used as the local repo path
|
/// Given a temporary project checkout that can be used as the local repo path
|
||||||
/// And a gosmee script that starts forwarding without delivering an event
|
/// And a gosmee script that starts forwarding without delivering an event
|
||||||
/// And a tea script that returns a new issue only on a later reconciliation poll
|
/// And a Gitea API fixture that returns a new issue only on a later reconciliation poll
|
||||||
/// And a responder that emits a planning reply
|
/// And a responder that emits a planning reply
|
||||||
/// And an orchestrator-style config that uses tea/gosmee with continuous reconciliation
|
/// And an orchestrator-style config that uses tea/gosmee with continuous reconciliation
|
||||||
/// When the long-running app starts and enough time passes for reconciliation polling
|
/// When the long-running app starts and enough time passes for reconciliation polling
|
||||||
|
|
@ -245,11 +205,6 @@ exit 1
|
||||||
''');
|
''');
|
||||||
await makeScriptExecutable(gosmeeScript);
|
await makeScriptExecutable(gosmeeScript);
|
||||||
|
|
||||||
// And a tea script that returns a new issue only on a later reconciliation poll.
|
|
||||||
final teaScript = File(p.join(sandbox.path, 'tea'));
|
|
||||||
final teaLog = File(p.join(sandbox.path, 'tea-log.jsonl'));
|
|
||||||
final postedBody = File(p.join(sandbox.path, 'posted-body.md'));
|
|
||||||
final issueListCount = File(p.join(sandbox.path, 'issue-list-count'));
|
|
||||||
final issue = {
|
final issue = {
|
||||||
'index': 18,
|
'index': 18,
|
||||||
'title': 'Need reconciliation after a missed webhook',
|
'title': 'Need reconciliation after a missed webhook',
|
||||||
|
|
@ -271,70 +226,30 @@ exit 1
|
||||||
'poster': {'login': 'reporter'},
|
'poster': {'login': 'reporter'},
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
await teaScript.writeAsString('''#!/usr/bin/env bash
|
|
||||||
set -euo pipefail
|
|
||||||
printf '%s\n' "\$*" >> "${bashScriptPath(teaLog.path)}"
|
|
||||||
|
|
||||||
if [[ "\$1" == "api" && "\$2" == "user" ]]; then
|
// And a Gitea API fixture that returns a new issue only on a later reconciliation poll.
|
||||||
cat <<'JSON'
|
final giteaServer = await FakeGiteaApiServer.start(
|
||||||
${jsonEncode({'login': 'tea-octocat'})}
|
issueListResponseSequenceByRepo: {
|
||||||
JSON
|
'owner/sample': [
|
||||||
exit 0
|
const <Map<String, Object?>>[],
|
||||||
fi
|
[issue],
|
||||||
|
],
|
||||||
if [[ "\$1" == "api" && "\$4" == repos/owner/sample/issues\\?state=open* ]]; then
|
},
|
||||||
count=0
|
commentResponsesByRepo: {
|
||||||
if [[ -f "${bashScriptPath(issueListCount.path)}" ]]; then
|
'owner/sample': {18: comments},
|
||||||
count="\$(cat "${bashScriptPath(issueListCount.path)}")"
|
},
|
||||||
fi
|
postCommentResponsesByRepo: {
|
||||||
count=\$((count + 1))
|
'owner/sample': {
|
||||||
printf '%s' "\$count" > "${bashScriptPath(issueListCount.path)}"
|
18: {
|
||||||
if [[ "\$count" -eq 1 ]]; then
|
'id': 802,
|
||||||
cat <<'JSON'
|
'body': 'posted',
|
||||||
[]
|
'html_url': 'https://gitea.example.test/comment/802',
|
||||||
JSON
|
},
|
||||||
else
|
},
|
||||||
cat <<'JSON'
|
},
|
||||||
${jsonEncode([issue])}
|
viewerLogin: 'tea-octocat',
|
||||||
JSON
|
);
|
||||||
fi
|
addTearDown(giteaServer.close);
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "api" && "\$4" == "repos/owner/sample/issues/18/comments?limit=100" ]]; then
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode(comments)}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "api" && "\$4" == "repos/owner/sample/issues/18/comments" ]]; then
|
|
||||||
for arg in "\$@"; do
|
|
||||||
if [[ "\$arg" == body=* ]]; then
|
|
||||||
printf '%s' "\${arg#body=}" > "${bashScriptPath(postedBody.path)}"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode({'id': 802, 'url': 'https://gitea.example.test/comment/802', 'body': 'posted'})}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "webhooks" && "\$2" == "create" ]]; then
|
|
||||||
cat <<'JSON'
|
|
||||||
${jsonEncode({'id': 82})}
|
|
||||||
JSON
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [[ "\$1" == "webhooks" && "\$2" == "delete" ]]; then
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "unexpected tea args: \$*" >&2
|
|
||||||
exit 1
|
|
||||||
''');
|
|
||||||
await makeScriptExecutable(teaScript);
|
|
||||||
|
|
||||||
// And a responder that emits a planning reply.
|
// And a responder that emits a planning reply.
|
||||||
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
final responderScript = File(p.join(sandbox.path, 'responder.sh'));
|
||||||
|
|
@ -366,18 +281,21 @@ projects:
|
||||||
reconcileInterval: 1s
|
reconcileInterval: 1s
|
||||||
repo: owner/sample
|
repo: owner/sample
|
||||||
path: ${repoDir.path}
|
path: ${repoDir.path}
|
||||||
|
''');
|
||||||
|
await File(p.join(sandbox.path, '.env')).writeAsString('''
|
||||||
|
CWS_GITEA_HOST=${giteaServer.baseUrl}
|
||||||
|
CWS_GITEA_TOKEN=test-token
|
||||||
''');
|
''');
|
||||||
final app = await IssueAssistantApp.open(
|
final app = await IssueAssistantApp.open(
|
||||||
config: await AppConfig.load(configFile.path),
|
config: await AppConfig.load(configFile.path),
|
||||||
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
databasePath: p.join(sandbox.path, 'state.sqlite3'),
|
||||||
gosmeeCommand: gosmeeScript.path,
|
gosmeeCommand: gosmeeScript.path,
|
||||||
teaCommand: teaScript.path,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// When the long-running app starts and enough time passes for reconciliation polling.
|
// When the long-running app starts and enough time passes for reconciliation polling.
|
||||||
final runFuture = app.run();
|
final runFuture = app.run();
|
||||||
for (var attempt = 0; attempt < 200; attempt += 1) {
|
for (var attempt = 0; attempt < 200; attempt += 1) {
|
||||||
if (await postedBody.exists()) {
|
if (giteaServer.postedBodies.isNotEmpty) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
||||||
|
|
@ -386,21 +304,19 @@ projects:
|
||||||
await runFuture;
|
await runFuture;
|
||||||
|
|
||||||
// Then it posts exactly one reply for the issue discovered by polling.
|
// Then it posts exactly one reply for the issue discovered by polling.
|
||||||
expect(await postedBody.exists(), isTrue);
|
expect(giteaServer.postedBodies, hasLength(1));
|
||||||
final postedComment = await postedBody.readAsString();
|
final postedComment =
|
||||||
|
(jsonDecode(giteaServer.postedBodies.single)
|
||||||
|
as Map<String, dynamic>)['body']
|
||||||
|
as String;
|
||||||
expect(
|
expect(
|
||||||
postedComment,
|
postedComment,
|
||||||
contains('Plan:\n- reconcile missed gosmee events through polling'),
|
contains('Plan:\n- reconcile missed gosmee events through polling'),
|
||||||
);
|
);
|
||||||
|
|
||||||
// And the app performs more than one Gitea issue listing request while running.
|
// And the app performs more than one Gitea issue listing request while running.
|
||||||
final teaLogLines = await teaLog.readAsLines();
|
|
||||||
expect(
|
expect(
|
||||||
teaLogLines
|
giteaServer.issueListRequestCount('owner/sample'),
|
||||||
.where(
|
|
||||||
(line) => line.contains('repos/owner/sample/issues?state=open'),
|
|
||||||
)
|
|
||||||
.length,
|
|
||||||
greaterThanOrEqualTo(2),
|
greaterThanOrEqualTo(2),
|
||||||
);
|
);
|
||||||
final gosmeeLogLines = await gosmeeLog.readAsLines();
|
final gosmeeLogLines = await gosmeeLog.readAsLines();
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:code_work_spawner/code_work_spawner.dart';
|
import 'package:code_work_spawner/code_work_spawner.dart';
|
||||||
|
|
@ -189,72 +191,282 @@ void main() {
|
||||||
/// Feature: Gitea tracker client integration
|
/// Feature: Gitea tracker client integration
|
||||||
///
|
///
|
||||||
/// As a maintainer debugging Gitea tracker failures
|
/// As a maintainer debugging Gitea tracker failures
|
||||||
/// I want tea-specific API and webhook errors to preserve the original cause
|
/// I want issue operations to use the Gitea HTTP API directly
|
||||||
/// So that misconfigured repos and tea output changes are easy to trace
|
/// So that issue polling and replies do not depend on `tea api`
|
||||||
/// ```
|
/// ```
|
||||||
group('IssueTrackerClient Gitea', () {
|
group('IssueTrackerClient Gitea', () {
|
||||||
|
setUp(() {
|
||||||
|
AppEnvironment.resetForTest();
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() {
|
||||||
|
AppEnvironment.resetForTest();
|
||||||
|
});
|
||||||
|
|
||||||
/// ```gherkin
|
/// ```gherkin
|
||||||
/// Scenario: Read a webhook id from tea plain-text success output
|
/// Scenario: Create a Gitea webhook through the HTTP API
|
||||||
/// Given a fake tea executable whose webhook create command prints plain text instead of JSON
|
/// Given a local Gitea API server that accepts webhook creation requests
|
||||||
/// When the tracker client creates a Gitea webhook
|
/// When the tracker client creates a Gitea webhook
|
||||||
/// Then it still extracts and returns the created webhook id
|
/// Then it returns the created webhook id
|
||||||
|
/// And the webhook options are sent to the Gitea API
|
||||||
/// ```
|
/// ```
|
||||||
test('Read a webhook id from tea plain-text success output', () async {
|
test('Create a Gitea webhook through the HTTP API', () async {
|
||||||
// Given a fake tea executable whose webhook create command prints plain text instead of JSON.
|
// Given a local Gitea API server that accepts webhook creation requests.
|
||||||
final sandbox = await Directory.systemTemp.createTemp('cws-tea-webhook-');
|
final giteaServer = await FakeGiteaApiServer.start();
|
||||||
final teaScript = File(p.join(sandbox.path, 'tea'));
|
_configureGiteaTestEnvironmentFromBaseUrl(giteaServer.baseUrl);
|
||||||
await teaScript.writeAsString('''#!/usr/bin/env bash
|
final client = IssueTrackerClient();
|
||||||
set -euo pipefail
|
|
||||||
|
|
||||||
if [[ "\$1" == "webhooks" && "\$2" == "create" ]]; then
|
|
||||||
printf '%s\n' 'Webhook created successfully (ID: 40)'
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "unexpected tea args: \$*" >&2
|
|
||||||
exit 1
|
|
||||||
''');
|
|
||||||
await makeScriptExecutable(teaScript);
|
|
||||||
final client = IssueTrackerClient(teaCommand: teaScript.path);
|
|
||||||
|
|
||||||
|
try {
|
||||||
// When the tracker client creates a Gitea webhook.
|
// When the tracker client creates a Gitea webhook.
|
||||||
final webhookId = await client.createGiteaIssueWebhook(
|
final webhookId = await client.createGiteaIssueWebhook(
|
||||||
repo: RepositorySlug('owner', 'sample'),
|
repo: RepositorySlug('owner', 'sample'),
|
||||||
url: 'https://gosmee.example.test/sample',
|
url: 'https://gosmee.example.test/sample',
|
||||||
);
|
);
|
||||||
|
|
||||||
// Then it still extracts and returns the created webhook id.
|
// Then it returns the created webhook id.
|
||||||
expect(webhookId, 40);
|
expect(webhookId, 81);
|
||||||
|
|
||||||
|
// And the webhook options are sent to the Gitea API.
|
||||||
|
expect(giteaServer.createdWebhooks, hasLength(1));
|
||||||
|
expect(giteaServer.createdWebhooks.single['type'], 'gitea');
|
||||||
|
expect(giteaServer.createdWebhooks.single['active'], isTrue);
|
||||||
|
expect(giteaServer.createdWebhooks.single['events'], [
|
||||||
|
'issues',
|
||||||
|
'issue_comment',
|
||||||
|
]);
|
||||||
|
expect(
|
||||||
|
(giteaServer.createdWebhooks.single['config'] as Map)['url'],
|
||||||
|
'https://gosmee.example.test/sample',
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
await giteaServer.close();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
/// ```gherkin
|
/// ```gherkin
|
||||||
/// Scenario: Surface Gitea API error details when issue listing returns an error object
|
/// Scenario: Fetch Gitea issues, comments, and login through the HTTP API
|
||||||
/// Given a fake tea executable whose issue list request returns a Gitea error object
|
/// Given a local Gitea API server with one issue and one comment
|
||||||
|
/// When the tracker client fetches updates, loads the thread, resolves the login, and posts a comment
|
||||||
|
/// Then the Gitea issue payload is normalized into the shared issue summary model
|
||||||
|
/// And the posted comment response is returned to the caller
|
||||||
|
/// ```
|
||||||
|
test('Fetch Gitea issues, comments, and login through the HTTP API', () async {
|
||||||
|
// Given a local Gitea API server with one issue and one comment.
|
||||||
|
String? postedBody;
|
||||||
|
final server = await _startGiteaApiServer((request) async {
|
||||||
|
if (request.uri.path == '/api/v1/repos/owner/sample/issues' &&
|
||||||
|
request.method == 'GET') {
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.ok
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(
|
||||||
|
jsonEncode(<Map<String, Object?>>[
|
||||||
|
<String, Object?>{
|
||||||
|
'number': 12,
|
||||||
|
'title': 'Need Gitea support',
|
||||||
|
'body': 'Please add direct API support.',
|
||||||
|
'state': 'open',
|
||||||
|
'html_url':
|
||||||
|
'https://gitea.example.test/owner/sample/issues/12',
|
||||||
|
'updated_at': '2026-04-04T12:00:00Z',
|
||||||
|
'user': <String, Object?>{'login': 'reporter'},
|
||||||
|
'labels': <Map<String, Object?>>[
|
||||||
|
<String, Object?>{'name': 'enhancement'},
|
||||||
|
],
|
||||||
|
'pull_request': null,
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await request.response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.uri.path ==
|
||||||
|
'/api/v1/repos/owner/sample/issues/12/comments' &&
|
||||||
|
request.method == 'GET') {
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.ok
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(
|
||||||
|
jsonEncode(<Map<String, Object?>>[
|
||||||
|
<String, Object?>{
|
||||||
|
'id': 501,
|
||||||
|
'body': 'Working on it.',
|
||||||
|
'created_at': '2026-04-04T12:05:00Z',
|
||||||
|
'updated_at': '2026-04-04T12:06:00Z',
|
||||||
|
'html_url':
|
||||||
|
'https://gitea.example.test/owner/sample/issues/12#issuecomment-501',
|
||||||
|
'user': <String, Object?>{'login': 'maintainer'},
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
await request.response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.uri.path == '/api/v1/user' && request.method == 'GET') {
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.ok
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(jsonEncode(<String, Object?>{'login': 'gitea-user'}));
|
||||||
|
await request.response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.uri.path ==
|
||||||
|
'/api/v1/repos/owner/sample/issues/12/comments' &&
|
||||||
|
request.method == 'POST') {
|
||||||
|
postedBody =
|
||||||
|
(jsonDecode(await utf8.decodeStream(request))
|
||||||
|
as Map<String, dynamic>)['body']
|
||||||
|
as String?;
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.created
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(
|
||||||
|
jsonEncode(<String, Object?>{
|
||||||
|
'id': 901,
|
||||||
|
'body': 'posted',
|
||||||
|
'html_url':
|
||||||
|
'https://gitea.example.test/owner/sample/issues/12#issuecomment-901',
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
await request.response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.notFound
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(jsonEncode(<String, Object?>{'message': 'not found'}));
|
||||||
|
await request.response.close();
|
||||||
|
});
|
||||||
|
_configureGiteaTestEnvironment(server);
|
||||||
|
final client = IssueTrackerClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// When the tracker client fetches updates, loads the thread, resolves the login, and posts a comment.
|
||||||
|
final issues = await client.fetchUpdatedIssuesForRepos(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
trackerProjects: const <String>['owner/sample'],
|
||||||
|
since: null,
|
||||||
|
);
|
||||||
|
final thread = await client.fetchThread(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
trackerProject: 'owner/sample',
|
||||||
|
issue: issues.single,
|
||||||
|
);
|
||||||
|
final login = await client.getAuthenticatedLogin(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
);
|
||||||
|
final posted = await client.createIssueComment(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
trackerProject: 'owner/sample',
|
||||||
|
issueNumber: 12,
|
||||||
|
body: 'Reply from gitea api',
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then the Gitea issue payload is normalized into the shared issue summary model.
|
||||||
|
expect(login, 'gitea-user');
|
||||||
|
expect(issues.single.trackerProject, 'owner/sample');
|
||||||
|
expect(issues.single.number, 12);
|
||||||
|
expect(issues.single.state, 'open');
|
||||||
|
expect(issues.single.labels, ['enhancement']);
|
||||||
|
expect(thread.comments.single.url, contains('#issuecomment-501'));
|
||||||
|
|
||||||
|
// And the posted comment response is returned to the caller.
|
||||||
|
expect(postedBody, 'Reply from gitea api');
|
||||||
|
expect(posted.id, 901);
|
||||||
|
expect(posted.url, contains('#issuecomment-901'));
|
||||||
|
expect(posted.body, 'posted');
|
||||||
|
} finally {
|
||||||
|
await server.close(force: true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Reuse a tea login from the Windows app data config path
|
||||||
|
/// Given a tea config file under LOCALAPPDATA with a default Gitea login
|
||||||
|
/// When the tracker client resolves the authenticated Gitea login
|
||||||
|
/// Then it calls the Gitea HTTP API using the host and token from that config file
|
||||||
|
/// ```
|
||||||
|
test('Reuse a tea login from the Windows app data config path', () async {
|
||||||
|
// Given a tea config file under LOCALAPPDATA with a default Gitea login.
|
||||||
|
final sandbox = await Directory.systemTemp.createTemp('cws-tea-config-');
|
||||||
|
final localAppData = Directory(p.join(sandbox.path, 'AppData', 'Local'));
|
||||||
|
final teaConfigDir = Directory(p.join(localAppData.path, 'tea'));
|
||||||
|
await teaConfigDir.create(recursive: true);
|
||||||
|
|
||||||
|
late final HttpServer server;
|
||||||
|
server = await _startGiteaApiServer((request) async {
|
||||||
|
expect(request.uri.path, '/api/v1/user');
|
||||||
|
expect(
|
||||||
|
request.headers.value(HttpHeaders.authorizationHeader),
|
||||||
|
'token config-token',
|
||||||
|
);
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.ok
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write(jsonEncode(<String, Object?>{'login': 'config-user'}));
|
||||||
|
await request.response.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
await File(p.join(teaConfigDir.path, 'config.yml')).writeAsString('''
|
||||||
|
logins:
|
||||||
|
- name: company
|
||||||
|
url: http://${server.address.host}:${server.port}
|
||||||
|
token: config-token
|
||||||
|
default: true
|
||||||
|
insecure: true
|
||||||
|
''');
|
||||||
|
AppEnvironment.resetForTest(<String, String>{
|
||||||
|
'LOCALAPPDATA': localAppData.path,
|
||||||
|
});
|
||||||
|
final client = IssueTrackerClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
// When the tracker client resolves the authenticated Gitea login.
|
||||||
|
final login = await client.getAuthenticatedLogin(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then it calls the Gitea HTTP API using the host and token from that config file.
|
||||||
|
expect(login, 'config-user');
|
||||||
|
} finally {
|
||||||
|
await server.close(force: true);
|
||||||
|
await sandbox.delete(recursive: true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Surface Gitea API error details when issue listing returns an error response
|
||||||
|
/// Given a local Gitea API server whose issue list request returns a Gitea error object
|
||||||
/// When the tracker client fetches updated Gitea issues
|
/// When the tracker client fetches updated Gitea issues
|
||||||
/// Then it throws a format error that includes the repo and API error details
|
/// Then it throws a format error that includes the repo and API error details
|
||||||
/// ```
|
/// ```
|
||||||
test(
|
test(
|
||||||
'Surface Gitea API error details when issue listing returns an error object',
|
'Surface Gitea API error details when issue listing returns an error response',
|
||||||
() async {
|
() async {
|
||||||
// Given a fake tea executable whose issue list request returns a Gitea error object.
|
// Given a local Gitea API server whose issue list request returns a Gitea error object.
|
||||||
final sandbox = await Directory.systemTemp.createTemp('cws-tea-api-');
|
final server = await _startGiteaApiServer((request) async {
|
||||||
final teaScript = File(p.join(sandbox.path, 'tea'));
|
request.response
|
||||||
await teaScript.writeAsString('''#!/usr/bin/env bash
|
..statusCode = HttpStatus.notFound
|
||||||
set -euo pipefail
|
..headers.contentType = ContentType.json
|
||||||
|
..write(
|
||||||
if [[ "\$1" == "api" && "\$4" == "repos/owner/sample/issues?state=open"* ]]; then
|
jsonEncode(<String, Object?>{
|
||||||
cat <<'JSON'
|
'errors': <String>[
|
||||||
{"errors":["user redirect does not exist [name: owner]"],"message":"GetUserByName","url":"https://gitea.example.test/api/swagger"}
|
'user redirect does not exist [name: owner]',
|
||||||
JSON
|
],
|
||||||
exit 0
|
'message': 'GetUserByName',
|
||||||
fi
|
'url': 'https://gitea.example.test/api/swagger',
|
||||||
|
}),
|
||||||
echo "unexpected tea args: \$*" >&2
|
);
|
||||||
exit 1
|
await request.response.close();
|
||||||
''');
|
});
|
||||||
await makeScriptExecutable(teaScript);
|
_configureGiteaTestEnvironment(server);
|
||||||
final client = IssueTrackerClient(teaCommand: teaScript.path);
|
final client = IssueTrackerClient();
|
||||||
|
|
||||||
|
try {
|
||||||
// When the tracker client fetches updated Gitea issues.
|
// When the tracker client fetches updated Gitea issues.
|
||||||
final future = client.fetchUpdatedIssuesForRepos(
|
final future = client.fetchUpdatedIssuesForRepos(
|
||||||
provider: IssueTrackerProvider.gitea,
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
|
@ -270,14 +482,114 @@ exit 1
|
||||||
(error) => error.message,
|
(error) => error.message,
|
||||||
'message',
|
'message',
|
||||||
allOf(
|
allOf(
|
||||||
contains('owner/sample'),
|
|
||||||
contains('GetUserByName'),
|
contains('GetUserByName'),
|
||||||
contains('user redirect does not exist'),
|
contains('user redirect does not exist'),
|
||||||
|
contains('404'),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
await server.close(force: true);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
/// ```gherkin
|
||||||
|
/// Scenario: Fail fast and log when a Gitea API request exceeds the timeout
|
||||||
|
/// Given a local Gitea API server whose issue list request never returns promptly
|
||||||
|
/// When the tracker client fetches updated Gitea issues with a short timeout
|
||||||
|
/// Then it throws a timeout instead of waiting indefinitely
|
||||||
|
/// And the tracker logger records the timed out request
|
||||||
|
/// ```
|
||||||
|
test(
|
||||||
|
'Fail fast and log when a Gitea API request exceeds the timeout',
|
||||||
|
() async {
|
||||||
|
// Given a local Gitea API server whose issue list request never returns promptly.
|
||||||
|
final server = await _startGiteaApiServer((request) async {
|
||||||
|
await Future<void>.delayed(const Duration(seconds: 5));
|
||||||
|
request.response
|
||||||
|
..statusCode = HttpStatus.ok
|
||||||
|
..headers.contentType = ContentType.json
|
||||||
|
..write('[]');
|
||||||
|
await request.response.close();
|
||||||
|
});
|
||||||
|
_configureGiteaTestEnvironment(server);
|
||||||
|
final logMessages = <String>[];
|
||||||
|
void listener(AppLogRecord record) {
|
||||||
|
if (record.loggerName == 'code_work_spawner.issue_tracker') {
|
||||||
|
logMessages.add(record.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AppLogger.addListener(listener);
|
||||||
|
final client = IssueTrackerClient(
|
||||||
|
commandTimeout: const Duration(milliseconds: 200),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// When the tracker client fetches updated Gitea issues with a short timeout.
|
||||||
|
final future = client.fetchUpdatedIssuesForRepos(
|
||||||
|
provider: IssueTrackerProvider.gitea,
|
||||||
|
trackerProjects: const <String>['owner/sample'],
|
||||||
|
since: null,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Then it throws a timeout instead of waiting indefinitely.
|
||||||
|
await expectLater(
|
||||||
|
future,
|
||||||
|
throwsA(
|
||||||
|
isA<TimeoutException>().having(
|
||||||
|
(error) => error.duration,
|
||||||
|
'duration',
|
||||||
|
const Duration(milliseconds: 200),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
AppLogger.removeListener(listener);
|
||||||
|
await server.close(force: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// And the tracker logger records the timed out request.
|
||||||
|
expect(
|
||||||
|
logMessages,
|
||||||
|
contains(
|
||||||
|
allOf(
|
||||||
|
contains('gitea request timeout'),
|
||||||
|
contains('timeout_ms=200'),
|
||||||
|
contains('/api/v1/repos/owner/sample/issues'),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _configureGiteaTestEnvironment(HttpServer server) {
|
||||||
|
_configureGiteaTestEnvironmentFromBaseUrl(
|
||||||
|
'http://${server.address.host}:${server.port}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _configureGiteaTestEnvironmentFromBaseUrl(String baseUrl) {
|
||||||
|
AppEnvironment.resetForTest(<String, String>{
|
||||||
|
...Platform.environment,
|
||||||
|
'CWS_GITEA_HOST': baseUrl,
|
||||||
|
'CWS_GITEA_TOKEN': 'test-token',
|
||||||
|
'CWS_GITEA_INSECURE': 'false',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<HttpServer> _startGiteaApiServer(
|
||||||
|
Future<void> Function(HttpRequest request) handler,
|
||||||
|
) async {
|
||||||
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
||||||
|
unawaited(() async {
|
||||||
|
await for (final request in server) {
|
||||||
|
await handler(request);
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,160 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
class FakeGiteaApiServer {
|
||||||
|
FakeGiteaApiServer._({
|
||||||
|
required HttpServer server,
|
||||||
|
required this.issueListResponsesByRepo,
|
||||||
|
required this.issueListResponseSequenceByRepo,
|
||||||
|
required this.commentResponsesByRepo,
|
||||||
|
required this.postCommentResponsesByRepo,
|
||||||
|
required this.viewerLogin,
|
||||||
|
required this.token,
|
||||||
|
}) : _server = server;
|
||||||
|
|
||||||
|
final HttpServer _server;
|
||||||
|
final Map<String, List<Map<String, Object?>>> issueListResponsesByRepo;
|
||||||
|
final Map<String, List<List<Map<String, Object?>>>>
|
||||||
|
issueListResponseSequenceByRepo;
|
||||||
|
final Map<String, Map<int, List<Map<String, Object?>>>>
|
||||||
|
commentResponsesByRepo;
|
||||||
|
final Map<String, Map<int, Map<String, Object?>>> postCommentResponsesByRepo;
|
||||||
|
final String viewerLogin;
|
||||||
|
final String token;
|
||||||
|
final List<String> requestPaths = <String>[];
|
||||||
|
final List<String> postedBodies = <String>[];
|
||||||
|
final List<Map<String, dynamic>> createdWebhooks = <Map<String, dynamic>>[];
|
||||||
|
final List<int> deletedWebhookIds = <int>[];
|
||||||
|
final Map<String, int> _issueListRequestCounts = <String, int>{};
|
||||||
|
|
||||||
|
String get baseUrl => 'http://${_server.address.host}:${_server.port}';
|
||||||
|
|
||||||
|
int issueListRequestCount(String repo) => _issueListRequestCounts[repo] ?? 0;
|
||||||
|
|
||||||
|
static Future<FakeGiteaApiServer> start({
|
||||||
|
Map<String, List<Map<String, Object?>>> issueListResponsesByRepo =
|
||||||
|
const <String, List<Map<String, Object?>>>{},
|
||||||
|
Map<String, List<List<Map<String, Object?>>>>
|
||||||
|
issueListResponseSequenceByRepo =
|
||||||
|
const <String, List<List<Map<String, Object?>>>>{},
|
||||||
|
Map<String, Map<int, List<Map<String, Object?>>>> commentResponsesByRepo =
|
||||||
|
const <String, Map<int, List<Map<String, Object?>>>>{},
|
||||||
|
Map<String, Map<int, Map<String, Object?>>> postCommentResponsesByRepo =
|
||||||
|
const <String, Map<int, Map<String, Object?>>>{},
|
||||||
|
String viewerLogin = 'tea-octocat',
|
||||||
|
String token = 'test-token',
|
||||||
|
}) async {
|
||||||
|
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
|
||||||
|
final fixture = FakeGiteaApiServer._(
|
||||||
|
server: server,
|
||||||
|
issueListResponsesByRepo: issueListResponsesByRepo,
|
||||||
|
issueListResponseSequenceByRepo: issueListResponseSequenceByRepo,
|
||||||
|
commentResponsesByRepo: commentResponsesByRepo,
|
||||||
|
postCommentResponsesByRepo: postCommentResponsesByRepo,
|
||||||
|
viewerLogin: viewerLogin,
|
||||||
|
token: token,
|
||||||
|
);
|
||||||
|
unawaited(
|
||||||
|
server.forEach((request) async {
|
||||||
|
await fixture._handleRequest(request);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
return fixture;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _handleRequest(HttpRequest request) async {
|
||||||
|
requestPaths.add(request.uri.toString());
|
||||||
|
if (request.headers.value(HttpHeaders.authorizationHeader) !=
|
||||||
|
'token $token') {
|
||||||
|
request.response.statusCode = HttpStatus.unauthorized;
|
||||||
|
await request.response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final response = request.response..headers.contentType = ContentType.json;
|
||||||
|
if (request.method == 'GET' && request.uri.path == '/api/v1/user') {
|
||||||
|
response.write(jsonEncode(<String, Object?>{'login': viewerLogin}));
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final segments = request.uri.pathSegments;
|
||||||
|
if (segments.length >= 6 &&
|
||||||
|
segments[0] == 'api' &&
|
||||||
|
segments[1] == 'v1' &&
|
||||||
|
segments[2] == 'repos') {
|
||||||
|
final repo = '${segments[3]}/${segments[4]}';
|
||||||
|
if (segments.length == 6 &&
|
||||||
|
segments[5] == 'hooks' &&
|
||||||
|
request.method == 'POST') {
|
||||||
|
final body = await utf8.decodeStream(request);
|
||||||
|
createdWebhooks.add(jsonDecode(body) as Map<String, dynamic>);
|
||||||
|
response.statusCode = HttpStatus.created;
|
||||||
|
response.write(jsonEncode(<String, Object?>{'id': 81}));
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments.length == 7 &&
|
||||||
|
segments[5] == 'hooks' &&
|
||||||
|
request.method == 'DELETE') {
|
||||||
|
deletedWebhookIds.add(int.parse(segments[6]));
|
||||||
|
response.statusCode = HttpStatus.noContent;
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments.length == 6 &&
|
||||||
|
segments[5] == 'issues' &&
|
||||||
|
request.method == 'GET') {
|
||||||
|
final requestCount = (_issueListRequestCounts[repo] ?? 0) + 1;
|
||||||
|
_issueListRequestCounts[repo] = requestCount;
|
||||||
|
final sequence = issueListResponseSequenceByRepo[repo];
|
||||||
|
final payload = sequence == null || sequence.isEmpty
|
||||||
|
? issueListResponsesByRepo[repo] ?? const <Map<String, Object?>>[]
|
||||||
|
: sequence[(requestCount - 1).clamp(0, sequence.length - 1)];
|
||||||
|
response.write(jsonEncode(payload));
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (segments.length == 8 &&
|
||||||
|
segments[5] == 'issues' &&
|
||||||
|
segments[7] == 'comments') {
|
||||||
|
final issueNumber = int.parse(segments[6]);
|
||||||
|
if (request.method == 'GET') {
|
||||||
|
final payload =
|
||||||
|
commentResponsesByRepo[repo]?[issueNumber] ??
|
||||||
|
const <Map<String, Object?>>[];
|
||||||
|
response.write(jsonEncode(payload));
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.method == 'POST') {
|
||||||
|
final body = await utf8.decodeStream(request);
|
||||||
|
postedBodies.add(body);
|
||||||
|
final payload =
|
||||||
|
postCommentResponsesByRepo[repo]?[issueNumber] ??
|
||||||
|
<String, Object?>{
|
||||||
|
'id': 700,
|
||||||
|
'body': 'posted',
|
||||||
|
'html_url':
|
||||||
|
'https://gitea.example.test/$repo/issues/$issueNumber#issuecomment-700',
|
||||||
|
};
|
||||||
|
response.statusCode = HttpStatus.created;
|
||||||
|
response.write(jsonEncode(payload));
|
||||||
|
await response.close();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response.statusCode = HttpStatus.notFound;
|
||||||
|
response.write(jsonEncode(<String, Object?>{'message': 'not found'}));
|
||||||
|
await response.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> close() => _server.close(force: true);
|
||||||
|
}
|
||||||
|
|
@ -2,6 +2,7 @@ export 'support/script_utils.dart';
|
||||||
export 'support/fake_gh.dart';
|
export 'support/fake_gh.dart';
|
||||||
export 'support/fake_tea.dart';
|
export 'support/fake_tea.dart';
|
||||||
export 'support/fake_glab.dart';
|
export 'support/fake_glab.dart';
|
||||||
|
export 'support/fake_gitea_api.dart';
|
||||||
export 'support/fake_responders.dart';
|
export 'support/fake_responders.dart';
|
||||||
export 'support/fake_gosmee.dart';
|
export 'support/fake_gosmee.dart';
|
||||||
export 'support/git_utils.dart';
|
export 'support/git_utils.dart';
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,292 @@
|
||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:args/args.dart';
|
||||||
|
import 'package:code_work_spawner/src/core/process_launcher.dart';
|
||||||
|
|
||||||
|
// The file is used to probe different modes of running external processes in Dart
|
||||||
|
|
||||||
|
// Usage example:
|
||||||
|
// Running command with Process.runSync: tea api -X GET "repos/bkinnightskytw/code_work_spawner/issues?state=open&limit=100&page=1&since=2026-04-10T06%3A06%3A25.000Z" -r bkinnightskytw/code_work_spawner
|
||||||
|
// `dart run .\tool\process_mode_probe.dart --mode process-run-sync --command tea --arg api --arg -X --arg GET --arg '"repos/bkinnightskytw/code_work_spawner/issues?state=open&limit=100&page=1"' --arg -r --arg 'bkinnightskytw/code_work_spawner'`
|
||||||
|
// `dart run .\tool\process_mode_probe.dart --mode process-run-sync --command tea --arg login --arg default`
|
||||||
|
// Aware that `tea` has known severe issues on Windows due to lipgloss/v2 query console color.
|
||||||
|
//
|
||||||
|
|
||||||
|
Future<void> main(List<String> arguments) async {
|
||||||
|
final parser = ArgParser()
|
||||||
|
..addOption(
|
||||||
|
'command',
|
||||||
|
help: 'Executable to run, for example `tea` or `gh`.',
|
||||||
|
mandatory: true,
|
||||||
|
)
|
||||||
|
..addMultiOption(
|
||||||
|
'arg',
|
||||||
|
help: 'Repeat for each argument that should be passed to the command.',
|
||||||
|
)
|
||||||
|
..addOption(
|
||||||
|
'mode',
|
||||||
|
help:
|
||||||
|
'Comma-separated modes: all, launcher, process-run, process-run-sync, process-start, powershell-command.',
|
||||||
|
defaultsTo: 'all',
|
||||||
|
)
|
||||||
|
..addOption(
|
||||||
|
'timeout-ms',
|
||||||
|
help: 'Per-mode timeout in milliseconds.',
|
||||||
|
defaultsTo: '5000',
|
||||||
|
)
|
||||||
|
..addOption(
|
||||||
|
'working-directory',
|
||||||
|
help: 'Optional working directory for the command.',
|
||||||
|
);
|
||||||
|
|
||||||
|
// add help
|
||||||
|
parser.addFlag(
|
||||||
|
'help',
|
||||||
|
abbr: 'h',
|
||||||
|
help: 'Show this help message.',
|
||||||
|
negatable: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
final results = parser.parse(arguments);
|
||||||
|
final command = results['command'] as String;
|
||||||
|
final args = (results['arg'] as List<String>).toList(growable: false);
|
||||||
|
final timeout = Duration(
|
||||||
|
milliseconds: int.parse(results['timeout-ms'] as String),
|
||||||
|
);
|
||||||
|
final workingDirectory = results['working-directory'] as String?;
|
||||||
|
final selectedModes = _parseModes(results['mode'] as String);
|
||||||
|
final resolved = resolveExternalCommandForCurrentPlatform(command, args);
|
||||||
|
|
||||||
|
final payload = <String, Object?>{
|
||||||
|
'command': command,
|
||||||
|
'arguments': args,
|
||||||
|
'timeout_ms': timeout.inMilliseconds,
|
||||||
|
'working_directory': workingDirectory,
|
||||||
|
'resolved': <String, Object?>{
|
||||||
|
'executable': resolved.executable,
|
||||||
|
'arguments': resolved.arguments,
|
||||||
|
'run_in_shell': resolved.runInShell,
|
||||||
|
'uses_bash_shim': resolved.usesBashShim,
|
||||||
|
},
|
||||||
|
'results': <Object?>[],
|
||||||
|
};
|
||||||
|
final resultList = payload['results'] as List<Object?>;
|
||||||
|
|
||||||
|
for (final mode in selectedModes) {
|
||||||
|
resultList.add(
|
||||||
|
await _runMode(
|
||||||
|
mode,
|
||||||
|
command: command,
|
||||||
|
arguments: args,
|
||||||
|
timeout: timeout,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
stdout.writeln(const JsonEncoder.withIndent(' ').convert(payload));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> _parseModes(String configuredModes) {
|
||||||
|
final trimmedModes = configuredModes
|
||||||
|
.split(',')
|
||||||
|
.map((mode) => mode.trim())
|
||||||
|
.where((mode) => mode.isNotEmpty)
|
||||||
|
.toList(growable: false);
|
||||||
|
if (trimmedModes.contains('all')) {
|
||||||
|
return const <String>[
|
||||||
|
'launcher',
|
||||||
|
'process-run',
|
||||||
|
'process-run-sync',
|
||||||
|
'process-start',
|
||||||
|
'powershell-command',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return trimmedModes;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<String, Object?>> _runMode(
|
||||||
|
String mode, {
|
||||||
|
required String command,
|
||||||
|
required List<String> arguments,
|
||||||
|
required Duration timeout,
|
||||||
|
required String? workingDirectory,
|
||||||
|
}) async {
|
||||||
|
final stopwatch = Stopwatch()..start();
|
||||||
|
try {
|
||||||
|
final result = switch (mode) {
|
||||||
|
'launcher' => await runExternalCommand(
|
||||||
|
command,
|
||||||
|
arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
timeout: timeout,
|
||||||
|
),
|
||||||
|
'process-run' =>
|
||||||
|
await Process.run(
|
||||||
|
command,
|
||||||
|
arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
runInShell: false,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
).timeout(
|
||||||
|
timeout,
|
||||||
|
onTimeout: () {
|
||||||
|
throw TimeoutException(
|
||||||
|
'Command timed out after ${timeout.inMilliseconds}ms.',
|
||||||
|
timeout,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
'process-start' => await _runWithProcessStart(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
timeout: timeout,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
),
|
||||||
|
'process-run-sync' => _runWithProcessRunSync(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
),
|
||||||
|
'powershell-command' => await _runViaPowerShellCommand(
|
||||||
|
command: command,
|
||||||
|
arguments: arguments,
|
||||||
|
timeout: timeout,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
),
|
||||||
|
_ => throw ArgumentError('Unsupported mode: $mode'),
|
||||||
|
};
|
||||||
|
stopwatch.stop();
|
||||||
|
return <String, Object?>{
|
||||||
|
'mode': mode,
|
||||||
|
'status': 'success',
|
||||||
|
'duration_ms': stopwatch.elapsedMilliseconds,
|
||||||
|
'exit_code': result.exitCode,
|
||||||
|
'stdout_length': result.stdout.toString().length,
|
||||||
|
'stderr_length': result.stderr.toString().length,
|
||||||
|
'stdout_preview': _preview(result.stdout.toString()),
|
||||||
|
'stderr_preview': _preview(result.stderr.toString()),
|
||||||
|
};
|
||||||
|
} on TimeoutException catch (error) {
|
||||||
|
stopwatch.stop();
|
||||||
|
return <String, Object?>{
|
||||||
|
'mode': mode,
|
||||||
|
'status': 'timeout',
|
||||||
|
'duration_ms': stopwatch.elapsedMilliseconds,
|
||||||
|
'message': error.message,
|
||||||
|
};
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
stopwatch.stop();
|
||||||
|
return <String, Object?>{
|
||||||
|
'mode': mode,
|
||||||
|
'status': 'error',
|
||||||
|
'duration_ms': stopwatch.elapsedMilliseconds,
|
||||||
|
'error': error.toString(),
|
||||||
|
'stack_preview': _preview(stackTrace.toString()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ProcessResult> _runWithProcessStart({
|
||||||
|
required String command,
|
||||||
|
required List<String> arguments,
|
||||||
|
required Duration timeout,
|
||||||
|
required String? workingDirectory,
|
||||||
|
}) async {
|
||||||
|
final process = await Process.start(
|
||||||
|
command,
|
||||||
|
arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
runInShell: false,
|
||||||
|
);
|
||||||
|
await process.stdin.close();
|
||||||
|
final stdoutFuture = process.stdout.transform(utf8.decoder).join();
|
||||||
|
final stderrFuture = process.stderr.transform(utf8.decoder).join();
|
||||||
|
final exitCode = await process.exitCode.timeout(
|
||||||
|
timeout,
|
||||||
|
onTimeout: () {
|
||||||
|
process.kill();
|
||||||
|
throw TimeoutException(
|
||||||
|
'Command timed out after ${timeout.inMilliseconds}ms.',
|
||||||
|
timeout,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
return ProcessResult(
|
||||||
|
process.pid,
|
||||||
|
exitCode,
|
||||||
|
await stdoutFuture,
|
||||||
|
await stderrFuture,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
ProcessResult _runWithProcessRunSync({
|
||||||
|
required String command,
|
||||||
|
required List<String> arguments,
|
||||||
|
required String? workingDirectory,
|
||||||
|
}) {
|
||||||
|
// print
|
||||||
|
print(
|
||||||
|
'Running command with Process.runSync: $command ${arguments.join(' ')}',
|
||||||
|
);
|
||||||
|
return Process.runSync(
|
||||||
|
command,
|
||||||
|
arguments,
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
runInShell: false,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ProcessResult> _runViaPowerShellCommand({
|
||||||
|
required String command,
|
||||||
|
required List<String> arguments,
|
||||||
|
required Duration timeout,
|
||||||
|
required String? workingDirectory,
|
||||||
|
}) async {
|
||||||
|
final powerShellScript = r'''
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$command = $args[0]
|
||||||
|
$commandArgs = if ($args.Length -gt 1) { $args[1..($args.Length - 1)] } else { @() }
|
||||||
|
& $command @commandArgs
|
||||||
|
if ($null -ne $LASTEXITCODE) {
|
||||||
|
exit $LASTEXITCODE
|
||||||
|
}
|
||||||
|
''';
|
||||||
|
|
||||||
|
return Process.run(
|
||||||
|
'powershell.exe',
|
||||||
|
<String>[
|
||||||
|
'-NoProfile',
|
||||||
|
'-NonInteractive',
|
||||||
|
'-Command',
|
||||||
|
powerShellScript,
|
||||||
|
command,
|
||||||
|
...arguments,
|
||||||
|
],
|
||||||
|
workingDirectory: workingDirectory,
|
||||||
|
runInShell: false,
|
||||||
|
stdoutEncoding: utf8,
|
||||||
|
stderrEncoding: utf8,
|
||||||
|
).timeout(
|
||||||
|
timeout,
|
||||||
|
onTimeout: () {
|
||||||
|
throw TimeoutException(
|
||||||
|
'Command timed out after ${timeout.inMilliseconds}ms.',
|
||||||
|
timeout,
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
String _preview(String text, {int maxLength = 200}) {
|
||||||
|
final singleLine = text.replaceAll(RegExp(r'\s+'), ' ').trim();
|
||||||
|
if (singleLine.length <= maxLength) {
|
||||||
|
return singleLine;
|
||||||
|
}
|
||||||
|
return '${singleLine.substring(0, maxLength)}...';
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue