This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,24 @@
using System.Collections.Generic;
using Kreta.Client.Jira.Model.Request;
using Kreta.Client.Jira.Model.Response;
namespace Kreta.Client.Jira.Interface
{
public interface IJiraClient
{
bool IsFileUploadEnabled { get; set; }
void CreateBasicAuthenticator(string username, string password);
string GetServiceDeskId();
GetRequestModel GetBejelentes(string id);
GetRequestTypeModel GetRequestTypes(string serviceDeskId);
GetRequestModel CreateBejelentes(TicketRequest ticketRequest);
TemporaryAttachmentsModel UploadAttachments(List<File> fileList, string serviceDeskId);
void AddAttachmentsToIssue(AddAttachments addAttachmentsModel, string isssueId);
string CreateCommentToBejelentes(string id, CommentRequest commentRequest);
GetRequestsModel GetBejelentesek(string requestType, string serviceDeskId, string requestStatus, string first, string limit);
GetRequestsComentsModel GetBejelentesCommentek(string id);
void ChangeAdminEmail(string intemzenyAzonosito, string newAddress);
void SubscribeToBejegyzes(string id);
}
}

View file

@ -0,0 +1,240 @@
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);
}
}
}
}

View file

@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using Kreta.Client.Jira.Model.Response;
using Kreta.Resources;
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Request
{
public class AddAttachments
{
[JsonProperty("temporaryAttachmentIds")]
public List<string> TemporaryAttachmentIds { get; set; }
[JsonProperty("public")]
public bool IsPublic => true;
[JsonProperty("additionalComment")]
public AdditionalCommentModel AdditionalComment { get; set; }
public static implicit operator AddAttachments(TemporaryAttachmentsModel temporaryAttachmentsModel) => new AddAttachments
{
TemporaryAttachmentIds = temporaryAttachmentsModel?.TemporaryAttachments.Select(attachment => attachment.TemporaryAttachmentId).ToList(),
AdditionalComment = new AdditionalCommentModel { Body = UgyfelszolgalatResource.AKovetkezoFajlokatAKretaUgyfelszolgalatBefogadta }
};
}
}

View file

@ -0,0 +1,10 @@
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Request
{
public class AdditionalCommentModel
{
[JsonProperty("body")]
public string Body { get; set; }
}
}

View file

