Skip to content

Commit 06e6008

Browse files
committed
Added support for new deployment notifications in raygun-cli.
1 parent 599d2ea commit 06e6008

File tree

3 files changed

+202
-0
lines changed

3 files changed

+202
-0
lines changed

lib/deployments/deployments.dart

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'dart:io';
2+
3+
import 'package:raygun_cli/deployments/deployments_api.dart';
4+
import 'package:args/args.dart';
5+
import '../config_props.dart';
6+
7+
class Deployments {
8+
final ArgResults command;
9+
final bool verbose;
10+
late final String appId;
11+
late final String token;
12+
13+
Deployments({
14+
required this.command,
15+
required this.verbose,
16+
required ConfigProps config,
17+
}) {
18+
appId = config.appId;
19+
token = config.token;
20+
}
21+
22+
Future<void> notify() async {
23+
if (!command.wasParsed('api-key')) {
24+
print('Error: Missing "--api-key"');
25+
print(' Please provide "--api-key" via argument');
26+
exit(2);
27+
}
28+
if (!command.wasParsed('version')) {
29+
print('Error: Missing "--version"');
30+
print(' Please provide "--version" via argument');
31+
exit(2);
32+
}
33+
34+
final apiKey = command.option('api-key') as String;
35+
final version = command.option('version') as String;
36+
final ownerName = command.option('owner-name');
37+
final emailAddress = command.option('email-address');
38+
final comment = command.option('comment');
39+
final scmIdentifier = command.option('scm-identifier');
40+
final scmType = command.option('scm-type');
41+
42+
if (verbose) {
43+
print('app-id: $appId');
44+
print('token: $token');
45+
print('api-key: $apiKey');
46+
print('version: $version');
47+
print('owner-name: $ownerName');
48+
print('email-address: $emailAddress');
49+
print('comment: $comment');
50+
print('scm-identifier: $scmIdentifier');
51+
print('scm-type: $scmType');
52+
}
53+
54+
final success = await createDeployment(
55+
token: token,
56+
apiKey: apiKey,
57+
version: version,
58+
ownerName: ownerName,
59+
emailAddress: emailAddress,
60+
comment: comment,
61+
scmIdentifier: scmIdentifier,
62+
scmType: scmType,
63+
);
64+
65+
if (success) {
66+
print('Deployment created successfully');
67+
exit(0);
68+
} else {
69+
print('Failed to create deployment');
70+
exit(2);
71+
}
72+
}
73+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'dart:convert';
2+
import 'package:http/http.dart' as http;
3+
4+
Future<bool> createDeployment({
5+
required String token,
6+
required String apiKey,
7+
required String version,
8+
String? ownerName,
9+
String? emailAddress,
10+
String? comment,
11+
String? scmIdentifier,
12+
String? scmType,
13+
}) async {
14+
15+
final url = 'https://api.raygun.com/v3/applications/api-key/$apiKey/deployments';
16+
17+
final payload = {
18+
'version': version,
19+
if (ownerName != null) 'ownerName': ownerName,
20+
if (emailAddress != null) 'emailAddress': emailAddress,
21+
if (comment != null) 'comment': comment,
22+
if (scmIdentifier != null) 'scmIdentifier': scmIdentifier,
23+
if (scmType != null) 'scmType': scmType,
24+
'deployedAt': DateTime.now().toUtc().toIso8601String(),
25+
};
26+
27+
try {
28+
final response = await http.post(
29+
Uri.parse(url),
30+
headers: {
31+
'Authorization': 'Bearer $token',
32+
'Content-Type': 'application/json',
33+
},
34+
body: jsonEncode(payload),
35+
);
36+
37+
if (response.statusCode == 200 || response.statusCode == 201) {
38+
print('Success creating deployment: ${response.statusCode}');
39+
print('Deployment identifier: ${jsonDecode(response.body)['identifier']}');
40+
return true;
41+
}
42+
43+
print('Error creating deployment: ${response.statusCode}');
44+
print('Response: ${response.body}');
45+
return false;
46+
} catch (e) {
47+
print('Exception while creating deployment: $e');
48+
return false;
49+
}
50+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import 'dart:io';
2+
3+
import 'package:args/args.dart';
4+
import 'package:raygun_cli/deployments/deployments.dart';
5+
6+
import '../config_props.dart';
7+
8+
const kDeploymentsCommand = 'deployments';
9+
10+
ArgParser buildParserDeployments() {
11+
return ArgParser()
12+
..addFlag(
13+
'help',
14+
abbr: 'h',
15+
negatable: false,
16+
help: 'Print deployments usage information',
17+
)
18+
..addOption(
19+
'app-id',
20+
help: 'Raygun application ID',
21+
)
22+
..addOption(
23+
'token',
24+
help: 'Raygun access token',
25+
)
26+
..addOption(
27+
'api-key',
28+
mandatory: true,
29+
help: 'API key from the Raygun account you are deploying to',
30+
)
31+
..addOption(
32+
'version',
33+
mandatory: true,
34+
help: 'Version of the software you are deploying and want Raygun to know about',
35+
)
36+
..addOption(
37+
'scm-type',
38+
mandatory: false,
39+
allowed: ['GitHub', 'Bitbucket', 'GitLab', 'AzureDevOps'],
40+
help: 'Type of the source control management system you are deploying from - if provided, one of [GitHub, Bitbucket, GitLab, AzureDevOps]',
41+
)
42+
..addOption(
43+
'scm-identifier',
44+
mandatory: false,
45+
help: 'Commit that this deployment is based on',
46+
)
47+
..addOption(
48+
'owner-name',
49+
mandatory: false,
50+
help: 'Name of the person deploying the software',
51+
)
52+
..addOption(
53+
'email-address',
54+
mandatory: false,
55+
help: 'Email address of the person deploying the software',
56+
)
57+
..addOption(
58+
'comment',
59+
mandatory: false,
60+
help: 'Deployment comment',
61+
);
62+
}
63+
64+
void parseDeploymentsCommand(ArgResults command, bool verbose) {
65+
if (command.wasParsed('help')) {
66+
print('Usage: raygun-cli deployments <arguments>');
67+
print(buildParserDeployments().usage);
68+
exit(0);
69+
}
70+
71+
final configProps = ConfigProps.load(command, verbose: verbose);
72+
73+
Deployments(
74+
command: command,
75+
verbose: verbose,
76+
config: configProps,
77+
).notify();
78+
79+
}

0 commit comments

Comments
 (0)