Skip to content

Commit e2771ed

Browse files
authored
Merge pull request #382 from github/copilot/fix-end-date-check
fix: validate END_DATE is not before or equal to START_DATE
2 parents f6f4814 + c6cc92e commit e2771ed

10 files changed

Lines changed: 115 additions & 12 deletions

File tree

.github/linters/.markdown-lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
###############
2121
# line length
2222
MD013: false
23-
# singe h1
23+
# single h1
2424
MD025: false
2525
# duplicate headers
2626
MD024: false

.github/workflows/copilot-setup-steps.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ jobs:
2626
# If you do not check out your code, Copilot will do this for you.
2727
steps:
2828
- name: Checkout code
29-
uses: actions/checkout@v6.0.2
29+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3030
with:
3131
persist-credentials: false
3232

3333
- name: Set up Python
34-
uses: actions/setup-python@v6.2.0
34+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
3535
with:
3636
python-version: 3.12
3737

.github/workflows/docker-ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
build:
1515
runs-on: ubuntu-latest
1616
steps:
17-
- uses: actions/checkout@v6.0.2
17+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
1818
with:
1919
persist-credentials: false
2020
- name: Build the Docker image

.github/workflows/python-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ jobs:
2020
matrix:
2121
python-version: [3.11, 3.12]
2222
steps:
23-
- uses: actions/checkout@v6.0.2
23+
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2424
with:
2525
persist-credentials: false
2626
- name: Set up Python ${{ matrix.python-version }}
27-
uses: actions/setup-python@v6.2.0
27+
uses: actions/setup-python@a309ff8b426b58ec0e2a45f0f869d46889d02405 # v6.2.0
2828
with:
2929
python-version: ${{ matrix.python-version }}
3030
- name: Install dependencies

.github/workflows/scorecard.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
steps:
2727
- name: "Checkout code"
28-
uses: actions/checkout@v6.0.2
28+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2929
with:
3030
persist-credentials: false
3131

@@ -36,7 +36,7 @@ jobs:
3636
results_format: sarif
3737
publish_results: true
3838
- name: "Upload artifact"
39-
uses: actions/upload-artifact@v6.0.0
39+
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
4040
with:
4141
name: SARIF file
4242
path: results.sarif

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
stale:
1212
runs-on: ubuntu-latest
1313
steps:
14-
- uses: actions/stale@v10.1.1
14+
- uses: actions/stale@997185467fa4f803885201cee163a9f38240193d # v10.1.1
1515
with:
1616
stale-issue-message: "This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 14 days."
1717
close-issue-message: "This issue was closed because it has been stalled for 35 days with no activity."

.github/workflows/super-linter.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
statuses: write
1919
steps:
2020
- name: Checkout Code
21-
uses: actions/checkout@v6.0.2
21+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2222
with:
2323
fetch-depth: 0
2424
persist-credentials: false
@@ -27,7 +27,7 @@ jobs:
2727
python -m pip install --upgrade pip
2828
pip install -r requirements.txt -r requirements-test.txt
2929
- name: Lint Code Base
30-
uses: super-linter/super-linter@502f4fe48a81a392756e173e39a861f8c8efe056
30+
uses: super-linter/super-linter@61abc07d755095a68f4987d1c2c3d1d64408f1f9 # v8.5.0
3131
env:
3232
DEFAULT_BRANCH: main
3333
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

contributor_stats.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def merge_contributors(contributors: list) -> list:
104104
merged_contributors: List[ContributorStats] = []
105105
for contributor_list in contributors:
106106
for contributor in contributor_list:
107-
# if the contributor is already in the merged list, merge their relavent attributes
107+
# if the contributor is already in the merged list, merge their relevant attributes
108108
if contributor.username in [c.username for c in merged_contributors]:
109109
for merged_contributor in merged_contributors:
110110
if merged_contributor.username == contributor.username:

env.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,36 @@ def validate_date_format(env_var_name: str) -> str:
7070
return date_to_validate
7171

7272

