Skip to content

Commit

Permalink
Add StylCop analyzer
Browse files Browse the repository at this point in the history
- Reformatted code slightly
- Change the executable name to not conflict with the official Clockify
  Desktop application
- Defined a publish directory for self-contained publishing
- Remove exception handling used to debug an issue
  • Loading branch information
eXpl0it3r committed Feb 21, 2022
1 parent 1e2bc4a commit f143a12
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 66 deletions.
18 changes: 14 additions & 4 deletions Clockify.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
<TargetFrameworks>net5.0-windows</TargetFrameworks>
<UseWindowsForms>false</UseWindowsForms>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<AssemblyName>Clockify</AssemblyName>
<RootNamespace></RootNamespace>
<AssemblyName>dev.duerrenberger.clockify</AssemblyName>
<OutputType>Exe</OutputType>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<StartupObject>Clockify.Program</StartupObject>
<CodeAnalysisRuleSet>StyleCop.ruleset</CodeAnalysisRuleSet>
<EnableNETAnalyzers>false</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AssemblyVersion>1.2</AssemblyVersion>
<PackageVersion>1.2</PackageVersion>
<Title>Clockify</Title>
Expand All @@ -19,21 +21,29 @@
<PackageLicenseUrl>https://github.com/eXpl0it3r/streamdeck-clockify/blob/master/LICENSE</PackageLicenseUrl>
<PackageIconUrl>https://github.com/eXpl0it3r/streamdeck-clockify/blob/master/Images/clockifyIcon%402x.png</PackageIconUrl>
<RepositoryUrl>https://github.com/eXpl0it3r/streamdeck-clockify</RepositoryUrl>
<RootNamespace />
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net5.0-windows|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>bin\Debug\dev.duerrenberger.clockify.sdPlugin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net5.0-windows|AnyCPU'">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>bin\Release\dev.duerrenberger.clockify.sdPlugin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<PublishDir>bin\Publish\dev.duerrenberger.clockify.sdPlugin\</PublishDir>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Clockify.Net" Version="1.14.0" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NLog" Version="4.7.13" />
<PackageReference Include="streamdeck-client-csharp" Version="4.3.0" />
<PackageReference Include="StreamDeck-Tools" Version="3.2.0" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<None Update="Images\categoryIcon%402x.png">
Expand Down
98 changes: 45 additions & 53 deletions Clockify/ClockifyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public class ClockifyContext
{
private string _apiKey = string.Empty;
private ClockifyClient _clockifyClient;
private CurrentUserDto _currentUser = new();
private List<WorkspaceDto> _workspaces = new();
private Dictionary<string, List<ProjectDtoImpl>> _projects = new();
private CurrentUserDto _currentUser = new ();
private Dictionary<string, List<ProjectDtoImpl>> _projects = new ();
private List<WorkspaceDto> _workspaces = new ();

public bool IsValid()
{
Expand All @@ -27,58 +27,50 @@ public bool IsValid()

public async Task ToggleTimerAsync(string workspaceName, string projectName = null, string taskName = null, string timerName = null)
{
try
if (_clockifyClient == null
|| _workspaces.All(w => w.Name != workspaceName)
|| !string.IsNullOrEmpty(projectName) && (!_projects.ContainsKey(workspaceName) || _projects[workspaceName].All(p => p.Name != projectName)))
{
Logger.Instance.LogMessage(TracingLevel.WARN, $"Invalid settings for toggle {workspaceName}, {projectName}, {timerName}");
return;
}

if (_clockifyClient == null
|| _workspaces.All(w => w.Name != workspaceName)
|| !string.IsNullOrEmpty(projectName) && (!_projects.ContainsKey(workspaceName) || _projects[workspaceName].All(p => p.Name != projectName)))
{
Logger.Instance.LogMessage(TracingLevel.WARN, $"Invalid settings for toggle {workspaceName}, {projectName}, {timerName}");
return;
}
var runningTimer = await GetRunningTimerAsync(workspaceName, projectName, timerName);

var runningTimer = await GetRunningTimerAsync(workspaceName, projectName, timerName);
if (runningTimer != null)
{
await StopRunningTimerAsync(workspaceName);
return;
}

if (runningTimer != null)
{
await StopRunningTimerAsync(workspaceName);
return;
}
await StopRunningTimerAsync(workspaceName);

await StopRunningTimerAsync(workspaceName);
var workspace = _workspaces.Single(w => w.Name == workspaceName);
var timeEntryRequest = new TimeEntryRequest
{
UserId = _currentUser.Id,
WorkspaceId = workspace.Id,
Description = timerName,
Start = DateTimeOffset.UtcNow
};

var workspace = _workspaces.Single(w => w.Name == workspaceName);
var timeEntryRequest = new TimeEntryRequest
{
UserId = _currentUser.Id,
WorkspaceId = workspace.Id,
Description = timerName,
Start = DateTimeOffset.UtcNow
};
if (!string.IsNullOrEmpty(projectName))
{
var project = _projects[workspaceName].Single(p => p.Name == projectName);
timeEntryRequest.ProjectId = project.Id;

if (!string.IsNullOrEmpty(projectName))
if (!string.IsNullOrEmpty(taskName))
{
var project = _projects[workspaceName].Single(p => p.Name == projectName);
timeEntryRequest.ProjectId = project.Id;

if (!string.IsNullOrEmpty(taskName))
var taskId = await FindOrCreateTaskAsync(workspace, project, taskName);
if (taskId != null)
{
var taskId = await FindOrCreateTaskAsync(workspace, project, taskName);
if (taskId != null)
{
timeEntryRequest.TaskId = taskId;
}
timeEntryRequest.TaskId = taskId;
}
}

await _clockifyClient.CreateTimeEntryAsync(workspace.Id, timeEntryRequest);
Logger.Instance.LogMessage(TracingLevel.INFO, $"Toggle Timer {workspaceName}, {projectName}, {taskName}, {timerName}");
}
catch (Exception exception)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, exception.Message);
}

await _clockifyClient.CreateTimeEntryAsync(workspace.Id, timeEntryRequest);
Logger.Instance.LogMessage(TracingLevel.INFO, $"Toggle Timer {workspaceName}, {projectName}, {taskName}, {timerName}");
}

