From 383afcfb382bf195579e2e6d5846ded0edd48088 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Mon, 12 Jun 2023 11:52:52 -0700 Subject: [PATCH 01/33] Bedevere as GitHub Actions Instead of getting information from webhook payload, get them from the Actions environment. Create fixtures for GitHub Actions environment vars. Adjust tests for __main__.py. Remove "ping" handler, not needed in Actions. --- .github/workflows/bedevere.yml | 45 ++++++++++++++++++++++++++++++++++ bedevere/__main__.py | 32 +++++++++++------------- tests/fixtures.py | 18 ++++++++++++++ tests/test___main__.py | 42 +++++++++++-------------------- 4 files changed, 91 insertions(+), 46 deletions(-) create mode 100644 .github/workflows/bedevere.yml create mode 100644 tests/fixtures.py diff --git a/.github/workflows/bedevere.yml b/.github/workflows/bedevere.yml new file mode 100644 index 00000000..30ca8f7d --- /dev/null +++ b/.github/workflows/bedevere.yml @@ -0,0 +1,45 @@ +name: Bedevere + +on: + pull_request: + types: + - edited + - opened + - synchronize + - reopened + - edited + - labeled + - unlabeled + - review_requested + - ready_for_review + - converted_to_draft + - closed + create: + types: + - branch + pull_request_review: + types: + - submitted + - dismissed + issue_comment: + types: + - created + push: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Check out repo + uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + python -m pip install -r requirements.txt + - name: execute bedevere + run: | + python -m bedevere \ No newline at end of file diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 3a3c0740..5dfcc9b1 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -1,5 +1,5 @@ import asyncio -import importlib + import os import sys import traceback @@ -10,26 +10,27 @@ from gidgethub import aiohttp as gh_aiohttp from gidgethub import routing from gidgethub import sansio +from gidgethub import actions from . import backport, gh_issue, close_pr, filepaths, news, stage -import sentry_sdk +# import sentry_sdk router = routing.Router(backport.router, gh_issue.router, close_pr.router, filepaths.router, news.router, stage.router) cache = cachetools.LRUCache(maxsize=500) -sentry_sdk.init(os.environ.get("SENTRY_DSN")) +# sentry_sdk.init(os.environ.get("SENTRY_DSN")) -async def main(request): +async def main(event_payload): try: - body = await request.read() - secret = os.environ.get("GH_SECRET") - event = sansio.Event.from_http(request.headers, body, secret=secret) + event_name = os.environ["GITHUB_EVENT_NAME"] + job_id = os.environ["GITHUB_JOB"] + event = sansio.Event(event_payload, event=event_name, delivery_id=job_id) + print('GH delivery ID', event.delivery_id, file=sys.stderr) - if event.event == "ping": - return web.Response(status=200) + oauth_token = os.environ.get("GH_AUTH") async with aiohttp.ClientSession() as session: gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", @@ -42,16 +43,11 @@ async def main(request): print('GH requests remaining:', gh.rate_limit.remaining) except AttributeError: pass - return web.Response(status=200) + return True except Exception as exc: traceback.print_exc(file=sys.stderr) - return web.Response(status=500) - + return False if __name__ == "__main__": # pragma: no cover - app = web.Application() - app.router.add_post("/", main) - port = os.environ.get("PORT") - if port is not None: - port = int(port) - web.run_app(app, port=port) + event_from = actions.event() + asyncio.run(main(event_from)) \ No newline at end of file diff --git a/tests/fixtures.py b/tests/fixtures.py new file mode 100644 index 00000000..baee2f36 --- /dev/null +++ b/tests/fixtures.py @@ -0,0 +1,18 @@ +import os +import pytest + + +@pytest.fixture +def tmp_event_name(request, monkeypatch): + monkeypatch.setenv("GITHUB_EVENT_NAME", request.param) + +@pytest.fixture +def tmp_job_id(monkeypatch): + monkeypatch.setenv("GITHUB_JOB", "12345") + +@pytest.fixture +def tmp_webhook(tmp_path, monkeypatch): + """Create a temporary file for an actions webhook event.""" + tmp_file_path = tmp_path / "event.json" + monkeypatch.setenv("GITHUB_EVENT_PATH", os.fspath(tmp_file_path)) + return tmp_file_path diff --git a/tests/test___main__.py b/tests/test___main__.py index e9b49172..2ea8e8ff 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -1,38 +1,24 @@ -from aiohttp import web import pytest -from bedevere import __main__ as main +from tests.fixtures import tmp_webhook, tmp_event_name, tmp_job_id -async def test_ping(aiohttp_client): - app = web.Application() - app.router.add_post("/", main.main) - client = await aiohttp_client(app) - headers = {"x-github-event": "ping", - "x-github-delivery": "1234"} - data = {"zen": "testing is good"} - response = await client.post("/", headers=headers, json=data) - assert response.status == 200 +@pytest.mark.parametrize('tmp_event_name', ["created"], indirect=True) +async def test_success(tmp_webhook, tmp_event_name, tmp_job_id, monkeypatch): + from bedevere import __main__ as main -async def test_success(aiohttp_client): - app = web.Application() - app.router.add_post("/", main.main) - client = await aiohttp_client(app) - headers = {"x-github-event": "project", - "x-github-delivery": "1234"} # Sending a payload that shouldn't trigger any networking, but no errors # either. - data = {"action": "created"} - response = await client.post("/", headers=headers, json=data) - assert response.status == 200 + event_payload = {"action": "created"} + response = await main.main(event_payload) + assert response +async def test_failure(tmp_webhook): + from bedevere import __main__ as main + """Even in the face of an exception, actions will not crash.""" -async def test_failure(aiohttp_client): - """Even in the face of an exception, the server should not crash.""" - app = web.Application() - app.router.add_post("/", main.main) - client = await aiohttp_client(app) - # Missing key headers. - response = await client.post("/", headers={}) - assert response.status == 500 + # Missing GitHub environment variables + event_payload = {} + response = await main.main(event_payload) + assert not response From 6f5ecee93a3bc216722e5033a3a6407d4f3bc281 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Mon, 12 Jun 2023 12:01:11 -0700 Subject: [PATCH 02/33] Set the python-version --- .github/workflows/bedevere.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/bedevere.yml b/.github/workflows/bedevere.yml index 30ca8f7d..f2204be4 100644 --- a/.github/workflows/bedevere.yml +++ b/.github/workflows/bedevere.yml @@ -29,6 +29,9 @@ on: jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + python-version: [ 3.10 ] steps: - name: Check out repo uses: actions/checkout@v2 From 3c8d9a9b4e2f818df179c513c07ea31520b63199 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Mon, 12 Jun 2023 14:18:42 -0700 Subject: [PATCH 03/33] Create Dockerfile --- Dockerfile | 9 +++++++++ action.yml | 10 ++++++++++ 2 files changed, 19 insertions(+) create mode 100644 Dockerfile create mode 100644 action.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..9f74012b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11-slim + +COPY requirements.txt requirements.txt +COPY dev-requirements.txt dev-requirements.txt + +RUN pip install -U pip +RUN pip install -r requirements.txt + +CMD ["python", "-m", "bedevere"] \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 00000000..76cb8a14 --- /dev/null +++ b/action.yml @@ -0,0 +1,10 @@ +--- +name: bedevere-action +description: Bedevere GitHub Actions + +branding: + color: blue + icon: activity +runs: + using: docker + image: Dockerfile From 6c0af4317c85e505530d8a027b970ef9f9c642b4 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Mon, 12 Jun 2023 14:21:06 -0700 Subject: [PATCH 04/33] use setup actions v3 and Python 3.11 --- .github/workflows/bedevere.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/bedevere.yml b/.github/workflows/bedevere.yml index f2204be4..fedf1bcc 100644 --- a/.github/workflows/bedevere.yml +++ b/.github/workflows/bedevere.yml @@ -31,10 +31,10 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [ 3.10 ] + python-version: [ "3.11" ] steps: - name: Check out repo - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: From dc5bd1d6ae2be3ad538b116f40e489907aeb5c0f Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 12:11:46 -0700 Subject: [PATCH 05/33] Update Dockerfile and copy the bedevere directory Pass the GITHUB_TOKEN secret Check if GITHUB_EVENT_PATH env var exists. --- .github/workflows/bedevere.yml | 2 ++ Dockerfile | 5 +++-- bedevere/__main__.py | 9 ++++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/bedevere.yml b/.github/workflows/bedevere.yml index fedf1bcc..5bdcc551 100644 --- a/.github/workflows/bedevere.yml +++ b/.github/workflows/bedevere.yml @@ -44,5 +44,7 @@ jobs: python -m pip install --upgrade pip python -m pip install -r requirements.txt - name: execute bedevere + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | python -m bedevere \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9f74012b..9e725098 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,9 @@ FROM python:3.11-slim COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt +COPY bedevere bedevere/ -RUN pip install -U pip -RUN pip install -r requirements.txt +RUN pip install --no-cache-dir -U pip +RUN pip install --no-cache-dir -r requirements.txt CMD ["python", "-m", "bedevere"] \ No newline at end of file diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 5dfcc9b1..0c01a533 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -31,7 +31,7 @@ async def main(event_payload): print('GH delivery ID', event.delivery_id, file=sys.stderr) - oauth_token = os.environ.get("GH_AUTH") + oauth_token = os.environ.get("GITHUB_TOKEN") async with aiohttp.ClientSession() as session: gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", oauth_token=oauth_token, @@ -49,5 +49,8 @@ async def main(event_payload): return False if __name__ == "__main__": # pragma: no cover - event_from = actions.event() - asyncio.run(main(event_from)) \ No newline at end of file + if os.environ.get("GITHUB_EVENT_PATH"): + event_from = actions.event() + asyncio.run(main(event_from)) + else: + print(f"Environment Variable 'GITHUB_EVENT_PATH' not found.") \ No newline at end of file From 1ad09f80ae1c26ba7ed74347ff7b01af4fa73a4d Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 12:15:44 -0700 Subject: [PATCH 06/33] File formatting. Empty line at the end of the file. --- .github/workflows/bedevere.yml | 3 +-- Dockerfile | 2 +- bedevere/__main__.py | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/bedevere.yml b/.github/workflows/bedevere.yml index 5bdcc551..bbde255d 100644 --- a/.github/workflows/bedevere.yml +++ b/.github/workflows/bedevere.yml @@ -3,7 +3,6 @@ name: Bedevere on: pull_request: types: - - edited - opened - synchronize - reopened @@ -47,4 +46,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - python -m bedevere \ No newline at end of file + python -m bedevere diff --git a/Dockerfile b/Dockerfile index 9e725098..41cd5c75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,4 @@ COPY bedevere bedevere/ RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt -CMD ["python", "-m", "bedevere"] \ No newline at end of file +CMD ["python", "-m", "bedevere"] diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 0c01a533..2648b5b4 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -53,4 +53,4 @@ async def main(event_payload): event_from = actions.event() asyncio.run(main(event_from)) else: - print(f"Environment Variable 'GITHUB_EVENT_PATH' not found.") \ No newline at end of file + print(f"Environment Variable 'GITHUB_EVENT_PATH' not found.") From c8afaddeb64f4733f1ff97ef2eca4ed666853890 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 12:30:19 -0700 Subject: [PATCH 07/33] Typo in Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 41cd5c75..5ab670b4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM python:3.11-slim COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt -COPY bedevere bedevere/ +COPY bedevere /bedevere RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt From 325010043cfeb708957c9e300e30fdae33c0b3e8 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 12:40:30 -0700 Subject: [PATCH 08/33] RUN dir -s --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 5ab670b4..e9ad3db2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,6 @@ COPY bedevere /bedevere RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt +RUN dir -s + CMD ["python", "-m", "bedevere"] From d562d69592b5847d0513d27d17711e374b59672c Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:18:22 -0700 Subject: [PATCH 09/33] Copy to GitHub workspace directory --- Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index e9ad3db2..7f2bd053 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,9 @@ FROM python:3.11-slim COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt -COPY bedevere /bedevere + +RUN mkdir -p /github/workspace/bedevere +COPY bedevere /github/workspace/bedevere RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt From e6d77d63fb930f58a265784ed365086185a9a55f Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:24:41 -0700 Subject: [PATCH 10/33] Create entrypoint.sh --- Dockerfile | 6 ++---- bedevere/entrypoint.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 bedevere/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 7f2bd053..7f4bc3a2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,9 @@ COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt RUN mkdir -p /github/workspace/bedevere -COPY bedevere /github/workspace/bedevere +COPY entrypoint.s /entrypoint.sh RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt -RUN dir -s - -CMD ["python", "-m", "bedevere"] +ENTRYPOINT ["/entrypoint.sh"] diff --git a/bedevere/entrypoint.sh b/bedevere/entrypoint.sh new file mode 100644 index 00000000..026b6a61 --- /dev/null +++ b/bedevere/entrypoint.sh @@ -0,0 +1,9 @@ +#!/bin/sh -l + +pwd + +ls -l + +ls -l / + +python -m bedevere \ No newline at end of file From e9c572e7e481d42110acc516fcff61d99dc57866 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:26:35 -0700 Subject: [PATCH 11/33] Entrypoint was in wrong dir --- bedevere/entrypoint.sh => entrypoint.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename bedevere/entrypoint.sh => entrypoint.sh (100%) diff --git a/bedevere/entrypoint.sh b/entrypoint.sh similarity index 100% rename from bedevere/entrypoint.sh rename to entrypoint.sh From 7d0c3a7c02826af4b217e6ce88f9bb20abb431ce Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:35:20 -0700 Subject: [PATCH 12/33] Typo in entrypoint filename --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7f4bc3a2..023e1620 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt RUN mkdir -p /github/workspace/bedevere -COPY entrypoint.s /entrypoint.sh +COPY entrypoint.sh /entrypoint.sh RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt From ae8aaabaaf86d8eb44659f757c340e3b219ffb85 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:40:12 -0700 Subject: [PATCH 13/33] Make entrypoint executable --- entrypoint.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 entrypoint.sh diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 From 494aba65b6b6c143d7ad4c2d22c4f53e3fe42745 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:42:45 -0700 Subject: [PATCH 14/33] Copy bedevere to GitHub workspace --- Dockerfile | 1 + entrypoint.sh | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index 023e1620..cc73c122 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ COPY dev-requirements.txt dev-requirements.txt RUN mkdir -p /github/workspace/bedevere COPY entrypoint.sh /entrypoint.sh +COPY bedevere /github/workspace/bedevere RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt diff --git a/entrypoint.sh b/entrypoint.sh index 026b6a61..5e60873d 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -6,4 +6,6 @@ ls -l ls -l / +ls -l /bedevere + python -m bedevere \ No newline at end of file From 5cf8707411f60618464a413dd0dbd58ff50fb74b Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:48:06 -0700 Subject: [PATCH 15/33] Copy bedevere folder --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index cc73c122..30c4b0fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,9 +3,9 @@ FROM python:3.11-slim COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt -RUN mkdir -p /github/workspace/bedevere + COPY entrypoint.sh /entrypoint.sh -COPY bedevere /github/workspace/bedevere +COPY bedevere/ /bedevere/ RUN pip install --no-cache-dir -U pip RUN pip install --no-cache-dir -r requirements.txt From 17e9c426570d39c76e57e5a8f49800c83de15ded Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 15:52:12 -0700 Subject: [PATCH 16/33] Copy bedevere folder --- entrypoint.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/entrypoint.sh b/entrypoint.sh index 5e60873d..e846d55e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -8,4 +8,8 @@ ls -l / ls -l /bedevere +cp -r /bedevere ./bedevere + +ls -l ./bedevere + python -m bedevere \ No newline at end of file From cde6ac19e3df9c71d071d934b4c8d2d3e6cc27bb Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 16:04:33 -0700 Subject: [PATCH 17/33] debug 400 error --- bedevere/util.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bedevere/util.py b/bedevere/util.py index 84305d4f..a132ec97 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -68,6 +68,8 @@ def create_status(context, state, *, description=None, target_url=None): async def post_status(gh, event, status): """Post a status in reaction to an event.""" + print(f"{event=}") # debug + print(f"{status=}") # debug await gh.post(event.data["pull_request"]["statuses_url"], data=status) From 4e267efe8f3b5624499fd23c8cbc1930ad2e66f9 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 16:05:26 -0700 Subject: [PATCH 18/33] debug gh token --- bedevere/__main__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 2648b5b4..ca91d839 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -32,6 +32,8 @@ async def main(event_payload): print('GH delivery ID', event.delivery_id, file=sys.stderr) oauth_token = os.environ.get("GITHUB_TOKEN") + if len(oauth_token) > 0: + print("got GH token") async with aiohttp.ClientSession() as session: gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", oauth_token=oauth_token, From 4b385c8251cb85bec80016c3fb484bbd88a5dbd7 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 16:15:03 -0700 Subject: [PATCH 19/33] Set the GH Token env var --- Dockerfile | 3 +++ action.yml | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/Dockerfile b/Dockerfile index 30c4b0fe..755b5040 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ FROM python:3.11-slim +ARG GITHUB_TOKEN +ENV GITHUB_TOKEN ${GITHUB_TOKEN} + COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt diff --git a/action.yml b/action.yml index 76cb8a14..ad3da049 100644 --- a/action.yml +++ b/action.yml @@ -8,3 +8,7 @@ branding: runs: using: docker image: Dockerfile + with: + context: . + build-args: | + "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" From d9d8c6c52a56c3b9fe6f18bcc0ae27a6a4b078db Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 16:51:16 -0700 Subject: [PATCH 20/33] Revert changes --- Dockerfile | 3 --- action.yml | 4 ---- 2 files changed, 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 755b5040..30c4b0fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,5 @@ FROM python:3.11-slim -ARG GITHUB_TOKEN -ENV GITHUB_TOKEN ${GITHUB_TOKEN} - COPY requirements.txt requirements.txt COPY dev-requirements.txt dev-requirements.txt diff --git a/action.yml b/action.yml index ad3da049..76cb8a14 100644 --- a/action.yml +++ b/action.yml @@ -8,7 +8,3 @@ branding: runs: using: docker image: Dockerfile - with: - context: . - build-args: | - "GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}" From 484ece3f9c5fa6287cffb610f9d43c778463ed0a Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 17:06:01 -0700 Subject: [PATCH 21/33] Remove debug prints --- bedevere/__main__.py | 2 -- bedevere/util.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index ca91d839..2648b5b4 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -32,8 +32,6 @@ async def main(event_payload): print('GH delivery ID', event.delivery_id, file=sys.stderr) oauth_token = os.environ.get("GITHUB_TOKEN") - if len(oauth_token) > 0: - print("got GH token") async with aiohttp.ClientSession() as session: gh = gh_aiohttp.GitHubAPI(session, "python/bedevere", oauth_token=oauth_token, diff --git a/bedevere/util.py b/bedevere/util.py index a132ec97..84305d4f 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -68,8 +68,6 @@ def create_status(context, state, *, description=None, target_url=None): async def post_status(gh, event, status): """Post a status in reaction to an event.""" - print(f"{event=}") # debug - print(f"{status=}") # debug await gh.post(event.data["pull_request"]["statuses_url"], data=status) From db8e6cbc4d8390f5958816620e355f332699f4fd Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 17:06:58 -0700 Subject: [PATCH 22/33] Remove Procfile Cleanup entrypoint.sh --- Procfile | 1 - entrypoint.sh | 10 ---------- 2 files changed, 11 deletions(-) delete mode 100644 Procfile diff --git a/Procfile b/Procfile deleted file mode 100644 index bda30287..00000000 --- a/Procfile +++ /dev/null @@ -1 +0,0 @@ -web: python3 -m bedevere diff --git a/entrypoint.sh b/entrypoint.sh index e846d55e..6ca740c6 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,15 +1,5 @@ #!/bin/sh -l -pwd - -ls -l - -ls -l / - -ls -l /bedevere - cp -r /bedevere ./bedevere -ls -l ./bedevere - python -m bedevere \ No newline at end of file From 3f9ea88576a003a6c62bfdfb99b955dc7ae701e7 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 17:07:31 -0700 Subject: [PATCH 23/33] Remove runtime.txt. It was for Heroku. --- runtime.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 runtime.txt diff --git a/runtime.txt b/runtime.txt deleted file mode 100644 index a5da7cc4..00000000 --- a/runtime.txt +++ /dev/null @@ -1 +0,0 @@ -python-3.10.5 From 784a19cfa7c0dc883e74bea7927d8c12dbfe2d47 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:08:58 -0700 Subject: [PATCH 24/33] Set exit code if bedevere errors out. --- bedevere/__main__.py | 4 ++-- tests/test___main__.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 2648b5b4..6cd8e4a3 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -43,10 +43,10 @@ async def main(event_payload): print('GH requests remaining:', gh.rate_limit.remaining) except AttributeError: pass - return True + except Exception as exc: traceback.print_exc(file=sys.stderr) - return False + sys.exit(1) if __name__ == "__main__": # pragma: no cover if os.environ.get("GITHUB_EVENT_PATH"): diff --git a/tests/test___main__.py b/tests/test___main__.py index 2ea8e8ff..ac6e78cc 100644 --- a/tests/test___main__.py +++ b/tests/test___main__.py @@ -11,14 +11,15 @@ async def test_success(tmp_webhook, tmp_event_name, tmp_job_id, monkeypatch): # Sending a payload that shouldn't trigger any networking, but no errors # either. event_payload = {"action": "created"} - response = await main.main(event_payload) - assert response + await main.main(event_payload) + async def test_failure(tmp_webhook): from bedevere import __main__ as main - """Even in the face of an exception, actions will not crash.""" + """Actions will have exit code in case of errors.""" # Missing GitHub environment variables event_payload = {} - response = await main.main(event_payload) - assert not response + with pytest.raises(SystemExit) as exc: + await main.main(event_payload) + assert exc.value.code == 1 From 9dac39a5caef233b3ab39b23e2fabcb28577f6a9 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:14:26 -0700 Subject: [PATCH 25/33] Debugging --- bedevere/stage.py | 1 + bedevere/util.py | 1 + 2 files changed, 2 insertions(+) diff --git a/bedevere/stage.py b/bedevere/stage.py index f3914d9b..e4438ae5 100644 --- a/bedevere/stage.py +++ b/bedevere/stage.py @@ -147,6 +147,7 @@ async def new_commit_pushed(event, gh, *arg, **kwargs): # get the latest commit hash commit_hash = commits[-1]["id"] pr = await util.get_pr_for_commit(gh, commit_hash) + print(f"{commit_hash=}") for label in util.labels(pr): if label == "awaiting merge": issue = await util.issue_for_PR(gh, pr) diff --git a/bedevere/util.py b/bedevere/util.py index 84305d4f..ffdf8f9c 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -77,6 +77,7 @@ def skip_label(what): def labels(issue): + print(f"{issue}") return {label_data["name"] for label_data in issue["labels"]} From 424509d36e9c58c3b71d75bfd4fc73a0a09a7998 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:17:37 -0700 Subject: [PATCH 26/33] Debugging --- bedevere/stage.py | 1 + bedevere/util.py | 1 + 2 files changed, 2 insertions(+) diff --git a/bedevere/stage.py b/bedevere/stage.py index e4438ae5..e9b0fe25 100644 --- a/bedevere/stage.py +++ b/bedevere/stage.py @@ -142,6 +142,7 @@ async def new_commit_pushed(event, gh, *arg, **kwargs): """If there is a new commit pushed to the PR branch that is in `awaiting merge` state, move it back to `awaiting core review` stage. """ + print(f"{event=}") commits = event.data["commits"] if len(commits) > 0: # get the latest commit hash diff --git a/bedevere/util.py b/bedevere/util.py index ffdf8f9c..7195253b 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -230,6 +230,7 @@ def no_labels(event_data): async def get_pr_for_commit(gh, sha): """Find the PR containing the specific commit hash.""" + prs_for_commit = await gh.getitem( f"/search/issues?q=type:pr+repo:python/cpython+sha:{sha}" ) From 6fa714ab090b55f7ec1c46acb0dc9e5050352e10 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:19:10 -0700 Subject: [PATCH 27/33] Debugging --- bedevere/stage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bedevere/stage.py b/bedevere/stage.py index e9b0fe25..92c6dc42 100644 --- a/bedevere/stage.py +++ b/bedevere/stage.py @@ -142,7 +142,7 @@ async def new_commit_pushed(event, gh, *arg, **kwargs): """If there is a new commit pushed to the PR branch that is in `awaiting merge` state, move it back to `awaiting core review` stage. """ - print(f"{event=}") + print(f"{event.data=}") commits = event.data["commits"] if len(commits) > 0: # get the latest commit hash From 9d1a990948a70e12cb0372729b65cbb6a66ca51d Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:26:00 -0700 Subject: [PATCH 28/33] Use repo_full_name when retrieving Git Hash --- bedevere/stage.py | 3 ++- bedevere/util.py | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/bedevere/stage.py b/bedevere/stage.py index 92c6dc42..041cac51 100644 --- a/bedevere/stage.py +++ b/bedevere/stage.py @@ -147,7 +147,8 @@ async def new_commit_pushed(event, gh, *arg, **kwargs): if len(commits) > 0: # get the latest commit hash commit_hash = commits[-1]["id"] - pr = await util.get_pr_for_commit(gh, commit_hash) + repo_full_name = event.data["repository"]["full_name"] + pr = await util.get_pr_for_commit(gh, commit_hash, repo_full_name) print(f"{commit_hash=}") for label in util.labels(pr): if label == "awaiting merge": diff --git a/bedevere/util.py b/bedevere/util.py index 7195253b..8c2d4e86 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -228,11 +228,12 @@ def no_labels(event_data): return False -async def get_pr_for_commit(gh, sha): +async def get_pr_for_commit(gh, sha, repo_full_name=None): """Find the PR containing the specific commit hash.""" - + if not repo_full_name: + repo_full_name = "python/cpython" prs_for_commit = await gh.getitem( - f"/search/issues?q=type:pr+repo:python/cpython+sha:{sha}" + f"/search/issues?q=type:pr+repo:{repo_full_name}+sha:{sha}" ) if prs_for_commit["total_count"] > 0: # there should only be one return prs_for_commit["items"][0] From f59a0065fec04bdb941966086cd8c8fb10aef2a0 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:38:33 -0700 Subject: [PATCH 29/33] Fix tests --- tests/test_stage.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/test_stage.py b/tests/test_stage.py index e5eaf072..b43a80e0 100644 --- a/tests/test_stage.py +++ b/tests/test_stage.py @@ -1097,16 +1097,19 @@ async def test_awaiting_label_not_removed_when_pr_not_merged(label): assert gh.delete_url is None -async def test_new_commit_pushed_to_approved_pr(): +@pytest.mark.parametrize("repo_full_name", ["python/cpython", "mariatta/cpython"]) +async def test_new_commit_pushed_to_approved_pr(repo_full_name): # There is new commit on approved PR username = "brettcannon" sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9" - data = {"commits": [{"id": sha}]} + data = {"commits": [{"id": sha}], + "repository": {"full_name": repo_full_name}, + } event = sansio.Event(data, event="push", delivery_id="12345") teams = [{"name": "python core", "id": 6}] items = { f"https://api.github.com/teams/6/memberships/{username}": "OK", - f"https://api.github.com/search/issues?q=type:pr+repo:python/cpython+sha:{sha}": { + f"https://api.github.com/search/issues?q=type:pr+repo:{repo_full_name}+sha:{sha}": { "total_count": 1, "items": [ { @@ -1169,13 +1172,15 @@ async def test_new_commit_pushed_to_approved_pr(): } -async def test_new_commit_pushed_to_not_approved_pr(): +@pytest.mark.parametrize("repo_full_name", ["python/cpython", "mariatta/cpython"]) +async def test_new_commit_pushed_to_not_approved_pr(repo_full_name): # There is new commit on approved PR sha = "f2393593c99dd2d3ab8bfab6fcc5ddee540518a9" - data = {"commits": [{"id": sha}]} + data = {"commits": [{"id": sha}], + "repository": {"full_name": repo_full_name},} event = sansio.Event(data, event="push", delivery_id="12345") items = { - f"https://api.github.com/search/issues?q=type:pr+repo:python/cpython+sha:{sha}": { + f"https://api.github.com/search/issues?q=type:pr+repo:{repo_full_name}+sha:{sha}": { "total_count": 1, "items": [ { From 05b6aa14603463f59fc8f5d4274832d26d72c32b Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:40:20 -0700 Subject: [PATCH 30/33] Remove prints --- bedevere/stage.py | 2 -- bedevere/util.py | 1 - 2 files changed, 3 deletions(-) diff --git a/bedevere/stage.py b/bedevere/stage.py index 041cac51..b5ec3d64 100644 --- a/bedevere/stage.py +++ b/bedevere/stage.py @@ -142,14 +142,12 @@ async def new_commit_pushed(event, gh, *arg, **kwargs): """If there is a new commit pushed to the PR branch that is in `awaiting merge` state, move it back to `awaiting core review` stage. """ - print(f"{event.data=}") commits = event.data["commits"] if len(commits) > 0: # get the latest commit hash commit_hash = commits[-1]["id"] repo_full_name = event.data["repository"]["full_name"] pr = await util.get_pr_for_commit(gh, commit_hash, repo_full_name) - print(f"{commit_hash=}") for label in util.labels(pr): if label == "awaiting merge": issue = await util.issue_for_PR(gh, pr) diff --git a/bedevere/util.py b/bedevere/util.py index 8c2d4e86..24d81639 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -77,7 +77,6 @@ def skip_label(what): def labels(issue): - print(f"{issue}") return {label_data["name"] for label_data in issue["labels"]} From 4834a6f96a3b47e48f62f1dc2d7b22b3911aeaf8 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:47:14 -0700 Subject: [PATCH 31/33] Debug post status --- bedevere/news.py | 1 + bedevere/util.py | 1 + 2 files changed, 2 insertions(+) diff --git a/bedevere/news.py b/bedevere/news.py index 1d9c30aa..6edebada 100644 --- a/bedevere/news.py +++ b/bedevere/news.py @@ -40,6 +40,7 @@ async def check_news(gh, pull_request, files=None): The routing is handled through the filepaths module. """ + print(f"Check news {pull_request=}") if not files: files = await util.files_for_PR(gh, pull_request) in_next_dir = file_found = False diff --git a/bedevere/util.py b/bedevere/util.py index 24d81639..e86739fe 100644 --- a/bedevere/util.py +++ b/bedevere/util.py @@ -68,6 +68,7 @@ def create_status(context, state, *, description=None, target_url=None): async def post_status(gh, event, status): """Post a status in reaction to an event.""" + print(f"Post status {event.data=}") await gh.post(event.data["pull_request"]["statuses_url"], data=status) From 8ec31af172ed71f34e33c090d423d9e331d450a8 Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 21:53:26 -0700 Subject: [PATCH 32/33] Debug post status --- bedevere/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 6cd8e4a3..16633739 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -28,7 +28,8 @@ async def main(event_payload): event_name = os.environ["GITHUB_EVENT_NAME"] job_id = os.environ["GITHUB_JOB"] event = sansio.Event(event_payload, event=event_name, delivery_id=job_id) - + print(f"{event.data=}") + print(f"{event_payload}") print('GH delivery ID', event.delivery_id, file=sys.stderr) oauth_token = os.environ.get("GITHUB_TOKEN") From 67849cf341b0416747699b23c770dc7094964f4f Mon Sep 17 00:00:00 2001 From: Mariatta Date: Tue, 13 Jun 2023 22:04:24 -0700 Subject: [PATCH 33/33] Debug post status --- bedevere/__main__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bedevere/__main__.py b/bedevere/__main__.py index 16633739..dc3e3827 100644 --- a/bedevere/__main__.py +++ b/bedevere/__main__.py @@ -30,6 +30,7 @@ async def main(event_payload): event = sansio.Event(event_payload, event=event_name, delivery_id=job_id) print(f"{event.data=}") print(f"{event_payload}") + print(f"{event_name=}") print('GH delivery ID', event.delivery_id, file=sys.stderr) oauth_token = os.environ.get("GITHUB_TOKEN")