-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: Add Python API for GitHub Actions (#210)
Supports: - Workflow commands: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions - Default environment variables: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables Does not include support for GitHub REST API
- Loading branch information
1 parent
abab4c2
commit 60625c4
Showing
6 changed files
with
100 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
python/cli/data_platform_workflows_cli/github_actions/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
"""Python API for GitHub Actions | ||
Supports: | ||
- Workflow commands: https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions | ||
- Default environment variables: https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables | ||
Does not include support for GitHub REST API | ||
""" | ||
|
||
import collections.abc | ||
import os | ||
import pathlib | ||
import sys | ||
|
||
_OutputBaseType = collections.abc.MutableMapping[str, str | None] | ||
_output_file = pathlib.Path(os.environ["GITHUB_OUTPUT"]) | ||
|
||
|
||
class _Output(_OutputBaseType): | ||
def __setitem__(self, key, value: str | None): | ||
if value is None: | ||
value = "" | ||
if "\n" in value: | ||
raise NotImplementedError( | ||
"Output of multi-line strings not supported by Python API. Write directly to GITHUB_OUTPUT file\n" | ||
"https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter" | ||
) | ||
with _output_file.open("a", encoding="utf-8") as file: | ||
file.write(f"{key}={value}\n") | ||
print(f"GitHub Actions step output: {key}={value}") | ||
|
||
def __delitem__(self, key): | ||
self.__setitem__(key, None) | ||
|
||
def __getitem__(self, key): | ||
raise NotImplementedError( | ||
"Cannot read GitHub Actions output. Output is write-only" | ||
) | ||
|
||
def __iter__(self): | ||
raise NotImplementedError( | ||
"Cannot read GitHub Actions output. Output is write-only" | ||
) | ||
|
||
def __len__(self): | ||
raise NotImplementedError( | ||
"Cannot read GitHub Actions output. Output is write-only" | ||
) | ||
|
||
|
||
class _ThisModule(sys.modules[__name__].__class__): | ||
"""Contains properties for this module | ||
https://stackoverflow.com/a/34829743 | ||
""" | ||
|
||
_output = _Output() | ||
|
||
@property | ||
def output(self) -> _OutputBaseType: | ||
return self._output | ||
|
||
@output.setter | ||
def output(self, value: _OutputBaseType): | ||
# Clear file contents | ||
with _output_file.open(mode="w", encoding="utf-8"): | ||
pass | ||
|
||
self._output = _Output() | ||
self._output.update(value) | ||
|
||
|
||
output: _OutputBaseType | ||
"""Step outputs | ||
https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-output-parameter | ||
""" | ||
|
||
sys.modules[__name__].__class__ = _ThisModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters