Repository: python/bedevere
Pull request #577 (2023-08-18 20:44:59)

Fix error with new commit pushed to existing PR

Mariatta: I'm seeing a number of errors about this in the logs of Heroku:
```
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114816&selected=1628609783438114816) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114816&selected=1628609783438114816) Traceback (most recent call last):
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114817&selected=1628609783438114817) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114817&selected=1628609783438114817)   File "/app/bedevere/__main__.py", line 40, in main
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114819&selected=1628609783438114819) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114819&selected=1628609783438114819)     await router.dispatch(event, gh, session=session)
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114820&selected=1628609783438114820) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114820&selected=1628609783438114820)   File "/app/.heroku/python/lib/python3.10/site-packages/gidgethub/routing.py", line 89, in dispatch
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114821&selected=1628609783438114821) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114821&selected=1628609783438114821)     await callback(event, *args, **kwargs)
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114822&selected=1628609783438114822) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114822&selected=1628609783438114822)   File "/app/bedevere/stage.py", line 152, in new_commit_pushed
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114823&selected=1628609783438114823) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114823&selected=1628609783438114823)     issue = await util.issue_for_PR(gh, pr)
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114824&selected=1628609783438114824) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114824&selected=1628609783438114824)   File "/app/bedevere/util.py", line 116, in issue_for_PR
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114825&selected=1628609783438114825) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114825&selected=1628609783438114825)     return await gh.getitem(pull_request["issue_url"])
Aug 16 16:20:52 [bedevere](https://my.papertrailapp.com/systems/bedevere/events?focus=1628609783438114826&selected=1628609783438114826) [app/web.1](https://my.papertrailapp.com/events?q=program%3Aapp%2Fweb.1&focus=1628609783438114826&selected=1628609783438114826) KeyError: 'issue_url'
```

Hopefully this will fix the error.
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) Report
> Merging [#577](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) (c0f8956) into [main](https://app.codecov.io/gh/python/bedevere/commit/7460cbe2349c3e0f252f57a9b834346fa8b5b373?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) (7460cbe) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://app.codecov.io/gh/python/bedevere/pull/577/graphs/tree.svg?width=650&height=150&src=pr&token=S7kyeapERi&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python)](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python)

```diff
@@            Coverage Diff            @@
##              main      #577   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           18        18           
  Lines         2083      2088    +5     
  Branches       234       237    +3     
=========================================
+ Hits          2083      2088    +5     
```

| [Flag](https://app.codecov.io/gh/python/bedevere/pull/577/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | Coverage Δ | |
|---|---|---|
| [Python_3.10.12](https://app.codecov.io/gh/python/bedevere/pull/577/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | `100.00% <100.00%> (ø)` | |
| [Python_3.11.4](https://app.codecov.io/gh/python/bedevere/pull/577/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | `100.00% <100.00%> (ø)` | |
| [Python_3.8.17](https://app.codecov.io/gh/python/bedevere/pull/577/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | `100.00% <100.00%> (ø)` | |
| [Python_3.9.17](https://app.codecov.io/gh/python/bedevere/pull/577/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | `100.00% <100.00%> (ø)` | |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Files Changed](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python) | Coverage Δ | |
|---|---|---|
| [bedevere/util.py](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python#diff-YmVkZXZlcmUvdXRpbC5weQ==) | `100.00% <100.00%> (ø)` | |
| [tests/test\_stage.py](https://app.codecov.io/gh/python/bedevere/pull/577?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=python#diff-dGVzdHMvdGVzdF9zdGFnZS5weQ==) | `100.00% <100.00%> (ø)` | |


Patch:
diff --git a/.coveragerc b/.coveragerc
index 841036d..7cbe7f5 100644
--- a/.coveragerc
+++ b/.coveragerc
@@ -1,5 +1,9 @@
 [run]
 branch = True
+dynamic_context = test_function
 
 [report]
 fail_under = 100
+
+[html]
+show_contexts = True
diff --git a/bedevere/util.py b/bedevere/util.py
index 84305d4..7cb6c7a 100644
--- a/bedevere/util.py
+++ b/bedevere/util.py
@@ -113,7 +113,12 @@ async def files_for_PR(gh, pull_request):
 async def issue_for_PR(gh, pull_request):
     """Return a dict with data about the given PR."""
     # "issue_url" is the API endpoint for the given pull_request (despite the name)
-    return await gh.getitem(pull_request["issue_url"])
+    # It could also come from "url"
+
+    url_key = "issue_url"
+    if not pull_request.get(url_key):
+        url_key = "url"
+    return await gh.getitem(pull_request[url_key])
 
 
 def build_pr_body(issue_number: int, body: str) -> str:


Test patch:
diff --git a/tests/test_stage.py b/tests/test_stage.py
index e5eaf07..39cc449 100644
--- a/tests/test_stage.py
+++ b/tests/test_stage.py
@@ -1096,8 +1096,8 @@ async def test_awaiting_label_not_removed_when_pr_not_merged(label):
     await awaiting.router.dispatch(event, gh)
     assert gh.delete_url is None
 
-
-async def test_new_commit_pushed_to_approved_pr():
+@pytest.mark.parametrize("issue_url_key", ["url", "issue_url"])
+async def test_new_commit_pushed_to_approved_pr(issue_url_key):
     # There is new commit on approved PR
     username = "brettcannon"
     sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
@@ -1121,7 +1121,8 @@ async def test_new_commit_pushed_to_approved_pr():
                             "name": "awaiting merge",
                         },
                     ],
-                    "issue_url": "/repos/python/cpython/issues/5547",
+                    # the key could be 'url' or 'issue_url'
+                    issue_url_key: "/repos/python/cpython/issues/5547",
                 }
             ],
         },
@@ -1168,8 +1169,8 @@ async def test_new_commit_pushed_to_approved_pr():
         )
     }
 
-
-async def test_new_commit_pushed_to_not_approved_pr():
+@pytest.mark.parametrize("issue_url_key", ["url", "issue_url"])
+async def test_new_commit_pushed_to_not_approved_pr(issue_url_key):
     # There is new commit on approved PR
     sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9"
     data = {"commits": [{"id": sha}]}
@@ -1190,7 +1191,8 @@ async def test_new_commit_pushed_to_not_approved_pr():
                             "name": "awaiting review",
                         },
                     ],
-                    "issue_url": "/repos/python/cpython/issues/5547",
+                    #
+                    issue_url_key: "/repos/python/cpython/issues/5547",
                 }
             ],
         },