Created
October 7, 2022 11:06
-
-
Save ArtDu/2e0429e9d1d0cc125f1a9b07341841c6 to your computer and use it in GitHub Desktop.
Get user statistics for a certain period of time from github via graphql api
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 json | |
| import requests | |
| import argparse | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("--user", help="User name", required=True) | |
| parser.add_argument("--token", help="YOUR API KEY", required=True) | |
| parser.add_argument("--from_datetime", help="From datetime (ISO 8601), e.g. 2022-07-01T00:00:00", required=True) | |
| parser.add_argument("--to_datetime", help="To datetime (ISO 8601), e.g. 2022-09-30T00:00:00", required=True) | |
| STATS_QUERY = """ | |
| query ($login: String!, $from: DateTime!, $to: DateTime!) { | |
| user(login: $login) { | |
| contributionsCollection(from: $from, to: $to) { | |
| totalCommitContributions | |
| totalPullRequestContributions | |
| totalIssueContributions | |
| totalPullRequestReviewContributions | |
| commitContributionsByRepository { | |
| repository { | |
| name | |
| } | |
| } | |
| } | |
| } | |
| } | |
| """ | |
| def run_query(query, variables, headers): | |
| request = requests.post( | |
| 'https://api.github.com/graphql', | |
| json={ | |
| 'query': query, | |
| 'variables': variables | |
| }, | |
| headers=headers | |
| ) | |
| if request.status_code == 200: | |
| return request.json() | |
| else: | |
| raise Exception("Query failed to run by returning code of {}. {}".format(request.status_code, query)) | |
| if __name__ == "__main__": | |
| args = parser.parse_args() | |
| headers = {"Authorization": f"Bearer {args.token}"} | |
| variables = { | |
| "login": args.user, | |
| "from": args.from_datetime, | |
| "to": args.to_datetime | |
| } | |
| print(json.dumps(run_query(STATS_QUERY, variables, headers), indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment