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

Commit

Permalink
Merge 78ad9f4 into 0405986
Browse files Browse the repository at this point in the history
  • Loading branch information
shana authored Feb 8, 2018
2 parents 0405986 + 78ad9f4 commit f741b66
Show file tree
Hide file tree
Showing 21 changed files with 1,228 additions and 403 deletions.
11 changes: 8 additions & 3 deletions src/GitHub.Api/SimpleApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async Task<Repository> GetRepositoryInternal()
if (repo != null)
{
hasWiki = await HasWikiInternal(repo);
isEnterprise = await IsEnterpriseInternal();
isEnterprise = isEnterprise ?? await IsEnterpriseInternal();
repositoryCache = repo;
}
owner = ownerLogin;
Expand All @@ -99,9 +99,14 @@ public bool HasWiki()
return hasWiki.HasValue && hasWiki.Value;
}

public bool IsEnterprise()
public async Task<bool> IsEnterprise()
{
return isEnterprise.HasValue && isEnterprise.Value;
if (!isEnterprise.HasValue)
{
isEnterprise = await IsEnterpriseInternal();
}

return isEnterprise ?? false;
}

async Task<bool> HasWikiInternal(Repository repo)
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.App/Api/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ static string GetMachineNameSafe()
}
catch (Exception e)
{
log.Information(e, "Failed to retrieve host name using `DNS.GetHostName`");
log.Warning(e, "Failed to retrieve host name using `DNS.GetHostName`");
try
{
return Environment.MachineName;
Expand Down
10 changes: 5 additions & 5 deletions src/GitHub.App/Services/TeamExplorerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void Refresh()
{
// Ignore when ActiveRepositories is empty and solution hasn't changed.
// https://github.com/github/VisualStudio/issues/1421
log.Information("Ignoring no ActiveRepository when solution hasn't changed");
log.Debug("Ignoring no ActiveRepository when solution hasn't changed");
}
else
{
Expand All @@ -76,22 +76,22 @@ void Refresh()

if (newRepositoryPath != repositoryPath)
{
log.Information("Fire PropertyChanged event for ActiveRepository");
log.Debug("Fire PropertyChanged event for ActiveRepository");
ActiveRepository = repo;
}
else if (newBranchName != branchName)
{
log.Information("Fire StatusChanged event when BranchName changes for ActiveRepository");
log.Debug("Fire StatusChanged event when BranchName changes for ActiveRepository");
StatusChanged?.Invoke(this, EventArgs.Empty);
}
else if (newHeadSha != headSha)
{
log.Information("Fire StatusChanged event when HeadSha changes for ActiveRepository");
log.Debug("Fire StatusChanged event when HeadSha changes for ActiveRepository");
StatusChanged?.Invoke(this, EventArgs.Empty);
}
else if (newTrackedSha != trackedSha)
{
log.Information("Fire StatusChanged event when TrackedSha changes for ActiveRepository");
log.Debug("Fire StatusChanged event when TrackedSha changes for ActiveRepository");
StatusChanged?.Invoke(this, EventArgs.Empty);
}

Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.App/ViewModels/Dialog/LoginTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ void HandleError(Exception ex)

if (ex.IsCriticalException()) return;

log.Information(ex, "Error logging into '{BaseUri}' as '{UsernameOrEmail}'", BaseUri, UsernameOrEmail);
log.Error(ex, "Error logging into '{BaseUri}' as '{UsernameOrEmail}'", BaseUri, UsernameOrEmail);
if (ex is Octokit.ForbiddenException)
{
Error = new UserError(Resources.LoginFailedForbiddenMessage, ex.Message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ async Task UpdateContent(ILocalRepositoryModel repository)
var repositoryUrl = repository.CloneUrl.ToRepositoryUrl();
var isDotCom = HostAddress.IsGitHubDotComUri(repositoryUrl);
var client = await apiClientFactory.Create(repository.CloneUrl);
var isEnterprise = isDotCom ? false : client.IsEnterprise();
var isEnterprise = isDotCom ? false : await client.IsEnterprise();

if ((isDotCom || isEnterprise) && await IsValidRepository(client))
{
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.Exports/Api/ISimpleApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface ISimpleApiClient
UriString OriginalUrl { get; }
Task<Repository> GetRepository();
bool HasWiki();
bool IsEnterprise();
Task<bool> IsEnterprise();
bool IsAuthenticated();
}
}
44 changes: 22 additions & 22 deletions src/GitHub.Exports/Models/UsageModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Reflection;

namespace GitHub.Models
namespace GitHub.Models
{
public class UsageModel
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815:OverrideEqualsAndOperatorEqualsOnValueTypes", Justification = "It'll use reflection by default and we're fine with that")]
public struct UsageModel
{
public bool IsGitHubUser { get; set; }
public bool IsEnterpriseUser { get; set; }
Expand Down Expand Up @@ -43,25 +41,27 @@ public class UsageModel

public UsageModel Clone(bool includeWeekly, bool includeMonthly)
{
var result = new UsageModel();
var properties = result.GetType().GetRuntimeProperties();

foreach (var property in properties)
{
var cloneValue = property.PropertyType == typeof(int);

if (property.Name == nameof(result.NumberOfStartupsWeek))
cloneValue = includeWeekly;
else if (property.Name == nameof(result.NumberOfStartupsMonth))
cloneValue = includeMonthly;
var result = this;
if (!includeWeekly)
result.NumberOfStartupsWeek = 0;
if (!includeMonthly)
result.NumberOfStartupsMonth = 0;
return result;
}

if (cloneValue)
{
var value = property.GetValue(this);
property.SetValue(result, value);
}
}
public UsageModel ClearCounters(bool clearWeekly, bool clearMonthly)
{
var result = new UsageModel();
if (!clearWeekly)
result.NumberOfStartupsWeek = NumberOfStartupsWeek;
if (!clearMonthly)
result.NumberOfStartupsMonth = NumberOfStartupsMonth;

result.IsGitHubUser = IsGitHubUser;
result.IsEnterpriseUser = IsEnterpriseUser;
result.AppVersion = AppVersion;
result.VSVersion = VSVersion;
result.Lang = Lang;
return result;
}
}
Expand Down
18 changes: 16 additions & 2 deletions src/GitHub.Exports/SimpleJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
using System.Runtime.Serialization;
using System.Text;
using GitHub.Reflection;
using System.Diagnostics;

// ReSharper disable LoopCanBeConvertedToQuery
// ReSharper disable RedundantExplicitArrayCreation
Expand Down Expand Up @@ -1838,7 +1839,13 @@ public static ConstructorDelegate GetConstructorByReflection(ConstructorInfo con
public static ConstructorDelegate GetConstructorByReflection(Type type, params Type[] argsType)
{
ConstructorInfo constructorInfo = GetConstructorInfo(type, argsType);
return constructorInfo == null ? null : GetConstructorByReflection(constructorInfo);
// if it's a value type (i.e., struct), it won't have a default constructor, so use Activator instead
return constructorInfo == null ? (type.IsValueType ? GetConstructorForValueType(type) : null) : GetConstructorByReflection(constructorInfo);
}

static ConstructorDelegate GetConstructorForValueType(Type type)
{
return delegate (object[] args) { return Activator.CreateInstance(type); };
}

#if !SIMPLE_JSON_NO_LINQ_EXPRESSION
Expand All @@ -1865,7 +1872,8 @@ public static ConstructorDelegate GetConstructorByExpression(ConstructorInfo con
public static ConstructorDelegate GetConstructorByExpression(Type type, params Type[] argsType)
{
ConstructorInfo constructorInfo = GetConstructorInfo(type, argsType);
return constructorInfo == null ? null : GetConstructorByExpression(constructorInfo);
// if it's a value type (i.e., struct), it won't have a default constructor, so use Activator instead
return constructorInfo == null ? (type.IsValueType ? GetConstructorForValueType(type) : null) : GetConstructorByExpression(constructorInfo);
}

#endif
Expand Down Expand Up @@ -1925,6 +1933,9 @@ public static SetDelegate GetSetMethod(PropertyInfo propertyInfo)
#if SIMPLE_JSON_NO_LINQ_EXPRESSION
return GetSetMethodByReflection(propertyInfo);
#else
// if it's a struct, we want to use reflection, as linq expressions modify copies of the object and not the real thing
if (propertyInfo.DeclaringType.IsValueType)
return GetSetMethodByReflection(propertyInfo);
return GetSetMethodByExpression(propertyInfo);
#endif
}
Expand All @@ -1934,6 +1945,9 @@ public static SetDelegate GetSetMethod(FieldInfo fieldInfo)
#if SIMPLE_JSON_NO_LINQ_EXPRESSION
return GetSetMethodByReflection(fieldInfo);
#else
// if it's a struct, we want to use reflection, as linq expressions modify copies of the object and not the real thing
if (fieldInfo.DeclaringType.IsValueType)
return GetSetMethodByReflection(fieldInfo);
return GetSetMethodByExpression(fieldInfo);
#endif
}
Expand Down
4 changes: 4 additions & 0 deletions src/GitHub.Logging/Logging/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ static Logger CreateLogger()

return new LoggerConfiguration()
.Enrich.WithThreadId()
#if DEBUG
.MinimumLevel.Debug()
#else
.MinimumLevel.Information()
#endif
.WriteTo.File(logPath,
fileSizeLimitBytes: null,
outputTemplate: outputTemplate)
Expand Down
8 changes: 2 additions & 6 deletions src/GitHub.TeamFoundation.14/Connect/GitHubConnectSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,7 @@ void ShowNotification(ILocalRepositoryModel newrepo, string msg)
}
})
);
#if DEBUG
log.Information("Notification");
#endif
log.Debug("Notification");
}

async Task RefreshRepositories()
Expand Down Expand Up @@ -496,12 +494,10 @@ void TrackState(object sender, PropertyChangedEventArgs e)
{
if (machine.PermittedTriggers.Contains(e.PropertyName))
{
#if DEBUG
log.Information("{PropertyName} title:{Title} busy:{IsBusy}",
log.Debug("{PropertyName} title:{Title} busy:{IsBusy}",
e.PropertyName,
((ITeamExplorerSection)sender).Title,
((ITeamExplorerSection)sender).IsBusy);
#endif
machine.Fire(e.PropertyName);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/GitHub.TeamFoundation.14/Services/VSGitExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public VSGitExt(IGitHubServiceProvider serviceProvider, IVSUIContextFactory fact
{
// If we're not in the UIContext or TryInitialize fails, have another go when the UIContext changes.
context.UIContextChanged += ContextChanged;
log.Information("VSGitExt will be initialized later");
log.Debug("VSGitExt will be initialized later");
InitializeTask = Task.CompletedTask;
}
}
Expand All @@ -69,7 +69,7 @@ void ContextChanged(object sender, VSUIContextChangedEventArgs e)
// Refresh ActiveRepositories on background thread so we don't delay UI context change.
InitializeTask = Task.Run(() => RefreshActiveRepositories());
context.UIContextChanged -= ContextChanged;
log.Information("Initialized VSGitExt on UIContextChanged");
log.Debug("Initialized VSGitExt on UIContextChanged");
}
}

Expand All @@ -86,11 +86,11 @@ bool TryInitialize()
}
};

