6
6
using System . Threading . Tasks ;
7
7
using Common ;
8
8
using Common . Messages ;
9
+ using Common . TableModels ;
9
10
using CompressImagesFunction . Compressors ;
10
11
using ImageMagick ;
11
12
using LibGit2Sharp ;
12
13
using LibGit2Sharp . Handlers ;
13
14
using Microsoft . Azure . WebJobs ;
14
15
using Microsoft . Extensions . Logging ;
16
+ using Microsoft . WindowsAzure . Storage ;
17
+ using Microsoft . WindowsAzure . Storage . Table ;
15
18
using Newtonsoft . Json ;
16
19
17
20
namespace CompressImagesFunction
@@ -26,8 +29,45 @@ public static class CompressImages
26
29
new GifsicleCompress ( ) ,
27
30
} ;
28
31
32
+ private static bool PaidPlan ( CloudStorageAccount storageAccount , string ownerLogin )
33
+ {
34
+ Console . WriteLine ( "checking" ) ;
35
+ var marketplaceTable = storageAccount . CreateCloudTableClient ( ) . GetTableReference ( "marketplace" ) ;
36
+ var paidPlans = KnownGitHubs . Plans . Keys . Where ( k => KnownGitHubs . Plans [ k ] != 0 ) ;
37
+ string plansQuery = string . Empty ;
38
+ string needsOr = string . Empty ;
39
+ if ( paidPlans . Count ( ) > 0 )
40
+ {
41
+ needsOr = " or " ;
42
+ }
43
+
44
+ int i = 0 ;
45
+ foreach ( int planId in paidPlans )
46
+ {
47
+ plansQuery += "PlanId eq " + planId . ToString ( ) ;
48
+ if ( i != paidPlans . Count ( ) - 1 )
49
+ {
50
+ plansQuery += needsOr ;
51
+ }
52
+
53
+ i ++ ;
54
+ }
55
+ Console . WriteLine ( "query" ) ;
56
+ Console . WriteLine ( plansQuery ) ;
57
+ var query = new TableQuery < Marketplace > ( ) . Where (
58
+ $ "AccountLogin eq '{ ownerLogin } ' and ({ plansQuery } )") ;
59
+
60
+ var rows = marketplaceTable . ExecuteQuerySegmentedAsync ( query , null ) . Result ;
61
+
62
+ var plan = rows . FirstOrDefault ( ) ;
63
+ Console . WriteLine ( "plan" ) ;
64
+ return plan != null ;
65
+ }
66
+
29
67
public static bool Run ( CompressimagesParameters parameters , ICollector < CompressImagesMessage > compressImagesMessages , ILogger logger )
30
68
{
69
+ var storageAccount = CloudStorageAccount . Parse ( Common . KnownEnvironmentVariables . AzureWebJobsStorage ) ;
70
+ var paidPlan = PaidPlan ( storageAccount , parameters . RepoOwner ) ;
31
71
CredentialsHandler credentialsProvider =
32
72
( url , user , cred ) =>
33
73
new UsernamePasswordCredentials { Username = KnownGitHubs . Username , Password = parameters . Password } ;
@@ -97,6 +137,24 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
97
137
if ( ! string . IsNullOrEmpty ( repoConfigJson ) )
98
138
{
99
139
repoConfiguration = JsonConvert . DeserializeObject < RepoConfiguration > ( repoConfigJson ) ;
140
+ //here
141
+ Console . WriteLine ( JsonConvert . SerializeObject ( repoConfiguration ) ) ;
142
+
143
+ if ( paidPlan && ( repoConfiguration . PrBody != null || repoConfiguration . PrTitle != null || repoConfiguration . Labels . Any ( ) ) )
144
+ {
145
+ Console . WriteLine ( "here" ) ;
146
+ var settingsTable = storageAccount . CreateCloudTableClient ( ) . GetTableReference ( "settings" ) ;
147
+ var settings = new Common . TableModels . Settings (
148
+ parameters . CompressImagesMessage . InstallationId . ToString ( ) ,
149
+ parameters . CompressImagesMessage . RepoName )
150
+ {
151
+ PrBody = repoConfiguration . PrBody ,
152
+ PrTitle = repoConfiguration . PrTitle ,
153
+ Labels = repoConfiguration . Labels ,
154
+ } ;
155
+
156
+ settingsTable . ExecuteAsync ( TableOperation . InsertOrReplace ( settings ) ) . Wait ( ) ;
157
+ }
100
158
}
101
159
}
102
160
catch
0 commit comments