Пример #1
0
        public async Task <JiraIssuesResponse> SelectIssuesAsync(JiraIssuesRequest request,
                                                                 CancellationToken cancellationToken)
        {
            var jqlCommand = JqlCompiler.Compile(request.Jql, metadataProvider);
            var issues     = jqlCommand.Execute(await store.GetAll());
            var maxResults = request.MaxResults == 0
                ? maxPacketSize
                : Math.Min(request.MaxResults, maxPacketSize);
            var expand = request.Expand == null
                ? null
                : string.Join(",", request.Expand);

            return(new JiraIssuesResponse
            {
                Total = issues.Length,
                StartAt = request.StartAt,
                MaxResults = maxResults,
                Expand = expand,
                Issues = issues.Skip(request.StartAt).Take(maxResults)
                         .Select(i =>
                {
                    var queriedFields = Restrict(i.IssueFields, request.Fields);
                    var controller = new JiraIssueFieldsController(queriedFields);
                    return new JiraIssue(controller)
                    {
                        Key = i.Key,
                        Id = i.Id,
                        Self = i.Self,
                        Expand = expand,
                    };
                })
                         .ToArray()
            });
        }
Пример #2
0
        public async Task <JiraIssuesResponse> SelectIssuesAsync(JiraIssuesRequest request,
                                                                 CancellationToken cancellationToken)
        {
            var requestModel = AutoMapper.Create <JiraIssuesRequest, JiraSearchRequestModel>().Map(request);
            var requestJson  = Json.Serialize(requestModel);

            cancellationToken.ThrowIfCancellationRequested();
            var responseMessage = await jira.PostAsync(hostUrl + "/rest/api/2/search",
                                                       new StringContent(requestJson, Encoding.UTF8, "application/json"),
                                                       cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            if (!responseMessage.IsSuccessStatusCode)
            {
                switch (responseMessage.StatusCode)
                {
                case HttpStatusCode.BadRequest:
                {
                    var body = await responseMessage.Content.ReadAsStringAsync();

                    cancellationToken.ThrowIfCancellationRequested();
                    var errors        = Json.Deserialize <JiraErrorMessages>(body);
                    var errorMessages = errors?.ErrorMessages ?? new string[0];
                    throw new JqlCompilationException(string.Join("\n", errorMessages));
                }

                case HttpStatusCode.Unauthorized:
                    throw new JiraAuthorizationException();

                default:
                    responseMessage.EnsureSuccessStatusCode();
                    break;
                }
            }

            var responseJson = await responseMessage.Content.ReadAsStringAsync();

            cancellationToken.ThrowIfCancellationRequested();
            var response = Json.Deserialize <JiraSearchResponseModel>(responseJson);

            return(new JiraIssuesResponse
            {
                Expand = response.Expand,
                Total = response.Total,
                MaxResults = response.MaxResults,
                StartAt = response.StartAt,
                Issues = response.Issues.Select(ToIssue).ToArray()
            });
        }