From 7ad0aac5e98b676c28d3bddc91fb16e320094de2 Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 28 May 2015 20:50:57 -0700 Subject: [PATCH 1/2] Add and (trivially) use services/travis.dart for branch detection --- lib/src/git_data.dart | 7 +++++-- lib/src/services/travis.dart | 8 ++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 lib/src/services/travis.dart diff --git a/lib/src/git_data.dart b/lib/src/git_data.dart index 5add3df..b9a799a 100644 --- a/lib/src/git_data.dart +++ b/lib/src/git_data.dart @@ -3,6 +3,7 @@ library dart_coveralls.git_data; import 'dart:io' show Directory, ProcessException, Platform; import 'log.dart'; +import 'services/travis.dart' as travis; import 'process_system.dart'; abstract class GitPerson { @@ -129,8 +130,10 @@ class GitBranch { Map environment}) { if (null == environment) environment = Platform.environment; if (null != environment["CI_BRANCH"]) return environment["CI_BRANCH"]; - if (null != environment["TRAVIS_BRANCH"]) return environment[ - "TRAVIS_BRANCH"]; + + var travisBranch = travis.getBranch(environment); + if (travisBranch != null) return travisBranch; + var args = ["rev-parse", "--abbrev-ref", "HEAD"]; var result = processSystem.runProcessSync("git", args, workingDirectory: dir.path); diff --git a/lib/src/services/travis.dart b/lib/src/services/travis.dart new file mode 100644 index 0000000..9472e41 --- /dev/null +++ b/lib/src/services/travis.dart @@ -0,0 +1,8 @@ +library coveralls_dart.src.services.travis; + +/// Returns the current branch name for the provided [environment] as defined +/// by Travis. +/// +/// If none exists, return `null`. +String getBranch(Map environment) => + environment["TRAVIS_BRANCH"]; From 888e820f347962c8e1cd01d4c8e92c3a464d33ef Mon Sep 17 00:00:00 2001 From: Kevin Moore Date: Thu, 28 May 2015 20:59:16 -0700 Subject: [PATCH 2/2] Provide travis values for service_name and service_job_id Per https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction Closes #34 --- CHANGELOG.md | 10 +++++++++ lib/src/cli_client.dart | 30 ++++++++++++-------------- lib/src/coveralls_entities.dart | 37 ++++++++++++++++++++++----------- lib/src/services/travis.dart | 10 +++++++++ pubspec.yaml | 2 +- test/coveralls_test.dart | 9 -------- 6 files changed, 59 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f218e3a..7dad1d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +### 0.3.0 + +* `serviceName` was removed from `CommandLineClient`. + +* `serviceName` and `serviceJobId` are now named paramaters on `CoverallsReport` + – constructor and `parse`. + +* `service_name` and `service_job_id` are correctly populated form Travis and + Coveralls. + ### 0.2.0 * A number of (breaking) changes to clarify and correctly distinguish between diff --git a/lib/src/cli_client.dart b/lib/src/cli_client.dart index 808642d..b67409d 100644 --- a/lib/src/cli_client.dart +++ b/lib/src/cli_client.dart @@ -12,15 +12,14 @@ import 'coveralls_endpoint.dart'; import 'coveralls_entities.dart'; import 'log.dart'; import 'process_system.dart'; +import 'services/travis.dart' as travis; class CommandLineClient { final String projectDirectory; final String packageRoot; final String token; - final String serviceName; - CommandLineClient._( - this.projectDirectory, this.packageRoot, this.token, this.serviceName); + CommandLineClient._(this.projectDirectory, this.packageRoot, this.token); factory CommandLineClient({String projectDirectory, String packageRoot, String token, Map environment}) { @@ -30,11 +29,9 @@ class CommandLineClient { packageRoot = _calcPackageRoot(projectDirectory, packageRoot); - var serviceName = getServiceName(environment); token = getToken(token, environment); - return new CommandLineClient._( - projectDirectory, packageRoot, token, serviceName); + return new CommandLineClient._(projectDirectory, packageRoot, token); } Future> getLcovResult(String testFile, @@ -56,13 +53,6 @@ class CommandLineClient { return environment["REPO_TOKEN"]; } - static String getServiceName([Map environment]) { - if (null == environment) environment = Platform.environment; - var serviceName = environment["COVERALLS_SERVICE_NAME"]; - if (serviceName == null) return "local"; - return serviceName; - } - Future reportToCoveralls(String testFile, {int workers, ProcessSystem processSystem: const ProcessSystem(), String coverallsAddress, bool dryRun: false, @@ -73,10 +63,14 @@ class CommandLineClient { rawLcov.printSummary(); var lcov = LcovDocument.parse(rawLcov.result.toString()); - var report = CoverallsReport.parse( - token, lcov, projectDirectory, serviceName, - excludeTestFiles: excludeTestFiles); - var endpoint = new CoverallsEndpoint(coverallsAddress); + + var serviceName = travis.getServiceName(Platform.environment); + var serviceJobId = travis.getServiceJobId(Platform.environment); + + var report = CoverallsReport.parse(token, lcov, projectDirectory, + excludeTestFiles: excludeTestFiles, + serviceName: serviceName, + serviceJobId: serviceJobId); if (printJson) { print(const JsonEncoder.withIndent(' ').convert(report)); @@ -84,6 +78,8 @@ class CommandLineClient { if (dryRun) return; + var endpoint = new CoverallsEndpoint(coverallsAddress); + try { var encoded = JSON.encode(report); await _sendLoop(endpoint, encoded, retry: retry); diff --git a/lib/src/coveralls_entities.dart b/lib/src/coveralls_entities.dart index e2ad4ee..1e915f4 100644 --- a/lib/src/coveralls_entities.dart +++ b/lib/src/coveralls_entities.dart @@ -237,25 +237,38 @@ class CoverallsReport { final GitData gitData; final SourceFileReports sourceFileReports; final String serviceName; + final String serviceJobId; - CoverallsReport( - this.repoToken, this.sourceFileReports, this.gitData, this.serviceName); + CoverallsReport(this.repoToken, this.sourceFileReports, this.gitData, + {this.serviceName, this.serviceJobId}); - static CoverallsReport parse(String repoToken, LcovDocument lcov, - String projectDirectory, String serviceName, - {bool excludeTestFiles: false}) { + static CoverallsReport parse( + String repoToken, LcovDocument lcov, String projectDirectory, + {String serviceName, String serviceJobId, bool excludeTestFiles: false}) { var gitData = GitData.getGitData(new Directory(projectDirectory)); var reports = SourceFileReports.parse(lcov, projectDirectory, excludeTestFiles: excludeTestFiles); - return new CoverallsReport(repoToken, reports, gitData, serviceName); + return new CoverallsReport(repoToken, reports, gitData, + serviceName: serviceName, serviceJobId: serviceJobId); } - Map toJson() => { - "repo_token": repoToken, - "git": gitData, - "service_name": serviceName, - "source_files": sourceFileReports.sourceFileReports.toList() - }; + Map toJson() { + var data = { + "repo_token": repoToken, + "git": gitData, + "source_files": sourceFileReports.sourceFileReports.toList() + }; + + if (serviceName != null) { + data['service_name'] = serviceName; + } + + if (serviceJobId != null) { + data['service_job_id'] = serviceJobId; + } + + return data; + } } /// Yields the Dart files represented by [entity]. diff --git a/lib/src/services/travis.dart b/lib/src/services/travis.dart index 9472e41..e9b91af 100644 --- a/lib/src/services/travis.dart +++ b/lib/src/services/travis.dart @@ -6,3 +6,13 @@ library coveralls_dart.src.services.travis; /// If none exists, return `null`. String getBranch(Map environment) => environment["TRAVIS_BRANCH"]; + +/// Following the coveralls conventions +/// See https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction +String getServiceName(Map environment) => + (environment['TRAVIS'] == 'true') ? 'travis-ci' : null; + +/// Following the coveralls conventions +/// See https://coveralls.zendesk.com/hc/en-us/articles/201774865-API-Introduction +String getServiceJobId(Map environment) => + environment['TRAVIS_JOB_ID']; diff --git a/pubspec.yaml b/pubspec.yaml index e944d34..804a356 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: dart_coveralls -version: 0.2.0 +version: 0.3.0-dev author: Axel Christ description: |- Pub package to calculate coverage, format it diff --git a/test/coveralls_test.dart b/test/coveralls_test.dart index a8daefa..53070d1 100644 --- a/test/coveralls_test.dart +++ b/test/coveralls_test.dart @@ -83,15 +83,6 @@ void main() { }); group("CommandLineClient", () { - test("getServiceName", () { - var s1 = CommandLineClient.getServiceName(); - var s2 = - CommandLineClient.getServiceName({"COVERALLS_SERVICE_NAME": "name"}); - - expect(s1, equals("local")); - expect(s2, equals("name")); - }); - group("getToken", () { test("with candidate", () { var t1 = CommandLineClient.getToken("test");