forked from panuoksala/streamdeck-azuredevops-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureDevOpsRunnerAction.cs
169 lines (147 loc) · 6.58 KB
/
AzureDevOpsRunnerAction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using Microsoft.Extensions.Logging;
using Serilog;
using StreamDeckAzureDevOps.Models;
using StreamDeckAzureDevOps.Services;
using StreamDeckLib;
using StreamDeckLib.Messages;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace StreamDeckAzureDevOps
{
[ActionUuid(Uuid = "net.oksala.azuredevops.runner")]
public class AzureDevOpsRunnerAction : BaseAction
{
private readonly AzureDevOpsService _service;
public AzureDevOpsRunnerAction()
{
_service = new AzureDevOpsService(Logger);
}
public async Task UpdateStatus(string context)
{
try
{
await Manager.SetImageAsync(context, "images/Azure-DevOps-updating.png");
string statusImage = (PipelineType)SettingsModel.PipelineType switch
{
PipelineType.Build => await _service.GetBuildStatusImage(SettingsModel),
PipelineType.Release => await _service.GetReleaseStatusImage(SettingsModel),
PipelineType.PullRequests => await _service.GetPrStatusImage(SettingsModel),
_ => throw new ArgumentOutOfRangeException($"Unsupported pipeline type {SettingsModel.PipelineType}."),
};
if (statusImage != null)
{
await Manager.SetImageAsync(context, statusImage);
}
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to update status.");
}
}
public override async Task OnDidReceiveSettings(StreamDeckEventPayload args)
{
await base.OnDidReceiveSettings(args);
}
public override async Task UpdateDisplay(StreamDeckEventPayload args)
{
await UpdateStatus(args.context);
}
public override async Task OnTap(StreamDeckEventPayload args)
{
await ExecuteKeyPress(args, (KeyPressAction)SettingsModel.TapAction);
}
public override async Task OnLongPress(StreamDeckEventPayload args)
{
await ExecuteKeyPress(args, (KeyPressAction)SettingsModel.LongPressAction);
}
public override async Task OnError(StreamDeckEventPayload args, Exception ex)
{
try
{
SettingsModel.ErrorMessage = ex.Message;
await Manager.ShowAlertAsync(args.context);
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-unknown.png");
await Manager.SetSettingsAsync(args.context, SettingsModel);
}
catch (Exception handlingException)
{
Logger.LogError(handlingException, $"Failed to handle error: {ex.Message}");
}
}
public override bool IsSettingsValid()
{
return !string.IsNullOrWhiteSpace(SettingsModel.ProjectName)
&& !string.IsNullOrWhiteSpace(SettingsModel.OrganizationURL)
&& !string.IsNullOrWhiteSpace(SettingsModel.PAT);
}
private async Task ExecuteKeyPress(StreamDeckEventPayload args, KeyPressAction keyPressAction)
{
try
{
PipelineType pipelineType = (PipelineType)SettingsModel.PipelineType;
SettingsModel.ErrorMessage = string.Empty;
var organization = SettingsModel.OrganizationNameFormatted();
switch (keyPressAction)
{
case KeyPressAction.DoNothing:
// Do nothing :)
break;
case KeyPressAction.UpdateStatus:
await UpdateDisplay(args);
await Manager.ShowOkAsync(args.context);
break;
case KeyPressAction.Run:
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-updating.png");
if (pipelineType == PipelineType.Build)
{
await _service.StartBuild(SettingsModel);
}
else if(pipelineType == PipelineType.Release)
{
await _service.StartRelease(SettingsModel);
}
else if(pipelineType == PipelineType.PullRequests)
{
// Open new pull request page
var newPrUrl = $"{organization}/{SettingsModel.ProjectName}/_git/{SettingsModel.DefinitionId}/pullrequestcreate?sourceRef=&targetRef=";
await Manager.OpenUrlAsync(args.context, newPrUrl);
}
await Manager.ShowOkAsync(args.context);
if ((StatusUpdateFrequency)SettingsModel.UpdateStatusEverySecond == StatusUpdateFrequency.Never)
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-success.png");
}
else
{
await Manager.SetImageAsync(args.context, "images/Azure-DevOps-waiting.png");
}
break;
case KeyPressAction.Open:
string url = "";
if (pipelineType == PipelineType.Build)
{
url = $"{organization}/{SettingsModel.ProjectName}/_build?definitionId={SettingsModel.DefinitionId}";
}
else if(pipelineType == PipelineType.Release)
{
url = $"{organization}/{SettingsModel.ProjectName}/_release?definitionId={SettingsModel.DefinitionId}";
}
else if(pipelineType == PipelineType.PullRequests)
{
// Show active pull requests page
url = $"{organization}/{SettingsModel.ProjectName}/_git/{SettingsModel.DefinitionId}/pullrequests?_a=active";
}
await Manager.OpenUrlAsync(args.context, url);
break;
}
await Manager.SetSettingsAsync(args.context, SettingsModel);
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to initiate action from keypress.");
throw;
}
}
}
}