|
1 | 1 | import json |
2 | 2 | import os |
3 | | -from socketsecurity.core import log, do_request |
4 | | -import requests |
5 | | -from socketsecurity.core.classes import Comment |
6 | | -from socketsecurity.core.scm_comments import Comments |
7 | 3 | import sys |
| 4 | +from dataclasses import dataclass |
8 | 5 |
|
| 6 | +from git import Optional |
| 7 | + |
| 8 | +from socketsecurity.core import do_request, log |
| 9 | +from socketsecurity.core.classes import Comment |
| 10 | +from socketsecurity.core.scm_comments import Comments |
9 | 11 |
|
10 | | -global github_sha |
11 | | -global github_api_url |
12 | | -global github_ref_type |
13 | | -global github_event_name |
14 | | -global github_workspace |
15 | | -global github_repository |
16 | | -global github_ref_name |
17 | | -global github_actor |
18 | | -global default_branch |
19 | | -global github_env |
20 | | -global pr_number |
21 | | -global pr_name |
22 | | -global is_default_branch |
23 | | -global commit_message |
24 | | -global committer |
25 | | -global gh_api_token |
26 | | -global github_repository_owner |
27 | | -global event_action |
| 12 | +# Declare all globals with initial None values |
| 13 | +github_sha: Optional[str] = None |
| 14 | +github_api_url: Optional[str] = None |
| 15 | +github_ref_type: Optional[str] = None |
| 16 | +github_event_name: Optional[str] = None |
| 17 | +github_workspace: Optional[str] = None |
| 18 | +github_repository: Optional[str] = None |
| 19 | +github_ref_name: Optional[str] = None |
| 20 | +github_actor: Optional[str] = None |
| 21 | +default_branch: Optional[str] = None |
| 22 | +github_env: Optional[str] = None |
| 23 | +pr_number: Optional[str] = None |
| 24 | +pr_name: Optional[str] = None |
| 25 | +is_default_branch: bool = False |
| 26 | +commit_message: Optional[str] = None |
| 27 | +committer: Optional[str] = None |
| 28 | +gh_api_token: Optional[str] = None |
| 29 | +github_repository_owner: Optional[str] = None |
| 30 | +event_action: Optional[str] = None |
28 | 31 |
|
29 | 32 | github_variables = [ |
30 | 33 | "GITHUB_SHA", |
|
45 | 48 | "EVENT_ACTION" |
46 | 49 | ] |
47 | 50 |
|
| 51 | +@dataclass |
| 52 | +class GithubConfig: |
| 53 | + """Configuration from GitHub environment variables""" |
| 54 | + sha: str |
| 55 | + api_url: str |
| 56 | + ref_type: str |
| 57 | + event_name: str |
| 58 | + workspace: str |
| 59 | + repository: str |
| 60 | + ref_name: str |
| 61 | + default_branch: bool |
| 62 | + pr_number: Optional[str] |
| 63 | + pr_name: Optional[str] |
| 64 | + commit_message: Optional[str] |
| 65 | + actor: str |
| 66 | + env: str |
| 67 | + token: str |
| 68 | + owner: str |
| 69 | + event_action: Optional[str] |
| 70 | + |
| 71 | + @classmethod |
| 72 | + def from_env(cls) -> 'GithubConfig': |
| 73 | + """Create config from environment variables""" |
| 74 | + token = os.getenv('GH_API_TOKEN') |
| 75 | + if not token: |
| 76 | + log.error("Unable to get Github API Token from GH_API_TOKEN") |
| 77 | + sys.exit(2) |
| 78 | + |
| 79 | + return cls( |
| 80 | + sha=os.getenv('GITHUB_SHA', ''), |
| 81 | + api_url=os.getenv('GITHUB_API_URL', ''), |
| 82 | + ref_type=os.getenv('GITHUB_REF_TYPE', ''), |
| 83 | + event_name=os.getenv('GITHUB_EVENT_NAME', ''), |
| 84 | + workspace=os.getenv('GITHUB_WORKSPACE', ''), |
| 85 | + repository=os.getenv('GITHUB_REPOSITORY', '').split('/')[-1], |
| 86 | + ref_name=os.getenv('GITHUB_REF_NAME', ''), |
| 87 | + default_branch=os.getenv('DEFAULT_BRANCH', '').lower() == 'true', |
| 88 | + pr_number=os.getenv('PR_NUMBER'), |
| 89 | + pr_name=os.getenv('PR_NAME'), |
| 90 | + commit_message=os.getenv('COMMIT_MESSAGE'), |
| 91 | + actor=os.getenv('GITHUB_ACTOR', ''), |
| 92 | + env=os.getenv('GITHUB_ENV', ''), |
| 93 | + token=token, |
| 94 | + owner=os.getenv('GITHUB_REPOSITORY_OWNER', ''), |
| 95 | + event_action=os.getenv('EVENT_ACTION') |
| 96 | + ) |
| 97 | + |
| 98 | + |
48 | 99 | for env in github_variables: |
49 | 100 | var_name = env.lower() |
50 | 101 | globals()[var_name] = os.getenv(env) or None |
51 | 102 | if var_name == "default_branch": |
52 | | - global is_default_branch |
53 | 103 | if default_branch is None or default_branch.lower() == "false": |
54 | 104 | is_default_branch = False |
55 | 105 | else: |
@@ -233,15 +283,14 @@ def post_reaction(comment_id: int) -> None: |
233 | 283 | def comment_reaction_exists(comment_id: int) -> bool: |
234 | 284 | repo = github_repository.rsplit("/", 1)[1] |
235 | 285 | path = f"repos/{github_repository_owner}/{repo}/issues/comments/{comment_id}/reactions" |
236 | | - response = do_request(path, headers=headers, base_url=github_api_url) |
237 | | - exists = False |
238 | 286 | try: |
| 287 | + response = do_request(path, headers=headers, base_url=github_api_url) |
239 | 288 | data = response.json() |
240 | 289 | for reaction in data: |
241 | 290 | content = reaction.get("content") |
242 | 291 | if content is not None and content == ":thumbsup:": |
243 | | - exists = True |
| 292 | + return True |
244 | 293 | except Exception as error: |
245 | 294 | log.error(f"Unable to get reaction for {comment_id} for PR {pr_number}") |
246 | 295 | log.error(error) |
247 | | - return exists |
| 296 | + return False |
0 commit comments