Skip to content

Commit ae583ed

Browse files
committed
Record most relevant commit hash in sketches report for PRs
Previously, the sketches report's commit_hash key was the hash of GitHub's automatically generated hypothetical merge commit when the workflow was triggered by a pull_request event. Instead, the report should show the hash of the PR's head commit. Unfortunately, the event data provided by GitHub doesn't give the short hash and I don't know a good way of converting the long hash to short. For consistency, the report will now always contain the long hash.
1 parent 133d0f4 commit ae583ed

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

compilesketches/compilesketches.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,9 +949,7 @@ def get_sketches_report(self, sketch_report_list):
949949
Keyword arguments:
950950
sketch_report_list -- list of reports from each sketch compilation
951951
"""
952-
# Get the short hash of the pull request head ref
953-
repository = git.Repo(path=os.environ["GITHUB_WORKSPACE"])
954-
current_git_ref = repository.git.rev_parse("HEAD", short=True)
952+
current_git_ref = get_head_commit_hash()
955953

956954
sketches_report = {
957955
self.ReportKeys.fqbn: self.fqbn,
@@ -1244,6 +1242,22 @@ def get_archive_root_path(archive_extract_path):
12441242
return archive_root_folder_name
12451243

12461244

1245+
def get_head_commit_hash():
1246+
"""Return the head commit's hash."""
1247+
if os.environ["GITHUB_EVENT_NAME"] == "pull_request":
1248+
# When the workflow is triggered by a pull_request event, actions/checkout checks out the hypothetical merge
1249+
# commit GitHub automatically generates for PRs. The user will expect the report to show the hash of the head
1250+
# commit of the PR, not of this hidden merge commit. So it's necessary it get it from GITHUB_EVENT_PATH instead
1251+
# of git rev-parse HEAD.
1252+
with open(file=os.environ["GITHUB_EVENT_PATH"]) as github_event_file:
1253+
commit_hash = json.load(github_event_file)["pull_request"]["head"]["sha"]
1254+
else:
1255+
repository = git.Repo(path=os.environ["GITHUB_WORKSPACE"])
1256+
commit_hash = repository.git.rev_parse("HEAD")
1257+
1258+
return commit_hash
1259+
1260+
12471261
# Only execute the following code if the script is run directly, not imported
12481262
if __name__ == "__main__":
12491263
main()

compilesketches/tests/test_compilesketches.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,18 +1500,9 @@ def test_get_sketches_report(monkeypatch, mocker):
15001500
fqbn_arg = "arduino:avr:uno"
15011501
current_git_ref = "fooref"
15021502

1503-
# Stub
1504-
class Repo:
1505-
def __init__(self):
1506-
self.git = self
1507-
1508-
def rev_parse(self):
1509-
pass
1510-
15111503
monkeypatch.setenv("GITHUB_REPOSITORY", github_repository)
15121504

1513-
mocker.patch("git.Repo", autospec=True, return_value=Repo())
1514-
mocker.patch.object(Repo, "rev_parse", return_value=current_git_ref)
1505+
mocker.patch("compilesketches.get_head_commit_hash", autospec=True, return_value=current_git_ref)
15151506

15161507
sizes_summary_report = unittest.mock.sentinel.sizes_summary_report
15171508
sketch_report_list = unittest.mock.sentinel.sketch_report_list
@@ -1533,8 +1524,6 @@ def rev_parse(self):
15331524
compilesketches.CompileSketches.ReportKeys.sketch: sketch_report_list
15341525
}
15351526

1536-
git.Repo.assert_called_once_with(path=os.environ["GITHUB_WORKSPACE"])
1537-
Repo.rev_parse.assert_called_once_with("HEAD", short=True)
15381527
compile_sketches.get_sizes_summary_report.assert_called_once_with(compile_sketches,
15391528
sketch_report_list=sketch_report_list)
15401529

@@ -2033,3 +2022,23 @@ def test_clone_repository(tmp_path, git_ref):
20332022
# Verify that the installation matches the test clone
20342023
assert directories_are_same(left_directory=destination_path,
20352024
right_directory=test_clone_path)
2025+
2026+
2027+
@pytest.mark.parametrize("github_event, expected_hash",
2028+
[("pull_request", "pull_request-head-sha"), ("push", "push-head-sha")])
2029+
def test_get_head_commit_hash(monkeypatch, mocker, github_event, expected_hash):
2030+
# Stub
2031+
class Repo:
2032+
def __init__(self):
2033+
self.git = self
2034+
2035+
def rev_parse(self):
2036+
pass
2037+
2038+
monkeypatch.setenv("GITHUB_EVENT_NAME", github_event)
2039+
monkeypatch.setenv("GITHUB_EVENT_PATH", str(test_data_path.joinpath("githubevent.json")))
2040+
2041+
mocker.patch("git.Repo", autospec=True, return_value=Repo())
2042+
mocker.patch.object(Repo, "rev_parse", return_value="push-head-sha")
2043+
2044+
assert compilesketches.get_head_commit_hash() == expected_hash
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"pull_request": {
3+
"head": {
4+
"sha": "pull_request-head-sha"
5+
},
36
"number": 42
47
}
58
}

0 commit comments

Comments
 (0)