Last active
October 8, 2020 17:45
-
-
Save foutoucour/cb695e9051b41b665607b96032d0c464 to your computer and use it in GitHub Desktop.
github api async python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import collections | |
import os | |
from pprint import pprint | |
import aiohttp | |
from gidgethub.aiohttp import GitHubAPI | |
async def get_repo(client, user): | |
return await client.getitem(f"/users/{user}/repos") | |
async def get_branches(client, repo): | |
return repo, await client.getitem(f"/repos/{repo['full_name']}/branches") | |
async def get_pulls(client, repo): | |
return repo, await client.getitem(f"repos/{repo['full_name']}/pulls") | |
async def main(): | |
async with aiohttp.ClientSession() as session: | |
gh = GitHubAPI(session, "jordi_gidgethub", oauth_token=os.getenv("GH_AUTH")) | |
owner = "foutoucour" | |
repos = await get_repo(gh, owner) | |
branches_tasks = [ | |
asyncio.ensure_future(get_branches(gh, repo)) | |
for repo in repos | |
] | |
results_branches = await asyncio.gather(*branches_tasks) | |
pr_tasks = [ | |
asyncio.ensure_future(get_pulls(gh, repo)) | |
for repo in repos | |
] | |
results_pr = await asyncio.gather(*pr_tasks) | |
mapping_branches = collections.defaultdict(list) | |
for repo, branches in results_branches: | |
for branch in branches: | |
mapping_branches[repo['full_name']].append(branch) | |
mapping_prs = collections.defaultdict(list) | |
for repo, prs in results_pr: | |
for pr in prs: | |
mapping_prs[repo['full_name']].append(pr) | |
for repo, branches in mapping_branches.items(): | |
print(repo) | |
print(await gh.getitem(f"/repos/{repo}/pulls/1/merge")) | |
break | |
if repo in mapping_prs: | |
prs = mapping_prs[repo] | |
for branch in branches: | |
for pr in prs: | |
if pr['head']['ref'] == branch['name'] and await gh.getitem(f"/repos/{repo}/pulls/{pr['number']}/merge"): | |
print(repo, branch['name'], pr['html_url']) | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment