kreta/Kreta.Client/Jira/JiraClient.cs
2024-03-13 00:33:46 +01:00

240 lines
8.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Kreta.Client.ClientBase;
using Kreta.Client.Jira.Interface;
using Kreta.Client.Jira.Model.Request;
using Kreta.Client.Jira.Model.Response;
using Kreta.Core;
using Kreta.Resources;
namespace Kreta.Client.Jira
{
internal class JiraClient : RestSharpClientBase, IJiraClient
{
private readonly IUgyfelszolgalatConfig UgyfelszolgalatConfig;
public bool IsFileUploadEnabled { get; set; }
private readonly int[] JiraGropuIdFilters;
private const string RequestPath = "request";
private const string RequestIdPath = RequestPath + "/{id}";
private const string ServicedeskPath = "servicedesk";
private const string RequestTypePath = ServicedeskPath + "/{id}/requesttype";
private const string CommentPath = RequestPath + "/{id}/comment";
private const string SubscribePath = RequestPath + "/{id}/notification";
private const string AttachmentUploadPath = ServicedeskPath + "/{id}/attachTemporaryFile";
private const string AddAttachmentToIssuePath = RequestIdPath + "/attachment";
private const string JiraServiceApiLdapEmail = "/api/v1/ldap/email";
public JiraClient(IUgyfelszolgalatConfig ugyfelszolgalatConfig)
{
UgyfelszolgalatConfig = ugyfelszolgalatConfig ?? throw new ArgumentNullException(nameof(ugyfelszolgalatConfig));
BaseUrl = ugyfelszolgalatConfig.Url;
IsFileUploadEnabled = ugyfelszolgalatConfig.IsFileUploadEnabled;
JiraGropuIdFilters = !string.IsNullOrWhiteSpace(UgyfelszolgalatConfig.CommaSeparatedGroupIdFilters) ?
UgyfelszolgalatConfig.CommaSeparatedGroupIdFilters.Split(',').Select(x => int.Parse(x)).ToArray() :
new int[] { };
}
public GetRequestsModel GetBejelentesek(string requestType, string serviceDeskId, string requestStatus, string first, string limit)
{
var parameters = new Dictionary<string, string>
{
{ "expand", "requestType" },
{ "serviceDeskId", serviceDeskId },
{ "start", first },
{ "limit", limit },
};
if (!string.IsNullOrWhiteSpace(requestStatus))
{
parameters.Add("requestStatus", requestStatus);
}
if (!string.IsNullOrWhiteSpace(requestType))
{
parameters.Add("requestTypeId", requestType);
}
var response = Get<GetRequestsModel>(RequestPath, parameters: parameters);
ErrorByStatusCode(response.StatusCode);
var result = response.Result;
return result;
}
public string CreateCommentToBejelentes(string id, CommentRequest commentRequest)
{
var urlSegments = new Dictionary<string, string>
{
{ "id", id }
};
var response = Post(CommentPath, urlSegments: urlSegments, body: commentRequest);
ErrorByStatusCode(response.StatusCode);
var result = response.Result;
return result;
}
public TemporaryAttachmentsModel UploadAttachments(List<File> fileList, string serviceDeskId)
{
if (fileList == null || fileList.Count < 1 || !UgyfelszolgalatConfig.IsFileUploadEnabled)
{
return null;
}
var headers = new Dictionary<string, string>
{
{ "X-Atlassian-Token", "no-check" },
{ "X-ExperimentalApi", "opt-in" },
{ "Content-Type", "multipart/form-data"}
};
var firstPostUrlSegments = new Dictionary<string, string>
{
{ "id", serviceDeskId }
};
var response = Post<TemporaryAttachmentsModel>(AttachmentUploadPath, headers: headers, urlSegments: firstPostUrlSegments, fileList: fileList);
ErrorByStatusCode(response.StatusCode);
return response.Result;
}
public void AddAttachmentsToIssue(AddAttachments addAttachmentsModel, string isssueId)
{
if (addAttachmentsModel.TemporaryAttachmentIds == null || addAttachmentsModel.TemporaryAttachmentIds.Count < 1 || !UgyfelszolgalatConfig.IsFileUploadEnabled)
{
return;
}
var secondPostUrlSegments = new Dictionary<string, string>
{
{ "id", isssueId }
};
var headers = new Dictionary<string, string>
{
{ "X-Atlassian-Token", "no-check" },
{ "X-ExperimentalApi", "opt-in" },
{ "Content-Type", "application/json" }
};
var response = Post(AddAttachmentToIssuePath, headers: headers, urlSegments: secondPostUrlSegments, body: addAttachmentsModel);
ErrorByStatusCode(response.StatusCode);
}
public string GetServiceDeskId()
{
var response = Get<GetServiceDeskModel>(ServicedeskPath);
ErrorByStatusCode(response.StatusCode);
return response.Result.Values.SingleOrDefault(a => a.ProjectKey == UgyfelszolgalatConfig.ProjectKey)?.Id;
}
public GetRequestModel CreateBejelentes(TicketRequest ticketRequest)
{
var response = Post<GetRequestModel>(RequestPath, body: ticketRequest);
ErrorByStatusCode(response.StatusCode);
return response.Result;
}
public GetRequestsComentsModel GetBejelentesCommentek(string id)
{
var urlSegments = new Dictionary<string, string>
{
{ "id", id }
};
var response = Get<GetRequestsComentsModel>(CommentPath, urlSegments: urlSegments);
ErrorByStatusCode(response.StatusCode);
return response.Result;
}
public GetRequestTypeModel GetRequestTypes(string serviceDeskId)
{
var urlSegments = new Dictionary<string, string>
{
{ "id", serviceDeskId }
};
var response = Get<GetRequestTypeModel>(RequestTypePath, urlSegments: urlSegments);
ErrorByStatusCode(response.StatusCode);
var result = response.Result;
var validRequestTypeItemList = JiraGropuIdFilters.Length > 0 ?
result.Values.Where(requestType => requestType.GroupIds.Exists(groupId => JiraGropuIdFilters.Contains(int.Parse(groupId)))).ToList() :
result.Values;
result.Values = validRequestTypeItemList;
return result;
}
public GetRequestModel GetBejelentes(string id)
{
var urlSegments = new Dictionary<string, string>
{
{ "id", id }
};
var parameters = new Dictionary<string, string>
{
{ "expand", "requestType,serviceDesk,action,comment" }
};
var response = Get<GetRequestModel>(RequestIdPath, parameters: parameters, urlSegments: urlSegments);
ErrorByStatusCode(response.StatusCode);
return response.Result;
}
public void ChangeAdminEmail(string intemzenyAzonosito, string newAddress)
{
var headers = new Dictionary<string, string>
{
{ "ApiKey", UgyfelszolgalatConfig.JiraServiceApiKey },
{ "Content-Type", "application/json-patch+json" }
};
BaseUrl = UgyfelszolgalatConfig.JiraServiceApiUrl;
var response = Patch($"{JiraServiceApiLdapEmail}/{intemzenyAzonosito}", headers, body: new object[] { new { op = "replace", path = "/mail", value = newAddress } });
ErrorByStatusCode(response.StatusCode);
}
public void SubscribeToBejegyzes(string id)
{
var urlSegments = new Dictionary<string, string>
{
{ "id", id }
};
var response = Post(SubscribePath, urlSegments: urlSegments);
ErrorByStatusCode(response.StatusCode);
}
private void ErrorByStatusCode(HttpStatusCode code)
{
switch (code)
{
case HttpStatusCode.BadRequest:
throw new ClientException(ErrorResource.JiraHibasKeres);
case HttpStatusCode.Unauthorized:
throw new ClientException(ErrorResource.JiraNincsBejelentkezve);
case HttpStatusCode.Forbidden:
throw new ClientException(ErrorResource.JiraNemMegfeleloJogosultsag);
case HttpStatusCode.NotFound:
throw new ClientException(ErrorResource.JiraNemMegfeleloUtvonal);
case HttpStatusCode.PreconditionFailed:
throw new ClientException(ErrorResource.JiraKisérletiJelleguHiba);
}
}
}
}