-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
4865c11
commit c505e51
Showing
1 changed file
with
33 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
|
@@ -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 |