Пример #1
0
        public async Task <JiraIssueReference> CreateIssueAsync(JiraIssue fields,
                                                                CancellationToken cancellationToken)
        {
            var project = fields.Project;

            if (project == (JiraProject)null)
            {
                throw new JiraException("Field 'project' is required");
            }
            var issueType = fields.IssueType;

            if (issueType == (JiraIssueType)null)
            {
                throw new JiraException("Field 'issueType' is required");
            }
            var key = $"{project.Key}-{await store.GenerateId()}";
            var id  = await store.GenerateId();

            var jObject = fields.Controller.GetFields();

            if (jObject.GetProperty <DateTime>("created") == default)
            {
                jObject.SetProperty("created", DateTime.Now);
            }
            if (jObject.GetProperty <DateTime>("updated") == default)
            {
                jObject.SetProperty("updated", DateTime.Now);
            }
            if (jObject.GetProperty <JiraUser>("creator") == (JiraUser)null)
            {
                jObject.SetProperty("creator", authorizedUser);
            }
            var(workflow, _) = metadataProvider.GetWorkflow()
                               .SingleOrDefault(x => x.workflow.IssueType == issueType);
            if (workflow != null)
            {
                if (jObject.GetProperty <JiraStatus>("status") == (JiraStatus)null &&
                    workflow.DefaultStatus != (JiraStatus)null)
                {
                    jObject.SetProperty("status", workflow.DefaultStatus);
                }
            }

            var issue = new JiraIssueDto
            {
                Key         = key,
                Id          = id.ToString(),
                Self        = $"{fakeJiraUrl}/rest/api/2/issue/{id}",
                IssueFields = jObject
            };
            await store.Insert(issue);

            return(new JiraIssueReference
            {
                Key = issue.Key,
                Id = issue.Id,
                Self = issue.Self,
            });
        }
Пример #2
0
 private static JiraIssueDto Clone(JiraIssueDto issue)
 {
     return(new JiraIssueDto
     {
         Id = issue.Id,
         Key = issue.Key,
         Self = issue.Self,
         IssueFields = issue.IssueFields.Clone()
     });
 }
Пример #3
0
 public Task Insert(JiraIssueDto jObject)
 {
     return(InvokeByLock(() =>
     {
         var key = ExtractLong(jObject.Key);
         var id = long.Parse(jObject.Id);
         issueStorage.Write(key, jObject.ToBytes());
         issueIdsIndex.Write(id, BitConverter.GetBytes(key));
         return true;
     }));
 }
Пример #4
0
 public Task Update(string keyOrId, JiraIssueFields fields)
 {
     return(InvokeByLock(() =>
     {
         var key = GetIssueStorageKey(keyOrId);
         if (!issueStorage.TryRead(key, out var issueBytes))
         {
             throw new JiraException($"issue [{keyOrId}] is not found");
         }
         var replacedIssue = JiraIssueDto.FromBytes(issueBytes);
         fields.CopyTo(replacedIssue.IssueFields);
         issueStorage.Write(key, replacedIssue.ToBytes());
         return true;
     }));
 }
Пример #5
0
        public Task Insert(JiraIssueDto issue)
        {
            var source = new TaskCompletionSource <bool>();

            try
            {
                lock (lockObject) issues.Add(Clone(issue));

                source.SetResult(true);
            }
            catch (Exception e)
            {
                source.SetException(e);
            }

            return(source.Task);
        }
Пример #6
0
        public Task <JiraIssueDto> Get(string keyOrId)
        {
            return(InvokeByLock(() =>
            {
                var key = GetIssueStorageKey(keyOrId);
                if (!issueStorage.TryRead(key, out var bytes))
                {
                    throw new JiraException($"issue '{keyOrId}' is not found");
                }
                var result = JiraIssueDto.FromBytes(bytes);
                if (issueAttachmentsIndex.TryRead(key, out var attachmentBytes))
                {
                    var attachmentIds = LongArrayFromBytes(attachmentBytes);
                    var issuesAttachments = new JiraAttachment[attachmentIds.Length];
                    for (var i = 0; i < issuesAttachments.Length; ++i)
                    {
                        if (!attachmentsHeaders.TryRead(attachmentIds[i], out var headerBytes))
                        {
                            throw new InvalidOperationException(
                                $"attachment '{attachmentIds[i]}' is not found");
                        }
                        issuesAttachments[i] =
                            Json.Deserialize <JiraAttachment>(Encoding.UTF8.GetString(headerBytes));
                    }

                    result.IssueFields.SetProperty("attachment", issuesAttachments);
                }
                else
                {
                    result.IssueFields.SetProperty("attachment", new JiraAttachment[0]);
                }

                var issueComments = ReadIssueComments(key);
                result.IssueFields.SetProperty("comment", new JiraIssueComments
                {
                    Comments = issueComments,
                    StartAt = 0,
                    Total = issueComments.Length,
                    MaxResults = issueComments.Length
                });
                return result;
            }));
        }
