11import celery
2+ import asyncio
23import os
34import subprocess
5+ import aiohttp
6+ from gidgethub import aiohttp as gh_aiohttp
7+
8+ import cachetools
49
510from celery import bootsteps
611
1419 BROKER_URL = os .environ ["REDIS_URL" ], CELERY_RESULT_BACKEND = os .environ ["REDIS_URL" ]
1520)
1621
22+ cache = cachetools .LRUCache (maxsize = 500 )
23+
1724
1825@app .task ()
1926def setup_cpython_repo ():
@@ -39,52 +46,78 @@ def setup_cpython_repo():
3946
4047@app .task ()
4148def backport_task (commit_hash , branch , * , issue_number , created_by , merged_by ):
42- """Backport a commit into a branch."""
43- if not util .is_cpython_repo ():
44- # cd to cpython if we're not already in it
45- if "cpython" in os .listdir ("." ):
46- os .chdir ("./cpython" )
47- else :
48- print (f"pwd: { os .getcwd ()} , listdir: { os .listdir ('.' )} " )
49- util .comment_on_pr (
50- issue_number ,
51- f"""{ util .get_participants (created_by , merged_by )} , Something is wrong... I can't backport for now.
52- Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
53- ```
54- cherry_picker { commit_hash } { branch }
55- ```
56- """ ,
57- )
58- util .assign_pr_to_core_dev (issue_number , merged_by )
59- cp = cherry_picker .CherryPicker (
60- "origin" , commit_hash , [branch ], prefix_commit = False
49+ loop = asyncio .get_event_loop ()
50+ loop .run_until_complete (
51+ backport_task_asyncio (
52+ commit_hash ,
53+ branch ,
54+ issue_number = issue_number ,
55+ created_by = created_by ,
56+ merged_by = merged_by ,
57+ )
6158 )
62- try :
63- cp . backport ()
64- except cherry_picker . BranchCheckoutException :
65- util . comment_on_pr (
66- issue_number ,
67- f """Sorry { util . get_participants ( created_by , merged_by ) } , I had trouble checking out the ` { branch } ` backport branch.
68- Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
69- ```
70- cherry_picker { commit_hash } { branch }
71- ```
72- """ ,
59+
60+
61+ async def backport_task_asyncio (
62+ commit_hash , branch , * , issue_number , created_by , merged_by
63+ ):
64+ """Backport a commit into a branch."""
65+
66+ oauth_token = os . environ . get ( "GH_AUTH" )
67+ async with aiohttp . ClientSession () as session :
68+ gh = gh_aiohttp . GitHubAPI (
69+ session , "python/cpython" , oauth_token = oauth_token , cache = cache
7370 )
74- util .assign_pr_to_core_dev (issue_number , merged_by )
75- cp .abort_cherry_pick ()
76- except cherry_picker .CherryPickException :
77- util .comment_on_pr (
78- issue_number ,
79- f"""Sorry, { util .get_participants (created_by , merged_by )} , I could not cleanly backport this to `{ branch } ` due to a conflict.
80- Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
81- ```
82- cherry_picker { commit_hash } { branch }
83- ```
84- """ ,
71+
72+ if not util .is_cpython_repo ():
73+ # cd to cpython if we're not already in it
74+ if "cpython" in os .listdir ("." ):
75+ os .chdir ("./cpython" )
76+ else :
77+ print (f"pwd: { os .getcwd ()} , listdir: { os .listdir ('.' )} " )
78+
79+ await util .comment_on_pr (
80+ gh ,
81+ issue_number ,
82+ f"""{ util .get_participants (created_by , merged_by )} , Something is wrong... I can't backport for now.
83+ Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
84+ ```
85+ cherry_picker { commit_hash } { branch }
86+ ```
87+ """ ,
88+ )
89+ await util .assign_pr_to_core_dev (gh , issue_number , merged_by )
90+ cp = cherry_picker .CherryPicker (
91+ "origin" , commit_hash , [branch ], prefix_commit = False
8592 )
86- util .assign_pr_to_core_dev (issue_number , merged_by )
87- cp .abort_cherry_pick ()
93+ try :
94+ cp .backport ()
95+ except cherry_picker .BranchCheckoutException :
96+ await util .comment_on_pr (
97+ gh ,
98+ issue_number ,
99+ f"""Sorry { util .get_participants (created_by , merged_by )} , I had trouble checking out the `{ branch } ` backport branch.
100+ Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
101+ ```
102+ cherry_picker { commit_hash } { branch }
103+ ```
104+ """ ,
105+ )
106+ await util .assign_pr_to_core_dev (gh , issue_number , merged_by )
107+ cp .abort_cherry_pick ()
108+ except cherry_picker .CherryPickException :
109+ await util .comment_on_pr (
110+ gh ,
111+ issue_number ,
112+ f"""Sorry, { util .get_participants (created_by , merged_by )} , I could not cleanly backport this to `{ branch } ` due to a conflict.
113+ Please backport using [cherry_picker](https://pypi.org/project/cherry-picker/) on command line.
114+ ```
115+ cherry_picker { commit_hash } { branch }
116+ ```
117+ """ ,
118+ )
119+ await util .assign_pr_to_core_dev (gh , issue_number , merged_by )
120+ cp .abort_cherry_pick ()
88121
89122
90123class InitRepoStep (bootsteps .StartStopStep ):
0 commit comments