Skip to content

Commit 11a1a3f

Browse files
authored
Merge pull request #624 from dabutvin/threshold
configuration option for compression threshold (fixes #562)
2 parents 1ce6611 + a336f6a commit 11a1a3f

File tree

8 files changed

+174
-11
lines changed

8 files changed

+174
-11
lines changed

Common/RepoConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public class RepoConfiguration
99
public bool AggressiveCompression { get; set; }
1010

1111
public bool CompressWiki { get; set; }
12+
13+
public int? MinKBReduced { get; set; } = 10;
1214
}
1315
}

CompressImagesFunction/CompressImages.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ public static bool Run(CompressimagesParameters parameters, ICollector<CompressI
139139
if (optimizedImages.Length == 0)
140140
return false;
141141

142+
if (!Threshold.MeetsThreshold(repoConfiguration, optimizedImages))
143+
{
144+
logger.LogInformation($"Did not meet threshold. {parameters.RepoOwner}/{parameters.RepoName}");
145+
return false;
146+
}
147+
142148
// create commit message based on optimizations
143149
foreach (var image in optimizedImages)
144150
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Linq;
2+
using Common;
3+
4+
namespace CompressImagesFunction
5+
{
6+
public class Threshold
7+
{
8+
/// <summary>
9+
/// Using the compressionResults and the repoConfiguration determine whether
10+
/// the optimization warrants a PR at this time.
11+
/// </summary>
12+
/// <returns>True when the images are compressed enough to warrant a PR.</returns>
13+
public static bool MeetsThreshold(RepoConfiguration repoConfiguration, CompressionResult[] compressionResults)
14+
{
15+
if (repoConfiguration.MinKBReduced == null || repoConfiguration.MinKBReduced <= 0)
16+
{
17+
// no threshold specified - let's continue
18+
return true;
19+
}
20+
21+
// determine total KB reduced
22+
var totalKBReduced = compressionResults.Sum(x => x.SizeBefore - x.SizeAfter);
23+
return repoConfiguration.MinKBReduced <= totalKBReduced;
24+
}
25+
}
26+
}

Docs/configuration.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ Here is an example .imgbotconfig setup that shows some of the options.
1414
"image1.png", // ignore by filename
1515
"public/special_images/*", // ignore by folderpath
1616
],
17-
"aggressiveCompression": "true" // true|false
18-
"compressWiki": "true" // true|false
17+
"aggressiveCompression": "true", // true|false
18+
"compressWiki": "true", // true|false
19+
"minKBReduced": 500 // delay new prs until size reduction meets this threshold (default to 10)
1920
}
2021
```
2122

Docs/min-kb-threshold.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
You can set a space saved threshold using the `.imgbotconfig` file.
2+
3+
- This configuration is optional and is only required if you want to change the default threshold
4+
- Default setting is 10KB
5+
- Accepts only numbers as input (e.g. `"minKBReduced": 500` for a 500 KB threshold)
6+
- Can be used to limit the frequency of PRs Imgbot will open over time
7+
8+
`.imgbotconfig`
9+
10+
Setting 500 KB threshold
11+
12+
```
13+
{
14+
"minKBReduced": 500
15+
}
16+
```
17+
18+
To disable this threshold and always open a PR no matter how much size is reduced unset the default
19+
```
20+
{
21+
"minKBReduced": null
22+
}
23+
```

README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# ImgBot
1+
# Imgbot
22

3-
ImgBot crawls all your image files in GitHub and submits pull requests after applying a lossless compression.
3+
Imgbot crawls all your image files in GitHub and submits pull requests after applying a lossless compression.
44
This will make the file size go down, but leave the dimensions and quality just as good.
55

66
![screenshot](https://imgbot.net/images/screen.png?cache=2)
77

88
## Configuration
99

10-
ImgBot supports optional configuration through a `.imgbotconfig` json file.
11-
This is not a required step to using ImgBot and is only for more advanced scenarios.
10+
Imgbot supports optional configuration through a `.imgbotconfig` json file.
11+
This is not a required step to using Imgbot and is only for more advanced scenarios.
1212
This file should be placed in the root of the repository and set to your liking.
1313

1414
```
@@ -20,7 +20,8 @@ This file should be placed in the root of the repository and set to your liking.
2020
"public/special_images/*", // by folderpath
2121
],
2222
"aggressiveCompression": "true", // true|false
23-
"compressWiki": "true" // true|false
23+
"compressWiki": "true", // true|false
24+
"minKBReduced": 500 // set reduction threshold (default to 10)
2425
}
2526
```
2627

@@ -33,14 +34,14 @@ to [email protected]
3334

3435
- optional
3536
- Accepts: daily|weekly|monthly
36-
- Limits the PRs from ImgBot to once a day, once a week, or once a month respectively
37-
- The default behavior is to receive ImgBot PRs as images require optimization
37+
- Limits the PRs from Imgbot to once a day, once a week, or once a month respectively
38+
- The default behavior is to receive Imgbot PRs as images require optimization
3839

3940
**ignoredFiles**
4041

4142
- optional
4243
- Accepts the syntax for searchPattern on [Directory.EnumerateFiles()](https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.enumeratefiles)
43-
- Limits the images optimized by ImgBot by esentially ignoring them
44+
- Limits the images optimized by Imgbot by esentially ignoring them
4445
- When ignoring by filename no path is necessary, when ignoring by foldername full path from root is necessary
4546

4647
**aggressiveCompression**
@@ -58,10 +59,18 @@ to [email protected]
5859
- Example: `https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.wiki.git`
5960
- The default behavior is opt out
6061

62+
63+
**minKBReduced**
64+
65+
- optional
66+
- Accepts only numbers as input (e.g. `"minKBReduced": 500` for a 500 KB threshold)
67+
- Can be used to limit the frequency of PRs Imgbot will open over time
68+
- The default setting is 10
69+
6170
Find out more: https://imgbot.net/docs
6271

6372
## Contributing
6473

65-
All the code for ImgBot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how ImgBot works.
74+
All the code for Imgbot is available on GitHub. We will gladly accept contributions for the service, the website, and the documentation. This is where you can find out how to get set up to run locally as well as detailed information on exactly how Imgbot works.
6675

6776
https://imgbot.net/docs#contributing

Test/ThresholdTests.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Common;
2+
using CompressImagesFunction;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace Test
6+
{
7+
[TestClass]
8+
public class ThresholdTests
9+
{
10+
/// We have a default threshold set so it won't meet it by default
11+
[TestMethod]
12+
public void GivenDefaultConfiguration_ShouldNotOptimizeImages()
13+
{
14+
var compressionResults = new CompressionResult[] { };
15+
var configuration = new RepoConfiguration();
16+
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults);
17+
Assert.IsFalse(shouldOptimize);
18+
}
19+
20+
[TestMethod]
21+
public void GivenDisabledConfiguration_ShouldOptimizeImages()
22+
{
23+
var compressionResults = new CompressionResult[] { };
24+
var configuration = new RepoConfiguration
25+
{
26+
MinKBReduced = null
27+
};
28+
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults);
29+
Assert.IsTrue(shouldOptimize);
30+
}
31+
32+
[TestMethod]
33+
public void Given0_ShouldOptimizeImages()
34+
{
35+
var compressionResults = new CompressionResult[] { };
36+
var configuration = new RepoConfiguration
37+
{
38+
MinKBReduced = 0
39+
};
40+
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults);
41+
Assert.IsTrue(shouldOptimize);
42+
}
43+
44+
[TestMethod]
45+
public void GivenBelowThreshold_ShouldOptimizeImages()
46+
{
47+
var compressionResults = new CompressionResult[]
48+
{
49+
new CompressionResult
50+
{
51+
SizeBefore = 5000,
52+
SizeAfter = 4000,
53+
},
54+
new CompressionResult
55+
{
56+
SizeBefore = 5000,
57+
SizeAfter = 4999,
58+
},
59+
};
60+
var configuration = new RepoConfiguration
61+
{
62+
MinKBReduced = 500
63+
};
64+
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults);
65+
Assert.IsTrue(shouldOptimize);
66+
}
67+
68+
[TestMethod]
69+
public void GivenAboveThreshold_ShouldNotOptimizeImages()
70+
{
71+
var compressionResults = new CompressionResult[]
72+
{
73+
new CompressionResult
74+
{
75+
SizeBefore = 5000,
76+
SizeAfter = 4900,
77+
},
78+
new CompressionResult
79+
{
80+
SizeBefore = 5000,
81+
SizeAfter = 4999,
82+
},
83+
};
84+
var configuration = new RepoConfiguration
85+
{
86+
MinKBReduced = 500
87+
};
88+
var shouldOptimize = Threshold.MeetsThreshold(configuration, compressionResults);
89+
Assert.IsFalse(shouldOptimize);
90+
}
91+
}
92+
}

Web/src/docs/metadata.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
"slug": "compress-wiki",
3232
"title": "Compress wiki"
3333
},
34+
{
35+
"slug": "min-kb-threshold",
36+
"title": "Min KB threshold"
37+
},
3438
{
3539
"slug": "authorization",
3640
"title": "Authorization"

0 commit comments

Comments
 (0)