Skip to content

Commit 138a671

Browse files
committed
chore: added the changes for the configurable pr
1 parent d4ec2b4 commit 138a671

File tree

5 files changed

+93
-55
lines changed

5 files changed

+93
-55
lines changed

Common/RepoConfiguration.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class RepoConfiguration
1515
public string PrTitle { get; set; }
1616

1717
public string PrBody { get; set; }
18-
19-
public string[] Labels { get; set; }
18+
19+
public string Labels { get; set; }
20+
21+
public bool? PrDetails { get; set; }
2022
}
2123
}

Common/TableModels/Settings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public Settings(string installationId, string repoName)
2626

2727
public string PrTitle { get; set; }
2828

29-
public string[] Labels { get; set; } //todo store as string
29+
public string Labels { get; set; }
30+
31+
public bool? PrDetails { get; set; }
3032
}
3133
}

CompressImagesFunction/CompressImages.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public static class CompressImages
3131

3232
private static bool PaidPlan (CloudStorageAccount storageAccount, string ownerLogin)
3333
{
34-
Console.WriteLine("checking");
3534
var marketplaceTable = storageAccount.CreateCloudTableClient().GetTableReference("marketplace");
3635
var paidPlans = KnownGitHubs.Plans.Keys.Where(k => KnownGitHubs.Plans[k] != 0);
3736
string plansQuery = string.Empty;
@@ -52,15 +51,13 @@ private static bool PaidPlan (CloudStorageAccount storageAccount, string ownerLo
5251

5352
i++;
5453
}
55-
Console.WriteLine("query");
56-
Console.WriteLine(plansQuery);
54+
5755
var query = new TableQuery<Marketplace>().Where(
5856
$"AccountLogin eq '{ownerLogin}' and ({plansQuery})");
5957

6058
var rows = marketplaceTable.ExecuteQuerySegmentedAsync(query, null).Result;
61-
6259
var plan = rows.FirstOrDefault();
63-
Console.WriteLine("plan");
60+
6461
return plan != null;
6562
}
6663

@@ -137,12 +134,9 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
137134
if (!string.IsNullOrEmpty(repoConfigJson))
138135
{
139136
repoConfiguration = JsonConvert.DeserializeObject<RepoConfiguration>(repoConfigJson);
140-
//here
141-
Console.WriteLine(JsonConvert.SerializeObject(repoConfiguration));
142137

143-
if (paidPlan && (repoConfiguration.PrBody != null || repoConfiguration.PrTitle != null || repoConfiguration.Labels.Any()))
138+
if (paidPlan && (repoConfiguration.PrBody != null || repoConfiguration.PrTitle != null || repoConfiguration.Labels.Any() || repoConfiguration.PrDetails != null))
144139
{
145-
Console.WriteLine("here");
146140
var settingsTable = storageAccount.CreateCloudTableClient().GetTableReference("settings");
147141
var settings = new Common.TableModels.Settings(
148142
parameters.CompressImagesMessage.InstallationId.ToString(),
@@ -152,6 +146,10 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
152146
PrTitle = repoConfiguration.PrTitle,
153147
Labels = repoConfiguration.Labels,
154148
};
149+
if (repoConfiguration.PrDetails != null)
150+
{
151+
settings.PrDetails = repoConfiguration.PrDetails;
152+
}
155153

156154
settingsTable.ExecuteAsync(TableOperation.InsertOrReplace(settings)).Wait();
157155
}

OpenPrFunction/PullRequest.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Threading.Tasks;
45
using Common;
@@ -47,21 +48,38 @@ public async Task<Pr> OpenAsync(GitHubClientParameters parameters, bool update,
4748

4849
var pru = new PullRequestUpdate()
4950
{
50-
Body = PullRequestBody.Generate(stats),
51+
Body = PullRequestBody.Generate(stats, settings),
5152
};
5253

5354
result = await githubClient.PullRequest.Update(parameters.RepoOwner, parameters.RepoName, pr.Number, pru);
5455
}
5556
else
5657
{
57-
var pr = new NewPullRequest(KnownGitHubs.CommitMessageTitle, KnownGitHubs.BranchName, baseBranch)
58+
var commitMessageTitle = KnownGitHubs.CommitMessageTitle;
59+
if (settings?.PrTitle != null)
5860
{
59-
Body = PullRequestBody.Generate(stats),
60-
};
61+
commitMessageTitle = settings.PrTitle;
62+
}
6163

64+
var pr = new NewPullRequest(commitMessageTitle, KnownGitHubs.BranchName, baseBranch)
65+
{
66+
Body = PullRequestBody.Generate(stats, settings),
67+
};
6268
result = await githubClient.PullRequest.Create(parameters.RepoOwner, parameters.RepoName, pr);
6369
}
6470

