using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using Kreta.BusinessLogic.HelperClasses.Ugyfelszolgalat; using Kreta.BusinessLogic.Interfaces; using Kreta.Client.Jira.Interface; using Kreta.Client.Jira.Model.Request; using Kreta.Client.Jira.Model.Response; using Kreta.Core.Enum; using Kreta.Core.Exceptions; using Kreta.Resources; namespace Kreta.BusinessLogic.Helpers { internal class JiraHelper : IJiraHelper { private readonly IJiraClient JiraClient; public bool IsFileUploadEnabled { get; set; } private const long MaxFileSizeMB = 4; private const long MaxFilesSizeMB = MaxFileSizeMB * 4; private static Dictionary s_allowedFileTypes = new Dictionary() { { ".txt", "text/plain" }, { ".pdf", "application/pdf" }, { ".png", "image/png" }, { ".bmp", "image/bmp" }, { ".jpg", "image/jpeg" }, { ".jpeg", "image/jpeg" }, { ".xls", "application/vnd.ms-excel" }, { ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, { ".doc", "application/msword" }, { ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }, { ".xml", "text/xml" }, { ".roz", "application/roz" }, {".rar", "application/octet-stream"}, {".zip", "application/x-zip-compressed"} }; private long GetSizeInBytes(long sizeInMB) => sizeInMB * (1024 * 1024); public int GetMaxFileSizeInBytes() => (int)GetSizeInBytes(MaxFileSizeMB); public string[] GetAllowedFileExtensionArray() => s_allowedFileTypes.Keys.ToArray(); public JiraHelper(IJiraClient jiraClient) { JiraClient = jiraClient ?? throw new ArgumentNullException(nameof(jiraClient)); IsFileUploadEnabled = jiraClient.IsFileUploadEnabled; } private void AttachmentsValidation(long contentLength, string contentType, string fileName) { Match match = Regex.Match(fileName, @".*(\" + string.Join("|\\", GetAllowedFileExtensionArray()) + @")$", RegexOptions.IgnoreCase); if (match.Success) { var extension = match.Groups[1].Value.ToLower(); if (!(s_allowedFileTypes.ContainsKey(extension) && s_allowedFileTypes[extension] == contentType)) { throw new BlException(ErrorResource.AFajlKiterjeszteseVagyTipusaNemMegfelelo, BlExceptionType.JiraFileError); } } else { throw new BlException(ErrorResource.AFajlKiterjeszteseVagyTipusaNemMegfelelo, BlExceptionType.JiraFileError); } if (contentLength > GetSizeInBytes(MaxFileSizeMB)) throw new BlException(string.Format(ErrorResource.HibaTortentXMBNalNemLehetNagyobbAFajlMerete, MaxFileSizeMB), BlExceptionType.JiraFileError); } private List CreateFileList(List csatolmanyArray) { var fileList = new List(); long alreadyAddedFilesSize = 0; foreach (var csatolmanyFile in csatolmanyArray) { string contentType = string.Empty; int contentLength = 0; File file = new File { FileName = csatolmanyFile.Name, Name = "file" }; string[] parts = csatolmanyFile.ContentAsBase64EncodedString.Split(new string[] { ";", ":", "," }, StringSplitOptions.None); if (parts.Length == 4) { file.ContentType = parts[1]; file.Content = Convert.FromBase64String(parts[3]); } else { throw new BlException(ErrorResource.HibaTortentAFajlFeltolteseKozben, BlExceptionType.JiraFileError, new Exception(ErrorResource.ArgumentNumberErrorException)); } contentLength = file.Content.Length; alreadyAddedFilesSize += contentLength; AttachmentsValidation(contentLength, file.ContentType, file.FileName); file.ContentType = "application/octet-stream"; fileList.Add(file); } if (GetSizeInBytes(MaxFilesSizeMB) < alreadyAddedFilesSize) { throw new BlException(string.Format(ErrorResource.HibaTortentACsatolmanyokEgyuttesMereteNemLehetNagyobbXMBNal, MaxFilesSizeMB), BlExceptionType.JiraFileError); } return fileList; } private string GetJiraPassword(string intezmenyAzonosito) { HashAlgorithm algorithm = SHA1.Create(); byte[] hashBytes = algorithm.ComputeHash(Encoding.UTF8.GetBytes(intezmenyAzonosito)); var hashString = new StringBuilder(); foreach (byte hashByte in hashBytes) { hashString.Append(hashByte.ToString("x2")); } return hashString.ToString().Substring(0, 6); } public string GetServiceDeskId(string intemzenyAzonosito) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); string response; try { response = JiraClient.GetServiceDeskId(); } catch (Exception ex) { throw new BlException(ex.Message); } return !string.IsNullOrWhiteSpace(response) ? response : throw new BlException(ErrorResource.JiraHibasKeres); } public (DataSet data, int dataTotalCount) GetBejelentesek(string intemzenyAzonosito, string requestType, string serviceDeskId, string requestStatus, string first, string limit) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); GetRequestsModel response; try { response = JiraClient.GetBejelentesek(requestType, serviceDeskId, requestStatus, first, limit); } catch (Exception ex) { throw new BlException(ex.Message); } var ds = new DataSet(); var dt = ds.Tables.Add(); dt.Columns.Add("ID", typeof(string)); dt.Columns.Add("BejelentesTipus", typeof(string)); dt.Columns.Add("Referencia", typeof(string)); dt.Columns.Add("BejelentesCime", typeof(string)); dt.Columns.Add("Status", typeof(string)); dt.Columns.Add("BejelentesDatuma", typeof(DateTime)); dt.Columns.Add("UtolsoFrissites", typeof(DateTime)); foreach (var item in response.values) { var row = dt.NewRow(); var summary = item.RequestFieldValues.FirstOrDefault(a => a.fieldId == "summary"); if (item.RequestType != null) { row["BejelentesTipus"] = item.RequestType.Name; } row["ID"] = item.IssueId; row["Referencia"] = item.IssueKey; if (summary != null) { row["BejelentesCime"] = summary.value; } row["Status"] = item.CurrentStatus.Status; row["BejelentesDatuma"] = item.CreatedDate.Iso8601; row["UtolsoFrissites"] = item.CurrentStatus.StatusDate.Iso8601; dt.Rows.Add(row); } //TODO: jira lekérdezésben kéne megoldani ds.Tables[0]?.AsEnumerable().OrderByDescending(x => x["UtolsoFrissites"]); return (ds, response.Size); } public string CreateCommentToBejelentes(string intemzenyAzonosito, CreateBejelentesComment createBejelentesComment) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); // string response = string.Empty; if (!string.IsNullOrWhiteSpace(createBejelentesComment.Comment)) { try { response = JiraClient.CreateCommentToBejelentes(createBejelentesComment.Id, createBejelentesComment); } catch (Exception ex) { throw new BlException(ex.Message); } } List fileList = CreateFileList(createBejelentesComment.CsatolmanyArray); try { var uploadedAttavments = JiraClient.UploadAttachments(fileList, GetServiceDeskId(intemzenyAzonosito)); JiraClient.AddAttachmentsToIssue(uploadedAttavments, createBejelentesComment.Id); } catch (Exception ex) { throw new BlException(ex.Message); } return response; } public GetRequestModel CreateBejelentes(string intemzenyAzonosito, CreateBejelentes createBejelentes) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); createBejelentes.ServiceDeskId = GetServiceDeskId(intemzenyAzonosito); GetRequestModel response; try { response = JiraClient.CreateBejelentes(createBejelentes); } catch (Exception ex) { throw new BlException(ex.Message); } if (createBejelentes.CsatolmanyArray != null && createBejelentes.CsatolmanyArray.Count > 0) { List fileList = CreateFileList(createBejelentes.CsatolmanyArray); try { var uploadedAttavments = JiraClient.UploadAttachments(fileList, GetServiceDeskId(intemzenyAzonosito)); JiraClient.AddAttachmentsToIssue(uploadedAttavments, response.IssueId); } catch (Exception ex) { throw new BlException(ex.Message); } } return response; } public GetRequestModel CreateDbVisszaallitas(string intemzenyAzonosito, CreateDbVisszaallitas createDbVisszaallitas) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); createDbVisszaallitas.ServiceDeskId = GetServiceDeskId(intemzenyAzonosito); try { return JiraClient.CreateBejelentes(createDbVisszaallitas); } catch (Exception ex) { throw new BlException(ex.Message); } } public GetRequestModel CreateKonferenciaJelentkezes(string intemzenyAzonosito, CreateKonferenciaJelentkezes createKonferenciaJelentkezes) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); createKonferenciaJelentkezes.ServiceDeskId = GetServiceDeskId(intemzenyAzonosito); try { return JiraClient.CreateBejelentes(createKonferenciaJelentkezes); } catch (Exception ex) { throw new BlException(ex.Message); } } public GetRequestModel CreateUjUrlIgenyles(string intemzenyAzonosito, CreateUjUrlIgenyles createUjUrlIgenyles) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); createUjUrlIgenyles.ServiceDeskId = GetServiceDeskId(intemzenyAzonosito); try { return JiraClient.CreateBejelentes(createUjUrlIgenyles); } catch (Exception ex) { throw new BlException(ex.Message); } } public List GetBejelentesCommentek(string intemzenyAzonosito, string id) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); List comments; try { comments = JiraClient.GetBejelentesCommentek(id).Values; } catch (Exception ex) { throw new BlException(ex.Message); } comments.Reverse(); return comments; } public GetRequestTypeModel GetRequestTypes(string intemzenyAzonosito) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); string serviceDeskId = GetServiceDeskId(intemzenyAzonosito); try { return JiraClient.GetRequestTypes(serviceDeskId); } catch (Exception ex) { throw new BlException(ex.Message); } } public GetRequestModel GetBejelentes(string intemzenyAzonosito, string id) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); try { return JiraClient.GetBejelentes(id); } catch (Exception ex) { throw new BlException(ex.Message); } } public void ChangeAdminEmail(string intemzenyAzonosito, string newAddress) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); try { JiraClient.ChangeAdminEmail(intemzenyAzonosito, newAddress); } catch (Exception ex) { throw new BlException(ex.Message); } } public void SubscribeToBejegyzes(string intemzenyAzonosito, string id) { JiraClient.CreateBasicAuthenticator(intemzenyAzonosito, GetJiraPassword(intemzenyAzonosito)); try { JiraClient.SubscribeToBejegyzes(id); } catch (Exception ex) { throw new BlException(ex.Message); } } } }