fineanmol / Hacktoberfest2024

Make your first Pull Request on Hacktoberfest 2024. Don't forget to spread love and if you like give us a ⭐️
https://fineanmol.github.io/Hacktoberfest2024/
GNU General Public License v3.0
2.39k stars 7.6k forks source link

Build a script should give you the information about the top contributor of GitHub repository #7727

Open chawdamrunal opened 4 days ago

chawdamrunal commented 4 days ago

`import requests

Replace with the repository owner and repository name

owner = 'octocat' repo = 'Hello-World'

Optional: GitHub personal access token for authentication

If you don't want to use it, leave auth=None

token = 'your_github_token_here' auth = (token, '') if token else None

GitHub API endpoint for repository contributors

url = f'https://api.github.com/repos/{owner}/{repo}/contributors'

def get_highest_contributor(): try:

Send GET request to GitHub API

    response = requests.get(url, auth=auth)

    # Check if the request was successful
    if response.status_code == 200:
        contributors = response.json()

        # Check if contributors exist
        if not contributors:
            print("No contributors found for this repository.")
            return

        # Find the highest contributor by the number of contributions
        highest_contributor = max(contributors, key=lambda x: x['contributions'])

        # Print the highest contributor's information
        print(f"Highest contributor: {highest_contributor['login']}")
        print(f"Contributions: {highest_contributor['contributions']}")
        print(f"Profile: {highest_contributor['html_url']}")

    else:
        print(f"Failed to retrieve contributors. Status code: {response.status_code}")
        print(f"Response: {response.text}")

except Exception as e:
    print(f"An error occurred: {e}")

Run the function

get_highest_contributor() `

Replace owner and repo with the appropriate GitHub repository information (e.g., octocat/Hello-World). The script sends a GET request to the GitHub API's /contributors endpoint to retrieve the list of contributors. It then identifies the highest contributor based on the number of contributions by using Python’s max() function with a lambda to sort by the contributions field. Finally, it prints the highest contributor’s username, contribution count, and profile URL.

fineanmol commented 4 days ago

@chawdamrunal Please Star ⭐️ the repo to earn 'hacktober-accepted' label for the event.\nMeanwhile, if you want to work on this issue, please raise a PR, and we will review and merge it.