Skip to content

Commit 4873918

Browse files
committed
Implement cancel run
- Fix #25
1 parent 2a2aa04 commit 4873918

File tree

9 files changed

+147
-4
lines changed

9 files changed

+147
-4
lines changed

src/Converters/NullToVisibilityConverter.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Globalization;
1+
using GitHubActionsVS.Models;
2+
using System.Globalization;
23
using System.Windows;
34
using System.Windows.Data;
45

@@ -15,3 +16,29 @@ public object ConvertBack(object value, Type targetType, object parameter, Cultu
1516
throw new NotImplementedException();
1617
}
1718
}
19+
20+
public class NullToBooleanConverter : IValueConverter
21+
{
22+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
23+
{
24+
return value == null ? false : true;
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
28+
{
29+
throw new NotImplementedException();
30+
}
31+
}
32+
33+
public class BoolToVisibilityConverter : IValueConverter
34+
{
35+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
36+
{
37+
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
38+
}
39+
40+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
41+
{
42+
throw new NotImplementedException();
43+
}
44+
}

src/GitHubActionsVS.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="Converters\ConclusionColorConverter.cs" />
5151
<Compile Include="Converters\ConclusionIconConverter.cs" />
5252
<Compile Include="Converters\NullToVisibilityConverter.cs" />
53+
<Compile Include="Helpers\ConclusionFilter.cs" />
5354
<Compile Include="Helpers\CredentialManager.cs" />
5455
<Compile Include="Helpers\RepoInfo.cs" />
5556
<Compile Include="Models\BaseWorkflowType.cs" />
@@ -104,6 +105,7 @@
104105
<Resource Include="Resources\AddItem.png" />
105106
<Resource Include="Resources\Delete.png" />
106107
<Resource Include="Resources\Edit.png" />
108+
<Resource Include="Resources\CancelBuild.png" />
107109
<Content Include="Resources\Icon.png">
108110
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
109111
<IncludeInVSIX>true</IncludeInVSIX>

src/Helpers/ConclusionFilter.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace GitHubActionsVS.Helpers;
8+
public class ConclusionFilter
9+
{
10+
public static bool IsFinished(string conclusion)
11+
{
12+
return conclusion.ToLower() switch
13+
{
14+
"pending" or "waiting" or "queued" or "in_progress" or "inprogress" or "requested" => false,
15+
_ => true,
16+
};
17+
}
18+
}

src/Models/BaseWorkflowType.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,22 @@ public abstract class BaseWorkflowType
1010
public string DisplayDate => $"{LogDate:g}";
1111
public string? Url { get; set; }
1212
public string Id { get; set; }
13+
public string TriggerEvent { get; set; }
14+
public string TriggerLogin { get; set; }
15+
16+
public bool HasActions
17+
{
18+
get
19+
{
20+
return TriggerEvent is not null || Url is not null;
21+
}
22+
}
23+
24+
public bool Cancellable
25+
{
26+
get
27+
{
28+
return TriggerEvent is not null && !Helpers.ConclusionFilter.IsFinished(Conclusion);
29+
}
30+
}
1331
}

src/Resources/CancelBuild.png

510 Bytes
Loading

src/Resources/UIStrings.Designer.cs

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Resources/UIStrings.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@
133133
<value>Update</value>
134134
<comment>Button text for update</comment>
135135
</data>
136+
<data name="CANCEL_RUN" xml:space="preserve">
137+
<value>Cancel Run</value>
138+
<comment>Menu for cancelling a run</comment>
139+
</data>
136140
<data name="CONFIRM_DELETE" xml:space="preserve">
137141
<value>Are you sure you want to delete this secret?</value>
138142
<comment>Question for deleting an item</comment>
@@ -209,6 +213,14 @@
209213
<value>Run Workflow</value>
210214
<comment>Menu for running a workflow</comment>
211215
</data>
216+
<data name="TRIGGERED_BY" xml:space="preserve">
217+
<value> by </value>
218+
<comment>Message separating event and actor - requries spaces</comment>
219+
</data>
220+
<data name="TRIGGERED_VIA" xml:space="preserve">
221+
<value>Triggered via </value>
222+
<comment>Message starting the trigger sentence</comment>
223+
</data>
212224
<data name="VIEW_LOG" xml:space="preserve">
213225
<value>View Log</value>
214226
<comment>Menu for viewing a log</comment>

