1
1
"""
2
- This module defines the `GithubEvent ` class for handling GitHub event details.
2
+ This module defines the `GitHubEvent ` class for handling GitHub event details.
3
3
4
4
Note:
5
5
This module relies on the presence of specific environment variables
12
12
13
13
14
14
# pylint: disable=R0902; Too many instance attributes
15
- class GithubEvent :
15
+ class GitHubEvent :
16
16
"""Class representing GitHub events.
17
17
18
18
This class provides methods for loading and accessing various details of
@@ -24,6 +24,7 @@ class GithubEvent:
24
24
ref (str): The Git reference (branch or tag) for the event.
25
25
workflow (str): The name of the GitHub workflow.
26
26
action (str): The action that triggered the event.
27
+ repository (str): The GitHub repository name.
27
28
actor (str): The GitHub username of the user or app that triggered the event.
28
29
job (str): The name of the job associated with the event.
29
30
run_attempt (str): The current attempt number for the job run.
@@ -34,20 +35,19 @@ class GithubEvent:
34
35
payload (dict): The GitHub event payload.
35
36
36
37
Raises:
37
- EnvironmentError: If the required environment variable 'GITHUB_EVENT_PATH'
38
- is not found.
38
+ EnvironmentError: If GitHub env are not set properly.
39
39
40
40
Example:
41
41
```python
42
- github_event = GithubEvent ()
42
+ github_event = GitHubEvent ()
43
43
print(github_event.event_name)
44
44
print(github_event.sha)
45
45
print(github_event.payload)
46
46
```
47
47
"""
48
48
49
49
def __init__ (self ) -> None :
50
- """Initialize a new instance of the GithubEvent class."""
50
+ """Initialize a new instance of the GitHubEvent class."""
51
51
self .__load_details ()
52
52
53
53
def __load_details (self ) -> None :
@@ -58,30 +58,31 @@ def __load_details(self) -> None:
58
58
environment variables set by GitHub Actions and loading the event payload
59
59
from a file.
60
60
"""
61
- self .event_name = os .environ .get ("GITHUB_EVENT_NAME" )
62
- self .sha = os .environ .get ("GITHUB_SHA" )
63
- self .ref = os .environ .get ("GITHUB_REF" )
64
- self .workflow = os .environ .get ("GITHUB_WORKFLOW" )
65
- self .action = os .environ .get ("GITHUB_ACTION" )
66
- self .actor = os .environ .get ("GITHUB_ACTOR" )
67
- self .job = os .environ .get ("GITHUB_JOB" )
68
- self .run_attempt = os .environ .get ("GITHUB_RUN_ATTEMPT" )
69
- self .run_number = os .environ .get ("GITHUB_RUN_NUMBER" )
70
- self .run_id = os .environ .get ("GITHUB_RUN_ID" )
71
-
72
- if "GITHUB_EVENT_PATH" not in os .environ :
73
- raise EnvironmentError ("GITHUB_EVENT_PATH not found on the environment." )
74
-
75
- self .event_path = os .environ ["GITHUB_EVENT_PATH" ]
76
- with open (self .event_path , encoding = "utf-8" ) as file :
77
- self .payload = json .load (file )
61
+ try :
62
+ self .event_name = os .environ ["GITHUB_EVENT_NAME" ]
63
+ self .sha = os .environ ["GITHUB_SHA" ]
64
+ self .ref = os .environ ["GITHUB_REF" ]
65
+ self .workflow = os .environ ["GITHUB_WORKFLOW" ]
66
+ self .action = os .environ ["GITHUB_ACTION" ]
67
+ self .actor = os .environ ["GITHUB_ACTOR" ]
68
+ self .repository = os .environ ["GITHUB_REPOSITORY" ]
69
+ self .job = os .environ ["GITHUB_JOB" ]
70
+ self .run_attempt = os .environ ["GITHUB_RUN_ATTEMPT" ]
71
+ self .run_number = os .environ ["GITHUB_RUN_NUMBER" ]
72
+ self .run_id = os .environ ["GITHUB_RUN_ID" ]
73
+
74
+ self .event_path = os .environ ["GITHUB_EVENT_PATH" ]
75
+ with open (self .event_path , encoding = "utf-8" ) as file :
76
+ self .payload : Dict [str , Any ] = json .load (file )
77
+ except KeyError as ex :
78
+ raise EnvironmentError ("GitHub env not found." ) from ex
78
79
79
80
def to_dict (self ) -> Dict [str , Any ]:
80
81
"""
81
- Convert the GithubEvent instance to a dictionary.
82
+ Convert the GitHubEvent instance to a dictionary.
82
83
83
84
Returns:
84
- dict: A dictionary containing the attributes of the GithubEvent instance.
85
+ dict: A dictionary containing the attributes of the GitHubEvent instance.
85
86
"""
86
87
return {
87
88
attr : getattr (self , attr )
0 commit comments