73+
def validate_date_range(start_date: str, end_date: str) -> None:
74+
"""Validate that start_date is before end_date.
75+
76+
Does nothing if either date is not set.
77+
78+
Args:
79+
start_date: The start date string in YYYY-MM-DD format.
80+
end_date: The end date string in YYYY-MM-DD format.
81+
82+
Raises:
83+
ValueError: If end_date is before or equal to start_date.
84+
"""
85+
if not start_date or not end_date:
86+
return
87+
88+
pattern = "%Y-%m-%d"
89+
try:
90+
start = datetime.datetime.strptime(start_date, pattern).date()
91+
end = datetime.datetime.strptime(end_date, pattern).date()
92+
except ValueError as exc:
93+
raise ValueError(
94+
"start_date and end_date must be in the format YYYY-MM-DD"
95+
) from exc
96+
97+
if end <= start:
98+
raise ValueError(
99+
f"END_DATE ('{end_date}') must be after START_DATE ('{start_date}')"
100+
)
101+
102+
73103
def get_env_vars(
74104
test: bool = False,
75105
) -> tuple[
@@ -142,6 +172,7 @@ def get_env_vars(
142172

143173
start_date = validate_date_format("START_DATE")
144174
end_date = validate_date_format("END_DATE")
175+
validate_date_range(start_date, end_date)
145176

146177
sponsor_info = get_bool_env_var("SPONSOR_INFO", False)
147178
link_to_profile = get_bool_env_var("LINK_TO_PROFILE", False)

test_env.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,78 @@ def test_get_env_vars_auth_with_github_app_installation_missing_inputs(self):
222222
"GH_APP_ID set and GH_APP_INSTALLATION_ID or GH_APP_PRIVATE_KEY variable not set",
223223
)
224224

225+
@patch.dict(
226+
os.environ,
227+
{
228+
"ORGANIZATION": "org",
229+
"REPOSITORY": "repo",
230+
"GH_TOKEN": "token",
231+
"START_DATE": "2025-01-01",
232+
"END_DATE": "2024-01-01",
233+
},
234+
clear=True,
235+
)
236+
def test_get_env_vars_end_date_before_start_date(self):
237+
"""Test that an error is raised when END_DATE is before START_DATE"""
238+
with self.assertRaises(ValueError) as cm:
239+
env.get_env_vars()
240+
the_exception = cm.exception
241+
self.assertEqual(
242+
str(the_exception),
243+
"END_DATE ('2024-01-01') must be after START_DATE ('2025-01-01')",
244+
)
245+
246+
@patch.dict(
247+
os.environ,
248+
{
249+
"ORGANIZATION": "org",
250+
"REPOSITORY": "repo",
251+
"GH_TOKEN": "token",
252+
"START_DATE": "2024-01-01",
253+
"END_DATE": "2024-01-01",
254+
},
255+
clear=True,
256+
)
257+
def test_get_env_vars_equal_start_and_end_date(self):
258+
"""Test that an error is raised when START_DATE equals END_DATE"""
259+
with self.assertRaises(ValueError) as cm:
260+
env.get_env_vars()
261+
the_exception = cm.exception
262+
self.assertEqual(
263+
str(the_exception),
264+
"END_DATE ('2024-01-01') must be after START_DATE ('2024-01-01')",
265+
)
266+
267+
@patch.dict(
268+
os.environ,
269+
{
270+
"ORGANIZATION": "org",
271+
"REPOSITORY": "repo",
272+
"GH_TOKEN": "token",
273+
"START_DATE": "2024-01-01",
274+
"END_DATE": "2025-01-01",
275+
},
276+
clear=True,
277+
)
278+
def test_get_env_vars_valid_date_range(self):
279+
"""Test that valid date range (START_DATE before END_DATE) is accepted"""
280+
(
281+
_organization,
282+
_repository_list,
283+
_gh_app_id,
284+
_gh_app_installation_id,
285+
_gh_app_private_key,
286+
_gh_app_enterprise_only,
287+
_token,
288+
_ghe,
289+
start_date,
290+
end_date,
291+
_sponsor_info,
292+
_link_to_profile,
293+
) = env.get_env_vars()
294+
self.assertEqual(start_date, "2024-01-01")
295+
self.assertEqual(end_date, "2025-01-01")
296+
225297

226298
if __name__ == "__main__":
227299
unittest.main()

0 commit comments

Comments
 (0)