diff --git a/VersionOne.JiraConnector/IJiraConnector.cs b/VersionOne.JiraConnector/IJiraConnector.cs index e02551c..8563adf 100644 --- a/VersionOne.JiraConnector/IJiraConnector.cs +++ b/VersionOne.JiraConnector/IJiraConnector.cs @@ -1,9 +1,12 @@ /*(c) Copyright 2012, VersionOne, Inc. All rights reserved. (c)*/ using System.Collections.Generic; -namespace VersionOne.JiraConnector { +namespace VersionOne.JiraConnector +{ // TODO impl IDisposable when login token in SOAP connector is properly encapsulated? - public interface IJiraConnector { + public interface IJiraConnector + { + bool Validate(); void Login(); void Logout(); Issue[] GetIssuesFromFilter(string issueFilterId); diff --git a/VersionOne.JiraConnector/Rest/JiraRestProxy.cs b/VersionOne.JiraConnector/Rest/JiraRestProxy.cs index f0103ad..4a19e3e 100644 --- a/VersionOne.JiraConnector/Rest/JiraRestProxy.cs +++ b/VersionOne.JiraConnector/Rest/JiraRestProxy.cs @@ -28,6 +28,20 @@ public JiraRestProxy(string baseUrl, string username, string password) } } + public bool Validate() + { + var request = new RestRequest + { + Method = Method.GET, + Resource = "myself", + RequestFormat = DataFormat.Json + }; + + var response = client.Execute(request); + + return response.StatusCode.Equals(HttpStatusCode.OK); + } + public void Login() { //throw new NotImplementedException(); diff --git a/VersionOne.JiraConnector/Soap/JiraSoapProxy.cs b/VersionOne.JiraConnector/Soap/JiraSoapProxy.cs index d90c674..e38e343 100644 --- a/VersionOne.JiraConnector/Soap/JiraSoapProxy.cs +++ b/VersionOne.JiraConnector/Soap/JiraSoapProxy.cs @@ -5,64 +5,96 @@ using VersionOne.JiraConnector.Exceptions; using System.Web.Services.Protocols; -namespace VersionOne.JiraConnector.Soap { - public class JiraSoapProxy : IJiraConnector { +namespace VersionOne.JiraConnector.Soap +{ + public class JiraSoapProxy : IJiraConnector + { private readonly string url; private readonly string username; private readonly string password; private readonly JiraSoapService soapService; - + // TODO use State private string loginToken; - public JiraSoapProxy(string url, string username, string password) { + public JiraSoapProxy(string url, string username, string password) + { this.url = url; this.username = username; this.password = password; - soapService = new JiraSoapService {Url = url}; + soapService = new JiraSoapService { Url = url }; + } + + public bool Validate() + { + try + { + Login(); + Logout(); + return true; + } + catch (Exception) + { + // TODO too generic exception type, probably should be changed to JiraLoginException; but how do we handle cases with invalid URL? + return false; + } } - public void Login() { + public void Login() + { loginToken = soapService.login(username, password); - if(string.IsNullOrEmpty(loginToken)) { + if (string.IsNullOrEmpty(loginToken)) + { throw new JiraLoginException(); } } - public Issue[] GetIssuesFromFilter(string issueFilterId) { + public Issue[] GetIssuesFromFilter(string issueFilterId) + { var remoteIssues = soapService.getIssuesFromFilter(loginToken, issueFilterId); return remoteIssues.Select(remoteIssue => CreateIssue(remoteIssue)).ToArray(); } - public Issue UpdateIssue(string issueKey, string fieldName, string fieldValue) { - try { - var remoteFieldValue = new RemoteFieldValue {id = fieldName, values = new[] {fieldValue}}; - return CreateIssue(soapService.updateIssue(loginToken, issueKey, new[] {remoteFieldValue})); - } catch(SoapException ex) { + public Issue UpdateIssue(string issueKey, string fieldName, string fieldValue) + { + try + { + var remoteFieldValue = new RemoteFieldValue { id = fieldName, values = new[] { fieldValue } }; + return CreateIssue(soapService.updateIssue(loginToken, issueKey, new[] { remoteFieldValue })); + } + catch (SoapException ex) + { ProcessException(ex); throw; } } - public void AddComment(string issueKey, string comment) { - var remoteComment = new RemoteComment {body = comment}; + public void AddComment(string issueKey, string comment) + { + var remoteComment = new RemoteComment { body = comment }; soapService.addComment(loginToken, issueKey, remoteComment); } - public void ProgressWorkflow(string issueKey, string action, string assignee) { - if(assignee != null) { - var assigneeField = new RemoteFieldValue {id = "assignee", values = new[] {assignee}}; - soapService.progressWorkflowAction(loginToken, issueKey, action, new[] {assigneeField}); - } else { - soapService.progressWorkflowAction(loginToken, issueKey, action, new RemoteFieldValue[] {}); + public void ProgressWorkflow(string issueKey, string action, string assignee) + { + if (assignee != null) + { + var assigneeField = new RemoteFieldValue { id = "assignee", values = new[] { assignee } }; + soapService.progressWorkflowAction(loginToken, issueKey, action, new[] { assigneeField }); + } + else + { + soapService.progressWorkflowAction(loginToken, issueKey, action, new RemoteFieldValue[] { }); } } - private Issue CreateIssue(RemoteIssue remote) { - var result = new Issue { + private Issue CreateIssue(RemoteIssue remote) + { + var result = new Issue + { Summary = remote.summary, Description = remote.description, Project = GetProjectNameFromKey(remote.project), @@ -76,51 +108,63 @@ private Issue CreateIssue(RemoteIssue remote) { return result; } - private string GetProjectNameFromKey(string projectKey) { + private string GetProjectNameFromKey(string projectKey) + { var remoteProject = soapService.getProjectByKey(loginToken, projectKey); return remoteProject.name; } - public IList GetPriorities() { + public IList GetPriorities() + { var remotePriorities = soapService.getPriorities(loginToken); return remotePriorities.Select(remotePriority => new Item(remotePriority.id, remotePriority.name)).ToList(); } - public IList GetProjects() { + public IList GetProjects() + { var remoteProjects = soapService.getProjects(loginToken); return remoteProjects.Select(remoteProject => new Item(remoteProject.id, remoteProject.name)).ToList(); } - public void Logout() { + public void Logout() + { soapService.logout(loginToken); loginToken = null; } - public IEnumerable GetAvailableActions(string issueId) { + public IEnumerable GetAvailableActions(string issueId) + { var remoteActions = soapService.getAvailableActions(loginToken, issueId); return remoteActions.Select(x => new Item(x.id, x.name)).ToList(); } - public IEnumerable GetCustomFields() { - try { + public IEnumerable GetCustomFields() + { + try + { var removeFields = soapService.getCustomFields(loginToken); return removeFields.Select(removeField => new Item(removeField.id, removeField.name)).ToList(); - } catch(SoapException ex) { + } + catch (SoapException ex) + { ProcessException(ex); throw; } } //TODO: if it's possible - find better way to process remote exception from JIRA - private static void ProcessException(SoapException exception) { + private static void ProcessException(SoapException exception) + { var remoteMessages = exception.Message.Split(':'); var message = remoteMessages.Count() > 1 ? String.Join(" ", remoteMessages, 1, remoteMessages.Count() - 1) : exception.Message; - - if (exception.Message.Contains("RemotePermissionException")) { + + if (exception.Message.Contains("RemotePermissionException")) + { throw new JiraPermissionException(message.Trim(), exception); } - - if( exception.Message.Contains("RemoteValidationException") ) { + + if (exception.Message.Contains("RemoteValidationException")) + { throw new JiraValidationException(message.Trim(), exception); } } diff --git a/VersionOne.ServiceHost.ConfigurationTool/DL/JiraConnectionValidator.cs b/VersionOne.ServiceHost.ConfigurationTool/DL/JiraConnectionValidator.cs index 2ca6e54..a5e1fd5 100644 --- a/VersionOne.ServiceHost.ConfigurationTool/DL/JiraConnectionValidator.cs +++ b/VersionOne.ServiceHost.ConfigurationTool/DL/JiraConnectionValidator.cs @@ -2,25 +2,21 @@ using VersionOne.JiraConnector; using VersionOne.ServiceHost.ConfigurationTool.Entities; -namespace VersionOne.ServiceHost.ConfigurationTool.DL { - public class JiraConnectionValidator : IConnectionValidator { +namespace VersionOne.ServiceHost.ConfigurationTool.DL +{ + public class JiraConnectionValidator : IConnectionValidator + { private readonly JiraServiceEntity entity; - public JiraConnectionValidator(JiraServiceEntity entity) { + public JiraConnectionValidator(JiraServiceEntity entity) + { this.entity = entity; } - public bool Validate() { + public bool Validate() + { var proxy = new JiraConnectorFactory(JiraConnectorType.Rest).Create(entity.Url, entity.UserName, entity.Password); - - try { - proxy.Login(); - proxy.Logout(); - return true; - } catch(Exception) { - // TODO too generic exception type, probably should be changed to JiraLoginException; but how do we handle cases with invalid URL? - return false; - } + return proxy.Validate(); } } } \ No newline at end of file