log.Information("Found IGitExt service and initialized VSGitExt");
log.Debug("Found IGitExt service and initialized VSGitExt");
return true;
}

log.Information("Couldn't find IGitExt service");
log.Error("Couldn't find IGitExt service");
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.VisualStudio.UI/Base/TeamExplorerItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected async Task<RepositoryOrigin> GetRepositoryOrigin()
{
var repo = await SimpleApiClient.GetRepository();

if ((repo.FullName == ActiveRepoName || repo.Id == 0) && SimpleApiClient.IsEnterprise())
if ((repo.FullName == ActiveRepoName || repo.Id == 0) && await SimpleApiClient.IsEnterprise())
{
return RepositoryOrigin.Enterprise;
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.VisualStudio/Menus/MenuBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected async Task<bool> IsGitHubRepo()
{
var repo = await SimpleApiClient.GetRepository();
var activeRepoFullName = ActiveRepo.Owner + '/' + ActiveRepo.Name;
return (repo.FullName == activeRepoFullName || repo.Id == 0) && SimpleApiClient.IsEnterprise();
return (repo.FullName == activeRepoFullName || repo.Id == 0) && await SimpleApiClient.IsEnterprise();
}
return isdotcom;
}
Expand Down
14 changes: 9 additions & 5 deletions src/GitHub.VisualStudio/Services/UsageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
using GitHub.Helpers;
using GitHub.Models;
using Task = System.Threading.Tasks.Task;
using GitHub.Logging;
using Serilog;

namespace GitHub.Services
{
[Export(typeof(IUsageService))]
public class UsageService : IUsageService
{
static readonly ILogger log = LogManager.ForContext<UsageService>();
const string StoreFileName = "ghfvs.usage";
static readonly Calendar cal = CultureInfo.InvariantCulture.Calendar;
readonly IGitHubServiceProvider serviceProvider;
Expand All @@ -32,12 +35,12 @@ public bool IsSameDay(DateTimeOffset lastUpdated)

public bool IsSameWeek(DateTimeOffset lastUpdated)
{
return GetIso8601WeekOfYear(lastUpdated) == GetIso8601WeekOfYear(DateTimeOffset.Now);
return GetIso8601WeekOfYear(lastUpdated) == GetIso8601WeekOfYear(DateTimeOffset.Now) && lastUpdated.Year == DateTimeOffset.Now.Year;
}

public bool IsSameMonth(DateTimeOffset lastUpdated)
{
return lastUpdated.Month == DateTimeOffset.Now.Month;
return lastUpdated.Month == DateTimeOffset.Now.Month && lastUpdated.Year == DateTimeOffset.Now.Year;
}

public IDisposable StartTimer(Func<Task> callback, TimeSpan dueTime, TimeSpan period)
Expand Down Expand Up @@ -65,8 +68,9 @@ public async Task<UsageData> ReadLocalData()
SimpleJson.DeserializeObject<UsageData>(json) :
new UsageData { Model = new UsageModel() };
}
catch
catch(Exception ex)
{
log.Error(ex, "Error deserializing usage");
return new UsageData { Model = new UsageModel() };
}
}
Expand All @@ -79,9 +83,9 @@ public async Task WriteLocalData(UsageData data)
var json = SimpleJson.SerializeObject(data);
await WriteAllTextAsync(storePath, json);
}
catch
catch(Exception ex)
{
// log.Warn("Failed to write usage data", ex);
log.Error(ex,"Failed to write usage data");
}
}

Expand Down
Loading

0 comments on commit f741b66

Please sign in to comment.