|
24 | 24 | using SonarLint.VisualStudio.Core.Binding; |
25 | 25 | using SonarLint.VisualStudio.SLCore.Listener.Branch; |
26 | 26 |
|
27 | | -namespace SonarLint.VisualStudio.ConnectedMode |
| 27 | +namespace SonarLint.VisualStudio.ConnectedMode; |
| 28 | + |
| 29 | +[Export(typeof(IServerBranchProvider))] |
| 30 | +internal class ServerBranchProvider : IServerBranchProvider |
28 | 31 | { |
29 | | - [Export(typeof(IServerBranchProvider))] |
30 | | - internal class ServerBranchProvider : IServerBranchProvider |
| 32 | + /// <summary> |
| 33 | + /// Factory method to create and return an <see cref="IRepository"/> instance. |
| 34 | + /// </summary> |
| 35 | + /// <remarks>Only used for testing</remarks> |
| 36 | + internal delegate IRepository CreateRepositoryObject(string repoRootPath); |
| 37 | + |
| 38 | + private readonly IConfigurationProvider configurationProvider; |
| 39 | + private readonly IGitWorkspaceService gitWorkspaceService; |
| 40 | + private readonly IBranchMatcher branchMatcher; |
| 41 | + private readonly ILogger logger; |
| 42 | + private readonly CreateRepositoryObject createRepo; |
| 43 | + |
| 44 | + [ImportingConstructor] |
| 45 | + public ServerBranchProvider( |
| 46 | + IConfigurationProvider configurationProvider, |
| 47 | + IGitWorkspaceService gitWorkspaceService, |
| 48 | + IBranchMatcher branchMatcher, |
| 49 | + ILogger logger) |
| 50 | + : this(configurationProvider, gitWorkspaceService, branchMatcher, logger, DoCreateRepo) |
31 | 51 | { |
32 | | - /// <summary> |
33 | | - /// Factory method to create and return an <see cref="IRepository"/> instance. |
34 | | - /// </summary> |
35 | | - /// <remarks>Only used for testing</remarks> |
36 | | - internal delegate IRepository CreateRepositoryObject(string repoRootPath); |
37 | | - |
38 | | - private readonly IConfigurationProvider configurationProvider; |
39 | | - private readonly IGitWorkspaceService gitWorkspaceService; |
40 | | - private readonly IBranchMatcher branchMatcher; |
41 | | - private readonly ILogger logger; |
42 | | - private readonly CreateRepositoryObject createRepo; |
43 | | - |
44 | | - [ImportingConstructor] |
45 | | - public ServerBranchProvider( |
46 | | - IConfigurationProvider configurationProvider, |
47 | | - IGitWorkspaceService gitWorkspaceService, |
48 | | - IBranchMatcher branchMatcher, |
49 | | - ILogger logger) |
50 | | - : this(configurationProvider, gitWorkspaceService, branchMatcher, logger, DoCreateRepo) |
51 | | - { |
52 | | - } |
53 | | - |
54 | | - internal /* for testing */ ServerBranchProvider( |
55 | | - IConfigurationProvider configurationProvider, |
56 | | - IGitWorkspaceService gitWorkspaceService, |
57 | | - IBranchMatcher branchMatcher, |
58 | | - ILogger logger, |
59 | | - CreateRepositoryObject createRepo) |
60 | | - { |
61 | | - this.configurationProvider = configurationProvider; |
62 | | - this.gitWorkspaceService = gitWorkspaceService; |
63 | | - this.branchMatcher = branchMatcher; |
64 | | - this.logger = logger; |
65 | | - this.createRepo = createRepo; |
66 | | - } |
67 | | - |
68 | | - public string GetServerBranchName(List<RemoteBranch> branches) |
69 | | - { |
70 | | - var config = configurationProvider.GetConfiguration(); |
| 52 | + } |
71 | 53 |
|
72 | | - if (config.Mode == SonarLintMode.Standalone) |
73 | | - { |
74 | | - logger.LogVerbose(Resources.BranchProvider_NotInConnectedMode); |
75 | | - return null; |
76 | | - } |
| 54 | + internal /* for testing */ ServerBranchProvider( |
| 55 | + IConfigurationProvider configurationProvider, |
| 56 | + IGitWorkspaceService gitWorkspaceService, |
| 57 | + IBranchMatcher branchMatcher, |
| 58 | + ILogger logger, |
| 59 | + CreateRepositoryObject createRepo) |
| 60 | + { |
| 61 | + this.configurationProvider = configurationProvider; |
| 62 | + this.gitWorkspaceService = gitWorkspaceService; |
| 63 | + this.branchMatcher = branchMatcher; |
| 64 | + this.logger = logger; |
| 65 | + this.createRepo = createRepo; |
| 66 | + } |
77 | 67 |
|
78 | | - var matchingBranchName = CalculateMatchingBranch(config, branches); |
| 68 | + public string GetServerBranchName(List<RemoteBranch> branches) |
| 69 | + { |
| 70 | + var config = configurationProvider.GetConfiguration(); |
79 | 71 |
|
80 | | - if (matchingBranchName == null) |
81 | | - { |
82 | | - logger.LogVerbose(Resources.BranchProvider_FailedToCalculateMatchingBranch); |
| 72 | + if (config.Mode == SonarLintMode.Standalone) |
| 73 | + { |
| 74 | + logger.LogVerbose(Resources.BranchProvider_NotInConnectedMode); |
| 75 | + return null; |
| 76 | + } |
83 | 77 |
|
84 | | - matchingBranchName = branches.First(rb => rb.IsMain).Name; |
85 | | - } |
| 78 | + var matchingBranchName = CalculateMatchingBranch(config, branches); |
86 | 79 |
|
87 | | - Debug.Assert(matchingBranchName != null); |
| 80 | + if (matchingBranchName == null) |
| 81 | + { |
| 82 | + logger.LogVerbose(Resources.BranchProvider_FailedToCalculateMatchingBranch); |
88 | 83 |
|
89 | | - logger.WriteLine(Resources.BranchProvider_MatchingServerBranchName, matchingBranchName); |
90 | | - return matchingBranchName; |
| 84 | + matchingBranchName = branches.First(rb => rb.IsMain).Name; |
91 | 85 | } |
92 | 86 |
|
93 | | - private string CalculateMatchingBranch(BindingConfiguration config, List<RemoteBranch> branches) |
94 | | - { |
95 | | - var gitRepoRoot = gitWorkspaceService.GetRepoRoot(); |
| 87 | + Debug.Assert(matchingBranchName != null); |
96 | 88 |
|
97 | | - if (gitRepoRoot == null) |
98 | | - { |
99 | | - logger.LogVerbose(Resources.BranchProvider_CouldNotDetectGitRepo); |
100 | | - return null; |
101 | | - } |
| 89 | + logger.WriteLine(Resources.BranchProvider_MatchingServerBranchName, matchingBranchName); |
| 90 | + return matchingBranchName; |
| 91 | + } |
102 | 92 |
|
103 | | - var repo = createRepo(gitRepoRoot); |
104 | | - var branchName = branchMatcher.GetMatchingBranch(config.Project.ServerProjectKey, repo, branches); |
| 93 | + private string CalculateMatchingBranch(BindingConfiguration config, List<RemoteBranch> branches) |
| 94 | + { |
| 95 | + var gitRepoRoot = gitWorkspaceService.GetRepoRoot(); |
105 | 96 |
|
106 | | - return branchName; |
| 97 | + if (gitRepoRoot == null) |
| 98 | + { |
| 99 | + logger.LogVerbose(Resources.BranchProvider_CouldNotDetectGitRepo); |
| 100 | + return null; |
107 | 101 | } |
108 | 102 |
|
109 | | - private static IRepository DoCreateRepo(string repoRootPath) => new Repository(repoRootPath); |
| 103 | + var repo = createRepo(gitRepoRoot); |
| 104 | + var branchName = branchMatcher.GetMatchingBranch(config.Project.ServerProjectKey, repo, branches); |
| 105 | + |
| 106 | + return branchName; |
110 | 107 | } |
| 108 | + |
| 109 | + private static IRepository DoCreateRepo(string repoRootPath) => new Repository(repoRootPath); |
111 | 110 | } |
0 commit comments