Skip to content

Commit 81cb1fb

Browse files
Update file write, fix issue saadmk11#17
1 parent a410d39 commit 81cb1fb

File tree

1 file changed

+16
-47
lines changed

1 file changed

+16
-47
lines changed

github_action_utils.py

+16-47
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import os
3+
import secrets
34
import subprocess
45
import sys
56
import uuid
@@ -27,8 +28,6 @@
2728
CommandTypes = str
2829
LogCommandTypes = str
2930

30-
31-
ACTION_ENV_DELIMITER: str = "__ENV_DELIMITER__"
3231
COMMAND_MARKER: str = "::"
3332

3433
COMMANDS_USE_SUBPROCESS: bool = bool(os.environ.get("COMMANDS_USE_SUBPROCESS", False))
@@ -137,32 +136,17 @@ def _build_options_string(**kwargs: Any) -> str:
137136
)
138137

139138

140-
def _build_file_input(name: str, value: Any) -> bytes:
141-
return (
142-
f"{_escape_property(name)}"
143-
f"<<{ACTION_ENV_DELIMITER}\n"
144-
f"{_escape_data(value)}\n"
145-
f"{ACTION_ENV_DELIMITER}\n".encode("utf-8")
146-
)
139+
def _build_file_input(name: str, value: str) -> str:
140+
delimiter = secrets.token_hex()
141+
return f"{_escape_property(name)}<<{delimiter}\n{value}\n{delimiter}\n"
147142

148143

149-
def set_output(name: str, value: Any, use_subprocess: Union[bool, None] = None) -> None:
144+
def set_output(name: str, value: str) -> None:
150145
"""
151-
sets out for your workflow using GITHUB_OUTPUT file.
152-
153-
:param name: name of the output
154-
:param value: value of the output
155-
:returns: None
146+
set workflow output using GITHUB_OUTPUT file.
156147
"""
157-
if use_subprocess is not None:
158-
warn(
159-
"Argument `use_subprocess` for `set_output()` is deprecated and "
160-
"going to be removed in the next version.",
161-
DeprecationWarning,
162-
)
163-
164-
with open(os.environ["GITHUB_OUTPUT"], "ab") as f:
165-
f.write(_build_file_input(name, value))
148+
with open(os.environ["GITHUB_OUTPUT"], "a") as fp:
149+
fp.write(_build_file_input(name, value))
166150

167151

168152
def echo(message: Any, use_subprocess: bool = False) -> None:
@@ -326,24 +310,13 @@ def error(
326310
)
327311

328312

329-
def save_state(name: str, value: Any, use_subprocess: Union[bool, None] = None) -> None:
313+
def save_state(name: str, value: str) -> None:
330314
"""
331-
sets state for your workflow using $GITHUB_STATE file
315+
set state for your workflow using $GITHUB_STATE file
332316
for sharing it with your workflow's pre: or post: actions.
333-
334-
:param name: Name of the state environment variable (e.g: STATE_{name})
335-
:param value: value of the state environment variable
336-
:returns: None
337317
"""
338-
if use_subprocess is not None:
339-
warn(
340-
"Argument `use_subprocess` for `save_state()` is deprecated and "
341-
"going to be removed in the next version.",
342-
DeprecationWarning,
343-
)
344-
345-
with open(os.environ["GITHUB_STATE"], "ab") as f:
346-
f.write(_build_file_input(name, value))
318+
with open(os.environ["GITHUB_STATE"], "a") as fp:
319+
fp.write(_build_file_input(name, value))
347320

348321

349322
def get_state(name: str) -> Union[str, None]:
@@ -484,16 +457,12 @@ def stop_commands(
484457
end_stop_commands(stop_token, use_subprocess=use_subprocess)
485458

486459

487-
def set_env(name: str, value: Any) -> None:
460+
def set_env(name: str, value: str) -> None:
488461
"""
489-
sets an environment variable for your workflows $GITHUB_ENV file.
490-
491-
:param name: name of the environment variable
492-
:param value: value of the environment variable
493-
:returns: None
462+
set an environment variable for your workflows $GITHUB_ENV file.
494463
"""
495-
with open(os.environ["GITHUB_ENV"], "ab") as f:
496-
f.write(_build_file_input(name, value))
464+
with open(os.environ["GITHUB_ENV"], "a") as fp:
465+
fp.write(_build_file_input(name, value))
497466

498467

499468
def get_workflow_environment_variables() -> Dict[str, Any]:

0 commit comments

Comments
 (0)