tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
FetchOptionsExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Threading;
3
4using LibGit2Sharp;
5using LibGit2Sharp.Handlers;
6
7using Microsoft.Extensions.Logging;
8
10
12{
17 {
27 public static FetchOptions Hydrate(
28 this FetchOptions fetchOptions,
29 ILogger logger,
30 JobProgressReporter progressReporter,
31 CredentialsHandler credentialsHandler,
32 CancellationToken cancellationToken)
33 {
34 ArgumentNullException.ThrowIfNull(fetchOptions);
35 ArgumentNullException.ThrowIfNull(logger);
36
37 fetchOptions.OnProgress = _ => !cancellationToken.IsCancellationRequested;
38 fetchOptions.OnTransferProgress = transferProgress =>
39 {
40 if (progressReporter != null)
41 {
42 var percentage = ((double)transferProgress.IndexedObjects + transferProgress.ReceivedObjects) / (transferProgress.TotalObjects * 2);
43 progressReporter.ReportProgress(percentage);
44 }
45
46 return !cancellationToken.IsCancellationRequested;
47 };
48 fetchOptions.OnUpdateTips = (_, _, _) => !cancellationToken.IsCancellationRequested;
49 fetchOptions.CredentialsProvider = credentialsHandler;
50 fetchOptions.RepositoryOperationStarting = _ => !cancellationToken.IsCancellationRequested;
51 fetchOptions.OnTransferProgress = TransferProgressHandler(
52 logger,
53 progressReporter,
54 cancellationToken);
55
56 return fetchOptions;
57 }
58
66 static TransferProgressHandler TransferProgressHandler(ILogger logger, JobProgressReporter progressReporter, CancellationToken cancellationToken) => transferProgress =>
67 {
68 double? percentage;
69 var totalObjectsToProcess = transferProgress.TotalObjects * 2;
70 var processedObjects = transferProgress.IndexedObjects + transferProgress.ReceivedObjects;
71 if (totalObjectsToProcess < processedObjects || totalObjectsToProcess == 0)
72 percentage = null;
73 else
74 {
75 percentage = (double)processedObjects / totalObjectsToProcess;
76 if (percentage < 0)
77 percentage = null;
78 }
79
80 if (percentage == null)
81 logger.LogDebug(
82 "Bad transfer progress values (Please tell Cyberboss)! Indexed: {indexed}, Received: {received}, Total: {total}",
83 transferProgress.IndexedObjects,
84 transferProgress.ReceivedObjects,
85 transferProgress.TotalObjects);
86
87 progressReporter?.ReportProgress(percentage);
88 return !cancellationToken.IsCancellationRequested;
89 };
90 }
91}
static TransferProgressHandler TransferProgressHandler(ILogger logger, JobProgressReporter progressReporter, CancellationToken cancellationToken)
Generate a LibGit2Sharp.Handlers.TransferProgressHandler from a given progressReporter and cancellat...
static FetchOptions Hydrate(this FetchOptions fetchOptions, ILogger logger, JobProgressReporter progressReporter, CredentialsHandler credentialsHandler, CancellationToken cancellationToken)
Hydrate a given set of fetchOptions .
void ReportProgress(double? progress)
Report progress.