@@ -9,58 +9,58 @@ namespace Documentation.Builder.LinkIndex;
99
1010public class S3LinkIndex : ILinkIndex
1111{
12- private readonly string _bucketName ;
13- private readonly ILogger < S3LinkIndex > _logger ;
12+ private readonly string _bucketName ;
13+ private readonly ILogger < S3LinkIndex > _logger ;
1414
15- public S3LinkIndex ( string bucketName , ILoggerFactory loggerFactory )
16- {
17- _bucketName = bucketName ;
18- _logger = loggerFactory . CreateLogger < S3LinkIndex > ( ) ;
19- }
15+ public S3LinkIndex ( string bucketName , ILoggerFactory loggerFactory )
16+ {
17+ _bucketName = bucketName ;
18+ _logger = loggerFactory . CreateLogger < S3LinkIndex > ( ) ;
19+ }
2020
21- public async Task UploadFileAsync ( string filePath , bool shouldUpload )
22- {
23- if ( ! shouldUpload )
24- {
25- _logger . LogInformation ( "Not uploading link index: skip flag is set" ) ;
26- return ;
27- }
21+ public async Task UploadFileAsync ( string filePath , bool shouldUpload )
22+ {
23+ if ( ! shouldUpload )
24+ {
25+ _logger . LogInformation ( "Not uploading link index: skip flag is set" ) ;
26+ return ;
27+ }
2828
29- var githubRef = Environment . GetEnvironmentVariable ( "GITHUB_REF" ) ;
30- if ( string . IsNullOrEmpty ( githubRef ) || githubRef != "refs/heads/main" )
31- {
32- _logger . LogWarning ( "Not uploading link index: GITHUB_REF '{GitHubRef}' is not main branch" , githubRef ) ;
33- return ;
34- }
35-
36- var s3DestinationPath = DeriveDestinationPath ( ) ;
37- if ( string . IsNullOrEmpty ( s3DestinationPath ) )
38- {
39- _logger . LogWarning ( "Failed to derive destination path - cannot upload to link index" ) ;
40- return ;
41- }
42- _logger . LogInformation ( "Uploading link index {FilePath} to S3://{Bucket}/{DestinationPath}" , filePath , _bucketName , s3DestinationPath ) ;
43- using var client = new AmazonS3Client ( ) ;
44- var fileTransferUtility = new TransferUtility ( client ) ;
45- try
46- {
47- await fileTransferUtility . UploadAsync ( filePath , _bucketName , s3DestinationPath ) ;
48- _logger . LogInformation ( "Successfully uploaded link reference {FilePath} to S3://{Bucket}/{DestinationPath}" ,
49- filePath , _bucketName , s3DestinationPath ) ;
50- }
51- catch ( Exception e )
52- {
53- _logger . LogError ( e , "Failed to upload link index {FilePath} to S3://{Bucket}/{DestinationPath}" ,
54- filePath , _bucketName , s3DestinationPath ) ;
55- throw ;
56- }
57- }
29+ var githubRef = Environment . GetEnvironmentVariable ( "GITHUB_REF" ) ;
30+ if ( string . IsNullOrEmpty ( githubRef ) || githubRef != "refs/heads/main" )
31+ {
32+ _logger . LogWarning ( "Not uploading link index: GITHUB_REF '{GitHubRef}' is not main branch" , githubRef ) ;
33+ return ;
34+ }
5835
59- private static string DeriveDestinationPath ( )
60- {
61- var repositoryName = Environment . GetEnvironmentVariable ( "GITHUB_REPOSITORY" ) ? . Split ( '/' ) . Last ( ) ;
62- return string . IsNullOrEmpty ( repositoryName )
63- ? string . Empty
64- : $ "{ repositoryName } .json";
65- }
36+ var s3DestinationPath = DeriveDestinationPath ( ) ;
37+ if ( string . IsNullOrEmpty ( s3DestinationPath ) )
38+ {
39+ _logger . LogWarning ( "Failed to derive destination path - cannot upload to link index" ) ;
40+ return ;
41+ }
42+ _logger . LogInformation ( "Uploading link index {FilePath} to S3://{Bucket}/{DestinationPath}" , filePath , _bucketName , s3DestinationPath ) ;
43+ using var client = new AmazonS3Client ( ) ;
44+ var fileTransferUtility = new TransferUtility ( client ) ;
45+ try
46+ {
47+ await fileTransferUtility . UploadAsync ( filePath , _bucketName , s3DestinationPath ) ;
48+ _logger . LogInformation ( "Successfully uploaded link reference {FilePath} to S3://{Bucket}/{DestinationPath}" ,
49+ filePath , _bucketName , s3DestinationPath ) ;
50+ }
51+ catch ( Exception e )
52+ {
53+ _logger . LogError ( e , "Failed to upload link index {FilePath} to S3://{Bucket}/{DestinationPath}" ,
54+ filePath , _bucketName , s3DestinationPath ) ;
55+ throw ;
56+ }
57+ }
58+
59+ private static string DeriveDestinationPath ( )
60+ {
61+ var repositoryName = Environment . GetEnvironmentVariable ( "GITHUB_REPOSITORY" ) ? . Split ( '/' ) . Last ( ) ;
62+ return string . IsNullOrEmpty ( repositoryName )
63+ ? string . Empty
64+ : $ "{ repositoryName } .json";
65+ }
6666}
0 commit comments