@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Request
{
public class CommentRequest
{
[JsonProperty("body")]
public string Body { get; set; }
[JsonProperty("public")]
internal bool IsPublic => true;
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Request
{
public class File
{
public string Name { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
public string ContentType { get; set; }
}
}

View file

@ -0,0 +1,13 @@
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Request
{
public class RequestFieldValues
{
[JsonProperty("summary")]
public string Summary { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
}
}

View file

@ -0,0 +1,17 @@
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Request
{
public class TicketRequest
{
[JsonProperty("serviceDeskId")]
public string ServiceDeskId { get; set; }
[JsonProperty("requestTypeId")]
public string RequestTypeId { get; set; }
[JsonProperty("requestFieldValues")]
public RequestFieldValues RequestFieldValues { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using Newtonsoft.Json;
namespace Kreta.Client.Jira.Model.Response
{
public class Attachment
{
[JsonProperty("temporaryAttachmentId")]
public string TemporaryAttachmentId { get; set; }
[JsonProperty("fileName")]
public string FileName { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Response
{
public class CreatedDate
{
public string Iso8601 { get; set; }
public string Jira { get; set; }
public string Friendly { get; set; }
public string EpochMillis { get; set; }
}
}

View file

@ -0,0 +1,8 @@
namespace Kreta.Client.Jira.Model.Response
{
public class CurrentStatus
{
public string Status { get; set; }
public CreatedDate StatusDate { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetRequestModel
{
public string IssueId { get; set; }
public string IssueKey { get; set; }
public string RequestTypeId { get; set; }
public GetRequestTypeModelValue RequestType { get; set; }
public string ServiceDeskId { get; set; }
public GetServiceDeskModelValue ServiceDesk { get; set; }
public CreatedDate CreatedDate { get; set; }
public JiraUser Reporter { get; set; }
public List<RequestFieldValue> RequestFieldValues { get; set; }
public CurrentStatus CurrentStatus { get; set; }
public RequestJiraLink Links { get; set; }
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetRequestTypeModel : JiraRequestBase
{
public List<GetRequestTypeModelValue> Values { get; set; }
}
}

View file

@ -0,0 +1,17 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetRequestTypeModelValue
{
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string HelpText { get; set; }
public string ServiceDeskId { get; set; }
public List<string> GroupIds { get; set; }
public JiraIcon Icon { get; set; }
public int Order { get; set; }
public bool IsFileUploadEnabled { get; set; }
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetRequestsComentsModel : JiraRequestBase
{
public List<RequestsComentValue> Values { get; set; }
}
}

View file

@ -0,0 +1,22 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetRequestsModel : JiraRequestBase
{
public List<GetRequestsModelValue> values { get; set; }
public class GetRequestsModelValue
{
public string IssueId { get; set; }
public string IssueKey { get; set; }
public string RequestTypeId { get; set; }
public GetRequestTypeModelValue RequestType { get; set; }
public CreatedDate CreatedDate { get; set; }
public JiraUser Reporter { get; set; }
public List<RequestFieldValue> RequestFieldValues { get; set; }
public CurrentStatus CurrentStatus { get; set; }
public RequestJiraLink Links { get; set; }
}
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class GetServiceDeskModel : JiraRequestBase
{
public List<GetServiceDeskModelValue> Values { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Response
{
public class GetServiceDeskModelValue
{
public string Id { get; set; }
public string ProjectId { get; set; }
public string ProjectName { get; set; }
public string ProjectKey { get; set; }
}
}

View file

@ -0,0 +1,19 @@
using RestSharp.Deserializers;
namespace Kreta.Client.Jira.Model.Response
{
public class IconUrls
{
[DeserializeAs(Name = "48x48")]
public string Large { get; set; }
[DeserializeAs(Name = "24x24")]
public string Small { get; set; }
[DeserializeAs(Name = "16x16")]
public string Xsmall { get; set; }
[DeserializeAs(Name = "32x32")]
public string Medium { get; set; }
}
}

View file

@ -0,0 +1,8 @@
namespace Kreta.Client.Jira.Model.Response
{
public class JiraIcon
{
public string Id { get; set; }
public JiraLink Links { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Response
{
public class JiraLink
{
public string JiraRest { get; set; }
public string Self { get; set; }
public IconUrls AvatarUrls { get; set; }
public IconUrls IconUrls { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Response
{
public abstract class JiraRequestBase
{
public int Size { get; set; }
public int Start { get; set; }
public int Limit { get; set; }
public bool IsLastPage { get; set; }
}
}

View file

@ -0,0 +1,13 @@
namespace Kreta.Client.Jira.Model.Response
{
public class JiraUser
{
public string Name { get; set; }
public string Key { get; set; }
public string EmailAddress { get; set; }
public string DisplayName { get; set; }
public bool Active { get; set; }
public string TimeZone { get; set; }
public JiraLink Links { get; set; }
}
}

View file

@ -0,0 +1,10 @@
namespace Kreta.Client.Jira.Model.Response
{
public class RequestFieldValue
{
public string fieldId { get; set; }
public string label { get; set; }
public object value { get; set; }
public object renderedValue { get; set; }
}
}

View file

@ -0,0 +1,8 @@
namespace Kreta.Client.Jira.Model.Response
{
public class RequestJiraLink
{
public string Web { get; set; }
public string Self { get; set; }
}
}

View file

@ -0,0 +1,18 @@
using RestSharp.Deserializers;
namespace Kreta.Client.Jira.Model.Response
{
public class RequestsComentValue
{
public string Id { get; set; }
public string Body { get; set; }
[DeserializeAs(Name = "public")]
public bool IsPublic { get; set; }
public JiraUser Author { get; set; }
public CreatedDate Created { get; set; }
}
}

View file

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Kreta.Client.Jira.Model.Response
{
public class TemporaryAttachmentsModel
{
public List<Attachment> TemporaryAttachments { get; set; }
}
}