Skip to content

Commit

Permalink
feat(allures): add header and basic auth support
Browse files Browse the repository at this point in the history
  • Loading branch information
rIIh committed Jun 10, 2024
1 parent d99bf79 commit 8b006f3
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 36 deletions.
7 changes: 7 additions & 0 deletions packages/allure_server_cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 7 additions & 2 deletions packages/allure_server_cli/README.md
Original file line number Diff line number Diff line change
@@ -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
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
55 changes: 53 additions & 2 deletions packages/allure_server_cli/bin/allures.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -47,14 +50,43 @@ Future<void> main(List<String> 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) {
Expand Down Expand Up @@ -85,3 +117,22 @@ Future<void> main(List<String> arguments) async {
printUsage(argParser);
}
}

MapEntry<String, String>? 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;
}
63 changes: 32 additions & 31 deletions packages/allure_server_cli/lib/src/args.dart
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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(),
);
}
2 changes: 1 addition & 1 deletion packages/allure_server_cli/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 8b006f3

Please sign in to comment.