Skip to content

Instantly share code, notes, and snippets.

@pgbezerra
Created March 9, 2022 22:22
Show Gist options
  • Save pgbezerra/844b59823cb7fa9de1b353fe79319f36 to your computer and use it in GitHub Desktop.
Save pgbezerra/844b59823cb7fa9de1b353fe79319f36 to your computer and use it in GitHub Desktop.
Write in a CSV repo, merged date, author and authors organization team
import csv
import os
import requests
import sys
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
from requests.auth import HTTPBasicAuth
org = sys.argv[1]
repo = sys.argv[2]
destination_csv = sys.argv[3]
username = os.environ['GH_USER']
passwd = os.environ['GH_PASS']
auth=HTTPBasicAuth(username, passwd)
transport = AIOHTTPTransport(
url="https://api.github.com/graphql",
headers={'Authorization': 'Bearer ' + passwd})
graphql_client = Client(transport=transport, fetch_schema_from_transport=True)
merged_result = graphql_client.execute(gql("""query($repo:String!, $org:String!) {
organization(login: $org) {
repository(name: $repo) {
pullRequests(
states: MERGED,
first: 100,
orderBy: {field: CREATED_AT, direction: DESC}
) {
edges {
node {
mergedAt
title
author { login }
}
}
}
}
}
}"""), variable_values={'org': org, 'repo': repo})
merged_list = merged_result['organization']['repository']['pullRequests']['edges']
merged_sanitized = []
author_teams = {}
for pr in merged_list:
author = pr['node']['author']['login']
if author not in author_teams:
author_teams[author] = []
author_result = graphql_client.execute(gql("""query($users: [String!]!, $org: String!) {
organization(login: $org) {
teams(first: 5, userLogins: $users) {
edges { node { name } }
}
}
}"""), variable_values={'org': org, 'users': author})
for team in author_result['organization']['teams']['edges']:
team_name = team['node']['name']
if team_name.startswith('team-'):
author_teams[author].append(team_name)
merged_sanitized.append(dict(
repo_name=repo,
merged_at=pr['node']['mergedAt'],
title=pr['node']['title'],
author=author,
teams=','.join(author_teams[author])
))
merged_sanitized.sort(key=lambda item:item['merged_at'], reverse=True)
with open(destination_csv, 'w', newline='') as csvfile:
fieldnames = ['repo_name', 'merged_at', 'author', 'teams', 'title']
writer = csv.DictWriter(csvfile, delimiter=';', quotechar='"', fieldnames=fieldnames)
writer.writeheader()
for item in merged_sanitized: writer.writerow(item)
@pgbezerra
Copy link
Author

This is how you can use

GH_USER='[YOUR_USER]' \
GH_PASS='[YOUR_TOKEN]' \
python scooby-merges.py '[YOUR_ORG]' '[YOUR_REPO]' '[CSV_DESTINATION].csv'

You should see an csv output like this:

my-repo;2021-09-27T14:04:38Z;,myuser;team-test;fix zzz
my-repo;2021-09-24T13:33:22Z;hisuser;team-test2;feature yyy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment