Skip to content

Commit

Permalink
🛠 partial rework of the APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Hraško committed Jan 26, 2024
1 parent 76986cc commit be72d95
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 136 deletions.
6 changes: 3 additions & 3 deletions jwl.core/JwlCoreProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class JwlCoreProcess : IDisposable
private AppConfig _config;
private HttpClientHandler _httpClientHandler;
private HttpClient _httpClient;
private JiraServerApi _jiraClient;
private VanillaJiraServerApi _jiraClient;

private jwl.jira.api.rest.common.JiraUserInfo? _userInfo;
private Dictionary<string, jira.api.rest.common.TempoWorklogAttributeStaticListValue> availableWorklogTypes = new ();
Expand All @@ -45,7 +45,7 @@ public JwlCoreProcess(AppConfig config, ICoreProcessInteraction interaction)
{
BaseAddress = new Uri(_config.JiraServer?.BaseUrl ?? string.Empty)
};
_jiraClient = new JiraServerApi(_httpClient);
_jiraClient = new VanillaJiraServerApi(_httpClient);

/* 2do!...
_jiraClient.WsClient.HttpRequestPostprocess = req =>
Expand Down Expand Up @@ -171,7 +171,7 @@ await MultiTask.WhenAll(
IEnumerable<jira.api.rest.response.TempoWorklogAttributeDefinition> attrEnumDefs = await _jiraClient.GetWorklogAttributesEnum();

return attrEnumDefs
.Where(attrDef => attrDef.Key?.Equals(TempoTimesheetsPluginApiExt.WorklogTypeAttributeKey) ?? false)
.Where(attrDef => attrDef.Key?.Equals(JiraWithTempoPluginApi.WorklogTypeAttributeKey) ?? false)
.Where(attrDef => attrDef.Type != null
&& attrDef.Type?.Value == jira.api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList
)
Expand Down
25 changes: 25 additions & 0 deletions jwl.jira/IJiraServerApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace jwl.jira;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

internal interface IJiraServerApi
{
Task<api.rest.common.JiraUserInfo> GetUserInfo();

Task<api.rest.response.TempoWorklogAttributeDefinition[]> GetWorklogTypes();

Task<api.rest.response.TempoWorklog[]> GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable<string>? issueKeys, IEnumerable<string>? userKeys);

Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? worklogType, string? comment);

Task AddWorklogPeriod(string issueKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? tempoWorklogType, string? comment, bool includeNonWorkingDays = false);

Task DeleteWorklog(long issueId, long worklogId, bool notifyUsers = false);

Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, int timeSpentSeconds, string? worklogType, string? comment);

Task UpdateWorklogPeriod(string issueKey, long worklogId, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? comment, string? tempoWorklogType, bool includeNonWorkingDays = false);
}
93 changes: 0 additions & 93 deletions jwl.jira/JiraServerApi.cs

This file was deleted.

104 changes: 64 additions & 40 deletions jwl.jira/TempoTimesheetsPluginApi.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
namespace jwl.jira;
using System.Net.Http.Json;
using jwl.infra;
using jwl.jira.api.rest.common;
using jwl.jira.api.rest.response;

// https://www.tempo.io/server-api-documentation/timesheets
public static class TempoTimesheetsPluginApiExt
public class JiraWithTempoPluginApi
: IJiraServerApi
{
public const string WorklogTypeAttributeKey = @"_WorklogType_";
private const string WorklogTypeAttributeKey = @"_WorklogType_";

public static async Task AddWorklogPeriod(this JiraServerApi self, string issueKey, string userKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string tempoWorklogType, string comment, bool includeNonWorkingDays = false)
private readonly HttpClient _httpClient;
private readonly VanillaJiraServerApi _vanillaJiraServerApi;

public string UserName { get; }

public JiraWithTempoPluginApi(HttpClient httpClient, string userName)
{
_httpClient = httpClient;
_vanillaJiraServerApi = new VanillaJiraServerApi(httpClient, userName);

UserName = userName;
}

public async Task<JiraUserInfo> GetUserInfo()
{
return await _vanillaJiraServerApi.GetUserInfo(UserName);
}

public async Task<api.rest.response.TempoWorklogAttributeDefinition[]> GetWorklogTypes()
{
return await _httpClient.GetAsJsonAsync<api.rest.response.TempoWorklogAttributeDefinition[]>(@"rest/tempo-core/1/work-attribute");
}

public async Task<api.rest.response.TempoWorklog[]> GetIssueWorklogs(DateOnly from, DateOnly to, IEnumerable<string>? issueKeys)
{
JiraUserInfo userInfo = await GetUserInfo();

var request = new api.rest.request.TempoFindWorklogs(from, to)
{
IssueKey = issueKeys?.ToArray(),
UserKey = new string[] { userInfo.Key }
};
var response = await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs/search", request);
return await HttpClientJsonExt.DeserializeJsonStreamAsync<api.rest.response.TempoWorklog[]>(await response.Content.ReadAsStreamAsync());
}

public async Task AddWorklog(string issueKey, DateOnly day, int timeSpentSeconds, string? worklogType, string? comment)
{
await AddWorklogPeriod(issueKey, day, day, timeSpentSeconds, worklogType, comment);
}

public async Task AddWorklogPeriod(string issueKey, string userKey, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? tempoWorklogType, string? comment, bool includeNonWorkingDays = false)
{
var request = new api.rest.request.TempoAddWorklogByIssueKey()
{
Expand All @@ -31,25 +75,26 @@ public static async Task AddWorklogPeriod(this JiraServerApi self, string issueK
}
}
};
await self.HttpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs", request);
}

public static async Task AddWorklog(this JiraServerApi self, string issueKey, string userKey, DateOnly day, int timeSpentSeconds, string tempoWorklogType, string comment)
{
await self.AddWorklogPeriod(issueKey, userKey, day, day, timeSpentSeconds, tempoWorklogType, comment);
await _httpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs", request);
}

public static async Task DeleteWorklog(this JiraServerApi self, long worklogId)
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())
};
await self.HttpClient.DeleteAsync(uriBuilder.Uri.PathAndQuery);
await _httpClient.DeleteAsync(uriBuilder.Uri.PathAndQuery);
}

public async Task UpdateWorklog(string issueKey, long worklogId, DateOnly day, int timeSpentSeconds, string? worklogType, string? comment)
{
await UpdateWorklogPeriod(issueKey, worklogId, day, day, timeSpentSeconds, comment, worklogType);
}

public static async Task UpdateWorklogPeriod(this JiraServerApi self, int worklogId, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string comment, string tempoWorklogType, bool includeNonWorkingDays = false)
public async Task UpdateWorklogPeriod(string issueKey, long worklogId, DateOnly dayFrom, DateOnly dayTo, int timeSpentSeconds, string? comment, string? tempoWorklogType, bool includeNonWorkingDays = false)
{
UriBuilder uriBuilder = new UriBuilder()
{
Expand All @@ -67,36 +112,15 @@ public static async Task UpdateWorklogPeriod(this JiraServerApi self, int worklo
Attributes = new Dictionary<string, api.rest.common.TempoWorklogAttribute>()
{
[WorklogTypeAttributeKey] = new api.rest.common.TempoWorklogAttribute()
{
WorkAttributeId = 1,
Key = WorklogTypeAttributeKey,
Name = @"Worklog Type",
Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList,
Value = tempoWorklogType
}
{
WorkAttributeId = 1,
Key = WorklogTypeAttributeKey,
Name = @"Worklog Type",
Type = api.rest.common.TempoWorklogAttributeTypeIdentifier.StaticList,
Value = tempoWorklogType
}
}
};
await self.HttpClient.PutAsJsonAsync(uriBuilder.Uri.PathAndQuery, request);
}

public static async Task UpdateWorklog(this JiraServerApi self, int worklogId, DateOnly day, int timeSpentSeconds, string comment, string tempoWorklogType)
{
await self.UpdateWorklogPeriod(worklogId, day, day, timeSpentSeconds, comment, tempoWorklogType);
}

public static async Task<api.rest.response.TempoWorklogAttributeDefinition[]> GetWorklogAttributesEnum(this JiraServerApi self)
{
return await self.HttpClient.GetAsJsonAsync<api.rest.response.TempoWorklogAttributeDefinition[]>(@"rest/tempo-core/1/work-attribute");
}

public static async Task<api.rest.response.TempoWorklog[]> GetIssueWorklogs(this JiraServerApi self, DateOnly from, DateOnly to, IEnumerable<string>? issueKeys, IEnumerable<string>? userKeys)
{
var request = new api.rest.request.TempoFindWorklogs(from, to)
{
IssueKey = issueKeys?.ToArray(),
UserKey = userKeys?.ToArray()
};
var response = await self.HttpClient.PostAsJsonAsync(@"rest/tempo-timesheets/4/worklogs/search", request);
return await HttpClientJsonExt.DeserializeJsonStreamAsync<api.rest.response.TempoWorklog[]>(await response.Content.ReadAsStreamAsync());
await _httpClient.PutAsJsonAsync(uriBuilder.Uri.PathAndQuery, request);
}
}
Loading

0 comments on commit be72d95

Please sign in to comment.