99using Microsoft . Extensions . DependencyInjection ;
1010using Microsoft . Extensions . Options ;
1111using Newtonsoft . Json ;
12- using NLog ;
1312using RadarrAPI . Database ;
1413using RadarrAPI . Release . AppVeyor . Responses ;
1514using RadarrAPI . Update ;
1615using Microsoft . EntityFrameworkCore ;
1716using RadarrAPI . Database . Models ;
17+ using OperatingSystem = RadarrAPI . Update . OperatingSystem ;
1818
1919namespace RadarrAPI . Release . AppVeyor
2020{
@@ -23,35 +23,44 @@ public class AppVeyorReleaseSource : ReleaseSourceBase
2323 private const string AccountName = "lidarr" ;
2424 private const string ProjectSlug = "lidarr" ;
2525
26+ private static int ? _lastBuildId ;
27+
28+ private readonly DatabaseContext _database ;
29+
30+ private readonly Config _config ;
31+
2632 private readonly HttpClient _httpClient ;
2733
2834 private readonly HttpClient _downloadHttpClient ;
2935
30- private int ? _lastBuildId ;
31-
32- public AppVeyorReleaseSource ( IServiceProvider serviceProvider , Branch branch ) : base ( serviceProvider , branch )
36+ public AppVeyorReleaseSource ( DatabaseContext database , IOptions < Config > config )
3337 {
34- var config = serviceProvider . GetService < IOptions < Config > > ( ) . Value ;
38+ _database = database ;
39+ _config = config . Value ;
3540
3641 _httpClient = new HttpClient ( ) ;
3742 _httpClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
38- _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , config . AppVeyorApiKey ) ;
43+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _config . AppVeyorApiKey ) ;
3944
4045 _downloadHttpClient = new HttpClient ( ) ;
4146 }
4247
4348 protected override async Task DoFetchReleasesAsync ( )
4449 {
50+ if ( ReleaseBranch == Branch . Unknown )
51+ {
52+ throw new ArgumentException ( "ReleaseBranch must not be unknown when fetching releases." ) ;
53+ }
54+
4555 var historyUrl = $ "https://ci.appveyor.com/api/projects/{ AccountName } /{ ProjectSlug } /history?recordsNumber=10&branch=develop";
4656
4757 var historyData = await _httpClient . GetStringAsync ( historyUrl ) ;
4858 var history = JsonConvert . DeserializeObject < AppVeyorProjectHistory > ( historyData ) ;
4959
5060 // Store here temporarily so we don't break on not processed builds.
5161 var lastBuild = _lastBuildId ;
52- var database = ServiceProvider . GetService < DatabaseContext > ( ) ;
5362
54- foreach ( var build in history . Builds )
63+ foreach ( var build in history . Builds . Take ( 5 ) . ToList ( ) ) // Only take last 5.
5564 {
5665 if ( lastBuild . HasValue &&
5766 lastBuild . Value >= build . BuildId ) break ;
@@ -78,7 +87,7 @@ protected override async Task DoFetchReleasesAsync()
7887 var artifacts = JsonConvert . DeserializeObject < AppVeyorArtifact [ ] > ( artifactsData ) ;
7988
8089 // Get an updateEntity
81- var updateEntity = database . UpdateEntities
90+ var updateEntity = _database . UpdateEntities
8291 . Include ( x => x . UpdateFiles )
8392 . FirstOrDefault ( x => x . Version . Equals ( buildExtended . Version ) && x . Branch . Equals ( ReleaseBranch ) ) ;
8493
@@ -103,7 +112,7 @@ protected override async Task DoFetchReleasesAsync()
103112 }
104113
105114 // Start tracking this object
106- await database . AddAsync ( updateEntity ) ;
115+ await _database . AddAsync ( updateEntity ) ;
107116 }
108117
109118 // Process artifacts
@@ -130,7 +139,7 @@ protected override async Task DoFetchReleasesAsync()
130139 }
131140
132141 // Check if exists in database.
133- var updateFileEntity = database . UpdateFileEntities
142+ var updateFileEntity = _database . UpdateFileEntities
134143 . FirstOrDefault ( x =>
135144 x . UpdateEntityId == updateEntity . UpdateEntityId &&
136145 x . OperatingSystem == operatingSystem ) ;
@@ -140,7 +149,7 @@ protected override async Task DoFetchReleasesAsync()
140149 // Calculate the hash of the zip file.
141150 var releaseDownloadUrl = $ "{ artifactsPath } /{ artifact . FileName } ";
142151 var releaseFileName = artifact . FileName . Split ( '/' ) . Last ( ) ;
143- var releaseZip = Path . Combine ( Config . DataDirectory , ReleaseBranch . ToString ( ) , releaseFileName ) ;
152+ var releaseZip = Path . Combine ( _config . DataDirectory , ReleaseBranch . ToString ( ) , releaseFileName ) ;
144153 string releaseHash ;
145154
146155 if ( ! File . Exists ( releaseZip ) )
@@ -170,7 +179,7 @@ protected override async Task DoFetchReleasesAsync()
170179 }
171180
172181 // Save all changes to the database.
173- await database . SaveChangesAsync ( ) ;
182+ await _database . SaveChangesAsync ( ) ;
174183
175184 // Make sure we atleast skip this build next time.
176185 if ( _lastBuildId == null ||
0 commit comments