Skip to content

Commit 3a1fbf4

Browse files
fix: default to GITHUB_SHA if the commit received from context is empty (#30)
When action is triggered in non-pull-request events, the default being set would be empty. In such cases, CLI would try to fetch head commit by executing git commands. That's going to fail for this action if safe directory isn't added.
1 parent 5e821bd commit 3a1fbf4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

main.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ def main() -> None:
2828
"""
2929

3030
input_data = {key: os.getenv(value) for key, value in INPUT_KEYS_MAP.items()}
31+
commit_sha = input_data["commit_sha"]
32+
33+
# Case: When user hasn't set the commit sha
34+
# or, when this action is run on a non-pull request event, the default value is going to
35+
# be an empty string.
36+
# in such case, fetch commit sha from the `GITHUB_SHA` environment variable.
37+
# Doing this is the best way to go because of the following reasons:
38+
# 1. we don't have to run any git commands to fetch the commit sha,
39+
# making the action work all the time
40+
# 2. the default being set to PR's head sha will take care of merge commit.
41+
# In all other cases, GITHUB_SHA would be accurate.
42+
# 3. GITHUB_SHA is always set, so we don't have to worry about it being empty.
43+
if not commit_sha:
44+
commit_sha = os.getenv("GITHUB_SHA")
3145

3246
command = [
3347
DEEPSOURCE_CLI_PATH,
@@ -49,7 +63,7 @@ def main() -> None:
4963
env=dict(
5064
os.environ,
5165
DEEPSOURCE_DSN=input_data["dsn"],
52-
GHA_HEAD_COMMIT_SHA=input_data["commit_sha"],
66+
GHA_HEAD_COMMIT_SHA=commit_sha,
5367
),
5468
capture_output=True,
5569
)

0 commit comments

Comments
 (0)