From 4029e0149165ac0059efc0305f7257039a479a1f Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 12 Jan 2017 21:02:07 +0100 Subject: [PATCH] Improved Jira code with the latest state of Dapplo.Jira [skip ci] --- GreenshotJiraPlugin/AsyncMemoryCache.cs | 2 +- .../GreenshotJiraPlugin.csproj | 27 ++-- GreenshotJiraPlugin/JiraConnector.cs | 23 ++-- GreenshotJiraPlugin/JiraMonitor.cs | 5 +- GreenshotJiraPlugin/JiraPlugin.cs | 2 +- GreenshotJiraPlugin/Log4NetLogger.cs | 2 +- .../SvgBitmapHttpContentConverter.cs | 126 ------------------ GreenshotJiraPlugin/packages.config | 8 +- 8 files changed, 37 insertions(+), 158 deletions(-) delete mode 100644 GreenshotJiraPlugin/SvgBitmapHttpContentConverter.cs diff --git a/GreenshotJiraPlugin/AsyncMemoryCache.cs b/GreenshotJiraPlugin/AsyncMemoryCache.cs index f7d029750..479bbbd20 100644 --- a/GreenshotJiraPlugin/AsyncMemoryCache.cs +++ b/GreenshotJiraPlugin/AsyncMemoryCache.cs @@ -29,7 +29,7 @@ using System; using System.Runtime.Caching; using System.Threading; using System.Threading.Tasks; -using Dapplo.Log.Facade; +using Dapplo.Log; #endregion diff --git a/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj b/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj index fe5838095..7defad044 100644 --- a/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj +++ b/GreenshotJiraPlugin/GreenshotJiraPlugin.csproj @@ -34,32 +34,42 @@ - - ..\packages\Dapplo.HttpExtensions.0.5.43\lib\net45\Dapplo.HttpExtensions.dll + + ..\packages\Dapplo.HttpExtensions.0.6.8\lib\net45\Dapplo.HttpExtensions.dll True - - ..\packages\Dapplo.Jira.0.1.65\lib\net45\Dapplo.Jira.dll + + ..\packages\Dapplo.Jira.0.3.1\lib\net45\Dapplo.Jira.dll True - - ..\packages\Dapplo.Log.Facade.0.5.4\lib\net45\Dapplo.Log.Facade.dll + + ..\packages\Dapplo.Log.1.0.22\lib\net45\Dapplo.Log.dll True ..\Greenshot\Lib\log4net.dll - - ..\packages\Svg.2.2.2\lib\net35\Svg.dll + + + + + ..\packages\Svg.2.3.0\lib\net35\Svg.dll True + + + + + + + @@ -94,7 +104,6 @@ - Always diff --git a/GreenshotJiraPlugin/JiraConnector.cs b/GreenshotJiraPlugin/JiraConnector.cs index ee9e98a2f..0aec2884d 100644 --- a/GreenshotJiraPlugin/JiraConnector.cs +++ b/GreenshotJiraPlugin/JiraConnector.cs @@ -24,12 +24,13 @@ using System; using System.Collections.Generic; using System.Drawing; using System.IO; -using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using Dapplo.HttpExtensions; +using Dapplo.HttpExtensions.Extensions; using Dapplo.Jira; +using Dapplo.Jira.Converters; using Dapplo.Jira.Entities; using Greenshot.IniFile; using GreenshotPlugin.Core; @@ -49,25 +50,17 @@ namespace GreenshotJiraPlugin { private readonly int _timeout; private JiraApi _jiraApi; private IssueTypeBitmapCache _issueTypeBitmapCache; - private static readonly SvgBitmapHttpContentConverter SvgBitmapHttpContentConverterInstance = new SvgBitmapHttpContentConverter(); /// /// Initialize some basic stuff, in the case the SVG to bitmap converter /// static JiraConnector() { - if (HttpExtensionsGlobals.HttpContentConverters.All(x => x.GetType() != typeof(SvgBitmapHttpContentConverter))) - { - HttpExtensionsGlobals.HttpContentConverters.Add(SvgBitmapHttpContentConverterInstance); - } - SvgBitmapHttpContentConverterInstance.Width = CoreConfig.IconSize.Width; - SvgBitmapHttpContentConverterInstance.Height = CoreConfig.IconSize.Height; CoreConfig.PropertyChanged += (sender, args) => { if (args.PropertyName == nameof(CoreConfig.IconSize)) { - SvgBitmapHttpContentConverterInstance.Width = CoreConfig.IconSize.Width; - SvgBitmapHttpContentConverterInstance.Height = CoreConfig.IconSize.Height; + JiraPlugin.Instance.JiraConnector._jiraApi?.Behaviour.SetConfig(new SvgConfiguration { Width = CoreConfig.IconSize.Width, Height = CoreConfig.IconSize.Height }); } }; @@ -111,6 +104,8 @@ namespace GreenshotJiraPlugin { return false; } _jiraApi = new JiraApi(new Uri(JiraConfig.Url)); + _jiraApi.Behaviour.SetConfig(new SvgConfiguration { Width = CoreConfig.IconSize.Width, Height = CoreConfig.IconSize.Height }); + _issueTypeBitmapCache = new IssueTypeBitmapCache(_jiraApi); LoginInfo loginInfo; try @@ -226,7 +221,7 @@ namespace GreenshotJiraPlugin { await CheckCredentialsAsync(); try { - return await _jiraApi.GetIssueAsync(issueKey).ConfigureAwait(false); + return await _jiraApi.Issue.GetAsync(issueKey).ConfigureAwait(false); } catch { @@ -248,7 +243,7 @@ namespace GreenshotJiraPlugin { { content.WriteToStream(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); - await _jiraApi.AttachAsync(issueKey, memoryStream, content.Filename, content.ContentType, cancellationToken).ConfigureAwait(false); + await _jiraApi.Attachment.AttachAsync(issueKey, memoryStream, content.Filename, content.ContentType, cancellationToken).ConfigureAwait(false); } } @@ -262,7 +257,7 @@ namespace GreenshotJiraPlugin { public async Task AddCommentAsync(string issueKey, string body, string visibility = null, CancellationToken cancellationToken = default(CancellationToken)) { await CheckCredentialsAsync(); - await _jiraApi.AddCommentAsync(issueKey, body, visibility, cancellationToken).ConfigureAwait(false); + await _jiraApi.Issue.AddCommentAsync(issueKey, body, visibility, cancellationToken).ConfigureAwait(false); } /// @@ -274,7 +269,7 @@ namespace GreenshotJiraPlugin { public async Task> SearchAsync(Filter filter, CancellationToken cancellationToken = default(CancellationToken)) { await CheckCredentialsAsync(); - var searchResult = await _jiraApi.SearchAsync(filter.Jql, 20, new[] { "summary", "reporter", "assignee", "created", "issuetype" }, cancellationToken).ConfigureAwait(false); + var searchResult = await _jiraApi.Issue.SearchAsync(filter.Jql, 20, new[] { "summary", "reporter", "assignee", "created", "issuetype" }, cancellationToken).ConfigureAwait(false); return searchResult.Issues; } diff --git a/GreenshotJiraPlugin/JiraMonitor.cs b/GreenshotJiraPlugin/JiraMonitor.cs index 81605bddf..7bea19b11 100644 --- a/GreenshotJiraPlugin/JiraMonitor.cs +++ b/GreenshotJiraPlugin/JiraMonitor.cs @@ -26,7 +26,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Dapplo.Jira; -using Dapplo.Log.Facade; +using Dapplo.Log; using GreenshotJiraPlugin.Hooking; namespace GreenshotJiraPlugin @@ -45,6 +45,7 @@ namespace GreenshotJiraPlugin private readonly IList _jiraInstances = new List(); private readonly IDictionary _projectJiraApiMap = new Dictionary(); private readonly int _maxEntries; + // TODO: Add issues from issueHistory (JQL -> Where.IssueKey.InIssueHistory()) private IDictionary _recentJiras = new Dictionary(); /// @@ -144,7 +145,7 @@ namespace GreenshotJiraPlugin JiraApi jiraApi; if (_projectJiraApiMap.TryGetValue(jiraDetails.ProjectKey, out jiraApi)) { - var issue = await jiraApi.GetIssueAsync(jiraDetails.JiraKey).ConfigureAwait(false); + var issue = await jiraApi.Issue.GetAsync(jiraDetails.JiraKey).ConfigureAwait(false); jiraDetails.JiraIssue = issue; } // Send event diff --git a/GreenshotJiraPlugin/JiraPlugin.cs b/GreenshotJiraPlugin/JiraPlugin.cs index a7240293e..24969d25f 100644 --- a/GreenshotJiraPlugin/JiraPlugin.cs +++ b/GreenshotJiraPlugin/JiraPlugin.cs @@ -24,7 +24,7 @@ using Greenshot.IniFile; using Greenshot.Plugin; using System; using System.Threading.Tasks; -using Dapplo.Log.Facade; +using Dapplo.Log; using GreenshotJiraPlugin.Forms; using GreenshotPlugin.Core; using log4net; diff --git a/GreenshotJiraPlugin/Log4NetLogger.cs b/GreenshotJiraPlugin/Log4NetLogger.cs index 1b7ce2661..3018a8823 100644 --- a/GreenshotJiraPlugin/Log4NetLogger.cs +++ b/GreenshotJiraPlugin/Log4NetLogger.cs @@ -19,7 +19,7 @@ * along with this program. If not, see . */ -using Dapplo.Log.Facade; +using Dapplo.Log; using log4net; namespace GreenshotJiraPlugin diff --git a/GreenshotJiraPlugin/SvgBitmapHttpContentConverter.cs b/GreenshotJiraPlugin/SvgBitmapHttpContentConverter.cs deleted file mode 100644 index 47b3a24d1..000000000 --- a/GreenshotJiraPlugin/SvgBitmapHttpContentConverter.cs +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2016 Thomas Braun, Jens Klingen, Robin Krom - * - * For more information see: http://getgreenshot.org/ - * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -using System; -using System.Collections.Generic; -using System.Drawing; -using System.IO; -using System.Threading; -using System.Threading.Tasks; -using Dapplo.HttpExtensions; -using Dapplo.HttpExtensions.ContentConverter; -using Dapplo.HttpExtensions.Extensions; -using Dapplo.HttpExtensions.Support; -using Dapplo.Log.Facade; -using System.Net.Http; -using System.Net.Http.Headers; - -namespace GreenshotJiraPlugin -{ - /// - /// This adds SVG image support for the Jira Plugin - /// - public class SvgBitmapHttpContentConverter : IHttpContentConverter - { - private static readonly LogSource Log = new LogSource(); - private static readonly IList SupportedContentTypes = new List(); - - static SvgBitmapHttpContentConverter() - { - SupportedContentTypes.Add(MediaTypes.Svg.EnumValueOf()); - } - - /// - public int Order => 0; - - public int Width { get; set; } - - public int Height { get; set; } - - /// - /// This checks if the HttpContent can be converted to a Bitmap and is assignable to the specified Type - /// - /// This should be something we can assign Bitmap to - /// HttpContent to process - /// true if it can convert - public bool CanConvertFromHttpContent(Type typeToConvertTo, HttpContent httpContent) - { - if (typeToConvertTo == typeof(object) || !typeToConvertTo.IsAssignableFrom(typeof(Bitmap))) - { - return false; - } - var httpBehaviour = HttpBehaviour.Current; - return !httpBehaviour.ValidateResponseContentType || SupportedContentTypes.Contains(httpContent.GetContentType()); - } - - /// - public async Task ConvertFromHttpContentAsync(Type resultType, HttpContent httpContent, CancellationToken cancellationToken = default(CancellationToken)) - { - if (!CanConvertFromHttpContent(resultType, httpContent)) - { - var exMessage = "CanConvertFromHttpContent resulted in false, ConvertFromHttpContentAsync is not supposed to be called."; - Log.Error().WriteLine(exMessage); - throw new NotSupportedException(exMessage); - } - using (var memoryStream = (MemoryStream) await StreamHttpContentConverter.Instance.ConvertFromHttpContentAsync(typeof(MemoryStream), httpContent, cancellationToken).ConfigureAwait(false)) - { - Log.Debug().WriteLine("Creating a Bitmap from the SVG."); - var svgImage = new SvgImage(memoryStream) - { - Height = Height, - Width = Width - }; - return svgImage.Image; - } - } - - /// - public bool CanConvertToHttpContent(Type typeToConvert, object content) - { - return false; - } - - /// - public HttpContent ConvertToHttpContent(Type typeToConvert, object content) - { - return null; - } - - /// - public void AddAcceptHeadersForType(Type resultType, HttpRequestMessage httpRequestMessage) - { - if (resultType == null) - { - throw new ArgumentNullException(nameof(resultType)); - } - if (httpRequestMessage == null) - { - throw new ArgumentNullException(nameof(httpRequestMessage)); - } - if (resultType == typeof(object) || !resultType.IsAssignableFrom(typeof(Bitmap))) - { - return; - } - httpRequestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(MediaTypes.Svg.EnumValueOf())); - Log.Debug().WriteLine("Modified the header(s) of the HttpRequestMessage: Accept: {0}", httpRequestMessage.Headers.Accept); - } - } -} diff --git a/GreenshotJiraPlugin/packages.config b/GreenshotJiraPlugin/packages.config index fade6b5dd..38a68ba7c 100644 --- a/GreenshotJiraPlugin/packages.config +++ b/GreenshotJiraPlugin/packages.config @@ -1,8 +1,8 @@  - - - + + + - + \ No newline at end of file