Finding total contributions of a user from GitHub API

Finding total contributions of a user from GitHub API

To find the total contributions of a user on GitHub using the GitHub API, you need to:

  1. Fetch User Data: Obtain the user's public profile and contributions data.
  2. Calculate Contributions: Aggregate the contributions from repositories.

1. GitHub API Endpoints

  • User Profile: Fetch user profile information including public contributions.
  • Repositories: List repositories and check contributions to each repository.

2. Fetching Data Using GitHub API

Here's a step-by-step approach using the GitHub API:

Step 1: Obtain a GitHub Personal Access Token

You need a GitHub Personal Access Token for authenticated requests. You can generate one from GitHub Settings.

Step 2: Fetch User Data

You can use the GitHub API to fetch data. Here's a Python example using requests to get the total contributions.

Note: Contributions in GitHub generally include commits, pull requests, issues, etc. The following method focuses on fetching commits from repositories.

Python Example Using Requests

import requests # Replace with your GitHub personal access token GITHUB_TOKEN = 'your_github_token' USER = 'username' # GitHub username def get_user_repositories(user): headers = {'Authorization': f'token {GITHUB_TOKEN}'} response = requests.get(f'https://api.github.com/users/{user}/repos', headers=headers) if response.status_code == 200: return response.json() else: raise Exception(f'Error fetching repositories: {response.status_code}') def get_total_contributions(user): repos = get_user_repositories(user) total_commits = 0 headers = {'Authorization': f'token {GITHUB_TOKEN}'} for repo in repos: repo_name = repo['name'] commits_url = f'https://api.github.com/repos/{user}/{repo_name}/commits' response = requests.get(commits_url, headers=headers) if response.status_code == 200: commits = response.json() total_commits += len(commits) else: print(f'Error fetching commits for {repo_name}: {response.status_code}') return total_commits # Get total contributions total_contributions = get_total_contributions(USER) print(f'Total contributions for {USER}: {total_contributions}') 

Explanation

  1. Get User Repositories:

    • Fetch the list of repositories for the user.
    • Use the endpoint https://api.github.com/users/{username}/repos.
  2. Get Commits:

    • For each repository, fetch commits using https://api.github.com/repos/{username}/{repo_name}/commits.
    • Count the number of commits.
  3. Aggregate Results:

    • Sum the total number of commits across all repositories.

Considerations

  • Rate Limiting: GitHub API has rate limits. For authenticated requests, the limit is higher than for unauthenticated requests.
  • Pagination: The GitHub API paginates results for large lists. You may need to handle pagination to get all repositories or commits.
  • Public Repositories: Contributions to private repositories or organizations might not be accessible unless you have appropriate permissions.
  • Repository Contributions: Contributions might include issues, pull requests, etc. The example above focuses on commits, but you can adapt it to other types of contributions.

Alternative Methods

  • GitHub GraphQL API: For more complex queries and to get more detailed data, you can use the GitHub GraphQL API. It allows for more precise queries and aggregation.

Here's a basic GraphQL example to get repositories and contributions:

import requests # GraphQL endpoint url = 'https://api.github.com/graphql' headers = { 'Authorization': f'token {GITHUB_TOKEN}', 'Content-Type': 'application/json' } query = """ { user(login: "username") { repositories(first: 100) { nodes { name defaultBranchRef { target { ... on Commit { history { totalCount } } } } } } } } """ def get_contributions_graphql(): response = requests.post(url, json={'query': query}, headers=headers) if response.status_code == 200: data = response.json() total_commits = 0 for repo in data['data']['user']['repositories']['nodes']: total_commits += repo['defaultBranchRef']['target']['history']['totalCount'] return total_commits else: raise Exception(f'Error fetching data: {response.status_code}') # Get total contributions using GraphQL total_contributions = get_contributions_graphql() print(f'Total contributions using GraphQL: {total_contributions}') 

This GraphQL query retrieves the total number of commits from all repositories for a given user. Adjust the query as needed for different types of contributions or data.

