Skip to content

Commit

Permalink
refactor: improve response handling and formatting
Browse files Browse the repository at this point in the history
Adds a new check_response method to handle HTTP response status codes and
consistent error reporting. Improves code organization by consolidating common
response handling logic. Also fixes formatting in README.md table for better
readability.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and pipelines-as-code[bot] committed Feb 11, 2025
1 parent c7a45dd commit 7a70e50
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following commands are supported:
| `/unlabel bug feature` | Removes labels from the PR |
| `/lgtm` | Approves the PR if at least 1 org members have commented `/lgtm` |
| `/merge` | Merges the PR if it has enough `/lgtm` approvals |
| `/rebase` | Rebases the PR branch on the base branch |
| `/rebase` | Rebases the PR branch on the base branch |
| `/help` | Shows this help message |

## Usage
Expand Down
28 changes: 21 additions & 7 deletions hack/pipeline-embedded-prow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,15 @@ spec:
self._pr_status = None
def check_response(self, resp: requests.Response) -> bool:
if resp.status_code > 200 and resp.status_code < 300:
return True
print(
f"Error while executing the command: status: {resp.status_code} {resp.text}",
file=sys.stderr,
)
return False
def post_comment(self, message: str) -> requests.Response:
"""
Posts a comment to the pull request.
Expand Down Expand Up @@ -698,20 +707,25 @@ spec:
print(f"⚠️ PR #{args.pr_num} is not open.", file=sys.stderr)
sys.exit(1)
response = None
if command in ("assign", "unassign"):
pr_handler.assign_unassign(command, values)
response = pr_handler.assign_unassign(command, values)
elif command == "label":
pr_handler.label(values)
response = pr_handler.label(values)
elif command == "unlabel":
pr_handler.unlabel(values)
response = pr_handler.unlabel(values)
elif command == "rebase":
response = pr_handler.rebase()
elif command == "help":
response = pr_handler.post_comment(HELP_TEXT.strip())
elif command == "lgtm":
pr_handler.lgtm()
elif command == "rebase":
pr_handler.rebase()
elif command == "merge":
pr_handler.merge_pr()
elif command == "help":
pr_handler.post_comment(HELP_TEXT.strip())
if response:
if not pr_handler.check_response(response):
sys.exit(1)
if __name__ == "__main__":
Expand Down
28 changes: 21 additions & 7 deletions prow/prow.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ def __init__(

self._pr_status = None

def check_response(self, resp: requests.Response) -> bool:
if resp.status_code > 200 and resp.status_code < 300:
return True
print(
f"Error while executing the command: status: {resp.status_code} {resp.text}",
file=sys.stderr,
)
return False

def post_comment(self, message: str) -> requests.Response:
"""
Posts a comment to the pull request.
Expand Down Expand Up @@ -633,20 +642,25 @@ def main():
print(f"⚠️ PR #{args.pr_num} is not open.", file=sys.stderr)
sys.exit(1)

response = None
if command in ("assign", "unassign"):
pr_handler.assign_unassign(command, values)
response = pr_handler.assign_unassign(command, values)
elif command == "label":
pr_handler.label(values)
response = pr_handler.label(values)
elif command == "unlabel":
pr_handler.unlabel(values)
response = pr_handler.unlabel(values)
elif command == "rebase":
response = pr_handler.rebase()
elif command == "help":
response = pr_handler.post_comment(HELP_TEXT.strip())
elif command == "lgtm":
pr_handler.lgtm()
elif command == "rebase":
pr_handler.rebase()
elif command == "merge":
pr_handler.merge_pr()
elif command == "help":
pr_handler.post_comment(HELP_TEXT.strip())

if response:
if not pr_handler.check_response(response):
sys.exit(1)


if __name__ == "__main__":
Expand Down

0 comments on commit 7a70e50

Please sign in to comment.