Created
March 9, 2022 22:22
-
-
Save pgbezerra/844b59823cb7fa9de1b353fe79319f36 to your computer and use it in GitHub Desktop.
Write in a CSV repo, merged date, author and authors organization team
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 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is how you can use
You should see an csv output like this: