From cecbd491b0ca2e17705e4b305c99be49da4d7e6c Mon Sep 17 00:00:00 2001 From: mishani0x0ef Date: Wed, 14 Oct 2015 02:54:25 +0300 Subject: [PATCH] Provide checking of connection to repository before add it to settings. --- Jira.Extension.Chrome/js/commitsService.js | 20 ++++++++ Jira.Extension.Chrome/js/optionsController.js | 46 +++++++++++-------- Jira.Extension.Chrome/options.html | 1 + .../ISvnService.cs | 4 ++ .../Jira.Extension.RepositoryApi.csproj | 2 + .../ServiceLocator.cs | 9 +++- .../Services/ISafeExecutor.cs | 14 ++++++ .../Services/SafeExecutor.cs | 20 ++++++++ .../SvnService.svc.cs | 9 ++++ 9 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/ISafeExecutor.cs create mode 100644 Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/SafeExecutor.cs diff --git a/Jira.Extension.Chrome/js/commitsService.js b/Jira.Extension.Chrome/js/commitsService.js index 560844a..ad37d5d 100644 --- a/Jira.Extension.Chrome/js/commitsService.js +++ b/Jira.Extension.Chrome/js/commitsService.js @@ -2,6 +2,26 @@ jiraReporterApp.service('commitsService', function ($q, $http) { var self = this; var baseApiUrl = "http://ws-if-cp0565/Jira.Extension.RepositoryApi/"; var defaultCommitsCount = 10; + + this.checkConnection = function(repository, resultHandler){ + var apiUrl = baseApiUrl + repository.type + "/connection/test"; + + $http({ + method: "GET", + url: apiUrl, + params: { + username: repository.userName, + password: repository.password, + repo: repository.url + } + }).success(function (data, status, headers, config) { + resultHandler(data); + }).error(function (data, status, headers, config) { + //todo: provide error handling. MR + alert("Cannot establish connection with API."); + console.error("Error while checking connection: " + status); + }); + }; this.getLastCommits = function (repository, handler) { var apiUrl = baseApiUrl + repository.type + "/commits"; diff --git a/Jira.Extension.Chrome/js/optionsController.js b/Jira.Extension.Chrome/js/optionsController.js index 57ccb28..b676567 100644 --- a/Jira.Extension.Chrome/js/optionsController.js +++ b/Jira.Extension.Chrome/js/optionsController.js @@ -1,4 +1,4 @@ -jiraReporterApp.controller('OptionsController', function ($scope, $interval, $timeout, storageService) { +jiraReporterApp.controller('OptionsController', function ($scope, $interval, $timeout, storageService, commitsService) { var showNotification = function (isSuccess, message) { $scope.isNoticaitionSuccess = isSuccess; @@ -46,26 +46,34 @@ jiraReporterApp.controller('OptionsController', function ($scope, $interval, $ti }; $scope.saveRepository = function (repository) { - if (typeof (repository.repositoryId) === "undefined") { - var repoId = 0; - angular.forEach($scope.repositories, function (repo) { - if (repo.repositoryId > repoId) { - repoId = repo.repositoryId; + commitsService.checkConnection(repository, function(connectionEstablished) { + if(!connectionEstablished){ + // todo: provide more beautiful dialog than default confirm. MR + if(!confirm("We wasn't able to establish connection using your settings. Save it anyway?")){ + return; } - }); - repository.repositoryId = ++repoId; - - $scope.repositories.push(repository); - } else { - angular.forEach($scope.repositories, function (repo) { - if (repo.repositoryId === repository.repositoryId) { - angular.copy(repository, repo); - } - }); - } + } + + if (typeof (repository.repositoryId) === "undefined") { + var repoId = 0; + angular.forEach($scope.repositories, function (repo) { + if (repo.repositoryId > repoId) { + repoId = repo.repositoryId; + } + }); + repository.repositoryId = ++repoId; + $scope.repositories.push(repository); + } else { + angular.forEach($scope.repositories, function (repo) { + if (repo.repositoryId === repository.repositoryId) { + angular.copy(repository, repo); + } + }); + } - $("#repositoryEditModal").modal("hide"); - $scope.saveSettings(); + $("#repositoryEditModal").modal("hide"); + $scope.saveSettings(); + }); }; $scope.removeRepository = function (repository) { diff --git a/Jira.Extension.Chrome/options.html b/Jira.Extension.Chrome/options.html index 7042d31..b1f1bc8 100644 --- a/Jira.Extension.Chrome/options.html +++ b/Jira.Extension.Chrome/options.html @@ -12,6 +12,7 @@ + diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ISvnService.cs b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ISvnService.cs index 28e1d96..269fbf3 100644 --- a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ISvnService.cs +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ISvnService.cs @@ -19,5 +19,9 @@ public interface ISvnService [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "commits?repo={repoUrl}&username={userName}&password={password}&count={count}&author={author}")] List GetCommits(string repoUrl, string userName, string password, int count, string author); + + [OperationContract] + [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "connection/test?repo={repoUrl}&username={userName}&password={password}")] + bool TestConnection(string repoUrl, string userName, string password); } } diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi.csproj b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi.csproj index 173325a..dddacc3 100644 --- a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi.csproj +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi.csproj @@ -93,6 +93,8 @@ + + SvnService.svc diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ServiceLocator.cs b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ServiceLocator.cs index 5ed6cb3..9151cf5 100644 --- a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ServiceLocator.cs +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/ServiceLocator.cs @@ -1,5 +1,6 @@ using Jira.Extension.RepoBase; using Jira.Extension.RepoBase.Svn; +using Jira.Extension.RepositoryApi.Services; using Microsoft.Practices.Unity; namespace Jira.Extension.RepositoryApi @@ -18,6 +19,7 @@ private ServiceLocator() { Container = new UnityContainer(); RegisterRepostiories(Container); + RegisterServices(Container); } /// @@ -29,10 +31,15 @@ public void BuildUp(object target) Container.BuildUp(target.GetType(), target); } - private void RegisterRepostiories(IUnityContainer container) + private static void RegisterRepostiories(IUnityContainer container) { container.RegisterType(new ContainerControlledLifetimeManager()); container.RegisterType("Mock", new ContainerControlledLifetimeManager()); } + + private static void RegisterServices(IUnityContainer container) + { + container.RegisterType(new ContainerControlledLifetimeManager()); + } } } \ No newline at end of file diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/ISafeExecutor.cs b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/ISafeExecutor.cs new file mode 100644 index 0000000..c3afb95 --- /dev/null +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/ISafeExecutor.cs @@ -0,0 +1,14 @@ +using System; + +namespace Jira.Extension.RepositoryApi.Services +{ + public interface ISafeExecutor + { + /// + /// Execute action and absorb any errors. + /// + /// + /// True if action was successfully executed otherwise return false. + bool TryExecute(Action action); + } +} diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/SafeExecutor.cs b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/SafeExecutor.cs new file mode 100644 index 0000000..366f3fa --- /dev/null +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/Services/SafeExecutor.cs @@ -0,0 +1,20 @@ +using System; + +namespace Jira.Extension.RepositoryApi.Services +{ + public class SafeExecutor : ISafeExecutor + { + public bool TryExecute(Action action) + { + try + { + action(); + return true; + } + catch (Exception) + { + return false; + } + } + } +} \ No newline at end of file diff --git a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/SvnService.svc.cs b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/SvnService.svc.cs index 5e78263..b88b543 100644 --- a/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/SvnService.svc.cs +++ b/Jira.Extension.RepositoryApi/Jira.Extension.RepositoryApi/SvnService.svc.cs @@ -14,6 +14,9 @@ public class SvnService : ISvnService [Dependency] public IRepoService RepoService { get; set; } + [Dependency] + public ISafeExecutor SafeExecutor { get; set; } + private const int DefautCommitsCount = 10; public SvnService() @@ -48,5 +51,11 @@ public List GetCommits(string repoUrl, string userName, string passwo return commits.ToDto().ToList(); } + + public bool TestConnection(string repoUrl, string userName, string password) + { + return SafeExecutor + .TryExecute(() => RepoService.GetLastCommits(repoUrl, new NetworkCredential(userName, password), 1)); + } } }