Skip to content

Commit

Permalink
Merge branch 'main' into patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackenmen authored Oct 16, 2024
2 parents 5a1fb95 + 820fcf4 commit 7bdf820
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 77 deletions.
34 changes: 20 additions & 14 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
version: 2
updates:
- package-ecosystem: pip
directory: "/"
schedule:
interval: monthly
assignees:
- "ezio-melotti"
open-pull-requests-limit: 10
- package-ecosystem: pip
directory: "/"
schedule:
interval: monthly
assignees:
- "ezio-melotti"
groups:
pip:
patterns:
- "*"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
assignees:
- "ezio-melotti"
open-pull-requests-limit: 10
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: monthly
assignees:
- "ezio-melotti"
groups:
actions:
patterns:
- "*"
12 changes: 7 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10", "3.11"] # see #590 for "3.12"
# remember to update the envlist in tox.ini too
python-version: ["3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
Expand All @@ -33,9 +34,10 @@ jobs:
# run against the current Python interpreter
TOXENV: py
run: tox
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
if: always()
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: Python_${{ matrix.python-version }}
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_ORG_TOKEN }}
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Lint

on: [push, pull_request, workflow_dispatch]

env:
FORCE_COLOR: 1

permissions:
contents: read

jobs:
lint:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.x"
cache: pip
- uses: pre-commit/action@v3.0.1
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
repos:
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: forbid-submodules
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.28.1
hooks:
- id: check-dependabot
- id: check-github-workflows

- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes

ci:
autoupdate_schedule: quarterly
8 changes: 4 additions & 4 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r requirements.txt
coverage
pytest==7.4.0
pytest-asyncio==0.21.1
pytest-aiohttp==1.0.4
pytest-cov==4.1.0
pytest==8.3.3
pytest-asyncio==0.24.0
pytest-aiohttp==1.0.5
pytest-cov==5.0.0
23 changes: 19 additions & 4 deletions miss_islington/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from aiohttp import web
from gidgethub import aiohttp as gh_aiohttp
from gidgethub import routing, sansio
from gidgethub import apps

import sentry_sdk
from sentry_sdk.integrations.aiohttp import AioHttpIntegration

Expand All @@ -30,11 +32,20 @@ async def main(request):
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/cpython", oauth_token=oauth_token, cache=cache
session, "python/cpython", cache=cache
)
# This path only works on GitHub App
installation_id = event.data["installation"]["id"]
installation_access_token = await apps.get_installation_access_token(
gh,
installation_id=installation_id,
app_id=os.environ.get("GH_APP_ID"),
private_key=os.environ.get("GH_PRIVATE_KEY")
)
gh.oauth_token = installation_access_token["token"]

# Give GitHub some time to reach internal consistency.
await asyncio.sleep(1)
await router.dispatch(event, gh)
Expand All @@ -43,8 +54,6 @@ async def main(request):
f"""\
GH requests remaining: {gh.rate_limit.remaining}/{gh.rate_limit.limit}, \
reset time: {gh.rate_limit.reset_datetime:%b-%d-%Y %H:%M:%S %Z}, \
oauth token length {len(oauth_token)}, \
last 4 digits {oauth_token[-4:]}, \
GH delivery ID {event.delivery_id} \
"""
)
Expand All @@ -56,6 +65,12 @@ async def main(request):
return web.Response(status=500)


@router.register("installation", action="created")
async def repo_installation_added(event, gh, *args, **kwargs):
# installation_id = event.data["installation"]["id"]
print(f"App installed by {event.data['installation']['account']['login']}, installation_id: {event.data['installation']['id']}")


sentry_sdk.init(dsn=os.environ.get("SENTRY_DSN"), integrations=[AioHttpIntegration()])
app = web.Application()
app.router.add_post("/", main)
Expand Down
7 changes: 4 additions & 3 deletions miss_islington/backport_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def backport_pr(event, gh, *args, **kwargs):
for label in pr_labels
if label["name"].startswith("needs backport to")
]

installation_id = event.data["installation"]["id"]
if branches:
easter_egg = ""
if random.random() < 0.1:
Expand All @@ -62,12 +62,12 @@ async def backport_pr(event, gh, *args, **kwargs):

for branch in sorted_branches:
await kickoff_backport_task(
gh, commit_hash, branch, issue_number, created_by, merged_by
gh, commit_hash, branch, issue_number, created_by, merged_by, installation_id=installation_id
)


