Skip to content

Commit

Permalink
Merge pull request #4 from dropbox/blake/versionCheck
Browse files Browse the repository at this point in the history
Add version check during app initialization.
  • Loading branch information
chadduffey authored Jul 22, 2016
2 parents 560d0a5 + b05f297 commit 5227be6
Show file tree
Hide file tree
Showing 7 changed files with 680 additions and 68 deletions.
10 changes: 10 additions & 0 deletions Source/DfBAdminToolkit/DfBAdminToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
<Compile Include="Presenter\ProvisioningPresenter.cs" />
<Compile Include="Presenter\SettingsPresenter.cs" />
<Compile Include="Presenter\TextSearchPresenter.cs" />
<Compile Include="Services\GitHubService.cs" />
<Compile Include="Tooltips.cs" />
<Compile Include="View\DataMigrationView.cs">
<SubType>Form</SubType>
Expand All @@ -157,6 +158,12 @@
<Compile Include="View\DumpUserContentView.Designer.cs">
<DependentUpon>DumpUserContentView.cs</DependentUpon>
</Compile>
<Compile Include="View\VersionWindow.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="View\VersionWindow.Designer.cs">
<DependentUpon>VersionWindow.cs</DependentUpon>
</Compile>
<Compile Include="View\GroupsView.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -242,6 +249,9 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<EmbeddedResource Include="View\VersionWindow.resx">
<DependentUpon>VersionWindow.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.manifest">
<SubType>Designer</SubType>
</None>
Expand Down
62 changes: 62 additions & 0 deletions Source/DfBAdminToolkit/Services/GitHubService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RestSharp;
using Newtonsoft.Json;

namespace DfBAdminToolkit.Services
{
public class GitHubService
{

private readonly RestClient _client;

public GitHubService()
{

_client = new RestClient(@"https://api.github.com/");
}

public async Task<GitHubRelease> LatestRelease()
{
GitHubRelease release = new GitHubRelease();
string releasesPath = @"repos/dropbox/DropboxBusinessAdminTool/releases";
RestRequest request = new RestRequest(releasesPath, Method.GET);
IRestResponse response = await _client.ExecuteTaskAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(response.Content);
release.version = new Version(jsonData[0]["tag_name"].ToString());
release.name = jsonData[0]["name"];
release.description = jsonData[0]["body"];
release.releaseUri = new Uri(jsonData[0]["html_url"].ToString());
release.releaseDate = Convert.ToDateTime(jsonData[0]["published_at"].ToString());
// Look for a zip attachment that contains just the pre-built exe.
foreach (var asset in jsonData[0]["assets"])
{
if (asset["content_type"] == "application/x-zip-compressed")
{
release.downloadUri = asset["browser_download_url"];
}
}
}
else
{
release.version = new Version(0, 0, 0, 0);
}
return release;
}
}

public class GitHubRelease
{
public string name;
public string description;
public Version version;
public DateTime releaseDate;
public Uri releaseUri;
public Uri downloadUri;
}
}
120 changes: 52 additions & 68 deletions Source/DfBAdminToolkit/View/GroupsView.Designer.cs

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Source/DfBAdminToolkit/View/MainView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Common.Services;
using Model;
using Presenter;
using Services;
using System;
using System.Net;
using System.Collections.Generic;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void Initialize() {
{
this.UpdateTitleBarTeamStats();
}
this.CheckLatestVersion();
}

public void ShowView() {
Expand Down Expand Up @@ -201,6 +203,25 @@ public void UpdateTitleBarTeamStats()
this.Refresh();
}

public async void CheckLatestVersion()
{
GitHubService service = new GitHubService();
Version currentVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
GitHubRelease latestRelease = await service.LatestRelease();
if (latestRelease.version > currentVersion)
{
this.Hide();
VersionWindow versionWindow = new VersionWindow(latestRelease);
versionWindow.StartPosition = FormStartPosition.CenterScreen;
versionWindow.MinimizeBox = false;
versionWindow.MaximizeBox = false;
versionWindow.Show();
versionWindow.Activate();
versionWindow.FormClosed += (sender, e) => {
this.Show();
};
}
}

private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Expand Down
104 changes: 104 additions & 0 deletions Source/DfBAdminToolkit/View/VersionWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions Source/DfBAdminToolkit/View/VersionWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DfBAdminToolkit.Services;

namespace DfBAdminToolkit.View
{
public partial class VersionWindow : Form
{
private GitHubRelease _release;

public VersionWindow(GitHubRelease release)
{
_release = release;
InitializeComponent();
versionAlertText.Text = String.Format(@"Version {0} of the Admin Toolkit is now available!", _release.version.ToString());
versionDetails.Text = String.Format(@"Release Notes:{0}{1}", Environment.NewLine, _release.description);
if (_release.downloadUri != null)
{
downloadButton.Text = @"Download Release";
}
else
{
downloadButton.Text = @"View Details";
}
downloadButton.Focus();
}

private void downloadButton_Clicked(object sender, EventArgs e)
{
if (_release.downloadUri != null)
{
System.Diagnostics.Process.Start(_release.downloadUri.ToString());
Application.Exit();
}
else
{
System.Diagnostics.Process.Start(_release.releaseUri.ToString());
}
this.Close();
}

private void dismissButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Loading

0 comments on commit 5227be6

Please sign in to comment.