Skip to content

Commit c094cc9

Browse files
committed
add doc strings to python functions
1 parent 93418db commit c094cc9

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

stash/restore/get_stash.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,37 @@
1313
# limitations under the License.
1414
import json
1515
import os
16-
import re
1716
import subprocess
1817
from typing import List
1918

2019

2120
def print_debug(msg: str):
21+
"""Print a message that is only visible when the GHA debug flag is set."""
2222
print(f"::debug::{msg}")
2323

2424

2525
def set_output(name: str, value: str):
26+
"""Set a GHA output variable."""
2627
with open(ensure_env_var("GITHUB_OUTPUT"), "a") as f:
2728
f.write(f"{name}={value}\n")
2829

2930

3031
def ensure_env_var(var: str) -> str:
32+
"""Return value of envvar `var`, throw if it's not set."""
3133
value = os.environ.get(var)
3234
if value is None or len(value) == 0:
3335
raise ValueError(f"Environment variable {var} is not set")
3436
return value
3537

3638

3739
def run_checked(args, **kwargs):
40+
"""Run command and caputre it's output and check that it exists succesfully."""
3841
result = subprocess.run(args, **kwargs, capture_output=True, check=True, text=True)
3942
return result
4043

4144

4245
def jq(file: str, query: str, args: List[str] = []):
46+
"""Wrapper to run `jq` query on a file on disk or on a JSON string."""
4347
if os.path.isfile(file):
4448
result = run_checked(["jq", *args, query, file])
4549
elif file.startswith("{"):
@@ -51,6 +55,7 @@ def jq(file: str, query: str, args: List[str] = []):
5155

5256

5357
def gh_api(endpoint: str, method: str = "get", options: List[str] = []):
58+
"""Wrapper to run `gh` REST API calls."""
5459
args = [
5560
"gh",
5661
"api",
@@ -67,6 +72,7 @@ def gh_api(endpoint: str, method: str = "get", options: List[str] = []):
6772

6873

6974
def ensure_json(output: str):
75+
"""Always return valid JSON."""
7076
if output.isspace():
7177
return json.loads("{}")
7278
else:

stash/shared/mung.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,42 @@
1616
import re
1717

1818

19-
def normalize(s):
19+
def normalize(s: str) -> str:
20+
"""
21+
Replaces all characters in the input string `s` that are not
22+
alphanumeric, underscores, hyphens, or periods with underscores.
23+
"""
2024
return re.sub(r"[^_\-.\w]", "_", s)
2125

2226

23-
def mung(key, ref):
27+
def mung(key: str, ref: str) -> str:
28+
"""Combine `key` and `ref` into a single string separated by a hyphen."""
2429
key = normalize(key)
2530
ref = normalize(ref)
2631
return f"{key}-{ref}"
2732

2833

29-
def output_munged_name(ref = "ref_name", key = "stash_key", output = "stash_name"):
34+
def output_munged_name(ref="ref_name", key="stash_key", output="stash_name"):
35+
"""
36+
Reads the stash key and ref name from the matching environment variables,
37+
combines them and saves the result in a GHA output variable.
38+
39+
Args:
40+
ref (str, optional): The name of the environment variable containing
41+
the reference string. Defaults to "ref_name".
42+
key (str, optional): The name of the environment variable containing
43+
the key string. Defaults to "stash_key".
44+
output (str, optional): The output variable name to be used in the
45+
GitHub Actions output file. Defaults to "stash_name".
46+
47+
Returns:
48+
None
49+
"""
3050
ref = os.environ[ref]
51+
3152
key = os.environ[key]
3253
name = mung(key, ref)
3354

3455
print(f"::debug::Creating output {output}={name} ")
35-
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
36-
f.write(f'{output}={name}' + '\n')
56+
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
57+
f.write(f"{output}={name}" + "\n")

0 commit comments

Comments
 (0)