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

samsmithnz / RepoAutomation / 8601094164

08 Apr 2024 01:38PM UTC coverage: 80.478%. Remained the same
8601094164

push

github

web-flow
Merge pull request #300 from samsmithnz/PackageUpdatesApr8

Package updates

339 of 496 branches covered (68.35%)

Branch coverage included in aggregate %.

807 of 928 relevant lines covered (86.96%)

7.76 hits per line

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

96.55
/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
        {
2✔
15
            Dictionary<string, int>? languages = await GitHubApiAccess.GetRepoLanguages(clientId, clientSecret, owner, repo);
2✔
16
            //Get language definition from from linguist repo
17
            string languageDefinitionOwner = "github-linguist";
2✔
18
            string languageDefinitionRepository = "linguist";
2✔
19
            string? languageDefinitionFilePath = "lib/linguist/languages.yml";
2✔
20
            //Don't use the PAT token here - as it's going against the linguist organization - instead let's rely on anon rates
21
            GitHubFile? fileResult = await GitHubFiles.GetFileContents(null, null,
2✔
22
                languageDefinitionOwner, languageDefinitionRepository, languageDefinitionFilePath);
2✔
23
            Dictionary<string, LanguageDefinition>? repoLanguageDetails = null;
2✔
24
            if (fileResult != null && fileResult.content != null)
2!
25
            {
2✔
26
                IDeserializer deserializer = new DeserializerBuilder()
2✔
27
                    .IgnoreUnmatchedProperties() //Ignore YAML that doesn't have a matching .NET property
2✔
28
                    .Build();
2✔
29
                repoLanguageDetails = deserializer.Deserialize<Dictionary<string, LanguageDefinition>>(fileResult.content);
2✔
30
            }
2✔
31
            List<RepoLanguage>? repoLanguages = null;
2✔
32
            if (repoLanguageDetails != null)
2✔
33
            {
2✔
34
                repoLanguages = TransformRepoLanguages(languages, repoLanguageDetails);
2✔
35
            }
2✔
36

37
            return repoLanguages;
2✔
38
        }
2✔
39

40

41
        public static List<RepoLanguage> TransformRepoLanguages(Dictionary<string, int> languages, Dictionary<string, LanguageDefinition>? repoLanguageDetails)
42
        {
2✔
43
            List<RepoLanguage> repoLanguages = new();
2✔
44

45
            //Get the total for the percent calculation
46
            int total = 0;
2✔
47
            foreach (KeyValuePair<string, int> item in languages)
26✔
48
            {
10✔
49
                total += item.Value;
10✔
50
            }
10✔
51

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

67
            return repoLanguages;
2✔
68
        }
2✔
69
    }
70
}
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