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{
2121 public class AppVeyorReleaseSource : ReleaseSourceBase
2222 {
2323 private const string AccountName = "galli-leo" ;
24+
2425 private const string ProjectSlug = "radarr-usby1" ;
2526
27+ private static int ? _lastBuildId ;
28+
29+ private readonly DatabaseContext _database ;
30+
31+ private readonly Config _config ;
32+
2633 private readonly HttpClient _httpClient ;
2734
2835 private readonly HttpClient _downloadHttpClient ;
2936
30- private int ? _lastBuildId ;
31-
32- public AppVeyorReleaseSource ( IServiceProvider serviceProvider , Branch branch ) : base ( serviceProvider , branch )
37+ public AppVeyorReleaseSource ( DatabaseContext database , IOptions < Config > config )
3338 {
34- var config = serviceProvider . GetService < IOptions < Config > > ( ) . Value ;
39+ _database = database ;
40+ _config = config . Value ;
3541
3642 _httpClient = new HttpClient ( ) ;
3743 _httpClient . DefaultRequestHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
38- _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , config . AppVeyorApiKey ) ;
44+ _httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( "Bearer" , _config . AppVeyorApiKey ) ;
3945
4046 _downloadHttpClient = new HttpClient ( ) ;
4147 }
4248
4349 protected override async Task DoFetchReleasesAsync ( )
4450 {
51+ if ( ReleaseBranch == Branch . Unknown )
52+ {
53+ throw new ArgumentException ( "ReleaseBranch must not be unknown when fetching releases." ) ;
54+ }
55+
4556 var historyUrl = $ "https://ci.appveyor.com/api/projects/{ AccountName } /{ ProjectSlug } /history?recordsNumber=10&branch=develop";
4657
4758 var historyData = await _httpClient . GetStringAsync ( historyUrl ) ;
4859 var history = JsonConvert . DeserializeObject < AppVeyorProjectHistory > ( historyData ) ;
4960
5061 // Store here temporarily so we don't break on not processed builds.
5162 var lastBuild = _lastBuildId ;
52- var database = ServiceProvider . GetService < DatabaseContext > ( ) ;
5363
54- foreach ( var build in history . Builds )
64+ foreach ( var build in history . Builds . Take ( 5 ) . ToList ( ) ) // Only take last 5.
5565 {
5666 if ( lastBuild . HasValue &&
5767 lastBuild . Value >= build . BuildId ) break ;
@@ -78,7 +88,7 @@ protected override async Task DoFetchReleasesAsync()
7888 var artifacts = JsonConvert . DeserializeObject < AppVeyorArtifact [ ] > ( artifactsData ) ;
7989
8090 // Get an updateEntity
81- var updateEntity = database . UpdateEntities
91+ var updateEntity = _database . UpdateEntities
8292 . Include ( x => x . UpdateFiles )
8393 . FirstOrDefault ( x => x . Version . Equals ( buildExtended . Version ) && x . Branch . Equals ( ReleaseBranch ) ) ;
8494
@@ -103,7 +113,7 @@ protected override async Task DoFetchReleasesAsync()
103113 }
104114
105115 // Start tracking this object
106- await database . AddAsync ( updateEntity ) ;
116+ await _database . AddAsync ( updateEntity ) ;
107117 }
108118
109119 // Process artifacts
@@ -130,7 +140,7 @@ protected override async Task DoFetchReleasesAsync()
130140 }
131141
132142 // Check if exists in database.
133- var updateFileEntity = database . UpdateFileEntities
143+ var updateFileEntity = _database . UpdateFileEntities
134144 . FirstOrDefault ( x =>
135145 x . UpdateEntityId == updateEntity . UpdateEntityId &&
136146 x . OperatingSystem == operatingSystem ) ;
@@ -140,7 +150,7 @@ protected override async Task DoFetchReleasesAsync()
140150 // Calculate the hash of the zip file.
141151 var releaseDownloadUrl = $ "{ artifactsPath } /{ artifact . FileName } ";
142152 var releaseFileName = artifact . FileName . Split ( '/' ) . Last ( ) ;
143- var releaseZip = Path . Combine ( Config . DataDirectory , ReleaseBranch . ToString ( ) , releaseFileName ) ;
153+ var releaseZip = Path . Combine ( _config . DataDirectory , ReleaseBranch . ToString ( ) , releaseFileName ) ;
144154 string releaseHash ;
145155
146156 if ( ! File . Exists ( releaseZip ) )
@@ -170,7 +180,7 @@ protected override async Task DoFetchReleasesAsync()
170180 }
171181
172182 // Save all changes to the database.
173- await database . SaveChangesAsync ( ) ;
183+ await _database . SaveChangesAsync ( ) ;
174184
175185 // Make sure we atleast skip this build next time.
176186 if ( _lastBuildId == null ||
0 commit comments