Skip to content

Commit

Permalink
Merge pull request #2179 from xccoreco/master
Browse files Browse the repository at this point in the history
Added paste support to hastebin for the log
  • Loading branch information
quajak committed Apr 3, 2022
2 parents 2bbd9a4 + 57e7cc4 commit 8d9f38e
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 3 deletions.
1 change: 1 addition & 0 deletions source/Cosmos.Build.Builder/Cosmos.Build.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Setup.Configuration.Interop" Version="1.16.30" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NuGet.Common" />
<PackageReference Include="NuGet.Configuration" />
<PackageReference Include="System.Runtime.WindowsRuntime" Version="4.6.0" />
Expand Down
68 changes: 68 additions & 0 deletions source/Cosmos.Build.Builder/Services/HasteBinClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace Cosmos.Build.Builder.Services
{
//https://gist.github.com/jwoff78/f8babb48922132ea20d37ef4aa8aa1dd
public class HasteBinClient
{
private static HttpClient _httpClient;
private string _baseUrl;

static HasteBinClient()
{
_httpClient = new HttpClient();
}

public HasteBinClient(string baseUrl)
{
_baseUrl = baseUrl;
}

public async Task<HasteBinResult> Post(string content)
{
string fullUrl = _baseUrl;
if (!fullUrl.EndsWith("/"))
{
fullUrl += "/";
}
string postUrl = $"{fullUrl}documents";

var request = new HttpRequestMessage(HttpMethod.Post, new Uri(postUrl));
request.Content = new StringContent(content);
HttpResponseMessage result = await _httpClient.SendAsync(request).ConfigureAwait(false);

if (result.IsSuccessStatusCode)
{
string json = await result.Content.ReadAsStringAsync();
HasteBinResult hasteBinResult = JsonConvert.DeserializeObject<HasteBinResult>(json);

if (hasteBinResult?.Key != null)
{
hasteBinResult.FullUrl = $"{fullUrl}{hasteBinResult.Key}";
hasteBinResult.IsSuccess = true;
hasteBinResult.StatusCode = 200;
return hasteBinResult;
}
}

return new HasteBinResult()
{
FullUrl = fullUrl,
IsSuccess = false,
StatusCode = (int)result.StatusCode
};
}
}

// Define other methods and classes here
public class HasteBinResult
{
public string Key { get; set; }
public string FullUrl { get; set; }
public bool IsSuccess { get; set; }
public int StatusCode { get; set; }
}
}
28 changes: 28 additions & 0 deletions source/Cosmos.Build.Builder/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ internal sealed class MainWindowViewModel : ViewModelBase

public ICommand CopyCommand { get; }

public ICommand PostPaste { get; }

public bool CloseWhenCompleted
{
get => _closeWhenCompleted;
Expand Down Expand Up @@ -61,6 +63,8 @@ public WindowState WindowState

CopyCommand = new RelayCommand(CopyLogToClipboard);

PostPaste = new RelayCommand(PostPasteCommand);

CloseWhenCompleted = true;

_logger = new MainWindowLogger(this);
Expand All @@ -70,6 +74,8 @@ public WindowState WindowState

private void CopyLogToClipboard(object parameter) => Clipboard.SetText(BuildLog());

private void PostPasteCommand(object parameter) => InternalPostPaste();

private string BuildLog()
{
var log = @"
Expand All @@ -93,6 +99,28 @@ Build Log
return log;
}

private void InternalPostPaste()
{
try
{
string baseUrl = "https://www.toptal.com/developers/hastebin/";
var hasteBinClient = new HasteBinClient(baseUrl);
HasteBinResult result = hasteBinClient.Post(BuildLog()).Result;

if (result.IsSuccess)
{
Views.MessageBox.Show($"link:{baseUrl}{result.Key}");
}
else
{
Views.MessageBox.Show($"Failed, status code was {result.StatusCode}");
}
} catch (Exception e)
{
Views.MessageBox.Show(e.Message);
}
}

private async Task BuildAsync()
{
_logger.NewSection("Checking Dependencies...");
Expand Down
9 changes: 7 additions & 2 deletions source/Cosmos.Build.Builder/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<Grid DockPanel.Dock="Bottom" Height="46">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
Expand All @@ -38,11 +39,15 @@
Content="Copy log" Width="96" Height="46"
Padding="10,10,10,10"/>

<CheckBox Grid.Column="2"
<Button Grid.Column="1"
Command="{Binding PostPaste}"
Content="Publish on Hastebin" Height="46" MaxWidth="200"
Padding="10,10,10,10" Margin="0,0,319,0" HorizontalAlignment="Right" Width="147"/>

<CheckBox Grid.Column="3"
IsChecked="{Binding CloseWhenCompleted}"
Content="Close when finished" Height="39" Margin="0,0,20,7" />
</Grid>

<Rectangle DockPanel.Dock="Top"
Height="5" />
<TextBlock DockPanel.Dock="Top"
Expand Down
23 changes: 22 additions & 1 deletion source/Cosmos.Build.Builder/Views/MessageBox.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Windows;
using System.Windows.Documents;

namespace Cosmos.Build.Builder.Views
{
Expand All @@ -7,15 +8,35 @@ public partial class MessageBox : Window
public MessageBox(string Content)
{
InitializeComponent();
lblMain.Text = Content;
if (Content.StartsWith("link:"))
{
Content = Content.Replace("link:", "");
var hlink = new Hyperlink();
hlink.Inlines.Add(Content);
hlink.NavigateUri = new System.Uri(Content);
hlink.RequestNavigate += (sender, e) =>
{
System.Diagnostics.Process.Start(e.Uri.ToString());
};
lblMain.Text = "";
lblMain.Inlines.Add("Click me: ");
lblMain.Inlines.Add(hlink);
}
else
{
lblMain.Text = Content;
}

}

public static void Show(string Content)
{
var window = new MessageBox(Content);

//this workarounds a bug that when the main window is minimized then brought to front, the message box is no longer visible
window.Topmost = true;
window.ShowDialog();

}

private void Button_Click(object sender, RoutedEventArgs e)
Expand Down

0 comments on commit 8d9f38e

Please sign in to comment.