Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 897340d

Browse files
authored
Merge branch 'master' into fixes/1654-pr-authoring-link
2 parents a3b4057 + b276534 commit 897340d

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

src/GitHub.InlineReviews/Services/PullRequestStatusBarManager.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
using System.Windows.Controls.Primitives;
66
using System.ComponentModel.Composition;
77
using System.Reactive.Linq;
8+
using System.Threading.Tasks;
89
using GitHub.Commands;
10+
using GitHub.Extensions;
11+
using GitHub.Primitives;
912
using GitHub.InlineReviews.Views;
1013
using GitHub.InlineReviews.ViewModels;
1114
using GitHub.Services;
@@ -32,6 +35,7 @@ public class PullRequestStatusBarManager
3235
// TeamExplorerContext needs to retrieve DTE using GetService.
3336
readonly Lazy<IPullRequestSessionManager> pullRequestSessionManager;
3437
readonly Lazy<ITeamExplorerContext> teamExplorerContext;
38+
readonly Lazy<IConnectionManager> connectionManager;
3539

3640
IDisposable currentSessionSubscription;
3741

@@ -41,7 +45,8 @@ public PullRequestStatusBarManager(
4145
IOpenPullRequestsCommand openPullRequestsCommand,
4246
IShowCurrentPullRequestCommand showCurrentPullRequestCommand,
4347
Lazy<IPullRequestSessionManager> pullRequestSessionManager,
44-
Lazy<ITeamExplorerContext> teamExplorerContext)
48+
Lazy<ITeamExplorerContext> teamExplorerContext,
49+
Lazy<IConnectionManager> connectionManager)
4550
{
4651
this.openPullRequestsCommand = new UsageTrackingCommand(usageTracker,
4752
x => x.NumberOfStatusBarOpenPullRequestList, openPullRequestsCommand);
@@ -50,6 +55,7 @@ public PullRequestStatusBarManager(
5055

5156
this.pullRequestSessionManager = pullRequestSessionManager;
5257
this.teamExplorerContext = teamExplorerContext;
58+
this.connectionManager = connectionManager;
5359
}
5460

5561
/// <summary>
@@ -76,24 +82,55 @@ void RefreshActiveRepository(ILocalRepositoryModel repository)
7682
{
7783
currentSessionSubscription?.Dispose();
7884
currentSessionSubscription = pullRequestSessionManager.Value.WhenAnyValue(x => x.CurrentSession)
79-
.Subscribe(x => RefreshCurrentSession(repository, x));
85+
.Subscribe(x => RefreshCurrentSession(repository, x).Forget());
8086
}
8187

82-
void RefreshCurrentSession(ILocalRepositoryModel repository, IPullRequestSession session)
88+
async Task RefreshCurrentSession(ILocalRepositoryModel repository, IPullRequestSession session)
8389
{
84-
var cloneUrl = repository?.CloneUrl;
85-
if (cloneUrl != null)
90+
try
8691
{
87-
// Only show PR status bar if repo has remote
92+
var showStatus = await IsDotComOrEnterpriseRepository(repository);
93+
if (!showStatus)
94+
{
95+
ShowStatus(null);
96+
return;
97+
}
98+
8899
var viewModel = CreatePullRequestStatusViewModel(session);
89100
ShowStatus(viewModel);
90101
}
91-
else
102+
catch (Exception e)
92103
{
93-
ShowStatus(null);
104+
log.Error(e, nameof(RefreshCurrentSession));
94105
}
95106
}
96107

108+
async Task<bool> IsDotComOrEnterpriseRepository(ILocalRepositoryModel repository)
109+
{
110+
var cloneUrl = repository?.CloneUrl;
111+
if (cloneUrl == null)
112+
{
113+
// No active repository or remote
114+
return false;
115+
}
116+
117+
var isDotCom = HostAddress.IsGitHubDotComUri(cloneUrl.ToRepositoryUrl());
118+
if (isDotCom)
119+
{
120+
// This is a github.com repository
121+
return true;
122+
}
123+
124+
var connection = await connectionManager.Value.GetConnection(repository);
125+
if (connection != null)
126+
{
127+
// This is an enterprise repository
128+
return true;
129+
}
130+
131+
return false;
132+
}
133+
97134
PullRequestStatusViewModel CreatePullRequestStatusViewModel(IPullRequestSession session)
98135
{
99136
var pullRequestStatusViewModel = new PullRequestStatusViewModel(openPullRequestsCommand, showCurrentPullRequestCommand);

src/GitHub.InlineReviews/Views/PullRequestStatusView.xaml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,11 @@
4949
VerticalAlignment="Bottom"
5050
Margin="0 0 4 0 "
5151
Icon="git_pull_request" />
52-
<TextBlock VerticalAlignment="Center">
53-
Pull requests
54-
</TextBlock>
5552
</StackPanel>
5653
</Button>
5754
<Grid.ToolTip>
5855
<TextBlock VerticalAlignment="Center">
59-
Open or Create a Pull request
56+
View, Checkout or Create a Pull request
6057
</TextBlock>
6158
</Grid.ToolTip>
6259
</Grid>

src/GitHub.VisualStudio/Services/UsageService.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel.Composition;
4-
using System.Globalization;
54
using System.IO;
65
using System.Text;
76
using System.Threading;
@@ -17,14 +16,15 @@
1716
namespace GitHub.Services
1817
{
1918
[Export(typeof(IUsageService))]
20-
public class UsageService : IUsageService
19+
public sealed class UsageService : IUsageService, IDisposable
2120
{
2221
const string StoreFileName = "metrics.json";
2322
const string UserStoreFileName = "user.json";
2423
static readonly ILogger log = LogManager.ForContext<UsageService>();
2524

2625
readonly IGitHubServiceProvider serviceProvider;
2726
readonly IEnvironment environment;
27+
readonly SemaphoreSlim writeSemaphoreSlim = new SemaphoreSlim(1, 1);
2828

2929
string storePath;
3030
string userStorePath;
@@ -37,6 +37,11 @@ public UsageService(IGitHubServiceProvider serviceProvider, IEnvironment environ
3737
this.environment = environment;
3838
}
3939

40+
public void Dispose()
41+
{
42+
writeSemaphoreSlim.Dispose();
43+
}
44+
4045
public async Task<Guid> GetUserGuid()
4146
{
4247
await Initialize();
@@ -102,7 +107,7 @@ public async Task<UsageData> ReadLocalData()
102107
SimpleJson.DeserializeObject<UsageData>(json) :
103108
new UsageData { Reports = new List<UsageModel>() };
104109
}
105-
catch(Exception ex)
110+
catch (Exception ex)
106111
{
107112
log.Error(ex, "Error deserializing usage");
108113
return new UsageData { Reports = new List<UsageModel>() };
@@ -115,11 +120,12 @@ public async Task WriteLocalData(UsageData data)
115120
{
116121
Directory.CreateDirectory(Path.GetDirectoryName(storePath));
117122
var json = SimpleJson.SerializeObject(data);
123+
118124
await WriteAllTextAsync(storePath, json);
119125
}
120126
catch (Exception ex)
121127
{
122-
log.Error(ex,"Failed to write usage data");
128+
log.Error(ex, "Failed to write usage data");
123129
}
124130
}
125131

@@ -149,10 +155,19 @@ async Task<string> ReadAllTextAsync(string path)
149155

150156
async Task WriteAllTextAsync(string path, string text)
151157
{
152-
using (var s = new FileStream(path, FileMode.Create))
153-
using (var w = new StreamWriter(s, Encoding.UTF8))
158+
// Avoid IOException when metrics updated multiple times in quick succession
159+
await writeSemaphoreSlim.WaitAsync();
160+
try
161+
{
162+
using (var s = new FileStream(path, FileMode.Create))
163+
using (var w = new StreamWriter(s, Encoding.UTF8))
164+
{
165+
await w.WriteAsync(text);
166+
}
167+
}
168+
finally
154169
{
155-
await w.WriteAsync(text);
170+
writeSemaphoreSlim.Release();
156171
}
157172
}
158173

0 commit comments

Comments
 (0)