From 8b006f349666eb19be0e5df765012f4eb8767edf Mon Sep 17 00:00:00 2001 From: melvspace Date: Mon, 10 Jun 2024 11:39:31 +0300 Subject: [PATCH] feat(allures): add header and basic auth support --- packages/allure_server_cli/CHANGELOG.md | 7 +++ packages/allure_server_cli/README.md | 9 ++- packages/allure_server_cli/bin/allures.dart | 55 ++++++++++++++++- packages/allure_server_cli/lib/src/args.dart | 63 ++++++++++---------- packages/allure_server_cli/pubspec.yaml | 2 +- 5 files changed, 100 insertions(+), 36 deletions(-) diff --git a/packages/allure_server_cli/CHANGELOG.md b/packages/allure_server_cli/CHANGELOG.md index effe43c..24b1ecc 100644 --- a/packages/allure_server_cli/CHANGELOG.md +++ b/packages/allure_server_cli/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.0.1 + +- Add `--header` option, supporting multiple values in format `KEY=VALUE` +- Add Basic Auth support with Environment Variables + - `ALLURES_BASIC_AUTH_USER` - for Basic Auth user + - `ALLURES_BASIC_AUTH_PASSWORD` - for Basic Auth password + ## 1.0.0 - Initial version. diff --git a/packages/allure_server_cli/README.md b/packages/allure_server_cli/README.md index 8277c34..5521738 100644 --- a/packages/allure_server_cli/README.md +++ b/packages/allure_server_cli/README.md @@ -1,7 +1,12 @@ -## Allure Server CLI +# Allure Server CLI `allures` is meant to be used to upload reports to OSS [Allure Server](https://github.com/kochetkov-ma/allure-server) See [Allure Server repository](https://github.com/kochetkov-ma/allure-server) to find instructions to build it up and running. -See repository [README](https://github.com/rIIh/dart_test_reporter) for detailed info \ No newline at end of file +See repository [README](https://github.com/rIIh/dart_test_reporter) for detailed info + +## Environment Variables + +- `ALLURES_BASIC_AUTH_USER` - Basic Auth user +- `ALLURES_BASIC_AUTH_PASSWORD` - Basic Auth password \ No newline at end of file diff --git a/packages/allure_server_cli/bin/allures.dart b/packages/allure_server_cli/bin/allures.dart index 2292efe..ae57d7a 100644 --- a/packages/allure_server_cli/bin/allures.dart +++ b/packages/allure_server_cli/bin/allures.dart @@ -1,3 +1,6 @@ +import 'dart:convert'; +import 'dart:io'; + import 'package:allure_server_cli/src/args.dart'; import 'package:allure_server_cli/src/logger.dart'; import 'package:allure_server_cli/src/report_command.dart'; @@ -47,14 +50,43 @@ Future main(List arguments) async { throw FormatException('host was not provided'); } + var headers = results.multiOption('header').map((e) { + final match = RegExp('([a-zA-Z]+)="?(.+)"?').firstMatch(e); + if (match == null) { + throw FormatException( + 'Invalid Header format - $e. ' + 'Expected Header in format `KEY=VALUE`', + ); + } + + final header = match.group(1)!; + final value = match.group(2)!; + + return MapEntry(header, value); + }); + + l.d('Headers: $headers'); + var host = results.option('host')!; var api = results.option('api')!; - l.d({'host': host, 'api': api}); if (!host.startsWith(RegExp(r'https?://'))) { host = 'https://$host'; } - final dio = Dio(BaseOptions(baseUrl: '$host$api')); + l.d('Host Options: ${{'host': host, 'api': api}}'); + + final dio = Dio( + BaseOptions( + baseUrl: '$host$api', + headers: { + if (getAuthorizationFromEnvironment() + case MapEntry(:final key, :final value)) + key: value, + ...Map.fromEntries(headers), + }, + ), + ); + l.d('Dio Client created with host base: ${dio.options.baseUrl}'); if (results.command case ArgResults command) { @@ -85,3 +117,22 @@ Future main(List arguments) async { printUsage(argParser); } } + +MapEntry? getAuthorizationFromEnvironment() { + if (Platform.environment.containsKey('ALLURES_BASIC_AUTH_USER') && + Platform.environment.containsKey('ALLURES_BASIC_AUTH_PASSWORD')) { + final user = Platform.environment.containsKey( + 'ALLURES_BASIC_AUTH_USER', + ); + final password = Platform.environment.containsKey( + 'ALLURES_BASIC_AUTH_PASSWORD', + ); + + return MapEntry( + "Authorization", + "Basic ${base64.encode(utf8.encode('$user:$password'))}", + ); + } + + return null; +} diff --git a/packages/allure_server_cli/lib/src/args.dart b/packages/allure_server_cli/lib/src/args.dart index 1f10f12..e84802a 100644 --- a/packages/allure_server_cli/lib/src/args.dart +++ b/packages/allure_server_cli/lib/src/args.dart @@ -1,5 +1,37 @@ import 'package:args/args.dart'; +ArgParser buildParser() { + return ArgParser() + ..addOption('host', help: 'Allure Server host', mandatory: true) + ..addOption('api', help: 'Allure Server API path', defaultsTo: '/api') + ..addMultiOption('header', help: 'Additional request header') + ..addFlag( + 'verbose', + abbr: 'v', + help: 'Verbose output', + defaultsTo: false, + ) + ..addFlag( + 'help', + abbr: 'h', + negatable: false, + help: 'Print this usage information.', + ) + ..addFlag( + 'version', + negatable: false, + help: 'Print the tool version.', + ) + ..addCommand( + 'report', + buildReportParser(), + ) + ..addCommand( + 'upload', + buildUploadParser(), + ); +} + ArgParser buildReportParser() { return ArgParser() // ..addMultiOption( @@ -61,34 +93,3 @@ ArgParser buildReportParser() { ArgParser buildUploadParser() { return ArgParser(); } - -ArgParser buildParser() { - return ArgParser() - ..addOption('host', help: 'Allure Server host', mandatory: true) - ..addOption('api', help: 'Allure Server API path', defaultsTo: '/api') - ..addFlag( - 'verbose', - abbr: 'v', - help: 'Verbose output', - defaultsTo: false, - ) - ..addFlag( - 'help', - abbr: 'h', - negatable: false, - help: 'Print this usage information.', - ) - ..addFlag( - 'version', - negatable: false, - help: 'Print the tool version.', - ) - ..addCommand( - 'report', - buildReportParser(), - ) - ..addCommand( - 'upload', - buildUploadParser(), - ); -} diff --git a/packages/allure_server_cli/pubspec.yaml b/packages/allure_server_cli/pubspec.yaml index c04a8f7..5e48731 100644 --- a/packages/allure_server_cli/pubspec.yaml +++ b/packages/allure_server_cli/pubspec.yaml @@ -1,6 +1,6 @@ name: allure_server_cli description: CLI tool for communication with [Allure Server](https://github.com/kochetkov-ma/allure-server) -version: 1.0.0 +version: 1.0.1 repository: https://github.com/rIIh/dart_test_reporter issue_tracker: https://github.com/rIIh/dart_test_reporter/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc+%5Ballure_server_cli%5D