Skip to content

Commit d4ec2b4

Browse files
committed
chore: added a part of configurable pr code
1 parent e86c478 commit d4ec2b4

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

Common/RepoConfiguration.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@ public class RepoConfiguration
1111
public bool CompressWiki { get; set; }
1212

1313
public int? MinKBReduced { get; set; } = 10;
14+
15+
public string PrTitle { get; set; }
16+
17+
public string PrBody { get; set; }
18+
19+
public string[] Labels { get; set; }
1420
}
1521
}

Common/TableModels/Settings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@ public Settings(string installationId, string repoName)
2121
public string RepoName { get; set; }
2222

2323
public string DefaultBranchOverride { get; set; }
24+
25+
public string PrBody { get; set; }
26+
27+
public string PrTitle { get; set; }
28+
29+
public string[] Labels { get; set; } //todo store as string
2430
}
2531
}

CompressImagesFunction/CompressImages.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
using System.Threading.Tasks;
77
using Common;
88
using Common.Messages;
9+
using Common.TableModels;
910
using CompressImagesFunction.Compressors;
1011
using ImageMagick;
1112
using LibGit2Sharp;
1213
using LibGit2Sharp.Handlers;
1314
using Microsoft.Azure.WebJobs;
1415
using Microsoft.Extensions.Logging;
16+
using Microsoft.WindowsAzure.Storage;
17+
using Microsoft.WindowsAzure.Storage.Table;
1518
using Newtonsoft.Json;
1619

1720
namespace CompressImagesFunction
@@ -26,8 +29,45 @@ public static class CompressImages
2629
new GifsicleCompress(),
2730
};
2831

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+
2967
public static bool Run(CompressimagesParameters parameters, ICollector<CompressImagesMessage> compressImagesMessages, ILogger logger)
3068
{
69+
var storageAccount = CloudStorageAccount.Parse(Common.KnownEnvironmentVariables.AzureWebJobsStorage);
70+
var paidPlan = PaidPlan(storageAccount, parameters.RepoOwner);
3171
CredentialsHandler credentialsProvider =
3272
(url, user, cred) =>
3373
new UsernamePasswordCredentials { Username = KnownGitHubs.Username, Password = parameters.Password };
@@ -97,6 +137,24 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
97137
if (!string.IsNullOrEmpty(repoConfigJson))
98138
{
99139
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+
}
100158
}
101159
}
102160
catch

WebHook/WebHookFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ private static async Task<bool> IsPrivateEligible(CloudTable marketplaceTable, s
376376
string needsOr = string.Empty;
377377
if (limitedPlans.Count() > 0)
378378
{
379-
needsOr = " or";
379+
needsOr = " or ";
380380
}
381381

382382
int i = 0;

0 commit comments

Comments
 (0)