Skip to content

Commit 7da8b39

Browse files
test: Refactor and Test Deployment command (#28)
* refactor sourcemap api class * refactor sourcemap command * update bin * completed command test * add tests and format * refactored proguard * add tests for proguard command * format * refactor deployments command * add deployments tests * fix error not appearing when using wrong name command
1 parent 6a77c2b commit 7da8b39

File tree

9 files changed

+761
-129
lines changed

9 files changed

+761
-129
lines changed

bin/raygun_cli.dart

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ ArgParser buildParser() {
3131
symbolsCommand.buildParser(),
3232
)
3333
..addCommand(
34-
kDeploymentsCommand,
35-
buildParserDeployments(),
34+
deploymentsCommand.name,
35+
deploymentsCommand.buildParser(),
3636
)
3737
..addCommand(
3838
proguardCommand.name,
@@ -58,6 +58,13 @@ void main(List<String> arguments) {
5858
try {
5959
final ArgResults results = argParser.parse(arguments);
6060
bool verbose = false;
61+
if (results.wasParsed('verbose')) {
62+
verbose = true;
63+
}
64+
65+
if (verbose) {
66+
print('[VERBOSE] All arguments: ${results.arguments}');
67+
}
6168

6269
if (results.wasParsed('help') || arguments.isEmpty) {
6370
printUsage(argParser);
@@ -67,9 +74,6 @@ void main(List<String> arguments) {
6774
print('raygun-cli version: $version');
6875
return;
6976
}
70-
if (results.wasParsed('verbose')) {
71-
verbose = true;
72-
}
7377

7478
if (results.command?.name == sourcemapCommand.name) {
7579
sourcemapCommand.execute(results.command!, verbose);
@@ -81,8 +85,8 @@ void main(List<String> arguments) {
8185
return;
8286
}
8387

84-
if (results.command?.name == kDeploymentsCommand) {
85-
parseDeploymentsCommand(results.command!, verbose);
88+
if (results.command?.name == deploymentsCommand.name) {
89+
deploymentsCommand.execute(results.command!, verbose);
8690
return;
8791
}
8892

@@ -91,12 +95,10 @@ void main(List<String> arguments) {
9195
return;
9296
}
9397

94-
if (verbose) {
95-
print('[VERBOSE] All arguments: ${results.arguments}');
96-
}
98+
throw FormatException('Unknown or missing command.');
9799
} on FormatException catch (e) {
98100
// Print usage information if an invalid argument was provided.
99-
print(e.message);
101+
print('Error: ${e.message}');
100102
print('');
101103
printUsage(argParser);
102104
}

lib/src/core/raygun_api.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:convert';
12
import 'dart:io';
23

34
import 'package:http/http.dart';
@@ -40,3 +41,26 @@ class RaygunMultipartRequestBuilder {
4041
return _request;
4142
}
4243
}
44+
45+
class RaygunPostRequestBuilder {
46+
late final Request _request;
47+
48+
RaygunPostRequestBuilder(String url) {
49+
_request = Request('POST', Uri.parse(url));
50+
}
51+
52+
RaygunPostRequestBuilder addBearerToken(String token) {
53+
_request.headers['Authorization'] = 'Bearer $token';
54+
return this;
55+
}
56+
57+
RaygunPostRequestBuilder addJsonBody(Map<String, dynamic> body) {
58+
_request.body = jsonEncode(body);
59+
_request.headers['Content-Type'] = 'application/json';
60+
return this;
61+
}
62+
63+
Request build() {
64+
return _request;
65+
}
66+
}

lib/src/deployments/deployments.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
import 'dart:io';
2-
3-
import 'package:raygun_cli/src/deployments/deployments_api.dart';
41
import 'package:args/args.dart';
2+
import 'package:raygun_cli/src/deployments/deployments_api.dart';
53
import '../config_props.dart';
64

75
/// Deployments command
86
class Deployments {
97
final ArgResults command;
108
final bool verbose;
9+
final DeploymentsApi deploymentsApi;
1110

1211
Deployments({
1312
required this.command,
1413
required this.verbose,
14+
required this.deploymentsApi,
1515
});
1616

1717
/// Notifies Raygun that a new deployment has been made.
18-
Future<void> notify() async {
18+
Future<bool> notify() async {
1919
if (!command.wasParsed('version')) {
2020
print('Error: Missing "--version"');
2121
print(' Please provide "--version" via argument');
22-
exit(2);
22+
return false;
2323
}
2424

2525
final version = command.option('version') as String;
@@ -42,7 +42,7 @@ class Deployments {
4242
print('scm-type: $scmType');
4343
}
4444

45-
final success = await createDeployment(
45+
final success = await deploymentsApi.createDeployment(
4646
token: token,
4747
apiKey: apiKey,
4848
version: version,
@@ -55,10 +55,10 @@ class Deployments {
5555

5656
if (success) {
5757
print('Deployment created successfully');
58-
exit(0);
58+
return true;
5959
} else {
6060
print('Failed to create deployment');
61-
exit(2);
61+
return false;
6262
}
6363
}
6464
}
Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,62 @@
11
import 'dart:convert';
2+
23
import 'package:http/http.dart' as http;
4+
import 'package:raygun_cli/src/core/raygun_api.dart';
35

4-
/// Creates a deployment in Raygun.
5-
Future<bool> createDeployment({
6-
required String token,
7-
required String apiKey,
8-
required String version,
9-
String? ownerName,
10-
String? emailAddress,
11-
String? comment,
12-
String? scmIdentifier,
13-
String? scmType,
14-
}) async {
15-
final url =
16-
'https://api.raygun.com/v3/applications/api-key/$apiKey/deployments';
17-
18-
final payload = {
19-
'version': version,
20-
if (ownerName != null) 'ownerName': ownerName,
21-
if (emailAddress != null) 'emailAddress': emailAddress,
22-
if (comment != null) 'comment': comment,
23-
if (scmIdentifier != null) 'scmIdentifier': scmIdentifier,
24-
if (scmType != null) 'scmType': scmType,
25-
'deployedAt': DateTime.now().toUtc().toIso8601String(),
26-
};
27-
28-
try {
29-
final response = await http.post(
30-
Uri.parse(url),
31-
headers: {
32-
'Authorization': 'Bearer $token',
33-
'Content-Type': 'application/json',
34-
},
35-
body: jsonEncode(payload),
36-
);
37-
38-
if (response.statusCode == 200 || response.statusCode == 201) {
39-
print('Success creating deployment: ${response.statusCode}');
40-
print(
41-
'Deployment identifier: ${jsonDecode(response.body)['identifier']}');
42-
return true;
43-
}
6+
class DeploymentsApi {
7+
const DeploymentsApi(this.httpClient);
8+
9+
final http.Client httpClient;
10+
11+
DeploymentsApi.create() : httpClient = http.Client();
12+
13+
/// Creates a deployment in Raygun.
14+
/// Returns true if the deployment was created successfully.
15+
Future<bool> createDeployment({
16+
required String token,
17+
required String apiKey,
18+
required String version,
19+
String? ownerName,
20+
String? emailAddress,
21+
String? comment,
22+
String? scmIdentifier,
23+
String? scmType,
24+
}) async {
25+
final url =
26+
'https://api.raygun.com/v3/applications/api-key/$apiKey/deployments';
4427

45-
print('Error creating deployment: ${response.statusCode}');
46-
print('Response: ${response.body}');
47-
return false;
48-
} catch (e) {
49-
print('Exception while creating deployment: $e');
50-
return false;
28+
final payload = {
29+
'version': version,
30+
if (ownerName != null) 'ownerName': ownerName,
31+
if (emailAddress != null) 'emailAddress': emailAddress,
32+
if (comment != null) 'comment': comment,
33+
if (scmIdentifier != null) 'scmIdentifier': scmIdentifier,
34+
if (scmType != null) 'scmType': scmType,
35+
'deployedAt': DateTime.now().toUtc().toIso8601String(),
36+
};
37+
38+
final request = RaygunPostRequestBuilder(url)
39+
.addBearerToken(token)
40+
.addJsonBody(payload)
41+
.build();
42+
43+
try {
44+
final response = await httpClient.send(request);
45+
final responseBody = await response.stream.bytesToString();
46+
47+
if (response.statusCode == 200 || response.statusCode == 201) {
48+
print('Success creating deployment: ${response.statusCode}');
49+
print(
50+
'Deployment identifier: ${jsonDecode(responseBody)['identifier']}');
51+
return true;
52+
}
53+
54+
print('Error creating deployment: ${response.statusCode}');
55+
print('Response: $responseBody');
56+
return false;
57+
} catch (e) {
58+
print('Exception while creating deployment: $e');
59+
return false;
60+
}
5161
}
5262
}

0 commit comments

Comments
 (0)