From b44c0f28967c8d25e1e2535f40752904ac66c476 Mon Sep 17 00:00:00 2001 From: Tim Heuer Date: Thu, 20 Jul 2023 22:11:15 -0700 Subject: [PATCH] Secrets and trim native binaries - Fix #7 Added context menus for add/edit/delete secrets at the repo level. - Removed arm/x86 binaries for lib2git --- src/GitHubActionsVS.csproj | 22 +++-- src/Properties/AssemblyInfo.cs | 1 + src/ToolWindows/GHActionsToolWindow.xaml | 24 ++++- src/ToolWindows/GHActionsToolWindow.xaml.cs | 94 +++++++++++++++++++- src/UserControls/AddEditSecret.xaml | 28 ++++++ src/UserControls/AddEditSecret.xaml.cs | 43 +++++++++ src/lib/win32/arm64/git2-e632535.dll | Bin 1462272 -> 0 bytes src/lib/win32/x86/git2-e632535.dll | Bin 1381376 -> 0 bytes src/libsodium.dll | Bin 0 -> 312320 bytes 9 files changed, 202 insertions(+), 10 deletions(-) create mode 100644 src/UserControls/AddEditSecret.xaml create mode 100644 src/UserControls/AddEditSecret.xaml.cs delete mode 100644 src/lib/win32/arm64/git2-e632535.dll delete mode 100644 src/lib/win32/x86/git2-e632535.dll create mode 100644 src/libsodium.dll diff --git a/src/GitHubActionsVS.csproj b/src/GitHubActionsVS.csproj index b5f41fe..fae559f 100644 --- a/src/GitHubActionsVS.csproj +++ b/src/GitHubActionsVS.csproj @@ -65,6 +65,9 @@ + + AddEditSecret.xaml + True True @@ -72,6 +75,10 @@ + + Always + true + Always @@ -81,18 +88,10 @@ Designer VsixManifestGenerator - - Always - true - true Always - - true - Always - PreserveNewest true @@ -111,6 +110,10 @@ Designer MSBuild:Compile + + Designer + MSBuild:Compile + @@ -150,6 +153,9 @@ 7.0.1 + + 1.3.3 + diff --git a/src/Properties/AssemblyInfo.cs b/src/Properties/AssemblyInfo.cs index f37e668..7b9990b 100644 --- a/src/Properties/AssemblyInfo.cs +++ b/src/Properties/AssemblyInfo.cs @@ -14,6 +14,7 @@ [assembly: ComVisible(false)] [assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\LibGit2Sharp.dll")] +[assembly: ProvideCodeBase(CodeBase = @"$PackageFolder$\Sodium.Core.dll")] namespace System.Runtime.CompilerServices; public class IsExternalInit { } \ No newline at end of file diff --git a/src/ToolWindows/GHActionsToolWindow.xaml b/src/ToolWindows/GHActionsToolWindow.xaml index 9db5919..4ba340f 100644 --- a/src/ToolWindows/GHActionsToolWindow.xaml +++ b/src/ToolWindows/GHActionsToolWindow.xaml @@ -56,7 +56,29 @@ - + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ToolWindows/GHActionsToolWindow.xaml.cs b/src/ToolWindows/GHActionsToolWindow.xaml.cs index 61c21ad..214a605 100644 --- a/src/ToolWindows/GHActionsToolWindow.xaml.cs +++ b/src/ToolWindows/GHActionsToolWindow.xaml.cs @@ -7,6 +7,10 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; +using GitHubActionsVS.UserControls; +using Application = System.Windows.Application; +using System.Windows.Media; +using MessageBox = Community.VisualStudio.Toolkit.MessageBox; namespace GitHubActionsVS; @@ -65,6 +69,12 @@ public async Task GetRepoInfoAsync() // find the git folder var solution = await VS.Solutions.GetCurrentSolutionAsync(); + if (solution is null) + { + Debug.WriteLine("No solution found"); + ShowInfoMessage("No project or solution loaded"); + return; + } var projectPath = solution?.FullPath; _repoInfo.FindGitFolder(projectPath, out string gitPath); @@ -135,7 +145,7 @@ private async Task LoadDataAsync() if (runs.TotalCount > 0) { // creating simplified model of the GH info for the treeview - + // iterate throught the runs foreach (var run in runs.WorkflowRuns) { @@ -257,5 +267,87 @@ private void HandlePreviewMouseWheel(object sender, MouseWheelEventArgs e) parent.RaiseEvent(eventArg); } } + + private async void AddSecret_Click(object sender, RoutedEventArgs e) + { + await UpsertRepositorySecret(string.Empty); + } + + private async void EditSecret_Click(object sender, RoutedEventArgs e) + { + MenuItem menuItem = (MenuItem)sender; + TextBlock tvi = GetParentTreeViewItem(menuItem); + if (tvi is not null) + { + string header = tvi.Text.ToString(); + string secretName = header.Substring(0, header.IndexOf(" (")); + await UpsertRepositorySecret(secretName); + } + } + + private TextBlock GetParentTreeViewItem(MenuItem menuItem) + { + var contextMenu = menuItem.CommandParameter as ContextMenu; + if (contextMenu is not null) + { + var treeViewItem = contextMenu.PlacementTarget as TextBlock; + if (treeViewItem is not null) + { + return treeViewItem; + } + } + return null; + } + + private async void DeleteSecret_Click(object sender, RoutedEventArgs e) + { + MenuItem menuItem = (MenuItem)sender; + TextBlock tvi = GetParentTreeViewItem(menuItem); + + if (tvi is not null) + { + await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); + // confirm the delete first + int result = VsShellUtilities.ShowMessageBox(ServiceProvider.GlobalProvider, "Are you sure you want to delete this secret?", "Confirm Delete", Microsoft.VisualStudio.Shell.Interop.OLEMSGICON.OLEMSGICON_QUERY, Microsoft.VisualStudio.Shell.Interop.OLEMSGBUTTON.OLEMSGBUTTON_YESNOCANCEL, Microsoft.VisualStudio.Shell.Interop.OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_THIRD); + + var confirmResult = (MessageBoxResult)result; + + if (confirmResult == MessageBoxResult.Yes) + { + string header = tvi.Text.ToString(); + string secretName = header.Substring(0, header.IndexOf(" (")); + + GitHubClient client = GetGitHubClient(); + await client.Repository.Actions.Secrets.Delete(_repoInfo.RepoOwner, _repoInfo.RepoName, secretName); + await RefreshSecretsAsync(client); + } + } + } + + private async Task UpsertRepositorySecret(string secretName) + { + AddEditSecret addEditSecret = new AddEditSecret(secretName) + { + Owner = Application.Current.MainWindow + }; + bool? result = addEditSecret.ShowDialog(); + if (result == true) + { + GitHubClient client = GetGitHubClient(); + var pubKey = await client.Repository.Actions.Secrets.GetPublicKey(_repoInfo.RepoOwner, _repoInfo.RepoName); + + UpsertRepositorySecret encryptedSecret = new UpsertRepositorySecret(); + if (pubKey != null) + { + var bytes = System.Text.Encoding.UTF8.GetBytes(addEditSecret.SecretValue); + var key = Convert.FromBase64String(pubKey.Key); + var sealedKeyBox = Sodium.SealedPublicKeyBox.Create(bytes, key); + encryptedSecret.KeyId = pubKey.KeyId; + encryptedSecret.EncryptedValue = Convert.ToBase64String(sealedKeyBox); + _ = await client.Repository.Actions.Secrets.CreateOrUpdate(_repoInfo.RepoOwner, _repoInfo.RepoName, addEditSecret.SecretName, encryptedSecret); + } + await RefreshSecretsAsync(client); + } + } } diff --git a/src/UserControls/AddEditSecret.xaml b/src/UserControls/AddEditSecret.xaml new file mode 100644 index 0000000..5e99855 --- /dev/null +++ b/src/UserControls/AddEditSecret.xaml @@ -0,0 +1,28 @@ + + + + + + + + + + + +