Skip to content

Commit

Permalink
Add print statements and remove unused vars to resource-dispatcher (#41)
Browse files Browse the repository at this point in the history
* remove unused vars

* Added url to print statements

* Add error handling to git fetch/pull and git push
  • Loading branch information
paulbarfuss authored Feb 10, 2021
1 parent 4865c11 commit c505e51
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions resource-dispatcher/execution/plugins/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]"
author_email = (
params["author_email"] if "author_email" in params else "[email protected]"
)
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)

Expand All @@ -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

0 comments on commit c505e51

Please sign in to comment.