From 0a3ce0b38e07407f211284ee13a61dc2a0df9c53 Mon Sep 17 00:00:00 2001 From: "pierre@audrey" Date: Wed, 31 Jan 2024 22:36:59 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E2=9E=95=20IJiraClient.GetIssueWorklogs()?= =?UTF-8?q?=20overload=20for=20a=20single=20issueKey?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwl.jira/IJiraClient.cs | 2 + jwl.jira/JiraWithICTimePluginApi.cs | 5 + jwl.jira/JiraWithTempoPluginApi.cs | 339 ++++++++++++++-------------- jwl.jira/VanillaJiraClient.cs | 67 ++++-- 4 files changed, 221 insertions(+), 192 deletions(-) diff --git a/jwl.jira/IJiraClient.cs b/jwl.jira/IJiraClient.cs index 9fa6adb..429081b 100644 --- a/jwl.jira/IJiraClient.cs +++ b/jwl.jira/IJiraClient.cs @@ -11,6 +11,8 @@ public interface IJiraClient Task GetAvailableActivities(); + Task GetIssueWorklogs(DateOnly from, DateOnly to, string issueKey); + Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys); Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? activity, string? comment); diff --git a/jwl.jira/JiraWithICTimePluginApi.cs b/jwl.jira/JiraWithICTimePluginApi.cs index e55ccd1..dc1adbd 100644 --- a/jwl.jira/JiraWithICTimePluginApi.cs +++ b/jwl.jira/JiraWithICTimePluginApi.cs @@ -36,6 +36,11 @@ public async Task GetAvailableActivities() return await _vanillaJiraApi.GetAvailableActivities(); } + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, string issueKey) + { + return await _vanillaJiraApi.GetIssueWorklogs(from, to, issueKey); + } + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) { return await _vanillaJiraApi.GetIssueWorklogs(from, to, issueKeys); diff --git a/jwl.jira/JiraWithTempoPluginApi.cs b/jwl.jira/JiraWithTempoPluginApi.cs index b85fb0a..f1b4e55 100644 --- a/jwl.jira/JiraWithTempoPluginApi.cs +++ b/jwl.jira/JiraWithTempoPluginApi.cs @@ -1,171 +1,174 @@ -namespace jwl.jira; -using System.Net.Http.Json; -using jwl.infra; -using jwl.jira.api.rest.common; - -// https://www.tempo.io/server-api-documentation/timesheets -public class JiraWithTempoPluginApi - : IJiraClient -{ - private const string WorklogTypeAttributeKey = @"_WorklogType_"; - - private readonly HttpClient _httpClient; - private readonly VanillaJiraClient _vanillaJiraServerApi; - - public string UserName { get; } - - public JiraWithTempoPluginApi(HttpClient httpClient, string userName) - { - _httpClient = httpClient; - _vanillaJiraServerApi = new VanillaJiraClient(httpClient, userName); - - UserName = userName; - } - - public async Task GetUserInfo() - { - return await _vanillaJiraServerApi.GetUserInfo(); - } - - public async Task GetWorklogAttributeDefinitions() - { - return await _httpClient.GetAsJsonAsync(@"rest/tempo-core/1/work-attribute"); - } - - public async Task GetAvailableActivities() - { - api.rest.response.TempoWorklogAttributeDefinition[] attrEnumDefs = await GetWorklogAttributeDefinitions(); - - var result = attrEnumDefs - .Where(attrDef => attrDef.Key?.Equals(WorklogTypeAttributeKey) ?? false) - .Where(attrDef => attrDef.Type != null - && attrDef.Type?.Value == TempoWorklogAttributeTypeIdentifier.StaticList - ) - .SelectMany(attrDef => attrDef.StaticListValues) - .Where(staticListItem => !string.IsNullOrEmpty(staticListItem.Name) && !string.IsNullOrEmpty(staticListItem.Value)) - .Select(staticListItem => new WorkLogType( - Key: staticListItem.Name ?? string.Empty, - Value: staticListItem.Value ?? string.Empty, - Sequence: staticListItem.Sequence ?? -1 - )) - .ToArray(); - - return result; - } - - public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) - { - JiraUserInfo userInfo = await GetUserInfo(); - string userKey = userInfo.Key ?? throw new ArgumentNullException($"{nameof(userInfo)}.{nameof(userInfo.Key)}"); - - var request = new api.rest.request.TempoFindWorklogs(from, to) - { - IssueKey = issueKeys?.ToArray(), - UserKey = new string[] { userKey } - }; - var response = await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs/search", request); - var tempoWorkLogs = await HttpClientJsonExt.DeserializeJsonStreamAsync(await response.Content.ReadAsStreamAsync()); - - var result = tempoWorkLogs - .Select(wl => new WorkLog( - Id: wl.Id ?? -1, - IssueId: wl.IssueId ?? -1, - AuthorName: wl.WorkerKey == userKey ? UserName : null, - AuthorKey: wl.WorkerKey, - Created: wl.Created?.Value ?? DateTime.MinValue, - Started: wl.Started?.Value ?? DateTime.MinValue, - TimeSpentSeconds: wl.TimeSpentSeconds ?? -1, - Activity: wl.Attributes?[WorklogTypeAttributeKey].Value, - Comment: wl.Comment ?? string.Empty - )) - .ToArray(); - - return result; - } - - public async Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? activity, string? comment) - { - await AddWorklogPeriod(issueKey, day, day, timeSpentSeconds, activity, comment); - } - - public async Task AddWorklogPeriod(string issueKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? tempoWorklogType, string? comment, bool includeNonWorkingDays = false) - { - JiraUserInfo userInfo = await GetUserInfo(); - string userKey = userInfo.Key ?? throw new ArgumentNullException($"{nameof(userInfo)}.{nameof(userInfo.Key)}"); - - var request = new api.rest.request.TempoAddWorklogByIssueKey() - { - IssueKey = issueKey, - Worker = userKey, - Started = new api.rest.common.TempoDate(dayFrom), - EndDate = new api.rest.common.TempoDate(dayTo), - TimeSpentSeconds = timeSpentSeconds, - BillableSeconds = timeSpentSeconds, - IncludeNonWorkingDays = includeNonWorkingDays, - Comment = comment, - Attributes = new Dictionary() - { - [WorklogTypeAttributeKey] = new api.rest.common.TempoWorklogAttribute() - { - WorkAttributeId = 1, - Key = WorklogTypeAttributeKey, - Name = @"Worklog Type", - Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList, - Value = tempoWorklogType - } - } - }; - - HttpResponseMessage response = await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs", request); - await VanillaJiraClient.CheckHttpResponseForErrorMessages(response); - } - - public async Task DeleteWorklog(long issueId, long worklogId, bool notifyUsers = false) - { - UriBuilder uriBuilder = new UriBuilder() - { - Path = new UriPathBuilder(@"rest/tempo-timesheets/4/worklogs") - .Add(worklogId.ToString()) - }; - +namespace jwl.jira; +using System.Net.Http.Json; +using jwl.infra; +using jwl.jira.api.rest.common; + +// https://www.tempo.io/server-api-documentation/timesheets +public class JiraWithTempoPluginApi + : IJiraClient +{ + private const string WorklogTypeAttributeKey = @"_WorklogType_"; + + private readonly HttpClient _httpClient; + private readonly VanillaJiraClient _vanillaJiraServerApi; + + public string UserName { get; } + + public JiraWithTempoPluginApi(HttpClient httpClient, string userName) + { + _httpClient = httpClient; + _vanillaJiraServerApi = new VanillaJiraClient(httpClient, userName); + + UserName = userName; + } + + public async Task GetUserInfo() + { + return await _vanillaJiraServerApi.GetUserInfo(); + } + + public async Task GetWorklogAttributeDefinitions() + { + return await _httpClient.GetAsJsonAsync(@"rest/tempo-core/1/work-attribute"); + } + + public async Task GetAvailableActivities() + { + api.rest.response.TempoWorklogAttributeDefinition[] attrEnumDefs = await GetWorklogAttributeDefinitions(); + + var result = attrEnumDefs + .Where(attrDef => attrDef.Key?.Equals(WorklogTypeAttributeKey) ?? false) + .Where(attrDef => attrDef.Type != null + && attrDef.Type?.Value == TempoWorklogAttributeTypeIdentifier.StaticList + ) + .SelectMany(attrDef => attrDef.StaticListValues) + .Where(staticListItem => !string.IsNullOrEmpty(staticListItem.Name) && !string.IsNullOrEmpty(staticListItem.Value)) + .Select(staticListItem => new WorkLogType( + Key: staticListItem.Name ?? string.Empty, + Value: staticListItem.Value ?? string.Empty, + Sequence: staticListItem.Sequence ?? -1 + )) + .ToArray(); + + return result; + } + + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, string issueKey) + { + return await GetIssueWorklogs(from, to, new string[] { issueKey }); + } + + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) + { + string userKey = _vanillaJiraServerApi.UserInfo.Key ?? throw new ArgumentNullException($"{nameof(_vanillaJiraServerApi.UserInfo)}.{nameof(_vanillaJiraServerApi.UserInfo.Key)}"); + + var request = new api.rest.request.TempoFindWorklogs(from, to) + { + IssueKey = issueKeys?.ToArray(), + UserKey = new string[] { userKey } + }; + var response = await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs/search", request); + var tempoWorkLogs = await HttpClientJsonExt.DeserializeJsonStreamAsync(await response.Content.ReadAsStreamAsync()); + + var result = tempoWorkLogs + .Select(wl => new WorkLog( + Id: wl.Id ?? -1, + IssueId: wl.IssueId ?? -1, + AuthorName: wl.WorkerKey == userKey ? UserName : null, + AuthorKey: wl.WorkerKey, + Created: wl.Created?.Value ?? DateTime.MinValue, + Started: wl.Started?.Value ?? DateTime.MinValue, + TimeSpentSeconds: wl.TimeSpentSeconds ?? -1, + Activity: wl.Attributes?[WorklogTypeAttributeKey].Value, + Comment: wl.Comment ?? string.Empty + )) + .ToArray(); + + return result; + } + + public async Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? activity, string? comment) + { + await AddWorklogPeriod(issueKey, day, day, timeSpentSeconds, activity, comment); + } + + public async Task AddWorklogPeriod(string issueKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? tempoWorklogType, string? comment, bool includeNonWorkingDays = false) + { + string userKey = _vanillaJiraServerApi.UserInfo.Key ?? throw new ArgumentNullException($"{nameof(_vanillaJiraServerApi.UserInfo)}.{nameof(_vanillaJiraServerApi.UserInfo.Key)}"); + + var request = new api.rest.request.TempoAddWorklogByIssueKey() + { + IssueKey = issueKey, + Worker = userKey, + Started = new api.rest.common.TempoDate(dayFrom), + EndDate = new api.rest.common.TempoDate(dayTo), + TimeSpentSeconds = timeSpentSeconds, + BillableSeconds = timeSpentSeconds, + IncludeNonWorkingDays = includeNonWorkingDays, + Comment = comment, + Attributes = new Dictionary() + { + [WorklogTypeAttributeKey] = new api.rest.common.TempoWorklogAttribute() + { + WorkAttributeId = 1, + Key = WorklogTypeAttributeKey, + Name = @"Worklog Type", + Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList, + Value = tempoWorklogType + } + } + }; + + HttpResponseMessage response = await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs", request); + await VanillaJiraClient.CheckHttpResponseForErrorMessages(response); + } + + public async Task DeleteWorklog(long issueId, long worklogId, bool notifyUsers = false) + { + UriBuilder uriBuilder = new UriBuilder() + { + Path = new UriPathBuilder(@"rest/tempo-timesheets/4/worklogs") + .Add(worklogId.ToString()) + }; + HttpResponseMessage response = await _httpClient.DeleteAsync(uriBuilder.Uri.PathAndQuery); - await VanillaJiraClient.CheckHttpResponseForErrorMessages(response); - } - - public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, int timeSpentSeconds, string? activity, string? comment) - { - await UpdateWorklogPeriod(issueKey, worklogId, day, day, timeSpentSeconds, comment, activity); - } - - private async Task UpdateWorklogPeriod(string issueKey, long worklogId, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? comment, string? activity, bool includeNonWorkingDays = false) - { - UriBuilder uriBuilder = new UriBuilder() - { - Path = new UriPathBuilder(@"rest/tempo-timesheets/4/worklogs") - .Add(worklogId.ToString()) - }; - var request = new api.rest.request.TempoUpdateWorklog() - { - Started = new api.rest.common.TempoDate(dayFrom), - EndDate = new api.rest.common.TempoDate(dayTo), - TimeSpentSeconds = timeSpentSeconds, - BillableSeconds = timeSpentSeconds, - IncludeNonWorkingDays = includeNonWorkingDays, - Comment = comment, - Attributes = new Dictionary() - { - [WorklogTypeAttributeKey] = new api.rest.common.TempoWorklogAttribute() - { - WorkAttributeId = 1, - Key = WorklogTypeAttributeKey, - Name = @"Worklog Type", - Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList, - Value = activity - } - } - }; - + await VanillaJiraClient.CheckHttpResponseForErrorMessages(response); + } + + public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, int timeSpentSeconds, string? activity, string? comment) + { + await UpdateWorklogPeriod(issueKey, worklogId, day, day, timeSpentSeconds, comment, activity); + } + + private async Task UpdateWorklogPeriod(string issueKey, long worklogId, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? comment, string? activity, bool includeNonWorkingDays = false) + { + UriBuilder uriBuilder = new UriBuilder() + { + Path = new UriPathBuilder(@"rest/tempo-timesheets/4/worklogs") + .Add(worklogId.ToString()) + }; + var request = new api.rest.request.TempoUpdateWorklog() + { + Started = new api.rest.common.TempoDate(dayFrom), + EndDate = new api.rest.common.TempoDate(dayTo), + TimeSpentSeconds = timeSpentSeconds, + BillableSeconds = timeSpentSeconds, + IncludeNonWorkingDays = includeNonWorkingDays, + Comment = comment, + Attributes = new Dictionary() + { + [WorklogTypeAttributeKey] = new api.rest.common.TempoWorklogAttribute() + { + WorkAttributeId = 1, + Key = WorklogTypeAttributeKey, + Name = @"Worklog Type", + Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList, + Value = activity + } + } + }; + HttpResponseMessage response = await _httpClient.PutAsJsonAsync(uriBuilder.Uri.PathAndQuery, request); await VanillaJiraClient.CheckHttpResponseForErrorMessages(response); - } -} + } +} diff --git a/jwl.jira/VanillaJiraClient.cs b/jwl.jira/VanillaJiraClient.cs index 1204540..7d7567d 100644 --- a/jwl.jira/VanillaJiraClient.cs +++ b/jwl.jira/VanillaJiraClient.cs @@ -13,13 +13,16 @@ public class VanillaJiraClient : IJiraClient { public string UserName { get; } + public api.rest.common.JiraUserInfo UserInfo => _lazyUserInfo.Value; private readonly HttpClient _httpClient; + private readonly Lazy _lazyUserInfo; public VanillaJiraClient(HttpClient httpClient, string userName) { _httpClient = httpClient; UserName = userName; + _lazyUserInfo = new Lazy(() => GetUserInfo().Result); } public static async Task CheckHttpResponseForErrorMessages(HttpResponseMessage responseMessage) @@ -36,13 +39,7 @@ public static async Task CheckHttpResponseForErrorMessages(HttpResponseMessage r public async Task GetUserInfo() { - UriBuilder uriBuilder = new UriBuilder() - { - Path = @"rest/api/2/user", - Query = new UriQueryBuilder() - .Add(@"username", UserName) - }; - return await _httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery); + return await GetUserInfo(_httpClient, UserName); } #pragma warning disable CS1998 @@ -52,28 +49,20 @@ public async Task GetAvailableActivities() } #pragma warning restore - public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, string issueKey) { - if (issueKeys is null) - return Array.Empty(); - - Task[] responseTasks = issueKeys - .Distinct() - .Select(issueKey => new UriBuilder() - { - Path = new UriPathBuilder(@"rest/api/2/issue") - .Add(issueKey) - .Add(@"worklog") - }) - .Select(uriBuilder => _httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery)) - .ToArray(); + UriBuilder uriBuilder = new UriBuilder() + { + Path = new UriPathBuilder(@"rest/api/2/issue") + .Add(issueKey) + .Add(@"worklog") + }; - await Task.WhenAll(responseTasks); + var response = await _httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery); (DateTime minDt, DateTime supDt) = DateOnlyUtils.DateOnlyRangeToDateTimeRange(from, to); - var result = responseTasks - .SelectMany(task => task.Result.Worklogs) + var result = response.Worklogs .Where(worklog => worklog.Author.Name == UserName) .Where(worklog => worklog.Started.Value >= minDt && worklog.Started.Value < supDt) .Select(wl => new WorkLog( @@ -92,6 +81,25 @@ public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnume return result; } + public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) + { + if (issueKeys is null) + return Array.Empty(); + + Task[] responseTasks = issueKeys + .Distinct() + .Select(issueKey => GetIssueWorklogs(from, to, issueKey)) + .ToArray(); + + await Task.WhenAll(responseTasks); + + var result = responseTasks + .SelectMany(task => task.Result) + .ToArray(); + + return result; + } + public async Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? activity, string? comment) { UriBuilder uriBuilder = new UriBuilder() @@ -177,4 +185,15 @@ public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, i HttpResponseMessage response = await _httpClient.PutAsJsonAsync(uriBuilder.Uri.PathAndQuery, request); await CheckHttpResponseForErrorMessages(response); } + + private static async Task GetUserInfo(HttpClient httpClient, string userName) + { + UriBuilder uriBuilder = new UriBuilder() + { + Path = @"rest/api/2/user", + Query = new UriQueryBuilder() + .Add(@"username", userName) + }; + return await httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery); + } } From 6e3140efa3ef24067fed9dc5f591f37220ae4b87 Mon Sep 17 00:00:00 2001 From: "pierre@audrey" Date: Wed, 31 Jan 2024 22:43:49 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=9B=A0=20refactor:=20IJiraClient.GetU?= =?UTF-8?q?serInfo()=20method=20removed=20in=20favour=20of=20(delayed)=20I?= =?UTF-8?q?JiraClient.UserInfo=20property?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwl.jira/IJiraClient.cs | 2 +- jwl.jira/JiraWithICTimePluginApi.cs | 9 ++------- jwl.jira/JiraWithTempoPluginApi.cs | 15 +++++---------- jwl.jira/VanillaJiraClient.cs | 11 +++-------- 4 files changed, 11 insertions(+), 26 deletions(-) diff --git a/jwl.jira/IJiraClient.cs b/jwl.jira/IJiraClient.cs index 429081b..526803e 100644 --- a/jwl.jira/IJiraClient.cs +++ b/jwl.jira/IJiraClient.cs @@ -7,7 +7,7 @@ public interface IJiraClient { - Task GetUserInfo(); + api.rest.common.JiraUserInfo UserInfo { get; } Task GetAvailableActivities(); diff --git a/jwl.jira/JiraWithICTimePluginApi.cs b/jwl.jira/JiraWithICTimePluginApi.cs index dc1adbd..dfd6c09 100644 --- a/jwl.jira/JiraWithICTimePluginApi.cs +++ b/jwl.jira/JiraWithICTimePluginApi.cs @@ -14,8 +14,10 @@ public class JiraWithICTimePluginApi : IJiraClient { public string UserName { get; } + public api.rest.common.JiraUserInfo UserInfo => _vanillaJiraApi.UserInfo; private readonly HttpClient _httpClient; + private readonly VanillaJiraClient _vanillaJiraApi; public JiraWithICTimePluginApi(HttpClient httpClient, string userName) { @@ -24,13 +26,6 @@ public JiraWithICTimePluginApi(HttpClient httpClient, string userName) _vanillaJiraApi = new VanillaJiraClient(httpClient, userName); } - private readonly VanillaJiraClient _vanillaJiraApi; - - public async Task GetUserInfo() - { - return await _vanillaJiraApi.GetUserInfo(); - } - public async Task GetAvailableActivities() { return await _vanillaJiraApi.GetAvailableActivities(); diff --git a/jwl.jira/JiraWithTempoPluginApi.cs b/jwl.jira/JiraWithTempoPluginApi.cs index f1b4e55..788df12 100644 --- a/jwl.jira/JiraWithTempoPluginApi.cs +++ b/jwl.jira/JiraWithTempoPluginApi.cs @@ -10,21 +10,16 @@ public class JiraWithTempoPluginApi private const string WorklogTypeAttributeKey = @"_WorklogType_"; private readonly HttpClient _httpClient; - private readonly VanillaJiraClient _vanillaJiraServerApi; + private readonly VanillaJiraClient _vanillaJiraApi; public string UserName { get; } + public api.rest.common.JiraUserInfo UserInfo => _vanillaJiraApi.UserInfo; public JiraWithTempoPluginApi(HttpClient httpClient, string userName) { _httpClient = httpClient; - _vanillaJiraServerApi = new VanillaJiraClient(httpClient, userName); - UserName = userName; - } - - public async Task GetUserInfo() - { - return await _vanillaJiraServerApi.GetUserInfo(); + _vanillaJiraApi = new VanillaJiraClient(httpClient, userName); } public async Task GetWorklogAttributeDefinitions() @@ -60,7 +55,7 @@ public async Task GetIssueWorklogs(DateOnly from, DateOnly to, string public async Task GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable? issueKeys) { - string userKey = _vanillaJiraServerApi.UserInfo.Key ?? throw new ArgumentNullException($"{nameof(_vanillaJiraServerApi.UserInfo)}.{nameof(_vanillaJiraServerApi.UserInfo.Key)}"); + string userKey = UserInfo.Key ?? throw new ArgumentNullException($"{nameof(UserInfo)}.{nameof(UserInfo.Key)}"); var request = new api.rest.request.TempoFindWorklogs(from, to) { @@ -94,7 +89,7 @@ public async Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds public async Task AddWorklogPeriod(string issueKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? tempoWorklogType, string? comment, bool includeNonWorkingDays = false) { - string userKey = _vanillaJiraServerApi.UserInfo.Key ?? throw new ArgumentNullException($"{nameof(_vanillaJiraServerApi.UserInfo)}.{nameof(_vanillaJiraServerApi.UserInfo.Key)}"); + string userKey = UserInfo.Key ?? throw new ArgumentNullException($"{nameof(UserInfo)}.{nameof(UserInfo.Key)}"); var request = new api.rest.request.TempoAddWorklogByIssueKey() { diff --git a/jwl.jira/VanillaJiraClient.cs b/jwl.jira/VanillaJiraClient.cs index 7d7567d..170f1c4 100644 --- a/jwl.jira/VanillaJiraClient.cs +++ b/jwl.jira/VanillaJiraClient.cs @@ -37,11 +37,6 @@ public static async Task CheckHttpResponseForErrorMessages(HttpResponseMessage r } } - public async Task GetUserInfo() - { - return await GetUserInfo(_httpClient, UserName); - } - #pragma warning disable CS1998 public async Task GetAvailableActivities() { @@ -186,14 +181,14 @@ public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, i await CheckHttpResponseForErrorMessages(response); } - private static async Task GetUserInfo(HttpClient httpClient, string userName) + private async Task GetUserInfo() { UriBuilder uriBuilder = new UriBuilder() { Path = @"rest/api/2/user", Query = new UriQueryBuilder() - .Add(@"username", userName) + .Add(@"username", UserName) }; - return await httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery); + return await _httpClient.GetAsJsonAsync(uriBuilder.Uri.PathAndQuery); } } From 3f02216aa198b7bb50c80e6d2dc7904da87c93f0 Mon Sep 17 00:00:00 2001 From: "pierre@audrey" Date: Wed, 31 Jan 2024 22:51:27 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=9B=A0=20fixes=20to=20the=20missing?= =?UTF-8?q?=20IJiraClient.GetUserInfo()=20method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jwl.core/JwlCoreProcess.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/jwl.core/JwlCoreProcess.cs b/jwl.core/JwlCoreProcess.cs index 744f874..cc9cdb1 100644 --- a/jwl.core/JwlCoreProcess.cs +++ b/jwl.core/JwlCoreProcess.cs @@ -21,8 +21,6 @@ public class JwlCoreProcess : IDisposable private HttpClient _httpClient; private IJiraClient _jiraClient; - private jwl.jira.api.rest.common.JiraUserInfo? _userInfo; - public JwlCoreProcess(AppConfig config, ICoreProcessInteraction interaction) { _config = config; @@ -59,6 +57,7 @@ public JwlCoreProcess(AppConfig config, ICoreProcessInteraction interaction) */ } + #pragma warning disable CS1998 public async Task PreProcess() { Feedback?.OverallProcessStart(); @@ -76,11 +75,8 @@ public async Task PreProcess() throw new ArgumentNullException($"Jira credentials not supplied"); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(@"Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(jiraUserName + ":" + jiraUserPassword))); - - Feedback?.PreloadUserInfoStart(jiraUserName); - _userInfo = await _jiraClient.GetUserInfo(); - Feedback?.PreloadUserInfoEnd(); } + #pragma warning restore public async Task Process(IEnumerable inputFiles) { @@ -143,7 +139,7 @@ private async Task FillJiraWithWorklogs(InputWorkLog[] inputWorklogs, WorkLog[] { Feedback?.FillJiraWithWorklogsSetTarget(inputWorklogs.Length, worklogsForDeletion.Length); - if (_userInfo == null || _userInfo.Key == null) + if (_jiraClient.UserInfo?.Key is null) throw new ArgumentNullException(@"Unresolved Jira key for the logged-on user"); Task[] fillJiraWithWorklogsTasks = worklogsForDeletion @@ -228,9 +224,7 @@ private async Task RetrieveWorklogsForDeletion(InputWorkLog[] inputWo { WorkLog[] result; - if (_userInfo == null) - throw new ArgumentNullException(@"User info not preloaded from Jira server"); - if (string.IsNullOrEmpty(_userInfo.Key)) + if (string.IsNullOrEmpty(_jiraClient.UserInfo?.Key)) throw new ArgumentNullException(@"Empty user key preloaded from Jira server"); DateTime[] inputWorklogDays = inputWorklogs