Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add mechanism to access pages in private repos (#248)
Dhruv Bhanushali9390d3aa5946
diff --git a/automations/python/Pipfile b/automations/python/Pipfileindex 311c0b188..b3ef8f3ec 100644--- a/automations/python/Pipfile+++ b/automations/python/Pipfile@@ -8,6 +8,8 @@ pygithub = "*"pyyaml = "*"beautifulsoup4 = "*"requests = "*"+mechanize = "*"+pyotp = "*"[dev-packages]ipython = "*"diff --git a/automations/python/Pipfile.lock b/automations/python/Pipfile.lockindex 7ee8db54d..16a2ce445 100644--- a/automations/python/Pipfile.lock+++ b/automations/python/Pipfile.lock@@ -1,7 +1,7 @@{"_meta": {"hash": {- "sha256": "a9c0f6a5ac5d5b091b2778cd8b37419650bf574d8fa63ae8c086a953bf44603c"+ "sha256": "05ef53546ac2662ac81778b265a5c4027b6e86071f35c42f59503602e466ca63"},"pipfile-spec": 6,"requires": {@@ -18,11 +18,11 @@"default": {"beautifulsoup4": {"hashes": [- "sha256:9a315ce70049920ea4572a4055bc4bd700c940521d36fc858205ad4fcde149bf",- "sha256:c23ad23c521d818955a4151a67d81580319d4bf548d3d49f4223ae041ff98891"+ "sha256:58d5c3d29f5a36ffeb94f02f0d786cd53014cf9b3b3951d42e0080d8a9498d30",+ "sha256:ad9aa55b65ef2808eb405f46cf74df7fcb7044d5cbc26487f96eb2ef2e436693"],"index": "pypi",- "version": "==4.10.0"+ "version": "==4.11.1"},"certifi": {"hashes": [@@ -103,6 +103,14 @@"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'","version": "==1.2.13"},+ "html5lib": {+ "hashes": [+ "sha256:0d78f8fde1c230e99fe37986a60526d7049ed4bf8a9fadbad5f00e22e58e041d",+ "sha256:b2e5b40261e20f354d198eae92afc10d750afb487ed5e50f9c4eaf07c184146f"+ ],+ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'",+ "version": "==1.1"+ },"idna": {"hashes": ["sha256:84d9dd047ffa80596e0f246e2eab0b391788b0503584e8945f2368256d2735ff",@@ -111,6 +119,14 @@"markers": "python_version >= '3'","version": "==3.3"},+ "mechanize": {+ "hashes": [+ "sha256:5e86ac0777357e006eb04cd28f7ed9f811d48dffa603d3891ac6d2b92280dc91",+ "sha256:961fd171b5eb37a7578fce62ba81ba85803dff3c5ba4ac24f6f569ae27198439"+ ],+ "index": "pypi",+ "version": "==0.4.8"+ },"pycparser": {"hashes": ["sha256:8ee45429555515e1f6b185e78100aea234072576aa43ab53aefcae078162fca9",@@ -131,7 +147,7 @@"sha256:72d1d253f32dbd4f5c88eaf1fdc62f3a19f676ccbadb9dbc5d07e951b2b26daf","sha256:d42908208c699b3b973cbeb01a969ba6a96c821eefb1c5bfe4c390c01d67abba"],- "index": "pypi",+ "markers": "python_version >= '3.6'","version": "==2.4.0"},"pynacl": {@@ -150,6 +166,14 @@"markers": "python_version >= '3.6'","version": "==1.5.0"},+ "pyotp": {+ "hashes": [+ "sha256:9d144de0f8a601d6869abe1409f4a3f75f097c37b50a36a3bf165810a6e23f28",+ "sha256:d28ddfd40e0c1b6a6b9da961c7d47a10261fb58f378cb00f05ce88b26df9c432"+ ],+ "index": "pypi",+ "version": "==2.6.0"+ },"pyyaml": {"hashes": ["sha256:0283c35a6a9fbf047493e3a0ce8d79ef5030852c51e9d911a27badfde0605293",@@ -197,6 +221,14 @@"index": "pypi","version": "==2.27.1"},+ "six": {+ "hashes": [+ "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926",+ "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"+ ],+ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'",+ "version": "==1.16.0"+ },"soupsieve": {"hashes": ["sha256:3b2503d3c7084a42b1ebd08116e5f81aadfaea95863628c80a3b774a11b7c759",@@ -213,6 +245,13 @@"markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4' and python_version < '4'","version": "==1.26.9"},+ "webencodings": {+ "hashes": [+ "sha256:a0af1213f3c2226497a97e2b3aa01a7e4bee4f403f95be16fc9acd2947514a78",+ "sha256:b36a1c245f2d304965eb4e0a82848379241dc04b865afcc4aab16748587e1923"+ ],+ "version": "==0.5.1"+ },"wrapt": {"hashes": ["sha256:00b6d4ea20a906c0ca56d84f93065b398ab74b927a7a3dbd470f6fc503f95dc3",@@ -285,6 +324,14 @@}},"develop": {+ "appnope": {+ "hashes": [+ "sha256:02bd91c4de869fbb1e1c50aafc4098827a7a54ab2f39d9dcba6c9547ed920e24",+ "sha256:265a455292d0bd8a72453494fa24df5a11eb18373a60c7c0430889f22548605e"+ ],+ "markers": "sys_platform == 'darwin'",+ "version": "==0.1.3"+ },"asttokens": {"hashes": ["sha256:0844691e88552595a6f4a4281a9f7f79b8dd45ca4ccea82e5e05b4bbdb76705c",@@ -347,11 +394,11 @@},"ipython": {"hashes": [- "sha256:6f56bfaeaa3247aa3b9cd3b8cbab3a9c0abf7428392f97b21902d12b2f42a381",- "sha256:8138762243c9b3a3ffcf70b37151a2a35c23d3a29f9743878c33624f4207be3d"+ "sha256:341456643a764c28f670409bbd5d2518f9b82c013441084ff2c2fc999698f83b",+ "sha256:807ae3cf43b84693c9272f70368440a9a7eaa2e7e6882dad943c32fbf7e51402"],"index": "pypi",- "version": "==8.1.1"+ "version": "==8.3.0"},"jedi": {"hashes": [@@ -409,11 +456,11 @@},"pre-commit": {"hashes": [- "sha256:725fa7459782d7bec5ead072810e47351de01709be838c2ce1726b9591dad616",- "sha256:c1a8040ff15ad3d648c70cc3e55b93e4d2d5b687320955505587fd79bbaed06a"+ "sha256:10c62741aa5704faea2ad69cb550ca78082efe5697d6f04e5710c3c229afdd10",+ "sha256:4233a1e38621c87d9dda9808c6606d7e7ba0e087cd56d3fe03202a01d2919615"],"index": "pypi",- "version": "==2.17.0"+ "version": "==2.19.0"},"prompt-toolkit": {"hashes": [diff --git a/automations/python/label_pr.py b/automations/python/label_pr.pyindex 3dc2c11c2..27d0157bd 100644--- a/automations/python/label_pr.py+++ b/automations/python/label_pr.py@@ -3,9 +3,13 @@ from __future__ import annotationsimport argparseimport logging+import osimport re+from http.cookiejar import CookieJarfrom typing import Optional+import mechanize+import pyotpimport requestsfrom bs4 import BeautifulSoupfrom github import Github@@ -76,21 +80,54 @@ def get_pull_request(gh: Github, html_url: str) -> PullRequest:return gh.get_organization(org).get_repo(repo).get_pull(pr_num)+def get_authenticated_html(url: str) -> str:+ """+ Login to the GitHub UI using the username and password, followed by 2FA. Then+ navigate to the specified URL as the authenticated user and scrape the text body.++ :param url: the URL to scrape after authenticating in GitHub+ :return: the text content of the scraped page+ """++ browser = mechanize.Browser()+ browser.set_cookiejar(CookieJar())+ browser.open("https://github.com/login/")+ browser.select_form(nr=0) # focus on the first (and only) form on the page+ browser.form["login"] = os.getenv("GH_LOGIN")+ browser.form["password"] = os.getenv("GH_PASSWORD")+ browser.submit()++ browser.select_form(nr=0) # focus on the first (and only) form on the page+ browser.form["otp"] = pyotp.TOTP(os.getenv("GH_2FA_SECRET")).now()+ browser.submit()++ browser.open(url)+ return browser.response().read()++def get_linked_issues(url: str) -> list[str]:"""Get the list of linked issues from the GitHub UI by parsing the HTML. This is aworkaround because GitHub API does not provide a reliable way to find the linkedissues for a PR.+ If the page returns a 404 response, it is assumed that the page belongs to a private+ repository and needs authentication. In these cases, ``get_authenticated_html`` is+ used to scrape the page.+:param url: the URL to the GitHub UI for a PR:return: the list of HTML URLs for linked issues"""res = requests.get(url)- if res.status_code != 200:+ if res.status_code == 404:+ text = get_authenticated_html(url)+ elif res.status_code == 200:+ text = res.text+ else:return []- soup = BeautifulSoup(res.text, "html.parser")+ soup = BeautifulSoup(text, "html.parser")divs = soup.find_all("div", **{"class": re.compile("css-truncate my-1")})anchors = [div.a["href"] for div in divs]return anchors