Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix validating Master branch in the form. #1732

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions server/fishtest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,14 +529,15 @@ def users_monthly(request):
return {"users": users_list}


def get_master_info():
bench_search = re.compile(r"(^|\s)[Bb]ench[ :]+([1-9]\d{5,9})(?!\d)")
commits = requests.get(
"https://api.github.com/repos/official-stockfish/Stockfish/commits"
).json()
for idx, commit in enumerate(commits):
if "commit" not in commit:
return None
def get_master_info(url):
peregrineshahin marked this conversation as resolved.
Show resolved Hide resolved
try:
commits = requests.get(url)
commits.raise_for_status()
except Exception as e:
print(f"Exception getting commits:\n{e}")
return None
bench_search = re.compile(r"(^|\s)[Bb]ench[ :]+([1-9]\d{5,7})(?!\d)")
for idx, commit in enumerate(commits.json()):
message_lines = commit["commit"]["message"].strip().split("\n")
bench = bench_search.search(message_lines[-1].strip())
if idx == 0:
Expand Down Expand Up @@ -664,7 +665,11 @@ def validate_form(request):
data["rescheduled_from"] = request.POST["rescheduled_from"]

def strip_message(m):
s = re.sub(r"[Bb]ench[ :]+[0-9]+\s*", "", m)
lines = m.strip().split("\n")
last_line = lines[-1].strip()
if re.match(r"(^|\s)[Bb]ench[ :]+([1-9]\d{5,7})(?!\d)", last_line):
lines[-1] = ""
s = "\n".join(lines)
s = re.sub(r"[ \t]+", " ", s)
s = re.sub(r"\n+", r"\n", s)
return s.rstrip()
Expand All @@ -682,8 +687,10 @@ def strip_message(m):
if "commit" not in c:
raise Exception("Cannot find branch in developer repository")
if len(data["new_signature"]) == 0:
bs = re.compile(r"(^|\s)[Bb]ench[ :]+([0-9]+)", re.MULTILINE)
m = bs.search(c["commit"]["message"])
bs = re.compile(r"(^|\s)[Bb]ench[ :]+([1-9]\d{5,7})(?!\d)")
lines = c["commit"]["message"].split("\n")
last_line = lines[-1].strip()
m = bs.search(last_line)
if m:
data["new_signature"] = m.group(2)
else:
Expand Down Expand Up @@ -736,18 +743,12 @@ def strip_message(m):

# Check entered bench
if data["base_tag"] == "master":
found = False
api_url = data["tests_repo"].replace(
"https://github.com", "https://api.github.com/repos"
)
api_url += "/commits"
bs = re.compile(r"(^|\s)[Bb]ench[ :]+([0-9]+)", re.MULTILINE)
for c in requests.get(api_url).json():
m = bs.search(c["commit"]["message"])
if m:
found = True
break
if not found or m.group(2) != data["base_signature"]:
master_info = get_master_info(api_url)
if master_info is None or master_info["bench"] != data["base_signature"]:
raise Exception(
"Bench signature of Base master does not match, "
+ 'please "git pull upstream master" !'
Expand Down Expand Up @@ -936,13 +937,15 @@ def tests_run(request):

username = request.authenticated_userid
u = request.userdb.get_user(username)

master_commits_url = (
"https://api.github.com/repos/official-stockfish/Stockfish/commits"
)
return {
"args": run_args,
"is_rerun": len(run_args) > 0,
"rescheduled_from": request.params["id"] if "id" in request.params else None,
"tests_repo": u.get("tests_repo", ""),
"master_info": get_master_info(),
"master_info": get_master_info(master_commits_url),
"valid_books": get_valid_books(),
"pt_info": request.rundb.pt_info,
}
Expand Down
7 changes: 6 additions & 1 deletion server/tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

class CreateRunTest(unittest.TestCase):
def test_10_get_bench(self):
self.assertTrue(re.match("[0-9]{7}|None", str(get_master_info()["bench"])))
master_commits_url = (
"https://api.github.com/repos/official-stockfish/Stockfish/commits"
)
self.assertTrue(
re.match("[0-9]{7}|None", str(get_master_info(master_commits_url)["bench"]))
)


if __name__ == "__main__":
Expand Down
Loading