Files
pipelines/app/lib/features/pipelines/pipeline_repository.dart
T
alan 5a9284ccf7 App: archive and unarchive pipelines
The pipeline's bar gets a Pipeline actions menu with Archive (confirm
dialog: history is kept) or Unarchive; an archived detail says so and
offers no Run. The list hides archived pipelines and the scope control
gains an Archived view. Server refusals (enabled schedule, newer
version) are shown verbatim. The capture fixture answers archive
requests.
2026-11-04 23:37:25 +00:00

124 lines
4.3 KiB
Dart

import '../../core/api_gateway.dart';
import 'pipeline_models.dart';
/// A refusal or malformed reply with a message that is safe to show verbatim.
class PipelineRequestException implements Exception {
const PipelineRequestException(this.message);
final String message;
@override
String toString() => message;
}
/// Pipeline requests. Reads mirror `web/src/repository.js`; the one mutation
/// is archiving, which keeps every version and run.
class PipelinesRepository {
PipelinesRepository(this._gateway);
final ApiGateway _gateway;
/// `list_pipelines` -> the whole `pipelines` array, archived ones included
/// (each marked by `archived_at`) so the list can offer an Archived view.
Future<List<PipelineSummary>> listPipelines() async {
final envelope = await _gateway.call('list_pipelines', {
'include_archived': true,
});
_throwIfError(envelope, 'The server returned no pipeline list.');
final type = envelope['type'];
if (type != null && type != 'pipelines') {
throw const PipelineRequestException(
'The server returned no pipeline list.',
);
}
final raw = envelope['pipelines'];
if (raw == null) {
if (type == 'pipelines') return const <PipelineSummary>[];
throw const PipelineRequestException(
'The server returned no pipeline list.',
);
}
if (raw is! List) {
throw const PipelineRequestException(
'The server returned no pipeline list.',
);
}
return List.unmodifiable(
raw.whereType<Map>().map(
(item) => PipelineSummary.fromJson(Map<String, dynamic>.from(item)),
),
);
}
/// `get_pipeline` with the web's exact `pipeline_id`/`version` fields.
/// Version 0 asks the server for the latest version.
Future<PipelineDetail> getPipeline(int id, int version) async {
final envelope = await _gateway.call('get_pipeline', {
'pipeline_id': id,
'version': version,
});
_throwIfError(envelope, 'The server returned no pipeline detail.');
if (envelope['type'] != null && envelope['type'] != 'pipeline') {
throw const PipelineRequestException(
'The server returned no pipeline detail.',
);
}
final detail = envelope['detail'];
if (detail is! Map) {
throw const PipelineRequestException(
'The server returned no pipeline detail.',
);
}
return PipelineDetail.fromJson(Map<String, dynamic>.from(detail));
}
/// `archive_pipeline` / `unarchive_pipeline`, guarded by the latest
/// version the operator saw. Replies with the updated detail.
Future<PipelineDetail> setArchived(
int id, {
required bool archived,
required int expectedVersion,
}) async {
final type = archived ? 'archive_pipeline' : 'unarchive_pipeline';
final envelope = await _gateway.call(type, {
'pipeline_id': id,
if (expectedVersion > 0) 'expected_version': expectedVersion,
});
const fallback = 'The server did not confirm the change.';
_throwIfError(envelope, fallback);
final expected = archived ? 'pipeline_archived' : 'pipeline_unarchived';
final detail = envelope['detail'];
if (envelope['type'] != expected || detail is! Map) {
throw const PipelineRequestException(fallback);
}
return PipelineDetail.fromJson(Map<String, dynamic>.from(detail));
}
/// `list_runs` is read only here for the pipeline list's last-run join.
Future<List<PipelineRun>> listRuns() async {
final envelope = await _gateway.call('list_runs');
_throwIfError(envelope, 'The server returned no run list.');
if (envelope['type'] != null && envelope['type'] != 'runs') {
throw const PipelineRequestException('The server returned no run list.');
}
final raw = envelope['runs'];
if (raw == null) return const <PipelineRun>[];
if (raw is! List) {
throw const PipelineRequestException('The server returned no run list.');
}
return List.unmodifiable(
raw.whereType<Map>().map(
(item) => PipelineRun.fromJson(Map<String, dynamic>.from(item)),
),
);
}
void _throwIfError(JsonMap envelope, String fallback) {
final error = envelope['error'];
if (envelope['type'] == 'error' || (error is String && error.isNotEmpty)) {
final message = error is String ? error.trim() : '';
throw PipelineRequestException(message.isEmpty ? fallback : message);
}
}
}