@@ -17,20 +17,23 @@ import 'dart:io';
1717import 'package:args/args.dart' ;
1818import 'package:http/http.dart' as http;
1919
20- void usage (ArgParser parser) {
20+ void usage (ArgParser parser, {exitCode = 0 } ) {
2121 print ('''
2222Usage: post_results_to_pubsub.dart [OPTIONS]
2323Posts Dart CI results as messages to Google Cloud Pub/Sub
2424
2525The options are as follows:
2626
2727${parser .usage }''' );
28- exit (1 );
28+ exit (exitCode );
2929}
3030
3131const resultsPerMessage = 100 ;
32- const postUrl =
33- 'https://pubsub.googleapis.com/v1/projects/dart-ci/topics/results:publish' ;
32+
33+ String getPostUrl (String project) {
34+ return 'https://pubsub.googleapis.com/v1/projects/$project '
35+ '/topics/results:publish' ;
36+ }
3437
3538main (List <String > args) async {
3639 final parser = new ArgParser ();
@@ -42,12 +45,26 @@ main(List<String> args) async {
4245 abbr: 'f' , help: 'File containing the results to send' );
4346 parser.addOption ('id' , abbr: 'i' , help: 'Buildbucket ID of this build' );
4447 parser.addOption ('base_revision' , help: 'A try build\' s patch base' );
48+ parser.addFlag ('staging' ,
49+ abbr: 's' , help: 'Publish to the staging system' , defaultsTo: false );
4550
4651 final options = parser.parse (args);
4752 if (options['help' ]) {
4853 usage (parser);
4954 }
5055
56+ if (options['result_file' ] == null ) {
57+ print ('Error: option "result_file" is required.\n ' );
58+ usage (parser, exitCode: 1 );
59+ }
60+
61+ if (options['auth_token' ] == null ) {
62+ print ('Error: option "auth_token" is required.\n ' );
63+ usage (parser, exitCode: 1 );
64+ }
65+
66+ final project = options['staging' ] ? "dart-ci-staging" : "dart-ci" ;
67+
5168 final client = http.Client ();
5269
5370 final lines = await File (options['result_file' ]).readAsLines ();
@@ -59,6 +76,8 @@ main(List<String> args) async {
5976 return ;
6077 }
6178
79+ // TODO(karlklose): parse and validate data before sending it.
80+
6281 final changedPattern = '"changed":true' ;
6382 List <String > changedResults =
6483 lines.where ((change) => change.contains (changedPattern)).toList ();
@@ -95,6 +114,7 @@ main(List<String> args) async {
95114 ]
96115 });
97116 final headers = {'Authorization' : 'Bearer $token ' };
117+ final postUrl = getPostUrl (project);
98118 final response =
99119 await client.post (postUrl, headers: headers, body: jsonMessage);
100120
0 commit comments