Skip to content

Commit a5f98e8

Browse files
committed
chore: reduce code duplication in ForgejoVersionCache#RefreshAsync
1 parent e35b939 commit a5f98e8

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

src/Server/Services/Forgejo/ForgejoVersionCache.cs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -142,42 +142,18 @@ public async Task RefreshAsync()
142142
{
143143
_logger.LogInformation("Reloading version cache for {project}", ProjectName);
144144

145-
if (!_deriveLatestManually)
146-
{
147-
_logger.LogInformation("Requesting latest version for {project}.", ProjectPath);
148-
try
149-
{
150-
_latestTag = await _fj.Client.Repository.GetReleaseLatestAsync(
151-
ProjectPath.Split('/')[0], ProjectPath.Split('/')[1]
152-
).Then(r => r.tag_name);
153-
_logger.LogInformation("Latest received: {latestTag}.", _latestTag);
154-
}
155-
catch (Exception e)
156-
{
157-
_logger.LogWarning("Errored trying to get latest release for {project}; aborting. Message: {message}",
158-
ProjectName, e.Message);
159-
return;
160-
}
161-
162-
if (_latestTag is null)
163-
{
164-
_logger.LogWarning("Latest version for {project} was a 404, aborting.", ProjectName);
165-
return;
166-
}
167-
}
168-
169145
var sw = Stopwatch.StartNew();
170146

171147
var releases = await _fj.ListReleasesForRepositoryAsync(
172148
ProjectPath.Split('/')[0],
173149
ProjectPath.Split('/')[1]
174150
);
175151

176-
if (_deriveLatestManually)
152+
try
177153
{
178-
_logger.LogInformation("Deriving latest version for {project}.", ProjectPath);
179-
try
154+
if (_deriveLatestManually)
180155
{
156+
_logger.LogInformation("Deriving latest version for {project}.", ProjectPath);
181157
Dictionary<Version, string> versionMapping = new();
182158
foreach (var ver in releases.Select(x => x.TagName).Where(x => x != null))
183159
{
@@ -196,18 +172,26 @@ public async Task RefreshAsync()
196172
_latestTag = versionMapping.OrderByDescending(x => x.Key).FirstOrDefault().Value;
197173
_logger.LogInformation("Latest found: {latestTag}.", _latestTag);
198174
}
199-
catch (Exception e)
175+
else
200176
{
201-
_logger.LogWarning("Errored trying to get latest release for {project}; aborting. Message: {message}",
202-
ProjectName, e.Message);
203-
return;
177+
_logger.LogInformation("Requesting latest version for {project}.", ProjectPath);
178+
_latestTag = await _fj.Client.Repository.GetReleaseLatestAsync(
179+
ProjectPath.Split('/')[0], ProjectPath.Split('/')[1]
180+
).Then(r => r.tag_name);
181+
_logger.LogInformation("Latest received: {latestTag}.", _latestTag);
204182
}
183+
}
184+
catch (Exception e)
185+
{
186+
_logger.LogWarning("Errored trying to get latest release for {project}; aborting. Message: {message}",
187+
ProjectName, e.Message);
188+
return;
189+
}
205190

206-
if (_latestTag is null)
207-
{
208-
_logger.LogWarning("Latest version for {project} was a 404, aborting.", ProjectName);
209-
return;
210-
}
191+
if (_latestTag is null)
192+
{
193+
_logger.LogWarning("Latest version for {project} was a 404, aborting.", ProjectName);
194+
return;
211195
}
212196

213197
var tempCacheEntries = releases

0 commit comments

Comments
 (0)