• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / RepoGovernance / 3707968538

pending completion
3707968538

push

github

GitHub
Update to dora metrics (#280)

130 of 202 branches covered (64.36%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 2 files covered. (100.0%)

395 of 543 relevant lines covered (72.74%)

6.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.27
/src/RepoGovernance.Core/APIAccess/SonarCloudAPI.cs
1
using Newtonsoft.Json;
2
using RepoGovernance.Core.Models;
3

4
namespace RepoGovernance.Core.APIAccess
5
{
6
    public static class SonarCloudAPI
7
    {
8
        public async static Task<SonarCloud?> GetSonarCloudMetrics(string owner, string repo)
9
        {
1✔
10
            string? codeSmellsBadgeImage = await GetSonarCloudCodeSmells(owner, repo);
1✔
11
            string? linesOfCodeBadgeImage = await GetSonarCloudLinesOfCode(owner, repo);
1✔
12

13
            SonarCloud? sonarCloud = new();
1✔
14
            if (string.IsNullOrEmpty(codeSmellsBadgeImage) == false)
1!
15
            {
×
16
                sonarCloud.CodeSmellsBadgeImage = codeSmellsBadgeImage;
×
17
                sonarCloud.CodeSmellsLink = $"https://sonarcloud.io/project/issues?resolved=false&id={owner}_{repo}";
×
18
            }
×
19
            if (string.IsNullOrEmpty(linesOfCodeBadgeImage) == false)
1!
20
            {
×
21
                sonarCloud.LinesOfCodeBadgeImage = linesOfCodeBadgeImage;
×
22
                sonarCloud.LinesOfCodeLink = $"https://sonarcloud.io/component_measures?metric=ncloc&id={owner}_{repo}";
×
23
            }
×
24
            //If there were no links, return null
25
            if (string.IsNullOrEmpty(codeSmellsBadgeImage) == true &&
1!
26
                string.IsNullOrEmpty(linesOfCodeBadgeImage) == true)
1✔
27
            {
1✔
28
                sonarCloud = null;
1✔
29
            }
1✔
30
            return sonarCloud;
1✔
31
        }
1✔
32

33
        public async static Task<string?> GetSonarCloudCodeSmells(string owner, string repo)
34
        {
1✔
35
            string url = $"https://sonarcloud.io/api/project_badges/measure?project={owner}_{repo}&metric=code_smells";
1✔
36
            return await GetResponseString(new(), url, true);
1✔
37
        }
1✔
38

39
        public async static Task<string?> GetSonarCloudLinesOfCode(string owner, string repo)
40
        {
1✔
41
            string url = $"https://sonarcloud.io/api/project_badges/measure?project={owner}_{repo}&metric=ncloc";
1✔
42
            return await GetResponseString(new(), url, true);
1✔
43
        }
1✔
44

45
        private async static Task<string?> GetResponseString(HttpClient client, string url, bool ignoreErrors = false)
46
        {
2✔
47
            string? obj = null;
2✔
48
            if (client != null && url != null)
2!
49
            {
2✔
50
                //Debug.WriteLine("Running url: " + client.BaseAddress.ToString() + url);
51
                //Console.WriteLine("Running url: " + client.BaseAddress.ToString() + url);
52
                using (HttpResponseMessage response = await client.GetAsync(url))
2✔
53
                {
2✔
54
                    if (ignoreErrors == true || response.IsSuccessStatusCode == true)
2!
55
                    {
2✔
56
                        string responseBody = await response.Content.ReadAsStringAsync();
2✔
57
                        if (string.IsNullOrEmpty(responseBody) == false &&
2!
58
                            responseBody.Contains("Project not found") == false &&
2✔
59
                            responseBody.Contains("Measure has not been found") == false)
2✔
60
                        {
×
61
                            obj = responseBody;
×
62
                        }
×
63
                    }
2✔
64
                    else
65
                    {
×
66
                        //Throw an exception
67
                        response.EnsureSuccessStatusCode();
×
68
                    }
×
69
                }
2✔
70
            }
2✔
71
            return obj;
2✔
72
        }
2✔
73

74
    }
75
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc