|
1 | 1 | namespace jwl.jira;
|
2 | 2 |
|
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Net.Http.Json;
|
| 5 | +using System.Xml.Linq; |
4 | 6 | using System.Xml.Serialization;
|
5 | 7 | using jwl.infra;
|
6 | 8 | using jwl.wadl;
|
7 | 9 |
|
8 | 10 | // https://interconcept.atlassian.net/wiki/spaces/ICTIME/pages/31686672/API
|
9 | 11 | // https://interconcept.atlassian.net/wiki/spaces/ICBIZ/pages/34701333/REST+Services
|
10 |
| -// {{JiraBaseURI}}/rest/ictime/1.0/application.wadl |
11 | 12 | public class JiraWithICTimePluginApi
|
12 | 13 | : IJiraClient
|
13 | 14 | {
|
@@ -38,9 +39,75 @@ public JiraWithICTimePluginApi(HttpClient httpClient, string userName)
|
38 | 39 | _vanillaJiraApi = new VanillaJiraClient(httpClient, userName);
|
39 | 40 | }
|
40 | 41 |
|
41 |
| - public async Task<WorkLogType[]> GetAvailableActivities() |
| 42 | + public async Task<WorkLogType[]> GetAvailableActivities(string issueKey) |
42 | 43 | {
|
43 |
| - return await _vanillaJiraApi.GetAvailableActivities(); |
| 44 | + var result = await GetAvailableActivities(new string[] { issueKey }); |
| 45 | + return result[issueKey]; |
| 46 | + } |
| 47 | + |
| 48 | + public async Task<Dictionary<string, WorkLogType[]>> GetAvailableActivities(IEnumerable<string> issueKeys) |
| 49 | + { |
| 50 | + IEnumerable<JiraIssueKey> issueKeysParsed = issueKeys |
| 51 | + .Select(issueKey => new JiraIssueKey(issueKey)) |
| 52 | + .ToArray(); |
| 53 | + |
| 54 | + IEqualityComparer<JiraIssueKey> projectComparer = new JiraIssueKey.ProjectOnlyEqualityComparer(); |
| 55 | + IEnumerable<JiraIssueKey> distinctProjects = issueKeysParsed |
| 56 | + .Distinct(projectComparer); |
| 57 | + |
| 58 | + ComposedWadlMethodDefinition endPoint = GetActivityTypesForProjectMethodDefinition; |
| 59 | + |
| 60 | + HashSet<string> missingParameters = endPoint.Parameters |
| 61 | + .Where(par => !string.IsNullOrEmpty(par.Name)) |
| 62 | + .Select(par => par.Name ?? string.Empty) |
| 63 | + .ToHashSet(); |
| 64 | + |
| 65 | + // define |
| 66 | + IEnumerable<KeyValuePair<JiraIssueKey, string>> uris = distinctProjects |
| 67 | + .Select(issueKey => new KeyValuePair<JiraIssueKey, string>(issueKey, endPoint.ResourcePath.Replace("{issueKey}", issueKey.ToString()))); |
| 68 | + |
| 69 | + missingParameters.Remove("issueKey"); |
| 70 | + |
| 71 | + // check |
| 72 | + if (missingParameters.Any()) |
| 73 | + throw new ArgumentNullException($"Missing assignment of {string.Join(',', missingParameters)} in the call of {endPoint.Id} at resource path {endPoint.ResourcePath}"); |
| 74 | + |
| 75 | + bool providesJsonResponses = endPoint.Response?.Representations? |
| 76 | + .Any(repr => repr.MediaType == WadlRepresentation.MediaTypes.Json) ?? false; |
| 77 | + |
| 78 | + if (providesJsonResponses) |
| 79 | + throw new InvalidDataException($"Method {endPoint.Id} at resource path {endPoint.ResourcePath} does not respond in JSON"); |
| 80 | + |
| 81 | + // execute |
| 82 | + KeyValuePair<JiraIssueKey, Task<api.rest.response.ICTimeActivityDefinition[]>>[] responseTaks = uris |
| 83 | + .Select(uri => new KeyValuePair<JiraIssueKey, Task<api.rest.response.ICTimeActivityDefinition[]>>(uri.Key, _httpClient.GetAsJsonAsync<api.rest.response.ICTimeActivityDefinition[]>(uri.Value))) |
| 84 | + .ToArray(); |
| 85 | + |
| 86 | + await Task.WhenAll(responseTaks.Select(x => x.Value)); |
| 87 | + |
| 88 | + Dictionary<string, WorkLogType[]> result = responseTaks |
| 89 | + .Join( |
| 90 | + inner: issueKeysParsed, |
| 91 | + outerKeySelector: outer => outer.Key, |
| 92 | + innerKeySelector: inner => inner, |
| 93 | + resultSelector: (outer, inner) => new KeyValuePair<string, WorkLogType[]>( |
| 94 | + outer.Key.ToString(), |
| 95 | + outer.Value.Result |
| 96 | + .Select((def, ix) => new WorkLogType( |
| 97 | + Key: def.Id.ToString(), |
| 98 | + Value: def.Name, |
| 99 | + Sequence: ix |
| 100 | + )) |
| 101 | + .ToArray() |
| 102 | + ), |
| 103 | + comparer: projectComparer |
| 104 | + ) |
| 105 | + .ToDictionary( |
| 106 | + keySelector: x => x.Key, |
| 107 | + elementSelector: x => x.Value |
| 108 | + ); |
| 109 | + |
| 110 | + return result; |
44 | 111 | }
|
45 | 112 |
|
46 | 113 | public async Task<WorkLog[]> GetIssueWorklogs(DateOnly from, DateOnly to, string issueKey)
|
|
0 commit comments