-
Notifications
You must be signed in to change notification settings - Fork 88
/
ConfigureAutoLinkCommandHandler.cs
52 lines (42 loc) · 1.98 KB
/
ConfigureAutoLinkCommandHandler.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
using System;
using System.Linq;
using System.Threading.Tasks;
using OctoshiftCLI.Commands;
using OctoshiftCLI.Extensions;
using OctoshiftCLI.Services;
namespace OctoshiftCLI.AdoToGithub.Commands.ConfigureAutoLink;
public class ConfigureAutoLinkCommandHandler : ICommandHandler<ConfigureAutoLinkCommandArgs>
{
private readonly OctoLogger _log;
private readonly GithubApi _githubApi;
public ConfigureAutoLinkCommandHandler(OctoLogger log, GithubApi githubApi)
{
_log = log;
_githubApi = githubApi;
}
public async Task Handle(ConfigureAutoLinkCommandArgs args)
{
if (args is null)
{
throw new ArgumentNullException(nameof(args));
}
_log.LogInformation("Configuring Autolink Reference...");
var keyPrefix = "AB#";
var urlTemplate = $"https://dev.azure.com/{args.AdoOrg.EscapeDataString()}/{args.AdoTeamProject.EscapeDataString()}/_workitems/edit/<num>/";
var autoLinks = await _githubApi.GetAutoLinks(args.GithubOrg, args.GithubRepo);
if (autoLinks.Any(al => al.KeyPrefix == keyPrefix && al.UrlTemplate == urlTemplate))
{
_log.LogSuccess($"Autolink reference already exists for key_prefix: '{keyPrefix}'. No operation will be performed");
return;
}
var autoLink = autoLinks.FirstOrDefault(al => al.KeyPrefix == keyPrefix);
if (autoLink != default((int, string, string)))
{
_log.LogInformation($"Autolink reference already exists for key_prefix: '{keyPrefix}', but the url template is incorrect");
_log.LogInformation($"Deleting existing Autolink reference for key_prefix: '{keyPrefix}' before creating a new Autolink reference");
await _githubApi.DeleteAutoLink(args.GithubOrg, args.GithubRepo, autoLink.Id);
}
await _githubApi.AddAutoLink(args.GithubOrg, args.GithubRepo, keyPrefix, urlTemplate);
_log.LogSuccess("Successfully configured autolink references");
}
}