Skip to content

Commit 420f960

Browse files
authored
Merge pull request #93 from github/jm-date-issue-fix
fix: ensure created_after_date is formatted string before date comparison
2 parents 8a2ec26 + 7de01f7 commit 420f960

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ This action can be configured to authenticate with GitHub App Installation or Pe
6868
| `TITLE` | False | "Enable Dependabot" | The title of the issue or pull request that will be created if dependabot could be enabled. |
6969
| `BODY` | False | **Pull Request:** "Dependabot could be enabled for this repository. Please enable it by merging this pull request so that we can keep our dependencies up to date and secure." **Issue:** "Please update the repository to include a Dependabot configuration file. This will ensure our dependencies remain updated and secure.Follow the guidelines in [creating Dependabot configuration files](https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file) to set it up properly.Here's an example of the code:" | The body of the issue or pull request that will be created if dependabot could be enabled. |
7070
| `COMMIT_MESSAGE` | False | "Create dependabot.yaml" | The commit message for the pull request that will be created if dependabot could be enabled. |
71-
| `CREATED_AFTER_DATE` | False | none | If a value is set, this action will only consider repositories created on or after this date for dependabot enablement. This is useful if you want to only consider newly created repositories. If I set up this action to run weekly and I only want to scan for repos created in the last week that need dependabot enabled, then I would set `CREATED_AFTER_DATE` to 7 days ago. That way only repositories created after 7 days ago will be considered for dependabot enablement. If not set or set to nothing, all repositories will be scanned and a duplicate issue/pull request may occur. Ex: 2023-12-31 for Dec. 31st 2023 |
71+
| `CREATED_AFTER_DATE` | False | none | If a value is set, this action will only consider repositories created on or after this date for dependabot enablement. This is useful if you want to only consider newly created repositories. If I set up this action to run weekly and I only want to scan for repos created in the last week that need dependabot enabled, then I would set `CREATED_AFTER_DATE` to 7 days ago. That way only repositories created after 7 days ago will be considered for dependabot enablement. If not set or set to nothing, all repositories will be scanned and a duplicate issue/pull request may occur. Ex: 2023-12-31 for Dec. 31st 2023 |
7272
| `PROJECT_ID` | False | "" | If set, this will assign the issue or pull request to the project with the given ID. ( The project ID on GitHub can be located by navigating to the respective project and observing the URL's end.) **The `ORGANIZATION` variable is required** |
7373
| `DRY_RUN` | False | false | If set to true, this action will not create any issues or pull requests. It will only log the repositories that could have dependabot enabled. This is useful for testing. |
7474
| `GROUP_DEPENDENCIES` | False | false | If set to true, dependabot configuration will group dependencies updates based on [dependency type](https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#groups) (production or development, where supported) |

env.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import os
6+
import re
67
from os.path import dirname, join
78

89
from dotenv import load_dotenv
@@ -54,7 +55,7 @@ def get_env_vars() -> tuple[
5455
str,
5556
str,
5657
str,
57-
str | None,
58+
str,
5859
bool,
5960
str,
6061
str | None,
@@ -184,10 +185,12 @@ def get_env_vars() -> tuple[
184185
else:
185186
commit_message = "Create dependabot.yaml"
186187

187-
created_after_date = os.getenv("CREATED_AFTER_DATE")
188-
# make sure that created_after_date is a date in the format YYYY-MM-DD
189-
if created_after_date and len(created_after_date) != 10:
190-
raise ValueError("CREATED_AFTER_DATE environment variable not in YYYY-MM-DD")
188+
created_after_date = os.getenv("CREATED_AFTER_DATE", "")
189+
is_match = re.match(r"\d{4}-\d{2}-\d{2}", created_after_date)
190+
if created_after_date and not is_match:
191+
raise ValueError(
192+
f"CREATED_AFTER_DATE '{created_after_date}' environment variable not in YYYY-MM-DD"
193+
)
191194

192195
group_dependencies_bool = get_bool_env_var("GROUP_DEPENDENCIES")
193196
enable_security_updates_bool = get_bool_env_var(

test_env.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def setUp(self):
4040
"TYPE": "issue",
4141
"TITLE": "Dependabot Alert custom title",
4242
"BODY": "Dependabot custom body",
43-
"CREATED_AFTER_DATE": "2023-01-01",
43+
"CREATED_AFTER_DATE": "2020-01-01",
4444
"COMMIT_MESSAGE": "Create dependabot configuration",
4545
"PROJECT_ID": "123",
4646
"GROUP_DEPENDENCIES": "false",
@@ -60,7 +60,7 @@ def test_get_env_vars_with_org(self):
6060
"issue",
6161
"Dependabot Alert custom title",
6262
"Dependabot custom body",
63-
"2023-01-01",
63+
"2020-01-01",
6464
False,
6565
"Create dependabot configuration",
6666
"123",
@@ -140,7 +140,7 @@ def test_get_env_vars_optional_values(self):
140140
"Dependabot could be enabled for this repository. \
141141
Please enable it by merging this pull request so that \
142142
we can keep our dependencies up to date and secure.",
143-
None,
143+
"",
144144
False,
145145
"Create dependabot.yaml",
146146
None,
@@ -192,7 +192,7 @@ def test_get_env_vars_auth_with_github_app_installation(self):
192192
"Dependabot could be enabled for this repository. Please enable it by merging "
193193
"this pull request so that we can keep our dependencies up to date and "
194194
"secure.",
195-
None,
195+
"",
196196
False,
197197
"Create dependabot.yaml",
198198
None,
@@ -252,7 +252,7 @@ def test_get_env_vars_with_repos_no_dry_run(self):
252252
"Dependabot could be enabled for this repository. \
253253
Please enable it by merging this pull request so that \
254254
we can keep our dependencies up to date and secure.",
255-
None,
255+
"",
256256
False,
257257
"Create dependabot.yaml",
258258
None,
@@ -290,7 +290,7 @@ def test_get_env_vars_with_repos_disabled_security_updates(self):
290290
"Dependabot could be enabled for this repository. \
291291
Please enable it by merging this pull request so that \
292292
we can keep our dependencies up to date and secure.",
293-
None,
293+
"",
294294
False,
295295
"Create dependabot.yaml",
296296
None,
@@ -329,7 +329,7 @@ def test_get_env_vars_with_repos_filter_visibility_multiple_values(self):
329329
"Dependabot could be enabled for this repository. \
330330
Please enable it by merging this pull request so that \
331331
we can keep our dependencies up to date and secure.",
332-
None,
332+
"",
333333
False,
334334
"Create dependabot.yaml",
335335
None,
@@ -368,7 +368,7 @@ def test_get_env_vars_with_repos_filter_visibility_single_value(self):
368368
"Dependabot could be enabled for this repository. \
369369
Please enable it by merging this pull request so that \
370370
we can keep our dependencies up to date and secure.",
371-
None,
371+
"",
372372
False,
373373
"Create dependabot.yaml",
374374
None,
@@ -437,7 +437,7 @@ def test_get_env_vars_with_repos_filter_visibility_no_duplicates(self):
437437
"Dependabot could be enabled for this repository. \
438438
Please enable it by merging this pull request so that \
439439
we can keep our dependencies up to date and secure.",
440-
None,
440+
"",
441441
False,
442442
"Create dependabot.yaml",
443443
None,
@@ -477,7 +477,7 @@ def test_get_env_vars_with_repos_exempt_ecosystems(self):
477477
"Dependabot could be enabled for this repository. \
478478
Please enable it by merging this pull request so that \
479479
we can keep our dependencies up to date and secure.",
480-
None,
480+
"",
481481
False,
482482
"Create dependabot.yaml",
483483
None,
@@ -516,7 +516,7 @@ def test_get_env_vars_with_no_batch_size(self):
516516
"Dependabot could be enabled for this repository. \
517517
Please enable it by merging this pull request so that \
518518
we can keep our dependencies up to date and secure.",
519-
None,
519+
"",
520520
False,
521521
"Create dependabot.yaml",
522522
None,
@@ -556,7 +556,7 @@ def test_get_env_vars_with_batch_size(self):
556556
"Dependabot could be enabled for this repository. \
557557
Please enable it by merging this pull request so that \
558558
we can keep our dependencies up to date and secure.",
559-
None,
559+
"",
560560
False,
561561
"Create dependabot.yaml",
562562
None,
@@ -581,7 +581,7 @@ def test_get_env_vars_with_batch_size(self):
581581
clear=True,
582582
)
583583
def test_get_env_vars_with_invalid_batch_size_int(self):
584-
"""Test that filter_visibility is set correctly when there are duplicate values"""
584+
"""Test that invalid batch size with negative 1 throws exception"""
585585
with self.assertRaises(ValueError):
586586
get_env_vars()
587587

@@ -597,10 +597,29 @@ def test_get_env_vars_with_invalid_batch_size_int(self):
597597
clear=True,
598598
)
599599
def test_get_env_vars_with_invalid_batch_size_str(self):
600-
"""Test that filter_visibility is set correctly when there are duplicate values"""
600+
"""Test that invalid batch size of string throws exception"""
601601
with self.assertRaises(ValueError):
602602
get_env_vars()
603603

604+
@patch.dict(
605+
os.environ,
606+
{
607+
"ORGANIZATION": "my_organization",
608+
"GH_TOKEN": "my_token",
609+
"CREATED_AFTER_DATE": "20200101",
610+
},
611+
clear=True,
612+
)
613+
def test_get_env_vars_with_badly_formatted_created_after_date(self):
614+
"""Test that"""
615+
with self.assertRaises(ValueError) as context_manager:
616+
get_env_vars()
617+
the_exception = context_manager.exception
618+
self.assertEqual(
619+
str(the_exception),
620+
"CREATED_AFTER_DATE '20200101' environment variable not in YYYY-MM-DD",
621+
)
622+
604623

605624
if __name__ == "__main__":
606625
unittest.main()

test_evergreen.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class TestIsRepoCreateDateBeforeCreatedAfterDate(unittest.TestCase):
584584
"""Test the is_repo_create_date_before_created_after_date function in evergreen.py"""
585585

586586
def test_is_repo_create_date_before_created_after_date(self):
587-
"""Test the repo.created_at date is before created_after_date."""
587+
"""Test the repo.created_at date is before created_after_date and has timezone."""
588588
repo_created_at = "2020-01-01T05:00:00Z"
589589
created_after_date = "2021-01-01"
590590

@@ -593,7 +593,7 @@ def test_is_repo_create_date_before_created_after_date(self):
593593
self.assertTrue(result)
594594

595595
def test_is_repo_create_date_is_after_created_after_date(self):
596-
"""Test the repo.created_at date is after created_after_date."""
596+
"""Test the repo.created_at date is after created_after_date and has timezone."""
597597
repo_created_at = "2022-01-01T05:00:00Z"
598598
created_after_date = "2021-01-01"
599599

@@ -602,7 +602,7 @@ def test_is_repo_create_date_is_after_created_after_date(self):
602602
self.assertFalse(result)
603603

604604
def test_is_repo_created_date_has_no_time_zone(self):
605-
"""Test the repo.created_at date is after created_after_date."""
605+
"""Test the repo.created_at date is before created_after_date with no timezone."""
606606
repo_created_at = "2020-01-01"
607607
created_after_date = "2021-01-01"
608608

@@ -619,6 +619,25 @@ def test_is_created_after_date_is_empty_string(self):
619619

620620
self.assertFalse(result)
621621

622+
def test_is_repo_created_date_is_before_created_after_date_without_timezene_again(
623+
self,
624+
):
625+
"""Test the repo.created_at date is before created_after_date without timezone again."""
626+
repo_created_at = "2018-01-01"
627+
created_after_date = "2020-01-01"
628+
629+
result = is_repo_created_date_before(repo_created_at, created_after_date)
630+
631+
self.assertTrue(result)
632+
633+
def test_is_repo_created_date_and_created_after_date_is_not_a_date(self):
634+
"""Test the repo.created_at date and the created_after_date argument is not a date."""
635+
repo_created_at = "2018-01-01"
636+
created_after_date = "Not a date"
637+
638+
with self.assertRaises(ValueError):
639+
is_repo_created_date_before(repo_created_at, created_after_date)
640+
622641

623642
if __name__ == "__main__":
624643
unittest.main()

0 commit comments

Comments
 (0)