src/ToolWindows/GHActionsToolWindow.xaml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<ivc:ConclusionIconConverter x:Key="ConclusionIconConverter" />
2323
<ivc:ConclusionColorConverter x:Key="ConclusionColorConverter" />
2424
<ivc:NullToVisibilityConverter x:Key="NullVisibilityConverter" />
25+
<ivc:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
26+
<ivc:NullToBooleanConverter x:Key="NullToBooleanConverter" />
2527
<Style TargetType="{x:Type Expander}">
2628
<Setter Property="toolkit:Themes.UseVsTheme" Value="True" />
2729
</Style>
@@ -59,13 +61,31 @@
5961
Foreground="{Binding Path=Conclusion, Converter={StaticResource ConclusionColorConverter}}"
6062
Text="{Binding Path=Conclusion, Converter={StaticResource ConclusionIconConverter}}"/>
6163
<emoji:TextBlock Text="{Binding DisplayName}" VerticalAlignment="Bottom" Tag="{Binding Url}">
64+
<emoji:TextBlock.ToolTip>
65+
<ToolTip Visibility="{Binding TriggerEvent, Converter={StaticResource NullVisibilityConverter}}">
66+
<StackPanel Orientation="Vertical">
67+
<TextBlock FontWeight="Bold" Text="{Binding Conclusion}"/>
68+
<TextBlock>
69+
<Run Text="{x:Static resx:UIStrings.TRIGGERED_VIA}"/>
70+
<Run FontWeight="Bold" Text="{Binding TriggerEvent}"/>
71+
<Run Text="{x:Static resx:UIStrings.TRIGGERED_BY}"/>
72+
<Run FontWeight="Bold" Text="{Binding TriggerLogin}"/>
73+
</TextBlock>
74+
</StackPanel>
75+
</ToolTip>
76+
</emoji:TextBlock.ToolTip>
6277
<TextBlock.ContextMenu>
63-
<ContextMenu Visibility="{Binding Url, Converter={StaticResource NullVisibilityConverter}}">
64-
<MenuItem Header="{x:Static resx:UIStrings.VIEW_LOG}" Click="ViewLog_Click" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}">
78+
<ContextMenu Visibility="{Binding HasActions, Converter={StaticResource BoolToVisibilityConverter}}">
79+
<MenuItem IsEnabled="{Binding Url, Converter={StaticResource NullToBooleanConverter}}" Header="{x:Static resx:UIStrings.VIEW_LOG}" Click="ViewLog_Click" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}">
6580
<MenuItem.Icon>
6681
<Image Source="pack://application:,,,/GitHubActionsVS;component/Resources/OpenWebSite.png" />
6782
</MenuItem.Icon>
6883
</MenuItem>
84+
<MenuItem IsEnabled="{Binding Cancellable}" Click="CancelRun_Click" Header="{x:Static resx:UIStrings.CANCEL_RUN}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}}">
85+
<MenuItem.Icon>
86+
<Image Source="pack://application:,,,/GitHubActionsVS;component/Resources/CancelBuild.png" />
87+
</MenuItem.Icon>
88+
</MenuItem>
6989
</ContextMenu>
7090
</TextBlock.ContextMenu>
7191
</emoji:TextBlock>

src/ToolWindows/GHActionsToolWindow.xaml.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ private async Task LoadDataAsync()
172172
Name = run.Name,
173173
LogDate = run.UpdatedAt,
174174
Id = run.Id.ToString(),
175-
RunNumber = run.RunNumber.ToString()
175+
RunNumber = run.RunNumber.ToString(),
176+
TriggerEvent = run.Event,
177+
TriggerLogin = run.TriggeringActor.Login
176178
};
177179

178180
if (refreshPending)
@@ -466,5 +468,22 @@ private async void Secret_MouseDoubleClick(object sender, MouseButtonEventArgs e
466468
}
467469
}
468470
}
471+
472+
private void CancelRun_Click(object sender, RoutedEventArgs e)
473+
{
474+
MenuItem menuItem = (MenuItem)sender;
475+
TextBlock tvi = GetParentTreeViewItem(menuItem);
476+
477+
// check the tag value to ensure it isn't null
478+
if (tvi is not null && tvi.DataContext is not null)
479+
{
480+
var run = tvi.DataContext as BaseWorkflowType;
481+
if (run is not null && run.Id is not null && !ConclusionFilter.IsFinished(run.Conclusion))
482+
{
483+
GitHubClient client = GetGitHubClient();
484+
_ = client.Actions.Workflows.Runs.Cancel(_repoInfo.RepoOwner, _repoInfo.RepoName, Int64.Parse(run.Id));
485+
}
486+
}
487+
}
469488
}
470489

0 commit comments

Comments
 (0)