async def kickoff_backport_task(
gh, commit_hash, branch, issue_number, created_by, merged_by
gh, commit_hash, branch, issue_number, created_by, merged_by, installation_id
):
try:
tasks.backport_task.delay(
Expand All @@ -76,6 +76,7 @@ async def kickoff_backport_task(
issue_number=issue_number,
created_by=created_by,
merged_by=merged_by,
installation_id=installation_id
)
except (redis_ex.ConnectionError, kombu_ex.OperationalError) as ex:
err_message = f"I'm having trouble backporting to `{branch}`. Reason: '`{ex}`'. Please retry by removing and re-adding the `needs backport to {branch}` label."
Expand Down
70 changes: 42 additions & 28 deletions miss_islington/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from cherry_picker import cherry_picker
from celery import bootsteps
from gidgethub import aiohttp as gh_aiohttp
from gidgethub import apps
import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration

Expand All @@ -19,6 +20,7 @@
app.conf.update(
broker_url=os.environ["HEROKU_REDIS_MAROON_URL"],
result_backend=os.environ["HEROKU_REDIS_MAROON_URL"],
broker_connection_retry_on_startup=True,
)

cache = cachetools.LRUCache(maxsize=500)
Expand All @@ -37,7 +39,7 @@

@app.task()
def setup_cpython_repo():
print("Setting up CPython repository") # pragma: nocover
print("Setting up CPython repository") # pragma: nocover
if "cpython" not in os.listdir("."):
subprocess.check_output(
f"git clone https://{os.environ.get('GH_AUTH')}:x-oauth-basic@github.com/miss-islington/cpython.git".split()
Expand All @@ -58,7 +60,7 @@ def setup_cpython_repo():


@app.task()
def backport_task(commit_hash, branch, *, issue_number, created_by, merged_by):
def backport_task(commit_hash, branch, *, issue_number, created_by, merged_by, installation_id):
loop = asyncio.get_event_loop()
loop.run_until_complete(
backport_task_asyncio(
Expand All @@ -67,21 +69,27 @@ def backport_task(commit_hash, branch, *, issue_number, created_by, merged_by):
issue_number=issue_number,
created_by=created_by,
merged_by=merged_by,
installation_id=installation_id
)
)


async def backport_task_asyncio(
commit_hash, branch, *, issue_number, created_by, merged_by
commit_hash, branch, *, issue_number, created_by, merged_by, installation_id
):
"""Backport a commit into a branch."""

oauth_token = os.environ.get("GH_AUTH")
async with aiohttp.ClientSession() as session:
gh = gh_aiohttp.GitHubAPI(
session, "python/cpython", oauth_token=oauth_token, cache=cache
session, "python/cpython", cache=cache
)

# This path only works on GitHub App
installation_access_token = await apps.get_installation_access_token(
gh,
installation_id=installation_id,
app_id=os.environ.get("GH_APP_ID"),
private_key=os.environ.get("GH_PRIVATE_KEY")
)
gh.oauth_token = installation_access_token["token"]
if not util.is_cpython_repo():
# cd to cpython if we're not already in it
if "cpython" in os.listdir("."):
Expand All @@ -92,15 +100,16 @@ async def backport_task_asyncio(
await util.comment_on_pr(
gh,
issue_number,
f"""{util.get_participants(created_by, merged_by)}, I can't backport for now. Please try again later or
backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""",
f"""\
{util.get_participants(created_by, merged_by)}, I can't backport for now. Please try again later or
backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""",
)
await util.assign_pr_to_core_dev(gh, issue_number, merged_by)

# Ensure that we don't have any changes lying around
subprocess.check_output(['git', 'reset', '--hard'])
subprocess.check_output(['git', 'clean', '-fxd'])
Expand All @@ -118,28 +127,33 @@ async def backport_task_asyncio(
await util.comment_on_pr(
gh,
issue_number,
f"""Sorry {util.get_participants(created_by, merged_by)}, I had trouble checking out the `{branch}` backport branch.
Please retry by removing and re-adding the "needs backport to {branch}" label.
Alternatively, you can backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on the command line.
```
cherry_picker {commit_hash} {branch}
```
""",
f"""\
Sorry {util.get_participants(created_by, merged_by)}, I had trouble checking out the `{branch}` backport branch.
Please retry by removing and re-adding the "needs backport to {branch}" label.
Alternatively, you can backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on the command line.
```
cherry_picker {commit_hash} {branch}
```
""",
)
await util.assign_pr_to_core_dev(gh, issue_number, merged_by)
cp.abort_cherry_pick()
except cherry_picker.CherryPickException:
except cherry_picker.CherryPickException as cpe:
await util.comment_on_pr(
gh,
issue_number,
f"""Sorry, {util.get_participants(created_by, merged_by)}, I could not cleanly backport this to `{branch}` due to a conflict.
Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""",
f"""\
Sorry, {util.get_participants(created_by, merged_by)}, I could not cleanly backport this to `{branch}` due to a conflict.
Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
```
cherry_picker {commit_hash} {branch}
```
""",
)
await util.assign_pr_to_core_dev(gh, issue_number, merged_by)
cpe_exc = cpe
cpe_state = cp.get_state_and_verify()
print(cpe_state)
cp.abort_cherry_pick()


Expand Down
7 changes: 2 additions & 5 deletions miss_islington/util.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import re
import subprocess

import gidgethub

import textwrap


async def comment_on_pr(gh, issue_number, message):
"""
Leave a comment on a PR/Issue
"""
issue_comment_url = f"/repos/python/cpython/issues/{issue_number}/comments"
message = textwrap.dedent(message)
data = {"body": message}
response = await gh.post(issue_comment_url, data=data)
print(f"Commented at {response['html_url']}, message: {message}")
Expand Down Expand Up @@ -79,4 +77,3 @@ def normalize_title(title, body):
else:
# Being paranoid in case \r\n is used.
return title[:-1] + body[1:].partition("\r\n")[0]

Loading

0 comments on commit 7bdf820

Please sign in to comment.