From 714153eaf3802101df34c0d99395db1a297756c8 Mon Sep 17 00:00:00 2001 From: Robert Alonso <17463757+rgalonso@users.noreply.github.com> Date: Thu, 24 Oct 2024 22:55:44 +0000 Subject: [PATCH] feat: add Local client This is just a safe fallback for local testing. If environment variables which activate the creation of the GitHub client aren't set, then the Local client is created. It acts on the most recent commit of the repo in the working directory. Minor edit to GitHubClient so that it raises an EnvironmentError exception if INPUT_GITHUB_URL environment variable is not defined. This allows main to detect the error and fall back to trying to use the Local client --- GitHubClient.py | 2 ++ LocalClient.py | 16 ++++++++++++++++ main.py | 13 +++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 LocalClient.py diff --git a/GitHubClient.py b/GitHubClient.py index fa5453b..5513f9d 100644 --- a/GitHubClient.py +++ b/GitHubClient.py @@ -9,6 +9,8 @@ class GitHubClient(object): def __init__(self): self.github_url = os.getenv('INPUT_GITHUB_URL') + if not self.github_url: + raise EnvironmentError self.base_url = f'{self.github_url}/' self.repos_url = f'{self.base_url}repos/' self.repo = os.getenv('INPUT_REPO') diff --git a/LocalClient.py b/LocalClient.py new file mode 100644 index 0000000..9d0b606 --- /dev/null +++ b/LocalClient.py @@ -0,0 +1,16 @@ +import subprocess + +class LocalClient(object): + def __init__(self): + self.diff_url = None + self.commits = ['placeholder'] # content doesn't matter, just length + self.insert_issue_urls = False + + def get_last_diff(self): + return subprocess.run(['git', 'diff', 'HEAD^..HEAD'], stdout=subprocess.PIPE).stdout.decode('utf-8') + + def create_issue(self, issue): + return [201, None] + + def close_issue(self, issue): + return 200 diff --git a/main.py b/main.py index 28d2242..dd1d7c6 100644 --- a/main.py +++ b/main.py @@ -11,12 +11,21 @@ import operator from collections import defaultdict from TodoParser import * from LineStatus import * +from LocalClient import * from GitHubClient import * if __name__ == "__main__": - # Create a basic client for communicating with GitHub, automatically initialised with environment variables. - client = GitHubClient() + # Try to create a basic client for communicating with the remote version control server, automatically initialised with environment variables. + try: + # try to build a GitHub client + client = GitHubClient() + except EnvironmentError: + # don't immediately give up + client = None + # if needed, fall back to using a local client for testing + client = client or LocalClient() + # Check to see if the workflow has been run manually. # If so, adjust the client SHA and diff URL to use the manually supplied inputs. manual_commit_ref = os.getenv('MANUAL_COMMIT_REF')