From c505e51137340935a174916a301ed91746bd7c4a Mon Sep 17 00:00:00 2001 From: Paul Barfuss <18050645+paulbarfuss@users.noreply.github.com> Date: Wed, 10 Feb 2021 14:55:19 -0500 Subject: [PATCH] Add print statements and remove unused vars to resource-dispatcher (#41) * remove unused vars * Added url to print statements * Add error handling to git fetch/pull and git push --- resource-dispatcher/execution/plugins/git.py | 54 ++++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/resource-dispatcher/execution/plugins/git.py b/resource-dispatcher/execution/plugins/git.py index 186d4e8..a78afa3 100644 --- a/resource-dispatcher/execution/plugins/git.py +++ b/resource-dispatcher/execution/plugins/git.py @@ -14,22 +14,22 @@ def run_action(params): branch = params["branch"] if "branch" in params else "master" message = params["message"] if "message" in params else "Commit message" author_name = params["author_name"] if "author_name" in params else "Automated Tool" - author_email = params["author_email"] if "author_email" in params else "email@email.com" + author_email = ( + params["author_email"] if "author_email" in params else "email@email.com" + ) url = params["url"] if "url" in params else None if "secret" in params and url and not url.startswith("http"): - env = { - "GIT_SSH_COMMAND": generate_ssh_command(params) - } + env = {"GIT_SSH_COMMAND": generate_ssh_command(params)} else: env = {} if params["action"] == "clone" or params["action"] == "pull": - repo = fetch(url, repo_directory, branch, env) + fetch(url, repo_directory, branch, env) elif params["action"] == "add-all-changes": - repo = add_all(repo_directory) + add_all(url, repo_directory) elif params["action"] == "commit": - commit(repo_directory, message, author_name, author_email) + commit(url, repo_directory, message, author_name, author_email) elif params["action"] == "push": push(url, repo_directory, env) @@ -38,36 +38,48 @@ def fetch(url, repo_directory, branch, env={}): if not os.path.exists(repo_directory): os.makedirs(repo_directory) print(f"Cloning {url}...") - repo = Repo.clone_from(url, repo_directory, branch=branch, env=env) - print(f"Cloned {url}") + try: + repo = Repo.clone_from(url, repo_directory, branch=branch, env=env) + print(f"Cloned {url}") + except Exception as e: + if "not found in upstream origin" in str(e): + print(f"Branch {branch} not found: {url}") + elif "The project you were looking for could not be found" in str(e): + print(f"Project not found: {url}") + else: + raise e else: repo = Repo.init(repo_directory) repo.remotes.origin.pull(env=env) - return repo -def add_all(repo_directory): +def add_all(url, repo_directory): repo = Repo.init(repo_directory) repo.git.add(all=True) - print("All files added") - return repo + print(f"All files added: {url}") -def commit(repo_directory, message, author_name, author_email): +def commit(url, repo_directory, message, author_name, author_email): repo = Repo.init(repo_directory) repo.config_writer().set_value("user", "name", author_name).release() repo.config_writer().set_value("user", "email", author_email).release() try: - repo.git.commit('-m', message, author=f"{author_name} <{author_email}>") - print("Committed to repository") + repo.git.commit("-m", message, author=f"{author_name} <{author_email}>") + print(f"Committed to repository: {url}") except Exception as e: - if "nothing to commit, working tree clean" in str(e): - print("Nothing to commit") + if "nothing to commit" in str(e): + print(f"Nothing to commit: {url}") else: raise e def push(url, repo_directory, env={}): - repo = Repo.init(repo_directory) - repo.remotes.origin.push(env=env) - print("Pushed repository") + try: + repo = Repo.init(repo_directory) + repo.remotes.origin.push(env=env) + print(f"Pushed repository: {url}") + except Exception as e: + if "object has no attribute 'origin'" in str(e): + print(f"Remote origin does not exist: {url}") + else: + raise e