public async Task StopRunningTimerAsync(string workspaceName)
Expand All @@ -87,14 +79,14 @@ public async Task StopRunningTimerAsync(string workspaceName)
{
return;
}

var workspace = _workspaces.Single(w => w.Name == workspaceName);
var runningTimer = await GetRunningTimerAsync(workspaceName);
if (runningTimer == null)
{
return;
}

var timerUpdate = new UpdateTimeEntryRequest
{
Billable = runningTimer.Billable,
Expand All @@ -104,7 +96,7 @@ public async Task StopRunningTimerAsync(string workspaceName)
TaskId = runningTimer.TaskId,
Description = runningTimer.Description
};

await _clockifyClient.UpdateTimeEntryAsync(workspace.Id, runningTimer.Id, timerUpdate);
Logger.Instance.LogMessage(TracingLevel.INFO, $"Timer Stopped {workspaceName}, {runningTimer.ProjectId}, {runningTimer.TaskId}, {runningTimer.Description}");
}
Expand All @@ -125,12 +117,12 @@ public async Task<TimeEntryDtoImpl> GetRunningTimerAsync(string workspaceName, s
{
return null;
}

if (string.IsNullOrEmpty(projectName))
{
return string.IsNullOrEmpty(timeName) ? timeEntries.Data.FirstOrDefault() : timeEntries.Data.FirstOrDefault(t => t.Description == timeName);
}

var project = _projects[workspaceName].Single(p => p.Name == projectName);
return string.IsNullOrEmpty(timeName) ? timeEntries.Data.FirstOrDefault(t => t.ProjectId == project.Id) : timeEntries.Data.FirstOrDefault(t => t.ProjectId == project.Id && t.Description == timeName);
}
Expand Down Expand Up @@ -158,7 +150,7 @@ public async Task<bool> SetApiKeyAsync(string apiKey)
{
await UpdateProjectsAsync(workspace.Name);
}

return true;
}

Expand Down Expand Up @@ -227,7 +219,7 @@ private async Task UpdateProjectsAsync(string workspaceName)
{
return;
}

_projects[workspace.Name] = projectResponse.Data;
}

