Skip to content

Commit 8c6700d

Browse files
IamXanderMongoDB Bot
authored andcommitted
SERVER-91650 Added more checks to pr description (#23659)
GitOrigin-RevId: f3d7cd1
1 parent cc9b1c8 commit 8c6700d

File tree

4 files changed

+134
-68
lines changed

4 files changed

+134
-68
lines changed
Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,64 @@
11
"""Unit tests for the evergreen_task_timeout script."""
22

33
import unittest
4+
from git import Commit, Repo
45

5-
from buildscripts.validate_commit_message import main, STATUS_OK, STATUS_ERROR
6+
from buildscripts.validate_commit_message import is_valid_commit
67

78

89
class ValidateCommitMessageTest(unittest.TestCase):
910
def test_valid(self):
11+
fake_repo = Repo()
1012
messages = [
11-
"SERVER-44338",
12-
'Revert "SERVER-60',
13-
"Import wiredtiger: 58115abb6fbb3c1cc7bfd087d41a47347bce9a69 from branch mongodb-4.4",
14-
'Revert "Import wiredtiger: 58115abb6fbb3c1cc7bfd087d41a47347bce9a69 from branch mongodb-4.4"',
13+
Commit(repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message="SERVER-44338"),
14+
Commit(repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message='Revert "SERVER-60'),
15+
Commit(
16+
repo=fake_repo,
17+
binsha=b"deadbeefdeadbeefdead",
18+
message="Import wiredtiger: 58115abb6fbb3c1cc7bfd087d41a47347bce9a69 from branch mongodb-4.4",
19+
),
20+
Commit(
21+
repo=fake_repo,
22+
binsha=b"deadbeefdeadbeefdead",
23+
message='Revert "Import wiredtiger: 58115abb6fbb3c1cc7bfd087d41a47347bce9a69 from branch mongodb-4.4"',
24+
),
25+
Commit(
26+
repo=fake_repo,
27+
binsha=b"deadbeefdeadbeefdead",
28+
message="SERVER-44338 blablablalbabla\nmultiline message\nasdfasdf",
29+
),
1530
]
1631

17-
self.assertTrue(all(main([message]) == STATUS_OK for message in messages))
32+
self.assertTrue(all(is_valid_commit(message) == True for message in messages))
1833

1934
def test_invalid(self):
35+
fake_repo = Repo()
2036
messages = [
21-
"SERVER-", # missing number
22-
"Revert SERVER-60", # missing quote before SERVER
23-
"", # empty value
24-
"nonsense", # nonsense value
37+
Commit(
38+
repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message="SERVER-"
39+
), # missing number
40+
Commit(
41+
repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message="Revert SERVER-60"
42+
), # missing quote before SERVER
43+
Commit(repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message=""), # empty value
44+
Commit(
45+
repo=fake_repo, binsha=b"deadbeefdeadbeefdead", message="nonsense"
46+
), # nonsense value
47+
Commit(
48+
repo=fake_repo,
49+
binsha=b"deadbeefdeadbeefdead",
50+
message="SERVER-123 asdf\nhttps://spruce.mongodb.com",
51+
), # Contains some banned strings
52+
Commit(
53+
repo=fake_repo,
54+
binsha=b"deadbeefdeadbeefdead",
55+
message="SERVER-123 asdf\nhttps://evergreen.mongodb.com",
56+
), # Contains some banned strings
57+
Commit(
58+
repo=fake_repo,
59+
binsha=b"deadbeefdeadbeefdead",
60+
message="SERVER-123 asdf\nAnything in this description will be included in the commit message. Replace or delete this text before merging. Add links to testing in the comments of the PR.",
61+
), # Contains some banned strings
2562
]
2663

27-
self.assertTrue(all(main([message]) == STATUS_ERROR for message in messages))
28-
29-
def test_message_is_empty_list(self):
30-
self.assertEqual(main([]), STATUS_ERROR)
64+
self.assertTrue(all(is_valid_commit(message) == False for message in messages))

buildscripts/validate_commit_message.py

Lines changed: 82 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,49 +28,103 @@
2828
#
2929
"""Validate that the commit message is ok."""
3030

31-
import argparse
3231
import re
33-
import sys
34-
import logging
32+
import pathlib
33+
import structlog
34+
import subprocess
35+
import typer
36+
from typing_extensions import Annotated
37+
from git import Commit, Repo
3538

36-
LOGGER = logging.getLogger(__name__)
39+
LOGGER = structlog.get_logger(__name__)
3740

3841
STATUS_OK = 0
3942
STATUS_ERROR = 1
4043

44+
repo_root = pathlib.Path(
45+
subprocess.run(
46+
"git rev-parse --show-toplevel", shell=True, text=True, capture_output=True
47+
).stdout.strip()
48+
)
4149

42-
def main(argv=None):
43-
"""Execute Main function to validate commit messages."""
44-
parser = argparse.ArgumentParser(
45-
usage="Validate the commit message. "
46-
"It validates the latest message when no arguments are provided."
47-
)
48-
parser.add_argument(
49-
"message",
50-
metavar="commit message",
51-
nargs="*",
52-
help="The commit message to validate",
53-
)
54-
args = parser.parse_args(argv)
50+
pr_template = ""
51+
with open(repo_root / ".github" / "pull_request_template.md", "r") as r:
52+
pr_template = r.read().strip()
53+
54+
BANNED_STRINGS = ["https://spruce.mongodb.com", "https://evergreen.mongodb.com", pr_template]
55+
56+
VALID_SUMMARY = re.compile(r'(Revert ")?(SERVER-[0-9]+|Import wiredtiger)')
5557

56-
if not args.message:
57-
LOGGER.error("Must specify non-empty value for --message")
58-
return STATUS_ERROR
59-
message = " ".join(args.message)
6058

59+
def is_valid_commit(commit: Commit) -> bool:
6160
# Valid values look like:
6261
# 1. SERVER-\d+
6362
# 2. Revert "SERVER-\d+
6463
# 3. Import wiredtiger
6564
# 4. Revert "Import wiredtiger
66-
valid_pattern = re.compile(r'(Revert ")?(SERVER-[0-9]+|Import wiredtiger)')
65+
if not VALID_SUMMARY.match(commit.summary):
66+
LOGGER.error(
67+
"Commit did not contain a valid summary",
68+
commit_hexsha=commit.hexsha,
69+
commit_summary=commit.summary,
70+
)
71+
return False
72+
73+
for banned_string in BANNED_STRINGS:
74+
if banned_string in commit.message:
75+
LOGGER.error(
76+
"Commit contains banned string",
77+
banned_string=banned_string,
78+
commit_hexsha=commit.hexsha,
79+
commit_message=commit.message,
80+
)
81+
return False
82+
83+
return True
84+
85+
86+
def main(
87+
branch_name: Annotated[
88+
str,
89+
typer.Option(envvar="BRANCH_NAME", help="Name of the branch to compare against HEAD"),
90+
],
91+
is_commit_queue: Annotated[
92+
str,
93+
typer.Option(
94+
envvar="IS_COMMIT_QUEUE",
95+
help="If this is being run in the commit/merge queue. Set to anything to be considered part of the commit/merge queue.",
96+
),
97+
] = "",
98+
):
99+
"""
100+
Validate the commit message.
101+
102+
It validates the latest message when no arguments are provided.
103+
"""
104+
105+
if not is_commit_queue:
106+
LOGGER.info("Exiting early since this is not running in the commit/merge queue")
107+
raise typer.Exit(code=STATUS_OK)
108+
109+
diff_commits = subprocess.run(
110+
["git", "log", '--pretty=format:"%H"', f"{branch_name}...HEAD"],
111+
check=True,
112+
capture_output=True,
113+
text=True,
114+
)
115+
# Comes back like "hash1"\n"hash2"\n...
116+
commit_hashs: list[str] = diff_commits.stdout.replace('"', "").splitlines()
117+
LOGGER.info("Diff commit hashes", commit_hashs=commit_hashs)
118+
repo = Repo(repo_root)
119+
120+
for commit_hash in commit_hashs:
121+
commit = repo.commit(commit_hash)
122+
if not is_valid_commit(commit):
123+
LOGGER.error("Found an invalid commit", commit=commit)
124+
raise typer.Exit(code=STATUS_ERROR)
67125

68-
if valid_pattern.match(message):
69-
return STATUS_OK
70-
else:
71-
LOGGER.error(f"Found a commit without a ticket\n{message}") # pylint: disable=logging-fstring-interpolation
72-
return STATUS_ERROR
126+
return
73127

74128

75129
if __name__ == "__main__":
76-
sys.exit(main(sys.argv[1:]))
130+
typer.run(main)

etc/evergreen_yml_components/tasks/misc_tasks.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,12 +1509,15 @@ tasks:
15091509
params:
15101510
binary: bash
15111511
args:
1512-
- "./src/evergreen/commit_message_validate.sh"
1512+
- "./src/evergreen/run_python_script.sh"
1513+
- "buildscripts/validate_commit_message.py"
15131514
env:
15141515
JIRA_AUTH_ACCESS_TOKEN: ${jira_auth_access_token}
15151516
JIRA_AUTH_ACCESS_TOKEN_SECRET: ${jira_auth_access_token_secret}
15161517
JIRA_AUTH_CONSUMER_KEY: ${jira_auth_consumer_key}
15171518
JIRA_AUTH_KEY_CERT: ${jira_auth_key_cert}
1519+
IS_COMMIT_QUEUE: ${is_commit_queue}
1520+
BRANCH_NAME: ${branch_name}
15181521

15191522
- name: version_burn_in_gen
15201523
run_on: ubuntu2004-medium

evergreen/commit_message_validate.sh

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)