Пример #1
0
        private IList <Issue> GetIssuesForComponent(Component component, string branchName, bool onlyFalsePositivesAndWontFixes)
        {
            Logger.LogDebug("Getting list of issues for component {0} (branch {1})", component.Key, branchName);

            var result = new List <Issue>();

            const int pageSize  = 500;
            var       pageIndex = 1;

            do
            {
                var uri = new Uri(string.Format(CultureInfo.InvariantCulture,
                                                "{0}/api/issues/search?componentKeys={1}&p={2}&ps={3}{4}{5}",
                                                SonarQubeUrl,
                                                component.Key,
                                                pageIndex,
                                                pageSize,
                                                onlyFalsePositivesAndWontFixes ? "&resolutions=FALSE-POSITIVE,WONTFIX" : string.Empty,
                                                !string.IsNullOrEmpty(branchName) ? "&branch=" + branchName : string.Empty));

                var responseContent = GetFromServer(uri);
                if (!string.IsNullOrEmpty(responseContent))
                {
                    ApiIssuesSearchResult apiResult = JsonConvert.DeserializeObject <ApiIssuesSearchResult>(responseContent);
                    result.AddRange(apiResult.Issues);
                    if (apiResult.Paging.Total < (apiResult.Paging.PageIndex * apiResult.Paging.PageSize))
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }

                pageIndex++;
            } while (true);

            Logger.LogDebug("\t{0} issues found", result.Count);

            return(result);
        }
Пример #2
0
        private int GetNumberOfIssuesForProject(string projectKey, string branchName, bool onlyFalsePositivesAndWontFixes)
        {
            Logger.LogDebug("Getting number of issues for project {0} (branch {1})", projectKey, branchName);

            var uri = new Uri(string.Format(CultureInfo.InvariantCulture,
                                            "{0}/api/issues/search?projectKeys={1}&p=1&ps=1{2}{3}",
                                            SonarQubeUrl,
                                            projectKey,
                                            onlyFalsePositivesAndWontFixes ? "&resolutions=FALSE-POSITIVE,WONTFIX" : string.Empty,
                                            !string.IsNullOrEmpty(branchName) ? "&branch=" + branchName : string.Empty));

            var responseContent = GetFromServer(uri);

            if (!string.IsNullOrEmpty(responseContent))
            {
                ApiIssuesSearchResult apiResult = JsonConvert.DeserializeObject <ApiIssuesSearchResult>(responseContent);
                return(apiResult.Paging.Total);
            }

            return(-1);
        }
Пример #3
0
        public List <Issue> GetIssuesForProject(string projectKey, string branchName, bool onlyFalsePositivesAndWontFixes)
        {
            var result = new List <Issue>();

            Logger.LogDebug("Getting list of issues for project {0} (branch {1})", projectKey, branchName);

            // SonarQube cannot return more than 10000 issues in one response.
            // Let's try to find out, what the number of issues is
            var numberOfIssues = GetNumberOfIssuesForProject(projectKey, branchName, onlyFalsePositivesAndWontFixes);

            if (numberOfIssues > 0)
            {
                if (numberOfIssues < 10000)
                {
                    const int pageSize  = 500;
                    var       pageIndex = 1;
                    do
                    {
                        var uri = new Uri(string.Format(CultureInfo.InvariantCulture,
                                                        "{0}/api/issues/search?projectKeys={1}&additionalFields=comments&p={2}&ps={3}{4}{5}",
                                                        SonarQubeUrl,
                                                        projectKey,
                                                        pageIndex,
                                                        pageSize,
                                                        onlyFalsePositivesAndWontFixes ? "&resolutions=FALSE-POSITIVE,WONTFIX" : string.Empty,
                                                        !string.IsNullOrEmpty(branchName) ? "&branch=" + branchName : string.Empty));

                        var responseContent = GetFromServer(uri);
                        if (!string.IsNullOrEmpty(responseContent))
                        {
                            ApiIssuesSearchResult apiResult = JsonConvert.DeserializeObject <ApiIssuesSearchResult>(responseContent);
                            result.AddRange(apiResult.Issues);
                            if (apiResult.Paging.Total < (apiResult.Paging.PageIndex * apiResult.Paging.PageSize))
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }

                        pageIndex++;
                    } while (true);
                }
                else
                {
                    // If the number of issues is too high, we need to get their list by components
                    var components = GetProjectComponents(projectKey, branchName);
                    if (components != null)
                    {
                        foreach (var component in components)
                        {
                            result.AddRange(GetIssuesForComponent(component, branchName, onlyFalsePositivesAndWontFixes));
                        }
                    }
                }
            }

            Logger.LogDebug("\t{0} issues found", result.Count);

            return(result);
        }