Пример #7
0
        private JiraTransition[] GetTransitions(JiraIssueDto issue)
        {
            var workflowList    = metadataProvider.GetWorkflow();
            var currentWorkflow =
                workflowList.SingleOrDefault(x =>
                                             x.workflow.IssueType == issue.IssueFields.GetProperty <JiraIssueType>("issueType"));
            var jiraIssue = new JiraIssue(new JiraIssueFieldsController(issue.IssueFields))
            {
                Key  = issue.Key,
                Id   = issue.Id,
                Self = issue.Self
            }.Cast(currentWorkflow.type);

            if (currentWorkflow == default || jiraIssue.Status == (JiraStatus)null)
            {
                return(new JiraTransition[0]);
            }

            return(currentWorkflow.workflow.GetTransitions(jiraIssue.Status)
                   .Select(x => (transitions: x, condition: currentWorkflow.workflow.GetCondition(x)))
                   .Where(x => x.condition(jiraIssue))
                   .Select(x => x.transitions)
                   .ToArray());
        }
Пример #8
0
        public Task <JiraIssueDto[]> GetAll()
        {
            return(InvokeByLock(() =>
            {
                var allAttachments = new Dictionary <long, JiraAttachment>();
                foreach (var pair in attachmentsHeaders.ReadAll())
                {
                    allAttachments[pair.Key] =
                        Json.Deserialize <JiraAttachment>(Encoding.UTF8.GetString(pair.Value));
                }
                var attachmentsIndex = new Dictionary <long, JiraAttachment[]>();
                foreach (var pair in issueAttachmentsIndex.ReadAll())
                {
                    var attachmentIds = LongArrayFromBytes(pair.Value);
                    var issueAttachments = new JiraAttachment[attachmentIds.Length];
                    for (var i = 0; i < attachmentIds.Length; ++i)
                    {
                        issueAttachments[i] = allAttachments[attachmentIds[i]];
                    }
                    attachmentsIndex[pair.Key] = issueAttachments;
                }

                var allComments = new Dictionary <long, JiraComment>();
                foreach (var pair in commentsStorage.ReadAll())
                {
                    allComments[pair.Key] = Json.Deserialize <JiraComment>(Encoding.UTF8.GetString(pair.Value));
                }

                var commentsIndex = new Dictionary <long, JiraComment[]>();
                foreach (var pair in issueCommentsIndex.ReadAll())
                {
                    var commentIds = LongArrayFromBytes(pair.Value);
                    var issueComments = new JiraComment[commentIds.Length];
                    for (var i = 0; i < commentIds.Length; ++i)
                    {
                        issueComments[i] = allComments[commentIds[i]];
                    }
                    commentsIndex[pair.Key] = issueComments;
                }

                var issues = new List <JiraIssueDto>();
                foreach (var pair in issueStorage.ReadAll())
                {
                    var jiraIssueDto = JiraIssueDto.FromBytes(pair.Value);
                    var keyId = ExtractLong(jiraIssueDto.Key);
                    if (attachmentsIndex.TryGetValue(keyId, out var a))
                    {
                        jiraIssueDto.IssueFields.SetProperty("attachment", a);
                    }
                    else
                    {
                        jiraIssueDto.IssueFields.SetProperty("attachment", emptyAttachments);
                    }
                    if (!commentsIndex.TryGetValue(keyId, out var c))
                    {
                        c = emptyComments;
                    }
                    jiraIssueDto.IssueFields.SetProperty("comment", new JiraIssueComments
                    {
                        Comments = c,
                        StartAt = 0,
                        Total = c.Length,
                        MaxResults = c.Length
                    });
                    issues.Add(jiraIssueDto);
                }

                return issues.ToArray();
            }));
        }