Skip to content

Commit

Permalink
Provide checking of connection to repository before add it to settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
mishani0x0ef committed Oct 13, 2015
1 parent 8afca8f commit cecbd49
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 20 deletions.
20 changes: 20 additions & 0 deletions Jira.Extension.Chrome/js/commitsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
46 changes: 27 additions & 19 deletions Jira.Extension.Chrome/js/optionsController.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions Jira.Extension.Chrome/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<script type="text/javascript" src="lib/angular.min.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="js/storageService.js"></script>
<script type="text/javascript" src="js/commitsService.js"></script>
<script type="text/javascript" src="js/optionsController.js"></script>
</head>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommitDto> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
<Compile Include="ServiceLocator.cs" />
<Compile Include="Services\AppSettings.cs" />
<Compile Include="Services\IAppSettings.cs" />
<Compile Include="Services\ISafeExecutor.cs" />
<Compile Include="Services\SafeExecutor.cs" />
<Compile Include="SvnService.svc.cs">
<DependentUpon>SvnService.svc</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,6 +19,7 @@ private ServiceLocator()
{
Container = new UnityContainer();
RegisterRepostiories(Container);
RegisterServices(Container);
}

/// <summary>
Expand All @@ -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<IRepoService, SvnRepositoryService>(new ContainerControlledLifetimeManager());
container.RegisterType<IRepoService, MockRepository>("Mock", new ContainerControlledLifetimeManager());
}

private static void RegisterServices(IUnityContainer container)
{
container.RegisterType<ISafeExecutor, SafeExecutor>(new ContainerControlledLifetimeManager());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Jira.Extension.RepositoryApi.Services
{
public interface ISafeExecutor
{
/// <summary>
/// Execute action and absorb any errors.
/// </summary>
/// <param name="action"></param>
/// <returns>True if action was successfully executed otherwise return false.</returns>
bool TryExecute(Action action);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -48,5 +51,11 @@ public List<CommitDto> 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));
}
}
}

0 comments on commit cecbd49

Please sign in to comment.