Examples

  1. GitHub API - Get total contributions of a user

    Description: This query demonstrates how to fetch the total contributions (commits, issues, pull requests) of a user using the GitHub API.

    import requests username = "octocat" url = f"https://api.github.com/users/{username}/events" response = requests.get(url) events = response.json() total_contributions = sum(1 for event in events if event['type'] in ['PushEvent', 'IssuesEvent', 'PullRequestEvent']) print(f"Total contributions: {total_contributions}") 
  2. GitHub API - Get total commits of a user

    Description: This query shows how to fetch the total number of commits made by a user using the GitHub API.

    import requests username = "octocat" url = f"https://api.github.com/search/commits?q=author:{username}" headers = {'Accept': 'application/vnd.github.cloak-preview'} response = requests.get(url, headers=headers) commits = response.json() total_commits = commits['total_count'] print(f"Total commits: {total_commits}") 
  3. GitHub API - Get total issues created by a user

    Description: This query explains how to fetch the total number of issues created by a user using the GitHub API.

    import requests username = "octocat" url = f"https://api.github.com/search/issues?q=author:{username}" response = requests.get(url) issues = response.json() total_issues = issues['total_count'] print(f"Total issues: {total_issues}") 
  4. GitHub API - Get total pull requests created by a user

    Description: This query shows how to fetch the total number of pull requests created by a user using the GitHub API.

    import requests username = "octocat" url = f"https://api.github.com/search/issues?q=author:{username}+type:pr" response = requests.get(url) pull_requests = response.json() total_pull_requests = pull_requests['total_count'] print(f"Total pull requests: {total_pull_requests}") 
  5. GitHub API - Get contributions from a specific repository

    Description: This query demonstrates how to fetch the total contributions of a user from a specific repository using the GitHub API.

    import requests owner = "octocat" repo = "Hello-World" username = "octocat" url = f"https://api.github.com/repos/{owner}/{repo}/commits?author={username}" response = requests.get(url) commits = response.json() total_commits = len(commits) print(f"Total commits in {repo}: {total_commits}") 
  6. GitHub API - Get contributions by type (commits, issues, PRs)

    Description: This query explains how to fetch the total contributions of a user by type (commits, issues, PRs) using the GitHub API.

    import requests username = "octocat" contributions = {'commits': 0, 'issues': 0, 'pull_requests': 0} # Fetch commits url = f"https://api.github.com/search/commits?q=author:{username}" headers = {'Accept': 'application/vnd.github.cloak-preview'} response = requests.get(url, headers=headers) contributions['commits'] = response.json()['total_count'] # Fetch issues url = f"https://api.github.com/search/issues?q=author:{username}" response = requests.get(url) contributions['issues'] = response.json()['total_count'] # Fetch pull requests url = f"https://api.github.com/search/issues?q=author:{username}+type:pr" response = requests.get(url) contributions['pull_requests'] = response.json()['total_count'] print(f"Contributions: {contributions}") 
  7. GitHub API - Get total contributions to public repositories

    Description: This query shows how to fetch the total contributions of a user to public repositories using the GitHub API.

    import requests username = "octocat" url = f"https://api.github.com/users/{username}/repos" response = requests.get(url) repos = response.json() total_contributions = 0 for repo in repos: repo_name = repo['name'] repo_owner = repo['owner']['login'] commits_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits?author={username}" commits = requests.get(commits_url).json() total_contributions += len(commits) print(f"Total contributions to public repositories: {total_contributions}") 
  8. GitHub API - Get total contributions over a period

    Description: This query demonstrates how to fetch the total contributions of a user over a specific period using the GitHub API.

    import requests username = "octocat" since = "2022-01-01T00:00:00Z" until = "2023-01-01T00:00:00Z" url = f"https://api.github.com/users/{username}/events?since={since}&until={until}" response = requests.get(url) events = response.json() total_contributions = sum(1 for event in events if event['type'] in ['PushEvent', 'IssuesEvent', 'PullRequestEvent']) print(f"Total contributions from {since} to {until}: {total_contributions}") 
  9. GitHub API - Get total contributions for an organization

    Description: This query explains how to fetch the total contributions of a user to repositories within a specific organization using the GitHub API.

    import requests org = "github" username = "octocat" url = f"https://api.github.com/orgs/{org}/repos" response = requests.get(url) repos = response.json() total_contributions = 0 for repo in repos: repo_name = repo['name'] commits_url = f"https://api.github.com/repos/{org}/{repo_name}/commits?author={username}" commits = requests.get(commits_url).json() total_contributions += len(commits) print(f"Total contributions to {org}: {total_contributions}") 
  10. GitHub API - Get detailed contributions (commits, issues, PRs) by year

    Description: This query shows how to fetch detailed contributions of a user by year using the GitHub API.

    import requests username = "octocat" year = "2023" contributions = {'commits': 0, 'issues': 0, 'pull_requests': 0} # Fetch commits url = f"https://api.github.com/search/commits?q=author:{username}+committer-date:{year}-01-01..{year}-12-31" headers = {'Accept': 'application/vnd.github.cloak-preview'} response = requests.get(url, headers=headers) contributions['commits'] = response.json()['total_count'] # Fetch issues url = f"https://api.github.com/search/issues?q=author:{username}+created:{year}-01-01..{year}-12-31" response = requests.get(url) contributions['issues'] = response.json()['total_count'] # Fetch pull requests url = f"https://api.github.com/search/issues?q=author:{username}+type:pr+created:{year}-01-01..{year}-12-31" response = requests.get(url) contributions['pull_requests'] = response.json()['total_count'] print(f"Contributions in {year}: {contributions}") 

More Tags

httpwebrequest django-2.0 positional-argument progress-indicator python-3.6 sendgrid mpvolumeview hashset native http-status-code-413

More Programming Questions

More Chemistry Calculators

More Organic chemistry Calculators

More Investment Calculators

More Genetics Calculators