-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathMigrationLogger.cs
138 lines (117 loc) · 4.8 KB
/
MigrationLogger.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.DotNet.ProductConstructionService.Client.Models;
using Microsoft.Extensions.Logging;
using Tools.Common;
namespace FlatFlowMigrationCli;
/// <summary>
/// Class that logs the operations in console and into a file instead of performing them.
/// </summary>
internal class MigrationLogger : ISubscriptionMigrator
{
private readonly ILogger<MigrationLogger> _logger;
private readonly string _outputPath;
public MigrationLogger(ILogger<MigrationLogger> logger, string outputPath)
{
_logger = logger;
_outputPath = outputPath;
}
public async Task DisableSubscriptionAsync(Subscription subscription)
{
_logger.LogInformation("Would disable a subscription {sourceRepository} -> {targetRepository} / {subscriptionId}",
RemoveUrlPrefix(subscription.SourceRepository),
RemoveUrlPrefix(subscription.TargetRepository),
subscription.Id);
await LogActionAsync(GetActionKey(subscription), Action.Disable, subscription.Id.ToString());
}
public async Task DeleteSubscriptionAsync(Subscription subscription)
{
_logger.LogInformation("Would delete an existing subscription {sourceRepository} -> {targetRepository} / {subscriptionId}...",
RemoveUrlPrefix(subscription.SourceRepository),
RemoveUrlPrefix(subscription.TargetRepository),
subscription.Id);
await LogActionAsync(GetActionKey(subscription), Action.Delete, subscription.Id.ToString());
}
public async Task CreateVmrSubscriptionAsync(Subscription subscription)
{
_logger.LogInformation("Would create subscription {vmrUri} -> {repoUri}",
RemoveUrlPrefix(Constants.VmrUri),
RemoveUrlPrefix(subscription.TargetRepository));
await LogActionAsync($"{Constants.VmrUri} - {subscription.TargetRepository}", Action.Create, null, new()
{
{ "codeflow", false },
{ "branch", subscription.TargetBranch },
});
}
public async Task CreateBackflowSubscriptionAsync(string mappingName, string repoUri, string branch, HashSet<string> excludedAssets)
{
_logger.LogInformation("Would create a backflow subscription for {repoUri}", RemoveUrlPrefix(repoUri));
await LogActionAsync($"{Constants.VmrUri} - {repoUri}", Action.Create, null, new()
{
{ "codeflow", true },
{ "branch", branch },
{ "excludedAssets", string.Join(", ", excludedAssets) },
});
}
public async Task CreateForwardFlowSubscriptionAsync(string mappingName, string repoUri, string channelName)
{
_logger.LogInformation("Would create a forward flow subscription for {repoUri}", RemoveUrlPrefix(repoUri));
await LogActionAsync($"{repoUri} - VMR", Action.Create, null, new()
{
{ "codeflow", true },
{ "channel", channelName },
});
}
private async Task LogActionAsync(string repoUri, Action action, string? id, Dictionary<string, object?>? Parameters = null)
{
var log = await ReadLog();
log[repoUri] = new RepoActionLog(action, id, Parameters);
await WriteLog(log);
}
private async Task<ActionLog> ReadLog()
{
if (!File.Exists(_outputPath))
{
return [];
}
using var file = File.Open(_outputPath, FileMode.Open);
try
{
var log = await JsonSerializer.DeserializeAsync<ActionLog>(file, SerializerOptions);
file.Close();
return log ?? [];
}
catch
{
file.Close();
return [];
}
}
private async Task WriteLog(ActionLog log)
{
using var file = File.Open(_outputPath, FileMode.Create);
await JsonSerializer.SerializeAsync(file, log, SerializerOptions);
file.Close();
}
private static string GetActionKey(Subscription subscription)
=> $"{subscription.SourceRepository} - {subscription.TargetRepository}";
private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web)
{
WriteIndented = true,
AllowTrailingCommas = true,
Converters = { new JsonStringEnumConverter() },
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
private static string RemoveUrlPrefix(string url) => url.Replace("https://github.com/", null);
}
internal class ActionLog : Dictionary<string, RepoActionLog>{}
internal record RepoActionLog(Action Action, string? Id, Dictionary<string, object?>? Parameters);
internal enum Action
{
Unknown,
Create,
Disable,
Delete,
}