-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gracefully Handle Missing Permissions #11
Changes from 3 commits
0327989
5b3126b
4eb817f
207c23c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,7 @@ def run( | |
logging.info(f"β (error ok) return code: {proc.returncode}") | ||
elif on_error == OnError.WARN: | ||
logging.warning(proc.stdout.decode()) | ||
logging.warning(f"β οΈ ERROR! return code: {proc.returncode}") | ||
logging.warning(f"β οΈ WARNING! return code: {proc.returncode}") | ||
else: # OnError.FAIL | ||
logging.fatal(proc.stdout.decode()) | ||
logging.fatal(f"β ERROR! return code: {proc.returncode}") | ||
|
@@ -180,14 +180,25 @@ def merge(repo: Repository.Repository, github_actor: str) -> Tuple[bool, List[st | |
return True, conflict_file_list | ||
|
||
|
||
def push(repo: Repository.Repository, branch_name: str, username: str, password: str): | ||
def push( | ||
repo: Repository.Repository, branch_name: str, username: str, password: str | ||
) -> bool: | ||
"""Push changes to remote.""" | ||
parts: ParseResult = urlparse(repo.clone_url) | ||
cred_url = parts._replace(netloc=f"{username}:{password}@{parts.netloc}").geturl() | ||
logging.info("Assigning credentials for push.") | ||
run([GIT, "remote", "set-url", "origin", cred_url], cwd=repo.full_name) | ||
logging.info(f"Pushing {branch_name} to remote.") | ||
run([GIT, "push", "--set-upstream", "origin", branch_name], cwd=repo.full_name) | ||
if not repo.permissions.push: | ||
logging.warning( | ||
f"β οΈ WARNING! Missing 'push' permission on '{repo.owner.login}/{repo.name}'" | ||
) | ||
return False | ||
else: | ||
parts: ParseResult = urlparse(repo.clone_url) | ||
cred_url = parts._replace( | ||
netloc=f"{username}:{password}@{parts.netloc}" | ||
).geturl() | ||
logging.info("Assigning credentials for push.") | ||
run([GIT, "remote", "set-url", "origin", cred_url], cwd=repo.full_name) | ||
logging.info(f"Pushing {branch_name} to remote.") | ||
run([GIT, "push", "--set-upstream", "origin", branch_name], cwd=repo.full_name) | ||
return True | ||
|
||
|
||
def create_pull_request( | ||
|
@@ -333,42 +344,47 @@ def main() -> int: | |
f"Already up to date with: {remote_url} {remote_branch or ''} " | ||
) | ||
continue | ||
push(repo, pr_branch_name, "git", access_token) | ||
# Display instructions for ignoring incoming Lineage config changes | ||
display_lineage_config_skip: bool = CONFIG_FILENAME in conflict_file_list | ||
if display_lineage_config_skip: | ||
# This will no longer be a conflict, we tell user how to ignore. | ||
conflict_file_list.remove(CONFIG_FILENAME) | ||
if branch_is_new: | ||
logging.info("Creating a new pull request since branch is new.") | ||
template_data = { | ||
"conflict_file_list": conflict_file_list, | ||
"display_lineage_config_skip": display_lineage_config_skip, | ||
"lineage_config_filename": CONFIG_FILENAME, | ||
"lineage_id": lineage_id, | ||
"local_branch": local_branch, | ||
"metadata": PR_METADATA, | ||
"pr_branch_name": pr_branch_name, | ||
"remote_branch": remote_branch, | ||
"remote_url": remote_url, | ||
"repo_name": repo.name, | ||
"ssh_url": repo.ssh_url, | ||
} | ||
if conflict_file_list: | ||
title = f"β οΈ CONFLICT! Lineage pull request for: {lineage_id}" | ||
body = pystache.render(conflict_template, template_data) | ||
create_pull_request( | ||
repo, pr_branch_name, local_branch, title, body, draft=True | ||
) | ||
# If we can successfully push, continue with generating a PR | ||
if push(repo, pr_branch_name, "git", access_token): | ||
# Display instructions for ignoring incoming Lineage config changes | ||
display_lineage_config_skip: bool = CONFIG_FILENAME in conflict_file_list | ||
if display_lineage_config_skip: | ||
# This will no longer be a conflict, we tell user how to ignore. | ||
conflict_file_list.remove(CONFIG_FILENAME) | ||
if branch_is_new: | ||
logging.info("Creating a new pull request since branch is new.") | ||
template_data = { | ||
"conflict_file_list": conflict_file_list, | ||
"display_lineage_config_skip": display_lineage_config_skip, | ||
"lineage_config_filename": CONFIG_FILENAME, | ||
"lineage_id": lineage_id, | ||
"local_branch": local_branch, | ||
"metadata": PR_METADATA, | ||
"pr_branch_name": pr_branch_name, | ||
"remote_branch": remote_branch, | ||
"remote_url": remote_url, | ||
"repo_name": repo.name, | ||
"ssh_url": repo.ssh_url, | ||
} | ||
if conflict_file_list: | ||
title = f"β οΈ CONFLICT! Lineage pull request for: {lineage_id}" | ||
body = pystache.render(conflict_template, template_data) | ||
create_pull_request( | ||
repo, pr_branch_name, local_branch, title, body, draft=True | ||
) | ||
else: | ||
title = f"Lineage pull request for: {lineage_id}" | ||
body = pystache.render(clean_template, template_data) | ||
create_pull_request( | ||
repo, pr_branch_name, local_branch, title, body, draft=False | ||
) | ||
else: | ||
title = f"Lineage pull request for: {lineage_id}" | ||
body = pystache.render(clean_template, template_data) | ||
create_pull_request( | ||
repo, pr_branch_name, local_branch, title, body, draft=False | ||
logging.info( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this a warning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's make it a warning, but make sure the warning string starts with "ERROR!' |
||
"Not creating a new pull request since the branch already existed." | ||
) | ||
else: | ||
logging.info( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Warning? |
||
"Not creating a new pull request since the branch already existed." | ||
"Not creating a new pull request because of insufficient permissions." | ||
) | ||
|
||
logging.info("Completed.") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point we should replace these git commands with GitPython.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #12, which I just created.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@felddy Do you remember why you didn't leverage that when you first wrote this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably didn't know about it yet. Not a technical reason.