71+
var labels = new List<string>();
72+
if (settings?.Labels != null)
73+
{
74+
var issueUpdate = new IssueUpdate();
75+
foreach (var label in settings.Labels.Split(','))
76+
{
77+
issueUpdate.AddLabel(label);
78+
}
79+
80+
await githubClient.Issue.Update(parameters.RepoOwner, parameters.RepoName, result.Number, issueUpdate);
81+
}
82+
6583
if (stats == null)
6684
{
6785
return null;

OpenPrFunction/PullRequestBody.cs

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Text;
3+
using Common.TableModels;
34

45
namespace OpenPrFunction
56
{
@@ -8,7 +9,7 @@ public static class PullRequestBody
89
/*
910
* Convert the ImageStat[] into a markdown PR body
1011
*/
11-
public static string Generate(ImageStat[] imageStats)
12+
public static string Generate(ImageStat[] imageStats, Settings settings = null)
1213
{
1314
if (imageStats == null || imageStats.Length == 0)
1415
{
@@ -17,61 +18,78 @@ public static string Generate(ImageStat[] imageStats)
1718
}
1819

1920
var sb = new StringBuilder();
20-
sb.AppendLine("## Beep boop. Your images are optimized!");
21-
sb.AppendLine();
22-
23-
if (Math.Round(imageStats[0].Percent) < 5)
21+
if (settings?.PrBody == null)
2422
{
25-
sb.AppendLine("Your image file size has been reduced!");
23+
sb.AppendLine("## Beep boop. Your images are optimized!");
24+
sb.AppendLine();
25+
26+
if (Math.Round(imageStats[0].Percent) < 5)
27+
{
28+
sb.AppendLine("Your image file size has been reduced!");
29+
}
30+
else
31+
{
32+
sb.AppendLine($"Your image file size has been reduced by **{imageStats[0].Percent:N0}%** 🎉");
33+
}
2634
}
2735
else
2836
{
29-
sb.AppendLine($"Your image file size has been reduced by **{imageStats[0].Percent:N0}%** 🎉");
37+
sb.AppendLine(settings.PrBody);
3038
}
3139

3240
sb.AppendLine();
33-
sb.AppendLine("<details>");
41+
if (settings?.PrDetails != false)
42+
{
43+
sb.AppendLine("<details>");
3444

35-
sb.AppendLine("<summary>");
36-
sb.AppendLine("Details");
37-
sb.AppendLine("</summary>");
38-
sb.AppendLine();
45+
sb.AppendLine("<summary>");
46+
sb.AppendLine("Details");
47+
sb.AppendLine("</summary>");
48+
sb.AppendLine();
3949

40-
sb.AppendLine("| File | Before | After | Percent reduction |");
41-
sb.AppendLine("|:--|:--|:--|:--|");
50+
sb.AppendLine("| File | Before | After | Percent reduction |");
51+
sb.AppendLine("|:--|:--|:--|:--|");
4252

43-
if (imageStats.Length == 1)
44-
{
45-
sb.AppendLine($"| {imageStats[0].Name} | {imageStats[0].Before} | {imageStats[0].After} | {imageStats[0].Percent:N2}% |");
46-
}
47-
else
48-
{
49-
// the zeroth item is the total; we print it at the bottom of the table
50-
for (var i = 1; i < imageStats.Length; i++)
53+
if (imageStats.Length == 1)
54+
{
55+
sb.AppendLine(
56+
$"| {imageStats[0].Name} | {imageStats[0].Before} | {imageStats[0].After} | {imageStats[0].Percent:N2}% |");
57+
}
58+
else
5159
{
52-
sb.AppendLine($"| {imageStats[i].Name} | {imageStats[i].Before} | {imageStats[i].After} | {imageStats[i].Percent:N2}% |");
60+
// the zeroth item is the total; we print it at the bottom of the table
61+
for (var i = 1; i < imageStats.Length; i++)
62+
{
63+
sb.AppendLine(
64+
$"| {imageStats[i].Name} | {imageStats[i].Before} | {imageStats[i].After} | {imageStats[i].Percent:N2}% |");
65+
}
66+
67+
sb.AppendLine("| | | | |");
68+
sb.AppendLine(
69+
$"| **Total :** | **{imageStats[0].Before}** | **{imageStats[0].After}** | **{imageStats[0].Percent:N2}%** |");
5370
}
5471

55-
sb.AppendLine("| | | | |");
56-
sb.AppendLine($"| **Total :** | **{imageStats[0].Before}** | **{imageStats[0].After}** | **{imageStats[0].Percent:N2}%** |");
72+
sb.AppendLine("</details>");
73+
sb.AppendLine();
5774
}
5875

59-
sb.AppendLine("</details>");
60-
sb.AppendLine();
61-
sb.AppendLine("---");
62-
sb.AppendLine();
76+
if (settings?.PrBody == null)
77+
{
78+
sb.AppendLine("---");
79+
sb.AppendLine();
80+
sb.Append("[📝 docs](https://imgbot.net/docs) | ");
81+
sb.Append("[:octocat: repo](https://github.com/imgbot/ImgBot) | ");
82+
sb.Append("[🙋🏾 issues](https://github.com/imgbot/ImgBot/issues) | ");
83+
sb.Append("[🏪 marketplace](https://github.com/marketplace/imgbot)");
6384

64-
sb.Append("[📝 docs](https://imgbot.net/docs) | ");
65-
sb.Append("[:octocat: repo](https://github.com/imgbot/ImgBot) | ");
66-
sb.Append("[🙋🏾 issues](https://github.com/imgbot/ImgBot/issues) | ");
67-
sb.Append("[🏪 marketplace](https://github.com/marketplace/imgbot)");
85+
sb.AppendLine();
86+
sb.AppendLine();
87+
sb.Append("<i>");
88+
sb.Append("~Imgbot - Part of [Optimole](https://optimole.com/) family");
89+
sb.Append("</i>");
90+
sb.AppendLine();
91+
}
6892

69-
sb.AppendLine();
70-
sb.AppendLine();
71-
sb.Append("<i>");
72-
sb.Append("~Imgbot - Part of [Optimole](https://optimole.com/) family");
73-
sb.Append("</i>");
74-
sb.AppendLine();
7593
return sb.ToString();
7694
}
7795
}

0 commit comments

Comments
 (0)