|
| 1 | +""" |
| 2 | +This module defines the `GithubEvent` class for handling GitHub event details. |
| 3 | +
|
| 4 | +Note: |
| 5 | + This module relies on the presence of specific environment variables |
| 6 | + set by GitHub Actions. |
| 7 | +""" |
| 8 | +import json |
| 9 | +import os |
| 10 | +from typing import Any, Dict |
| 11 | + |
| 12 | + |
| 13 | +# pylint: disable=R0902; Too many instance attributes |
| 14 | +class GithubEvent: |
| 15 | + """Class representing GitHub events. |
| 16 | +
|
| 17 | + This class provides methods for loading and accessing various details of |
| 18 | + GitHub events. |
| 19 | +
|
| 20 | + Attributes: |
| 21 | + event_name (str): The name of the GitHub event. |
| 22 | + sha (str): The commit SHA associated with the event. |
| 23 | + ref (str): The Git reference (branch or tag) for the event. |
| 24 | + workflow (str): The name of the GitHub workflow. |
| 25 | + action (str): The action that triggered the event. |
| 26 | + actor (str): The GitHub username of the user or app that triggered the event. |
| 27 | + job (str): The name of the job associated with the event. |
| 28 | + run_attempt (str): The current attempt number for the job run. |
| 29 | + run_number (str): The unique number assigned to the run by GitHub. |
| 30 | + run_id (str): The unique identifier for the run. |
| 31 | +
|
| 32 | + event_path (str): The path to the file containing the GitHub event payload. |
| 33 | + payload (dict): The GitHub event payload. |
| 34 | +
|
| 35 | + Raises: |
| 36 | + EnvironmentError: If the required environment variable 'GITHUB_EVENT_PATH' |
| 37 | + is not found. |
| 38 | +
|
| 39 | + Example: |
| 40 | + ```python |
| 41 | + github_event = GithubEvent() |
| 42 | + print(github_event.event_name) |
| 43 | + print(github_event.sha) |
| 44 | + print(github_event.payload) |
| 45 | + ``` |
| 46 | + """ |
| 47 | + |
| 48 | + def __init__(self) -> None: |
| 49 | + """Initialize a new instance of the GithubEvent class.""" |
| 50 | + self.__load_details() |
| 51 | + |
| 52 | + def __load_details(self) -> None: |
| 53 | + """ |
| 54 | + Load GitHub event details from environment variables and event payload file. |
| 55 | +
|
| 56 | + This method initializes the instance attributes by reading values from |
| 57 | + environment variables set by GitHub Actions and loading the event payload |
| 58 | + from a file. |
| 59 | + """ |
| 60 | + self.event_name = os.environ.get("GITHUB_EVENT_NAME") |
| 61 | + self.sha = os.environ.get("GITHUB_SHA") |
| 62 | + self.ref = os.environ.get("GITHUB_REF") |
| 63 | + self.workflow = os.environ.get("GITHUB_WORKFLOW") |
| 64 | + self.action = os.environ.get("GITHUB_ACTION") |
| 65 | + self.actor = os.environ.get("GITHUB_ACTOR") |
| 66 | + self.job = os.environ.get("GITHUB_JOB") |
| 67 | + self.run_attempt = os.environ.get("GITHUB_RUN_ATTEMPT") |
| 68 | + self.run_number = os.environ.get("GITHUB_RUN_NUMBER") |
| 69 | + self.run_id = os.environ.get("GITHUB_RUN_ID") |
| 70 | + |
| 71 | + if "GITHUB_EVENT_PATH" not in os.environ: |
| 72 | + raise EnvironmentError("GITHUB_EVENT_PATH not found on the environment.") |
| 73 | + |
| 74 | + self.event_path = os.environ["GITHUB_EVENT_PATH"] |
| 75 | + with open(self.event_path, encoding="utf-8") as file: |
| 76 | + self.payload = json.load(file) |
| 77 | + |
| 78 | + def to_dict(self) -> Dict[str, Any]: |
| 79 | + """ |
| 80 | + Convert the GithubEvent instance to a dictionary. |
| 81 | +
|
| 82 | + Returns: |
| 83 | + dict: A dictionary containing the attributes of the GithubEvent instance. |
| 84 | + """ |
| 85 | + return { |
| 86 | + attr: getattr(self, attr) |
| 87 | + for attr in dir(self) |
| 88 | + if not callable(getattr(self, attr)) and not attr.startswith("__") |
| 89 | + } |
| 90 | + |
| 91 | + def __str__(self) -> str: |
| 92 | + """ |
| 93 | + Returns string representation of the github event data. |
| 94 | +
|
| 95 | + Returns: |
| 96 | + str: Github event data. |
| 97 | + """ |
| 98 | + return str(self.to_dict()) |
0 commit comments