Skip to content

Commit 65837f6

Browse files
Surayya Huseyn ZadaSurayya Huseyn Zada
authored andcommitted
refactor ImagePublisher
1 parent e9f4af3 commit 65837f6

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

src/Containers/Microsoft.NET.Build.Containers/ImagePublisher.cs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static async Task PublishImageAsync(
1313
SourceImageReference sourceImageReference,
1414
DestinationImageReference destinationImageReference,
1515
Microsoft.Build.Utilities.TaskLoggingHelper Log,
16-
IBuildEngine? BuildEngine,
16+
bool isSafeLog,
1717
Telemetry telemetry,
1818
CancellationToken cancellationToken)
1919
{
@@ -27,7 +27,7 @@ await PushToLocalRegistryAsync(
2727
sourceImageReference,
2828
destinationImageReference,
2929
Log,
30-
BuildEngine,
30+
isSafeLog,
3131
telemetry,
3232
cancellationToken,
3333
destinationImageReference.LocalRegistry!.LoadAsync).ConfigureAwait(false);
@@ -38,7 +38,7 @@ await PushToRemoteRegistryAsync(
3838
sourceImageReference,
3939
destinationImageReference,
4040
Log,
41-
BuildEngine,
41+
isSafeLog,
4242
cancellationToken,
4343
destinationImageReference.RemoteRegistry!.PushAsync,
4444
Strings.ContainerBuilder_ImageUploadedToRegistry).ConfigureAwait(false);
@@ -55,7 +55,7 @@ public static async Task PublishImageAsync(
5555
SourceImageReference sourceImageReference,
5656
DestinationImageReference destinationImageReference,
5757
Microsoft.Build.Utilities.TaskLoggingHelper Log,
58-
IBuildEngine? BuildEngine,
58+
bool isSafeLog,
5959
Telemetry telemetry,
6060
CancellationToken cancellationToken)
6161
{
@@ -69,7 +69,7 @@ await PushToLocalRegistryAsync(
6969
sourceImageReference,
7070
destinationImageReference,
7171
Log,
72-
BuildEngine,
72+
isSafeLog,
7373
telemetry,
7474
cancellationToken,
7575
destinationImageReference.LocalRegistry!.LoadAsync).ConfigureAwait(false);
@@ -80,7 +80,7 @@ await PushToRemoteRegistryAsync(
8080
sourceImageReference,
8181
destinationImageReference,
8282
Log,
83-
BuildEngine,
83+
isSafeLog,
8484
cancellationToken,
8585
destinationImageReference.RemoteRegistry!.PushManifestListAsync,
8686
Strings.ImageIndexUploadedToRegistry).ConfigureAwait(false);
@@ -97,7 +97,7 @@ private static async Task PushToLocalRegistryAsync<T>(
9797
SourceImageReference sourceImageReference,
9898
DestinationImageReference destinationImageReference,
9999
Microsoft.Build.Utilities.TaskLoggingHelper Log,
100-
IBuildEngine? BuildEngine,
100+
bool isSafeLog,
101101
Telemetry telemetry,
102102
CancellationToken cancellationToken,
103103
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> loadFunc)
@@ -112,31 +112,34 @@ private static async Task PushToLocalRegistryAsync<T>(
112112
try
113113
{
114114
await loadFunc(image, sourceImageReference, destinationImageReference, cancellationToken).ConfigureAwait(false);
115-
if (BuildEngine != null)
115+
if (isSafeLog)
116116
{
117117
Log.LogMessage(MessageImportance.High, Strings.ContainerBuilder_ImageUploadedToLocalDaemon, destinationImageReference, localRegistry);
118118
}
119119
}
120-
catch (ContainerHttpException e)
120+
catch (ContainerHttpException e) when (isSafeLog)
121121
{
122-
if (BuildEngine != null)
123-
{
124-
Log.LogErrorFromException(e, true);
125-
}
122+
Log.LogErrorFromException(e, true);
126123
}
127124
catch (AggregateException ex) when (ex.InnerException is DockerLoadException dle)
128125
{
129126
telemetry.LogLocalLoadError();
130-
Log.LogErrorFromException(dle, showStackTrace: false);
127+
if (isSafeLog)
128+
{
129+
Log.LogErrorFromException(dle, showStackTrace: false);
130+
}
131131
}
132-
catch (ArgumentException argEx)
132+
catch (ArgumentException argEx) when (isSafeLog)
133133
{
134134
Log.LogErrorFromException(argEx, showStackTrace: false);
135135
}
136136
catch (DockerLoadException dle)
137137
{
138138
telemetry.LogLocalLoadError();
139-
Log.LogErrorFromException(dle, showStackTrace: false);
139+
if (isSafeLog)
140+
{
141+
Log.LogErrorFromException(dle, showStackTrace: false);
142+
}
140143
}
141144
}
142145

@@ -145,7 +148,7 @@ private static async Task PushToRemoteRegistryAsync<T>(
145148
SourceImageReference sourceImageReference,
146149
DestinationImageReference destinationImageReference,
147150
Microsoft.Build.Utilities.TaskLoggingHelper Log,
148-
IBuildEngine? BuildEngine,
151+
bool isSafeLog,
149152
CancellationToken cancellationToken,
150153
Func<T, SourceImageReference, DestinationImageReference, CancellationToken, Task> pushFunc,
151154
string successMessage)
@@ -157,32 +160,23 @@ await pushFunc(
157160
sourceImageReference,
158161
destinationImageReference,
159162
cancellationToken).ConfigureAwait(false);
160-
if (BuildEngine != null)
163+
if (isSafeLog)
161164
{
162165
Log.LogMessage(MessageImportance.High, successMessage, destinationImageReference, destinationImageReference.RemoteRegistry!.RegistryName);
163166
}
164167
}
165-
catch (UnableToAccessRepositoryException)
168+
catch (UnableToAccessRepositoryException) when (isSafeLog)
166169
{
167-
if (BuildEngine != null)
168-
{
169-
Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
170-
}
170+
Log.LogErrorWithCodeFromResources(nameof(Strings.UnableToAccessRepository), destinationImageReference.Repository, destinationImageReference.RemoteRegistry!.RegistryName);
171171
}
172-
catch (ContainerHttpException e)
172+
catch (ContainerHttpException e) when (isSafeLog)
173173
{
174-
if (BuildEngine != null)
175-
{
176-
Log.LogErrorFromException(e, true);
177-
}
174+
Log.LogErrorFromException(e, true);
178175
}
179-
catch (Exception e)
176+
catch (Exception e) when (isSafeLog)
180177
{
181-
if (BuildEngine != null)
182-
{
183-
Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
184-
Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
185-
}
178+
Log.LogErrorWithCodeFromResources(nameof(Strings.RegistryOutputPushFailed), e.Message);
179+
Log.LogMessage(MessageImportance.Low, "Details: {0}", e);
186180
}
187181
}
188182
}

src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateImageIndex.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
8383

8484
var telemetry = new Telemetry(sourceImageReference, destinationImageReference, Log);
8585

86-
await ImagePublisher.PublishImageAsync(multiArchImage, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
86+
await ImagePublisher.PublishImageAsync(multiArchImage, sourceImageReference, destinationImageReference, Log, BuildEngine != null, telemetry, cancellationToken)
8787
.ConfigureAwait(false);
8888

8989
return !Log.HasLoggedErrors;

src/Containers/Microsoft.NET.Build.Containers/Tasks/CreateNewImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ internal async Task<bool> ExecuteAsync(CancellationToken cancellationToken)
183183

184184
if (!SkipPublishing)
185185
{
186-
await ImagePublisher.PublishImageAsync(builtImage, sourceImageReference, destinationImageReference, Log, BuildEngine, telemetry, cancellationToken)
186+
await ImagePublisher.PublishImageAsync(builtImage, sourceImageReference, destinationImageReference, Log, BuildEngine != null, telemetry, cancellationToken)
187187
.ConfigureAwait(false);
188188
}
189189

0 commit comments

Comments
 (0)