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

samsmithnz / RepoAutomation / 3895898324

pending completion
3895898324

push

github

GitHub
Merge pull request #212 from samsmithnz/FixToLanguageTest

333 of 474 branches covered (70.25%)

Branch coverage included in aggregate %.

795 of 893 relevant lines covered (89.03%)

6.27 hits per line

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

96.15
/src/RepoAutomation.Core/Helpers/RepoLanguageHelper.cs
1
using GitHubActionsDotNet.Models;
2
using Newtonsoft.Json;
3
using RepoAutomation.Core.APIAccess;
4
using RepoAutomation.Core.Models;
5
using System.Text;
6
using YamlDotNet.Serialization;
7

8
namespace RepoAutomation.Core.Helpers
9
{
10
    public static class RepoLanguageHelper
11
    {
12
        public async static Task<List<RepoLanguage>> GetRepoLanguages(string? clientId, string? clientSecret,
13
            string owner, string repo)
14
        {
1✔
15
            Dictionary<string, int>? languages = await GitHubApiAccess.GetRepoLanguages(clientId, clientSecret, owner, repo);
1✔
16
            //Get language definition from from linguist repo
17
            string languageDefinitionOwner = "github";
1✔
18
            string languageDefinitionRepository = "linguist";
1✔
19
            string? languageDefinitionFilePath = "lib/linguist/languages.yml";
1✔
20
            //Don't use the PAT token here - as it's a different organization - instead let's rely on anon rates
21
            GitHubFile? fileResult = await GitHubFiles.GetFileContents(null, null,
1✔
22
                languageDefinitionOwner, languageDefinitionRepository, languageDefinitionFilePath);
1✔
23
            Dictionary<string, LanguageDefinition>? repoLanguageDetails = null;
1✔
24
            if (fileResult != null && fileResult.content != null)
1!
25
            {
1✔
26
                IDeserializer deserializer = new DeserializerBuilder()
1✔
27
                    .IgnoreUnmatchedProperties() //Ignore YAML that doesn't have a matching .NET property
1✔
28
                    .Build();
1✔
29
                repoLanguageDetails = deserializer.Deserialize<Dictionary<string, LanguageDefinition>>(fileResult.content);
1✔
30
            }
1✔
31
            List<RepoLanguage> repoLanguages = RepoLanguageHelper.TransformRepoLanguages(languages, repoLanguageDetails);
1✔
32

33
            return repoLanguages;
1✔
34
        }
1✔
35

36

37
        public static List<RepoLanguage> TransformRepoLanguages(Dictionary<string, int> languages, Dictionary<string, LanguageDefinition>? repoLanguageDetails)
38
        {
1✔
39
            List<RepoLanguage> repoLanguages = new();
1✔
40

41
            //Get the total for the percent calculation
42
            int total = 0;
1✔
43
            foreach (KeyValuePair<string, int> item in languages)
13✔
44
            {
5✔
45
                total += item.Value;
5✔
46
            }
5✔
47

48
            //Build the language with the percent calculation
49
            foreach (KeyValuePair<string, int> item in languages)
13✔
50
            {
5✔
51
                repoLanguages.Add(new()
5✔
52
                {
5✔
53
                    Name = item.Key,
5✔
54
                    Total = item.Value,
5✔
55
                    Percent = Math.Round((decimal)item.Value / total * 100, 1)
5✔
56
                });
5✔
57
                if (repoLanguageDetails != null && repoLanguageDetails.ContainsKey(item.Key))
5!
58
                {
5✔
59
                    repoLanguages[^1].Color = repoLanguageDetails[item.Key].color;
5✔
60
                }
5✔
61
            }
5✔
62

63
            return repoLanguages;
1✔
64
        }
1✔
65
    }
66
}
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

© 2025 Coveralls, Inc