From e985b8bb578e0f6a69c0804633ee6f3d0316b621 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Pi=C3=A9dallu?= Date: Wed, 7 Feb 2024 17:25:16 +0100 Subject: [PATCH] Handle updating pull requests --- update_app_levels/update_app_levels.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/update_app_levels/update_app_levels.py b/update_app_levels/update_app_levels.py index c6a736f..d9fb323 100755 --- a/update_app_levels/update_app_levels.py +++ b/update_app_levels/update_app_levels.py @@ -157,18 +157,25 @@ def make_pull_request(pr_body: str) -> None: with requests.Session() as s: s.headers.update({"Authorization": f"token {github_token()}"}) - response = s.post(f"https://api.github.com/repos/{APPS_REPO}/pulls", json.dumps(pr_data)) + response = s.post(f"https://api.github.com/repos/{APPS_REPO}/pulls", json=pr_data) if response.status_code == 422: response = s.get(f"https://api.github.com/repos/{APPS_REPO}/pulls", data={"head": "update_app_levels"}) - existing_url = response.json()[0]["html_url"] - logging.warning(f"A Pull Request already exists at {existing_url} !") + response.raise_for_status() + pr_number = response.json()[0]["number"] + + # head can't be updated + del pr_data["head"] + response = s.patch(f"https://api.github.com/repos/{APPS_REPO}/pulls/{pr_number}", json=pr_data) + response.raise_for_status() + existing_url = response.json()["html_url"] + logging.warning(f"An existing Pull Request has been updated at {existing_url} !") else: + response.raise_for_status() + new_url = response.json()["html_url"] logging.info(f"Opened a Pull Request at {new_url} !") - response.raise_for_status() - def main(): parser = argparse.ArgumentParser()