Expand All @@ -239,12 +231,12 @@ private async Task<string> FindOrCreateTaskAsync(WorkspaceDto workspace, Project
{
return null;
}

if (taskResponse.Data.Any())
{
return taskResponse.Data.First().Id;
}

var taskRequest = new TaskRequest
{
Name = taskName
Expand Down Expand Up @@ -272,7 +264,7 @@ private async Task<bool> TestConnectionAsync()
{
return false;
}

_currentUser = user.Data;
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions Clockify/PluginSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ public class PluginSettings
{
[JsonProperty(PropertyName = "apiKey")]
public string ApiKey { get; set; } = string.Empty;

[JsonProperty(PropertyName = "workspaceName")]
public string WorkspaceName { get; set; } = string.Empty;

[JsonProperty(PropertyName = "projectName")]
public string ProjectName { get; set; } = string.Empty;

[JsonProperty(PropertyName = "taskName")]
public string TaskName { get; set; } = string.Empty;

[JsonProperty(PropertyName = "timerName")]
public string TimeName { get; set; } = string.Empty;
}
Expand Down
2 changes: 1 addition & 1 deletion Clockify/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public static void Main(string[] args)
SDWrapper.Run(args);
}
}
}
}
6 changes: 3 additions & 3 deletions Clockify/ToggleAction.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using BarRaider.SdTools;
using Newtonsoft.Json.Linq;
using System.Threading.Tasks;

namespace Clockify
{
Expand All @@ -10,8 +10,8 @@ public class ToggleAction : PluginBase
{
private static readonly uint InactiveState = 0;
private static readonly uint ActiveState = 1;
private readonly PluginSettings _settings;
private readonly ClockifyContext _clockifyContext;
private readonly PluginSettings _settings;

public ToggleAction(ISDConnection connection, InitialPayload payload)
: base(connection, payload)
Expand Down Expand Up @@ -59,7 +59,7 @@ public override async void OnTick()
{
var timer = await _clockifyContext.GetRunningTimerAsync(_settings.WorkspaceName, _settings.ProjectName, _settings.TimeName);
var timerText = CreateTimerText();

if (timer?.TimeInterval.Start != null)
{
var timeDifference = DateTime.UtcNow - timer.TimeInterval.Start.Value.UtcDateTime;
Expand Down
41 changes: 41 additions & 0 deletions StyleCop.ruleset
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Custom StyleCop Rule Set" Description="Use this rule set to get a comprehensive picture of all issues in your code. This can help you decide which of the more focused rule sets are most appropriate to run for your projects." ToolsVersion="15.0">
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
<Rule Id="SA0001" Action="None" />
<Rule Id="SA1028" Action="None" />
<Rule Id="SA1101" Action="None" />
<Rule Id="SA1114" Action="None" />
<Rule Id="SA1115" Action="None" />
<Rule Id="SA1116" Action="None" />
<Rule Id="SA1118" Action="None" />
<Rule Id="SA1124" Action="None" />
<Rule Id="SA1200" Action="None" />
<Rule Id="SA1204" Action="None" />
<Rule Id="SA1309" Action="None" />
<Rule Id="SA1404" Action="None" />
<Rule Id="SA1408" Action="None" />
<Rule Id="SA1413" Action="None" />
<Rule Id="SA1516" Action="None" />
<Rule Id="SA1600" Action="None" />
<Rule Id="SA1601" Action="None" />
<Rule Id="SA1602" Action="None" />
<Rule Id="SA1604" Action="None" />
<Rule Id="SA1605" Action="None" />
<Rule Id="SA1608" Action="None" />
<Rule Id="SA1611" Action="None" />
<Rule Id="SA1615" Action="None" />
<Rule Id="SA1618" Action="None" />
<Rule Id="SA1623" Action="None" />
<Rule Id="SA1629" Action="None" />
<Rule Id="SA1633" Action="None" />
<Rule Id="SA1634" Action="None" />
<Rule Id="SA1635" Action="None" />
<Rule Id="SA1636" Action="None" />
<Rule Id="SA1637" Action="None" />
<Rule Id="SA1638" Action="None" />
<Rule Id="SA1640" Action="None" />
<Rule Id="SA1641" Action="None" />
<Rule Id="SA1652" Action="None" />
<Rule Id="SX1101" Action="Warning" />
</Rules>
</RuleSet>
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"Icon": "Images/clockifyIcon",
"URL": "https://duerrenberger.dev",
"Version": "1.2",
"CodePath": "Clockify",
"CodePath": "dev.duerrenberger.clockify",
"Category": "Time Tracking",
"CategoryIcon": "Images/categoryTimerIcon",
"OS": [
Expand Down

0 comments on commit f143a12

Please sign in to comment.